mobile: Implement Pause/Resume

This commit is contained in:
Hajime Hoshi 2016-05-24 00:00:54 +09:00
parent 2b477eef5a
commit 3195ae32d8
7 changed files with 78 additions and 10 deletions

View File

@ -29,7 +29,7 @@ import (
"github.com/hajimehoshi/ebiten/mobile" "github.com/hajimehoshi/ebiten/mobile"
) )
// EventDispacher must be redeclared and exported so that this is available on the Java/Objective-C side. // EventDispacher must be redeclared and exported to be available on the Java/Objective-C side.
type EventDispatcher mobile.EventDispatcher type EventDispatcher mobile.EventDispatcher

View File

@ -87,3 +87,26 @@ func (c *graphicsContext) Update() error {
} }
return nil return nil
} }
func (c *graphicsContext) Pause() error {
if err := theImages.evacuatePixels(); err != nil {
return err
}
if err := graphics.Finalize(ui.GLContext()); err != nil {
return err
}
return nil
}
func (c *graphicsContext) Resume() error {
if !c.imageTasksDone {
return nil
}
if err := graphics.Initialize(ui.GLContext()); err != nil {
return err
}
if err := theImages.restorePixels(); err != nil {
return err
}
return nil
}

View File

@ -17,6 +17,7 @@ package graphics
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
) )

View File

@ -95,6 +95,8 @@ func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
type GraphicsContext interface { type GraphicsContext interface {
SetSize(width, height, scale int) error SetSize(width, height, scale int) error
Update() error Update() error
Pause() error
Resume() error
} }
func Run(g GraphicsContext, width, height, scale int, title string, fps int) error { func Run(g GraphicsContext, width, height, scale int, title string, fps int) error {
@ -160,6 +162,14 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
beforeForFPS = n2 beforeForFPS = n2
frames = 0 frames = 0
} }
case ui.PauseEvent:
if err := g.Pause(); err != nil {
return err
}
case ui.ResumeEvent:
if err := g.Resume(); err != nil {
return err
}
default: default:
panic("not reach") panic("not reach")
} }

View File

@ -17,12 +17,18 @@ package ui
type CloseEvent struct { type CloseEvent struct {
} }
type RenderEvent struct {
}
type ScreenSizeEvent struct { type ScreenSizeEvent struct {
Width int Width int
Height int Height int
Scale int Scale int
ActualScale int ActualScale int
} }
type RenderEvent struct {
}
type PauseEvent struct {
}
type ResumeEvent struct {
}

View File

@ -56,8 +56,10 @@ type userInterface struct {
height int height int
scale int scale int
sizeChanged bool sizeChanged bool
render chan struct{} chRender chan struct{}
renderEnd chan struct{} chRenderEnd chan struct{}
chPause chan struct{}
chResume chan struct{}
} }
var ( var (
@ -65,8 +67,8 @@ var (
chRenderEnd = make(chan struct{}) chRenderEnd = make(chan struct{})
currentUI = &userInterface{ currentUI = &userInterface{
sizeChanged: true, sizeChanged: true,
render: chRender, chRender: chRender,
renderEnd: chRenderEnd, chRenderEnd: chRenderEnd,
} }
) )
@ -99,7 +101,11 @@ func (u *userInterface) Update() (interface{}, error) {
return e, nil return e, nil
} }
select { select {
case <-u.render: case <-u.chPause:
return PauseEvent{}, nil
case <-u.chResume:
return ResumeEvent{}, nil
case <-u.chRender:
return RenderEvent{}, nil return RenderEvent{}, nil
} }
} }
@ -109,7 +115,7 @@ func (u *userInterface) SwapBuffers() error {
} }
func (u *userInterface) FinishRendering() error { func (u *userInterface) FinishRendering() error {
u.renderEnd <- struct{}{} u.chRenderEnd <- struct{}{}
return nil return nil
} }
@ -131,6 +137,18 @@ func (u *userInterface) actualScreenScale() int {
return u.scale return u.scale
} }
func Pause() {
go func() {
currentUI.chPause <- struct{}{}
}()
}
func Resume() {
go func() {
currentUI.chResume <- struct{}{}
}()
}
func TouchDown(x, y int) { func TouchDown(x, y int) {
s := currentUI.actualScreenScale() s := currentUI.actualScreenScale()
currentInput.touchDown(x/s, y/s) currentInput.touchDown(x/s, y/s)

View File

@ -30,6 +30,8 @@ type EventDispatcher interface {
SetScreenSize(width, height int) SetScreenSize(width, height int)
SetScreenScale(scale int) SetScreenScale(scale int)
Render() error Render() error
Pause()
Resume()
TouchDown(x, y int) TouchDown(x, y int)
TouchUp(x, y int) TouchUp(x, y int)
TouchMove(x, y int) TouchMove(x, y int)
@ -64,6 +66,14 @@ func (e *eventDispatcher) Render() error {
return ui.Render(chError) return ui.Render(chError)
} }
func (e *eventDispatcher) Pause() {
ui.Pause()
}
func (e *eventDispatcher) Resume() {
ui.Resume()
}
func (e *eventDispatcher) TouchDown(x, y int) { func (e *eventDispatcher) TouchDown(x, y int) {
ui.TouchDown(x, y) ui.TouchDown(x, y)
} }