From 8b2ed6cddded0db08f7193405eabc87571bd768d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 2 Feb 2018 02:08:03 +0900 Subject: [PATCH] ui: Update GraphicsContext every frame (#490) --- internal/ui/ui_glfw.go | 35 +++++++++++++++++++++-------------- internal/ui/ui_js.go | 15 ++++++++++----- internal/ui/ui_mobile.go | 20 +++++++++++++------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 083923acb..562f1be7e 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -422,6 +422,23 @@ func (u *userInterface) pollEvents() { currentInput.update(u.window, u.getScale()*glfwScale()) } +func (u *userInterface) updateGraphicsContext(g GraphicsContext) { + actualScale := 0.0 + sizeChanged := false + _ = u.runOnMainThread(func() error { + if !u.sizeChanged { + return nil + } + u.sizeChanged = false + actualScale = u.actualScreenScale() + sizeChanged = true + return nil + }) + if sizeChanged { + g.SetSize(u.width, u.height, actualScale) + } +} + func (u *userInterface) update(g GraphicsContext) error { shouldClose := false _ = u.runOnMainThread(func() error { @@ -441,20 +458,8 @@ func (u *userInterface) update(g GraphicsContext) error { return nil }) - actualScale := 0.0 - sizeChanged := false - _ = u.runOnMainThread(func() error { - if !u.sizeChanged { - return nil - } - u.sizeChanged = false - actualScale = u.actualScreenScale() - sizeChanged = true - return nil - }) - if sizeChanged { - g.SetSize(u.width, u.height, actualScale) - } + // This call is needed for initialization. + u.updateGraphicsContext(g) _ = u.runOnMainThread(func() error { u.pollEvents() @@ -470,6 +475,8 @@ func (u *userInterface) update(g GraphicsContext) error { }) if err := g.Update(func() { currentInput.runeBuffer = currentInput.runeBuffer[:0] + // The offscreens must be updated every frame (#490). + u.updateGraphicsContext(g) }); err != nil { return err } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 2f7cd90c8..aba1d924b 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -123,6 +123,13 @@ func (u *userInterface) actualScreenScale() float64 { return u.getScale() * devicescale.DeviceScale() } +func (u *userInterface) updateGraphicsContext(g GraphicsContext) { + if u.sizeChanged { + u.sizeChanged = false + g.SetSize(u.width, u.height, u.actualScreenScale()) + } +} + func (u *userInterface) update(g GraphicsContext) error { if !u.runnableInBackground && !u.windowFocus { return nil @@ -132,13 +139,11 @@ func (u *userInterface) update(g GraphicsContext) error { g.Invalidate() } currentInput.updateGamepads() - if u.sizeChanged { - u.sizeChanged = false - g.SetSize(u.width, u.height, u.actualScreenScale()) - return nil - } + u.updateGraphicsContext(g) if err := g.Update(func() { currentInput.runeBuffer = nil + // The offscreens must be updated every frame (#490). + u.updateGraphicsContext(g) }); err != nil { return err } diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 9a9c0aeba..222eace26 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -82,12 +82,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext) erro } } -func (u *userInterface) update(g GraphicsContext) error { - <-chRender - defer func() { - chRenderEnd <- struct{}{} - }() - +func (u *userInterface) updateGraphicsContext(g GraphicsContext) { sizeChanged := false width, height := 0, 0 actualScale := 0.0 @@ -106,8 +101,19 @@ func (u *userInterface) update(g GraphicsContext) error { // Sizing also calls GL functions g.SetSize(width, height, actualScale) } +} - if err := g.Update(func() {}); err != nil { +func (u *userInterface) update(g GraphicsContext) error { + <-chRender + defer func() { + chRenderEnd <- struct{}{} + }() + + u.updateGraphicsContext(g) + + if err := g.Update(func() { + u.updateGraphicsContext(g) + }); err != nil { return err } return nil