diff --git a/examples/mobile/main_mobile.go b/examples/mobile/main_mobile.go index acd1d566a..4fe7d6def 100644 --- a/examples/mobile/main_mobile.go +++ b/examples/mobile/main_mobile.go @@ -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 diff --git a/graphicscontext.go b/graphicscontext.go index 7dbf33718..3553755f7 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -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 +} diff --git a/internal/graphics/draw.go b/internal/graphics/draw.go index 983f41f35..8dafe7d7b 100644 --- a/internal/graphics/draw.go +++ b/internal/graphics/draw.go @@ -17,6 +17,7 @@ package graphics import ( "errors" "fmt" + "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) diff --git a/internal/loop/run.go b/internal/loop/run.go index 7597d52bc..2863ec26a 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -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") } diff --git a/internal/ui/event.go b/internal/ui/event.go index ffd6a0473..a5417c01f 100644 --- a/internal/ui/event.go +++ b/internal/ui/event.go @@ -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 { +} diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index b6e90480e..35ed487dc 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -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) diff --git a/mobile/run.go b/mobile/run.go index 8ece510a9..4f69c66d7 100644 --- a/mobile/run.go +++ b/mobile/run.go @@ -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) }