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"
)
// 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

View File

@ -87,3 +87,26 @@ func (c *graphicsContext) Update() error {
}
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 (
"errors"
"fmt"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)

View File

@ -95,6 +95,8 @@ func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
type GraphicsContext interface {
SetSize(width, height, scale int) error
Update() error
Pause() error
Resume() 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
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:
panic("not reach")
}

View File

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

View File

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

View File

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