diff --git a/internal/driver/ui.go b/internal/driver/ui.go index 0cfaf5122..03f6e0468 100644 --- a/internal/driver/ui.go +++ b/internal/driver/ui.go @@ -21,6 +21,7 @@ import ( type UIContext interface { Update(afterFrameUpdate func()) error + Draw() error Layout(outsideWidth, outsideHeight float64) AdjustPosition(x, y float64) (float64, float64) } diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 1291c2695..23c0bf046 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -813,6 +813,9 @@ func (u *UserInterface) update(context driver.UIContext) error { }); err != nil { return err } + if err := context.Draw(); err != nil { + return err + } // Update the screen size when the window is resizable. var w, h int diff --git a/internal/uidriver/js/ui.go b/internal/uidriver/js/ui.go index 27ebba766..f8c6426c3 100644 --- a/internal/uidriver/js/ui.go +++ b/internal/uidriver/js/ui.go @@ -172,6 +172,9 @@ func (u *UserInterface) update() error { }); err != nil { return err } + if err := u.context.Draw(); err != nil { + return err + } return nil } diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index cc26b9417..62e9503e0 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -345,6 +345,9 @@ func (u *UserInterface) update(context driver.UIContext) error { }); err != nil { return err } + if err := context.Draw(); err != nil { + return err + } return nil } diff --git a/uicontext.go b/uicontext.go index daba86085..716b68057 100644 --- a/uicontext.go +++ b/uicontext.go @@ -242,14 +242,27 @@ func (c *uiContext) Update(afterFrameUpdate func()) error { if err := buffered.EndFrame(); err != nil { return err } + return nil +} +func (c *uiContext) Draw() error { + if err, ok := c.err.Load().(error); ok && err != nil { + return err + } + if err := buffered.BeginFrame(); err != nil { + return err + } + c.draw() + if err := buffered.EndFrame(); err != nil { + return err + } return nil } func (c *uiContext) update(afterFrameUpdate func()) error { updateCount := clock.Update(MaxTPS()) - if game, ok := c.game.(interface{ Draw(*Image) }); ok { + if _, ok := c.game.(interface{ Draw(*Image) }); ok { for i := 0; i < updateCount; i++ { c.updateOffscreen() @@ -269,9 +282,6 @@ func (c *uiContext) update(afterFrameUpdate func()) error { uiDriver().Input().ResetForFrame() afterFrameUpdate() } - - c.offscreen.Clear() - game.Draw(c.offscreen) } else { for i := 0; i < updateCount; i++ { c.updateOffscreen() @@ -290,10 +300,18 @@ func (c *uiContext) update(afterFrameUpdate func()) error { afterFrameUpdate() } } + return nil +} +func (c *uiContext) draw() { // c.screen might be nil when updateCount is 0 in the initial state (#1039). if c.screen == nil { - return nil + return + } + + if game, ok := c.game.(interface{ Draw(*Image) }); ok { + c.offscreen.Clear() + game.Draw(c.offscreen) } // This clear is needed for fullscreen mode or some mobile platforms (#622). @@ -326,7 +344,6 @@ func (c *uiContext) update(afterFrameUpdate func()) error { op.Filter = FilterLinear } _ = c.screen.DrawImage(c.offscreen, op) - return nil } func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {