internal/ui: bug fix: the aspect ratio must be updated whenever the window size is forcibly updated

This also adds a new flag -aspectratiofixed to examples/windowsize

Updates #1804
This commit is contained in:
Hajime Hoshi 2022-02-09 19:56:23 +09:00
parent 740dfd5aed
commit 7482cae978
3 changed files with 50 additions and 55 deletions

View File

@ -38,18 +38,19 @@ import (
) )
var ( var (
flagFullscreen = flag.Bool("fullscreen", false, "fullscreen") flagFullscreen = flag.Bool("fullscreen", false, "fullscreen")
flagResizable = flag.Bool("resizable", false, "make the window resizable") flagResizable = flag.Bool("resizable", false, "make the window resizable")
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)") flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
flagTransparent = flag.Bool("transparent", false, "screen transparent") flagTransparent = flag.Bool("transparent", false, "screen transparent")
flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting") flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting")
flagFloating = flag.Bool("floating", false, "make the window floating") flagFloating = flag.Bool("floating", false, "make the window floating")
flagMaximize = flag.Bool("maximize", false, "maximize the window") flagMaximize = flag.Bool("maximize", false, "maximize the window")
flagVsync = flag.Bool("vsync", true, "enable vsync") flagVsync = flag.Bool("vsync", true, "enable vsync")
flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically") flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically")
flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start") flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start")
flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)") flagAspectRatioFixed = flag.Bool("aspectratiofixed", false, "whether the window's aspect ratio is fixed or not")
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)") flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)")
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)")
) )
func init() { func init() {
@ -426,6 +427,10 @@ func main() {
ebiten.SetRunnableOnUnfocused(true) ebiten.SetRunnableOnUnfocused(true)
} }
if *flagAspectRatioFixed {
ebiten.SetWindowAspectRatioFixed(true)
}
minw, minh, maxw, maxh := -1, -1, -1, -1 minw, minh, maxw, maxh := -1, -1, -1, -1
reSize := regexp.MustCompile(`^(\d+)x(\d+)$`) reSize := regexp.MustCompile(`^(\d+)x(\d+)$`)
if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil { if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil {

View File

@ -61,15 +61,16 @@ type UserInterface struct {
maxWindowWidthInDIP int maxWindowWidthInDIP int
maxWindowHeightInDIP int maxWindowHeightInDIP int
running uint32 running uint32
origPosX int origPosX int
origPosY int origPosY int
runnableOnUnfocused bool runnableOnUnfocused bool
fpsMode FPSMode fpsMode FPSMode
iconImages []image.Image iconImages []image.Image
cursorShape CursorShape cursorShape CursorShape
windowClosingHandled bool windowClosingHandled bool
windowBeingClosed bool windowBeingClosed bool
windowAspectRatioFixed bool
// setSizeCallbackEnabled must be accessed from the main thread. // setSizeCallbackEnabled must be accessed from the main thread.
setSizeCallbackEnabled bool setSizeCallbackEnabled bool
@ -85,19 +86,18 @@ type UserInterface struct {
initFullscreenWidthInDIP int initFullscreenWidthInDIP int
initFullscreenHeightInDIP int initFullscreenHeightInDIP int
initFullscreen bool initFullscreen bool
initCursorMode CursorMode initCursorMode CursorMode
initWindowDecorated bool initWindowDecorated bool
initWindowResizable bool initWindowResizable bool
initWindowPositionXInDIP int initWindowPositionXInDIP int
initWindowPositionYInDIP int initWindowPositionYInDIP int
initWindowWidthInDIP int initWindowWidthInDIP int
initWindowHeightInDIP int initWindowHeightInDIP int
initWindowFloating bool initWindowFloating bool
initWindowMaximized bool initWindowMaximized bool
initWindowAspectRatioFixed bool initScreenTransparent bool
initScreenTransparent bool initFocused bool
initFocused bool
fpsModeInited bool fpsModeInited bool
@ -280,16 +280,16 @@ func (u *UserInterface) setWindowSizeLimitsInDIP(minw, minh, maxw, maxh int) boo
return true return true
} }
func (u *UserInterface) isInitWindowAspectRatioFixed() bool { func (u *UserInterface) isWindowAspectRatioFixed() bool {
u.m.RLock() u.m.RLock()
v := u.initWindowAspectRatioFixed v := u.windowAspectRatioFixed
u.m.RUnlock() u.m.RUnlock()
return v return v
} }
func (u *UserInterface) setInitWindowAspectRatioFixed(fixed bool) { func (u *UserInterface) setWindowAspectRatioFixed(fixed bool) {
u.m.Lock() u.m.Lock()
u.initWindowAspectRatioFixed = fixed u.windowAspectRatioFixed = fixed
u.m.Unlock() u.m.Unlock()
} }
@ -932,8 +932,6 @@ func (u *UserInterface) init() error {
u.window.Maximize() u.window.Maximize()
} }
u.setWindowAspectRatioFixed(u.isInitWindowAspectRatioFixed())
u.window.Show() u.window.Show()
if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok {
@ -1306,6 +1304,12 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
} }
} }
n, d := glfw.DontCare, glfw.DontCare
if !fullscreen && u.isWindowAspectRatioFixed() {
n, d = u.window.GetSize()
}
u.window.SetAspectRatio(n, d)
return windowRecreated return windowRecreated
} }
@ -1623,11 +1627,3 @@ func (u *UserInterface) setOrigPos(x, y int) {
u.origPosX = x u.origPosX = x
u.origPosY = y u.origPosY = y
} }
func (u *UserInterface) setWindowAspectRatioFixed(fixed bool) {
n, d := glfw.DontCare, glfw.DontCare
if fixed {
n, d = u.window.GetSize()
}
u.window.SetAspectRatio(n, d)
}

View File

@ -228,13 +228,7 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
} }
func (w *Window) SetAspectRatioFixed(fixed bool) { func (w *Window) SetAspectRatioFixed(fixed bool) {
if !w.ui.isRunning() { w.ui.setWindowAspectRatioFixed(fixed)
w.ui.setInitWindowAspectRatioFixed(fixed)
return
}
w.ui.t.Call(func() {
w.ui.setWindowAspectRatioFixed(fixed)
})
} }
func (w *Window) SetIcon(iconImages []image.Image) { func (w *Window) SetIcon(iconImages []image.Image) {