ui: Update GraphicsContext every frame (#490)

This commit is contained in:
Hajime Hoshi 2018-02-02 02:08:03 +09:00
parent 484d9797a3
commit 8b2ed6cddd
3 changed files with 44 additions and 26 deletions

View File

@ -422,6 +422,23 @@ func (u *userInterface) pollEvents() {
currentInput.update(u.window, u.getScale()*glfwScale()) 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 { func (u *userInterface) update(g GraphicsContext) error {
shouldClose := false shouldClose := false
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
@ -441,20 +458,8 @@ func (u *userInterface) update(g GraphicsContext) error {
return nil return nil
}) })
actualScale := 0.0 // This call is needed for initialization.
sizeChanged := false u.updateGraphicsContext(g)
_ = 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)
}
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
u.pollEvents() u.pollEvents()
@ -470,6 +475,8 @@ func (u *userInterface) update(g GraphicsContext) error {
}) })
if err := g.Update(func() { if err := g.Update(func() {
currentInput.runeBuffer = currentInput.runeBuffer[:0] currentInput.runeBuffer = currentInput.runeBuffer[:0]
// The offscreens must be updated every frame (#490).
u.updateGraphicsContext(g)
}); err != nil { }); err != nil {
return err return err
} }

View File

@ -123,6 +123,13 @@ func (u *userInterface) actualScreenScale() float64 {
return u.getScale() * devicescale.DeviceScale() 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 { func (u *userInterface) update(g GraphicsContext) error {
if !u.runnableInBackground && !u.windowFocus { if !u.runnableInBackground && !u.windowFocus {
return nil return nil
@ -132,13 +139,11 @@ func (u *userInterface) update(g GraphicsContext) error {
g.Invalidate() g.Invalidate()
} }
currentInput.updateGamepads() currentInput.updateGamepads()
if u.sizeChanged { u.updateGraphicsContext(g)
u.sizeChanged = false
g.SetSize(u.width, u.height, u.actualScreenScale())
return nil
}
if err := g.Update(func() { if err := g.Update(func() {
currentInput.runeBuffer = nil currentInput.runeBuffer = nil
// The offscreens must be updated every frame (#490).
u.updateGraphicsContext(g)
}); err != nil { }); err != nil {
return err return err
} }

View File

@ -82,12 +82,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext) erro
} }
} }
func (u *userInterface) update(g GraphicsContext) error { func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
<-chRender
defer func() {
chRenderEnd <- struct{}{}
}()
sizeChanged := false sizeChanged := false
width, height := 0, 0 width, height := 0, 0
actualScale := 0.0 actualScale := 0.0
@ -106,8 +101,19 @@ func (u *userInterface) update(g GraphicsContext) error {
// Sizing also calls GL functions // Sizing also calls GL functions
g.SetSize(width, height, actualScale) 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 err
} }
return nil return nil