diff --git a/gameforui.go b/gameforui.go index f43ed0fce..b61cc62ee 100644 --- a/gameforui.go +++ b/gameforui.go @@ -79,6 +79,7 @@ type gameForUI struct { offscreen *Image screen *Image screenShader *Shader + imageDumper imageDumper } func newGameForUI(game Game) *gameForUI { @@ -129,11 +130,21 @@ func (g *gameForUI) Layout(outsideWidth, outsideHeight int) (int, int) { } func (g *gameForUI) Update() error { - return g.game.Update() + if err := g.game.Update(); err != nil { + return err + } + if err := g.imageDumper.update(); err != nil { + return err + } + return nil } -func (g *gameForUI) DrawOffscreen() { +func (g *gameForUI) DrawOffscreen() error { g.game.Draw(g.offscreen) + if err := g.imageDumper.dump(g.offscreen); err != nil { + return err + } + return nil } func (g *gameForUI) DrawScreen() { diff --git a/imagedumper_mobile.go b/imagedumper_mobile.go index 6b6a6ef81..ff2451290 100644 --- a/imagedumper_mobile.go +++ b/imagedumper_mobile.go @@ -18,11 +18,11 @@ package ebiten type imageDumper struct { - g Game } func (i *imageDumper) update() error { - return i.g.Update() + // Do nothing + return nil } func (i *imageDumper) dump(screen *Image) error { diff --git a/imagedumper_notmobile.go b/imagedumper_notmobile.go index 2310b7771..8079542b1 100644 --- a/imagedumper_notmobile.go +++ b/imagedumper_notmobile.go @@ -57,8 +57,6 @@ func dumpInternalImages() error { } type imageDumper struct { - g Game - keyState map[Key]int hasScreenshotKey bool @@ -93,10 +91,6 @@ func (i *imageDumper) update() error { return i.err } - if err := i.g.Update(); err != nil { - return err - } - // If keyState is nil, all values are not initialized. if i.keyState == nil { i.keyState = map[Key]int{} diff --git a/internal/ui/context.go b/internal/ui/context.go index 708552a99..5d3f48aa1 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -38,7 +38,7 @@ type Game interface { NewScreenImage(width, height int) *Image Layout(outsideWidth, outsideHeight int) (int, int) Update() error - DrawOffscreen() + DrawOffscreen() error DrawScreen() ScreenScaleAndOffsets() (scale, offsetX, offsetY float64) } @@ -143,14 +143,16 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update } // Draw the game. - c.drawGame(graphicsDriver) + if err := c.drawGame(graphicsDriver); err != nil { + return err + } // All the vertices data are consumed at the end of the frame, and the data backend can be // available after that. Until then, lock the vertices backend. return nil } -func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) { +func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) error { if c.offscreen.volatile != theGlobalState.isScreenClearedEveryFrame() { w, h := c.offscreen.width, c.offscreen.height c.offscreen.MarkDisposed() @@ -163,7 +165,9 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) { if theGlobalState.isScreenClearedEveryFrame() { c.offscreen.clear() } - c.game.DrawOffscreen() + if err := c.game.DrawOffscreen(); err != nil { + return err + } if graphicsDriver.NeedsClearingScreen() { // This clear is needed for fullscreen mode or some mobile platforms (#622). @@ -171,6 +175,7 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) { } c.game.DrawScreen() + return nil } func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) { diff --git a/run.go b/run.go index 5fede6fd8..c6420391c 100644 --- a/run.go +++ b/run.go @@ -143,35 +143,6 @@ func IsScreenFilterEnabled() bool { return isScreenFilterEnabled() } -type imageDumperGame struct { - game Game - d *imageDumper - err error -} - -func (i *imageDumperGame) Update() error { - if i.err != nil { - return i.err - } - if i.d == nil { - i.d = &imageDumper{g: i.game} - } - return i.d.update() -} - -func (i *imageDumperGame) Draw(screen *Image) { - if i.err != nil { - return - } - - i.game.Draw(screen) - i.err = i.d.dump(screen) -} - -func (i *imageDumperGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { - return i.game.Layout(outsideWidth, outsideHeight) -} - // Termination is a special error which indicates Game termination without error. var Termination = ui.RegularTermination @@ -206,9 +177,7 @@ func RunGame(game Game) error { defer atomic.StoreInt32(&isRunGameEnded_, 1) initializeWindowPositionIfNeeded(WindowSize()) - g := newGameForUI(&imageDumperGame{ - game: game, - }) + g := newGameForUI(game) if err := ui.Get().Run(g); err != nil { if errors.Is(err, Termination) { return nil