From 1aa04cf03817f039b4de8002a5273d3556df4e73 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Fri, 19 Dec 2014 10:28:05 -0500 Subject: [PATCH] app: Start and Stop callbacks Change-Id: If8ea6aaf2fb2c62eaf4119526a8bb46b8a84b982 Reviewed-on: https://go-review.googlesource.com/1881 Reviewed-by: Hyang-Ah Hana Kim --- app/app.go | 29 +++++++++++++++++++++++++++++ app/loop_android.go | 9 +++++++++ example/basic/main.go | 17 ++++++++++------- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/app.go b/app/app.go index 09b1ef3..2857a78 100644 --- a/app/app.go +++ b/app/app.go @@ -20,6 +20,35 @@ func Run(cb Callbacks) { // Callbacks is the set of functions called by the app. type Callbacks struct { + // Start is called when the app enters the foreground. + // The app will start receiving Draw and Touch calls. + // + // Window geometry will be configured and an OpenGL context + // will be available. + // + // Start is an equivalent lifecycle state to onStart() on + // Android and applicationDidBecomeActive on iOS. + Start func() + + // Stop is called shortly before a program is suspended. + // + // When Stop is received, the app is no longer visible and not is + // receiving events. It should: + // + // - Save any state the user expects saved (for example text). + // - Release all resources that are not needed. + // + // Execution time in the stop state is limited, and the limit is + // enforced by the operating system. Stop as quickly as you can. + // + // An app that is stopped may be started again. For example, the user + // opens Recent Apps and switches to your app. A stopped app may also + // be terminated by the operating system with no further warning. + // + // Stop is equivalent to onStop() on Android and + // applicationDidEnterBackground on iOS. + Stop func() + // Draw is called by the render loop to draw the screen. // // Drawing is done into a framebuffer, which is then swapped onto the diff --git a/app/loop_android.go b/app/loop_android.go index 41a5b62..eef7c22 100644 --- a/app/loop_android.go +++ b/app/loop_android.go @@ -101,10 +101,19 @@ func windowDrawLoop(cb Callbacks, w *C.ANativeWindow, queue *C.AInputQueue) { geom.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt) geom.Height = geom.Pt(float32(C.windowHeight) / geom.PixelsPerPt) + // We start here rather than onStart so the window exists and the Gl + // context is configured. + if cb.Start != nil { + cb.Start() + } + for { processEvents(cb, queue) select { case <-windowDestroyed: + if cb.Stop != nil { + cb.Stop() + } return default: if cb.Draw != nil { diff --git a/example/basic/main.go b/example/basic/main.go index 3d3420e..e945d36 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -31,14 +31,14 @@ var ( func main() { app.Run(app.Callbacks{ + Start: start, + Stop: stop, Draw: draw, Touch: touch, }) } -// TODO(crawshaw): Need an easier way to do GL-dependent initialization. - -func initGL() { +func start() { var err error program, err = glutil.CreateProgram(vertexShader, fragmentShader) if err != nil { @@ -54,6 +54,13 @@ func initGL() { color = gl.GetUniformLocation(program, "color") offset = gl.GetUniformLocation(program, "offset") touchLoc = geom.Point{geom.Width / 2, geom.Height / 2} + + // TODO(crawshaw): the debug package needs to put GL state init here +} + +func stop() { + gl.DeleteProgram(program) + gl.DeleteBuffer(buf) } func touch(t event.Touch) { @@ -61,10 +68,6 @@ func touch(t event.Touch) { } func draw() { - if program.Value == 0 { - initGL() - } - gl.ClearColor(1, 0, 0, 1) gl.Clear(gl.COLOR_BUFFER_BIT)