Skip to content

Commit 1aa04cf

Browse files
committed
app: Start and Stop callbacks
Change-Id: If8ea6aaf2fb2c62eaf4119526a8bb46b8a84b982 Reviewed-on: https://go-review.googlesource.com/1881 Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 8952e43 commit 1aa04cf

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

app/app.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ func Run(cb Callbacks) {
2020

2121
// Callbacks is the set of functions called by the app.
2222
type Callbacks struct {
23+
// Start is called when the app enters the foreground.
24+
// The app will start receiving Draw and Touch calls.
25+
//
26+
// Window geometry will be configured and an OpenGL context
27+
// will be available.
28+
//
29+
// Start is an equivalent lifecycle state to onStart() on
30+
// Android and applicationDidBecomeActive on iOS.
31+
Start func()
32+
33+
// Stop is called shortly before a program is suspended.
34+
//
35+
// When Stop is received, the app is no longer visible and not is
36+
// receiving events. It should:
37+
//
38+
// - Save any state the user expects saved (for example text).
39+
// - Release all resources that are not needed.
40+
//
41+
// Execution time in the stop state is limited, and the limit is
42+
// enforced by the operating system. Stop as quickly as you can.
43+
//
44+
// An app that is stopped may be started again. For example, the user
45+
// opens Recent Apps and switches to your app. A stopped app may also
46+
// be terminated by the operating system with no further warning.
47+
//
48+
// Stop is equivalent to onStop() on Android and
49+
// applicationDidEnterBackground on iOS.
50+
Stop func()
51+
2352
// Draw is called by the render loop to draw the screen.
2453
//
2554
// Drawing is done into a framebuffer, which is then swapped onto the

app/loop_android.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,19 @@ func windowDrawLoop(cb Callbacks, w *C.ANativeWindow, queue *C.AInputQueue) {
101101
geom.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt)
102102
geom.Height = geom.Pt(float32(C.windowHeight) / geom.PixelsPerPt)
103103

104+
// We start here rather than onStart so the window exists and the Gl
105+
// context is configured.
106+
if cb.Start != nil {
107+
cb.Start()
108+
}
109+
104110
for {
105111
processEvents(cb, queue)
106112
select {
107113
case <-windowDestroyed:
114+
if cb.Stop != nil {
115+
cb.Stop()
116+
}
108117
return
109118
default:
110119
if cb.Draw != nil {

example/basic/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ var (
3131

3232
func main() {
3333
app.Run(app.Callbacks{
34+
Start: start,
35+
Stop: stop,
3436
Draw: draw,
3537
Touch: touch,
3638
})
3739
}
3840

39-
// TODO(crawshaw): Need an easier way to do GL-dependent initialization.
40-
41-
func initGL() {
41+
func start() {
4242
var err error
4343
program, err = glutil.CreateProgram(vertexShader, fragmentShader)
4444
if err != nil {
@@ -54,17 +54,20 @@ func initGL() {
5454
color = gl.GetUniformLocation(program, "color")
5555
offset = gl.GetUniformLocation(program, "offset")
5656
touchLoc = geom.Point{geom.Width / 2, geom.Height / 2}
57+
58+
// TODO(crawshaw): the debug package needs to put GL state init here
59+
}
60+
61+
func stop() {
62+
gl.DeleteProgram(program)
63+
gl.DeleteBuffer(buf)
5764
}
5865

5966
func touch(t event.Touch) {
6067
touchLoc = t.Loc
6168
}
6269

6370
func draw() {
64-
if program.Value == 0 {
65-
initGL()
66-
}
67-
6871
gl.ClearColor(1, 0, 0, 1)
6972
gl.Clear(gl.COLOR_BUFFER_BIT)
7073

0 commit comments

Comments
 (0)