mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
uidriver/glfw: Force to swap buffer or recreate an window after toggling fullscreen
This change resolves a bug that an image lag remains after toggling fullscreen. On OpenGL, swapping buffer is necessary after toggling fullscreen. On Metal, recreating a window works after toggling fullscreen. Fixes #1004
This commit is contained in:
parent
2180613e2a
commit
80fbc9701c
@ -21,6 +21,7 @@ package glfw
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
"math"
|
||||
"os"
|
||||
@ -323,22 +324,14 @@ func (u *UserInterface) SetScreenSize(width, height int) {
|
||||
if !u.isRunning() {
|
||||
panic("glfw: SetScreenSize can't be called before the main loop starts")
|
||||
}
|
||||
_ = u.t.Call(func() error {
|
||||
// TODO: What if the window is maximized? (#320)
|
||||
u.setScreenSize(width, height, u.scale, u.isFullscreen(), u.vsync)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UserInterface) SetScreenScale(scale float64) {
|
||||
if !u.isRunning() {
|
||||
panic("glfw: SetScreenScale can't be called before the main loop starts")
|
||||
}
|
||||
_ = u.t.Call(func() error {
|
||||
// TODO: What if the window is maximized? (#320)
|
||||
u.setScreenSize(u.width, u.height, scale, u.isFullscreen(), u.vsync)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UserInterface) ScreenScale() float64 {
|
||||
@ -378,10 +371,7 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) {
|
||||
u.setInitFullscreen(fullscreen)
|
||||
return
|
||||
}
|
||||
_ = u.t.Call(func() error {
|
||||
u.setScreenSize(u.width, u.height, u.scale, fullscreen, u.vsync)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UserInterface) SetRunnableInBackground(runnableInBackground bool) {
|
||||
@ -403,10 +393,7 @@ func (u *UserInterface) SetVsyncEnabled(enabled bool) {
|
||||
u.m.Unlock()
|
||||
return
|
||||
}
|
||||
_ = u.t.Call(func() error {
|
||||
u.setScreenSize(u.width, u.height, u.scale, u.isFullscreen(), enabled)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UserInterface) IsVsyncEnabled() bool {
|
||||
@ -676,6 +663,12 @@ func (u *UserInterface) createWindow() error {
|
||||
}
|
||||
|
||||
func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext) error {
|
||||
var (
|
||||
m *glfw.Monitor
|
||||
mx, my int
|
||||
v *glfw.VidMode
|
||||
)
|
||||
|
||||
if err := u.t.Call(func() error {
|
||||
if u.graphics.IsGL() {
|
||||
glfw.WindowHint(glfw.ContextVersionMajor, 2)
|
||||
@ -728,13 +721,20 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
||||
// there is not the active window but the foreground window. After showing the current window, the
|
||||
// current window will be the active window. Thus, currentMonitor result varies before and after
|
||||
// showing the window.
|
||||
m := u.currentMonitor()
|
||||
mx, my := m.GetPos()
|
||||
v := m.GetVideoMode()
|
||||
m = u.currentMonitor()
|
||||
mx, my = m.GetPos()
|
||||
v = m.GetVideoMode()
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// The game is in window mode (not fullscreen mode) at the first state.
|
||||
// Don't refer u.initFullscreen here to avoid some GLFW problems.
|
||||
u.setScreenSize(width, height, scale, false, u.vsync)
|
||||
|
||||
_ = u.t.Call(func() error {
|
||||
// Get the window size before showing since window.Show might change the current
|
||||
// monitor which affects glfwSize result.
|
||||
w, h := u.glfwSize()
|
||||
@ -754,9 +754,7 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
||||
u.window.SetPos(x, y)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
})
|
||||
|
||||
var w unsafe.Pointer
|
||||
_ = u.t.Call(func() error {
|
||||
@ -807,10 +805,10 @@ func (u *UserInterface) monitorScale() float64 {
|
||||
}
|
||||
|
||||
func (u *UserInterface) updateSize(context driver.UIContext) {
|
||||
sizeChanged := false
|
||||
_ = u.t.Call(func() error {
|
||||
u.setScreenSize(u.width, u.height, u.scale, u.isFullscreen(), u.vsync)
|
||||
|
||||
sizeChanged := false
|
||||
_ = u.t.Call(func() error {
|
||||
if !u.toChangeSize {
|
||||
return nil
|
||||
}
|
||||
@ -839,13 +837,10 @@ func (u *UserInterface) update(context driver.UIContext) error {
|
||||
return driver.RegularTermination
|
||||
}
|
||||
|
||||
_ = u.t.Call(func() error {
|
||||
if u.isInitFullscreen() {
|
||||
u.setScreenSize(u.width, u.height, u.scale, true, u.vsync)
|
||||
u.setInitFullscreen(false)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// This call is needed for initialization.
|
||||
u.updateSize(context)
|
||||
@ -876,7 +871,7 @@ func (u *UserInterface) update(context driver.UIContext) error {
|
||||
}
|
||||
|
||||
// Update the screen size when the window is resizable.
|
||||
_ = u.t.Call(func() error {
|
||||
// TODO: Need locks.
|
||||
w, h := u.reqWidth, u.reqHeight
|
||||
if w != 0 || h != 0 {
|
||||
u.setScreenSize(w, h, u.scale, u.isFullscreen(), u.vsync)
|
||||
@ -884,8 +879,6 @@ func (u *UserInterface) update(context driver.UIContext) error {
|
||||
u.reqWidth = 0
|
||||
u.reqHeight = 0
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserInterface) loop(context driver.UIContext) error {
|
||||
@ -941,10 +934,12 @@ func (u *UserInterface) swapBuffers() {
|
||||
}
|
||||
}
|
||||
|
||||
// setScreenSize must be called from the main thread.
|
||||
func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscreen bool, vsync bool) {
|
||||
windowRecreated := false
|
||||
|
||||
_ = u.t.Call(func() error {
|
||||
if u.width == width && u.height == height && u.scale == scale && u.isFullscreen() == fullscreen && u.vsync == vsync && u.lastMonitorScale == u.monitorScale() {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// On Windows, giving a too small width doesn't call a callback (#165).
|
||||
@ -985,11 +980,33 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
|
||||
m := u.currentMonitor()
|
||||
v := m.GetVideoMode()
|
||||
u.window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate)
|
||||
|
||||
// Swapping buffer is necesary to prevent the image lag (#1004).
|
||||
if u.graphics.IsGL() {
|
||||
glfw.PollEvents()
|
||||
u.swapBuffers()
|
||||
}
|
||||
} else {
|
||||
if u.window.GetMonitor() != nil {
|
||||
// Give dummy values as the window position and size.
|
||||
// The new window position should be specifying after SetSize.
|
||||
if u.graphics.IsGL() {
|
||||
// When OpenGL is used, swapping buffer is enough to solve the image-lag
|
||||
// issue (#1004). Rather, recreating window destroys GPU resources.
|
||||
u.window.SetMonitor(nil, 0, 0, 16, 16, 0)
|
||||
glfw.PollEvents()
|
||||
u.swapBuffers()
|
||||
} else {
|
||||
// Recreate the window since an image lag remains after coming back from
|
||||
// fullscreen (#1004).
|
||||
if u.window != nil {
|
||||
u.window.Destroy()
|
||||
u.window = nil
|
||||
}
|
||||
if err := u.createWindow(); err != nil {
|
||||
// TODO: This should return an error.
|
||||
panic(fmt.Sprintf("glfw: failed to recreate window: %v", err))
|
||||
}
|
||||
windowRecreated = true
|
||||
}
|
||||
}
|
||||
|
||||
oldW, oldH := u.window.GetSize()
|
||||
@ -1016,8 +1033,8 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
|
||||
x := u.origPosX
|
||||
y := u.origPosY
|
||||
u.window.SetPos(x, y)
|
||||
// Dirty hack for macOS (#703). Rendering doesn't work correctly with one SetPos, but work
|
||||
// with two or more SetPos.
|
||||
// Dirty hack for macOS (#703). Rendering doesn't work correctly with one SetPos, but
|
||||
// work with two or more SetPos.
|
||||
if runtime.GOOS == "darwin" {
|
||||
u.window.SetPos(x+1, y)
|
||||
u.window.SetPos(x, y)
|
||||
@ -1047,6 +1064,12 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
|
||||
u.graphics.SetVsyncEnabled(vsync)
|
||||
|
||||
u.toChangeSize = true
|
||||
return nil
|
||||
})
|
||||
|
||||
if windowRecreated {
|
||||
u.graphics.SetWindow(u.nativeWindow())
|
||||
}
|
||||
}
|
||||
|
||||
// currentMonitor returns the monitor most suitable with the current window.
|
||||
|
Loading…
Reference in New Issue
Block a user