mirror of
https://github.com/go-i2p/gomobile-java.git
synced 2025-07-19 10:35:48 -04:00
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:
29
app/app.go
29
app/app.go
@@ -20,6 +20,35 @@ func Run(cb Callbacks) {
|
|||||||
|
|
||||||
// Callbacks is the set of functions called by the app.
|
// Callbacks is the set of functions called by the app.
|
||||||
type Callbacks struct {
|
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.
|
// Draw is called by the render loop to draw the screen.
|
||||||
//
|
//
|
||||||
// Drawing is done into a framebuffer, which is then swapped onto the
|
// Drawing is done into a framebuffer, which is then swapped onto the
|
||||||
|
@@ -101,10 +101,19 @@ func windowDrawLoop(cb Callbacks, w *C.ANativeWindow, queue *C.AInputQueue) {
|
|||||||
geom.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt)
|
geom.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt)
|
||||||
geom.Height = geom.Pt(float32(C.windowHeight) / 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 {
|
for {
|
||||||
processEvents(cb, queue)
|
processEvents(cb, queue)
|
||||||
select {
|
select {
|
||||||
case <-windowDestroyed:
|
case <-windowDestroyed:
|
||||||
|
if cb.Stop != nil {
|
||||||
|
cb.Stop()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
if cb.Draw != nil {
|
if cb.Draw != nil {
|
||||||
|
@@ -31,14 +31,14 @@ var (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app.Run(app.Callbacks{
|
app.Run(app.Callbacks{
|
||||||
|
Start: start,
|
||||||
|
Stop: stop,
|
||||||
Draw: draw,
|
Draw: draw,
|
||||||
Touch: touch,
|
Touch: touch,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(crawshaw): Need an easier way to do GL-dependent initialization.
|
func start() {
|
||||||
|
|
||||||
func initGL() {
|
|
||||||
var err error
|
var err error
|
||||||
program, err = glutil.CreateProgram(vertexShader, fragmentShader)
|
program, err = glutil.CreateProgram(vertexShader, fragmentShader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -54,6 +54,13 @@ func initGL() {
|
|||||||
color = gl.GetUniformLocation(program, "color")
|
color = gl.GetUniformLocation(program, "color")
|
||||||
offset = gl.GetUniformLocation(program, "offset")
|
offset = gl.GetUniformLocation(program, "offset")
|
||||||
touchLoc = geom.Point{geom.Width / 2, geom.Height / 2}
|
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) {
|
func touch(t event.Touch) {
|
||||||
@@ -61,10 +68,6 @@ func touch(t event.Touch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func draw() {
|
func draw() {
|
||||||
if program.Value == 0 {
|
|
||||||
initGL()
|
|
||||||
}
|
|
||||||
|
|
||||||
gl.ClearColor(1, 0, 0, 1)
|
gl.ClearColor(1, 0, 0, 1)
|
||||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user