app: Start and Stop callbacks

Change-Id: If8ea6aaf2fb2c62eaf4119526a8bb46b8a84b982
Reviewed-on: https://go-review.googlesource.com/1881
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
David Crawshaw 2014-12-19 10:28:05 -05:00
parent 8952e43db8
commit 1aa04cf038
3 changed files with 48 additions and 7 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)