From 5278a7c6d63ae43d8ad7949f4440bf19e2b18bf7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 19 Sep 2020 00:21:08 +0900 Subject: [PATCH] uidriver/glfw: Bug fix: Use the correct scale for GLFW APIs on Linux/UNIX Updates #1307 --- internal/uidriver/glfw/ui.go | 16 +++------------- internal/uidriver/glfw/ui_darwin.go | 8 ++++++-- internal/uidriver/glfw/ui_unix.go | 10 ++++++++-- internal/uidriver/glfw/ui_windows.go | 10 ++++++++-- internal/uidriver/glfw/window.go | 8 ++++---- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 76163f52c..29900ec43 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -390,16 +390,6 @@ func (u *UserInterface) setInitFocused(focused bool) { u.m.Unlock() } -// toDeviceIndependentPixel must be called from the main thread. -func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { - return x / u.glfwScale() -} - -// toDeviceDependentPixel must be called from the main thread. -func (u *UserInterface) toDeviceDependentPixel(x float64) float64 { - return x * u.glfwScale() -} - func (u *UserInterface) ScreenSizeInFullscreen() (int, int) { if !u.isRunning() { return u.initFullscreenWidthInDP, u.initFullscreenHeightInDP @@ -736,8 +726,8 @@ func (u *UserInterface) run() error { } setSize := func() { ww, wh := u.getInitWindowSize() - ww = int(u.toDeviceDependentPixel(float64(ww))) - wh = int(u.toDeviceDependentPixel(float64(wh))) + ww = int(u.toGLFWPixel(float64(ww))) + wh = int(u.toGLFWPixel(float64(wh))) u.setWindowSize(ww, wh, u.isFullscreen()) } @@ -1000,7 +990,7 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) { // On Windows, giving a too small width doesn't call a callback (#165). // To prevent hanging up, return asap if the width is too small. // 126 is an arbitrary number and I guess this is small enough. - minWindowWidth := int(u.toDeviceDependentPixel(126)) + minWindowWidth := int(u.toGLFWPixel(126)) if u.window.GetAttrib(glfw.Decorated) == glfw.False { minWindowWidth = 1 } diff --git a/internal/uidriver/glfw/ui_darwin.go b/internal/uidriver/glfw/ui_darwin.go index 61a4e8ced..7839b649e 100644 --- a/internal/uidriver/glfw/ui_darwin.go +++ b/internal/uidriver/glfw/ui_darwin.go @@ -45,8 +45,12 @@ import ( "github.com/hajimehoshi/ebiten/internal/glfw" ) -func (u *UserInterface) glfwScale() float64 { - return 1 +func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { + return x +} + +func (u *UserInterface) toGLFWPixel(x float64) float64 { + return x } func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { diff --git a/internal/uidriver/glfw/ui_unix.go b/internal/uidriver/glfw/ui_unix.go index 7b825841e..da130a227 100644 --- a/internal/uidriver/glfw/ui_unix.go +++ b/internal/uidriver/glfw/ui_unix.go @@ -22,8 +22,14 @@ import ( "github.com/hajimehoshi/ebiten/internal/glfw" ) -func (u *UserInterface) glfwScale() float64 { - return u.deviceScaleFactor() +// toDeviceIndependentPixel must be called from the main thread. +func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { + return x / u.deviceScaleFactor() +} + +// toGLFWPixel must be called from the main thread. +func (u *UserInterface) toGLFWPixel(x float64) float64 { + return x } func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { diff --git a/internal/uidriver/glfw/ui_windows.go b/internal/uidriver/glfw/ui_windows.go index 679ae56e8..d427ffa3e 100644 --- a/internal/uidriver/glfw/ui_windows.go +++ b/internal/uidriver/glfw/ui_windows.go @@ -99,8 +99,14 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { return nil } -func (u *UserInterface) glfwScale() float64 { - return u.deviceScaleFactor() +// toDeviceIndependentPixel must be called from the main thread. +func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { + return x / u.deviceScaleFactor() +} + +// toGLFWPixel must be called from the main thread. +func (u *UserInterface) toGLFWPixel(x float64) float64 { + return x * u.deviceScaleFactor() } func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { diff --git a/internal/uidriver/glfw/window.go b/internal/uidriver/glfw/window.go index 2451503a2..70f475a9a 100644 --- a/internal/uidriver/glfw/window.go +++ b/internal/uidriver/glfw/window.go @@ -210,8 +210,8 @@ func (w *window) SetPosition(x, y int) { w.setPositionCalled = true }() mx, my := currentMonitor(w.ui.window).GetPos() - xf := w.ui.toDeviceDependentPixel(float64(x)) - yf := w.ui.toDeviceDependentPixel(float64(y)) + xf := w.ui.toGLFWPixel(float64(x)) + yf := w.ui.toGLFWPixel(float64(y)) x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf)) if w.ui.isFullscreen() { w.ui.origPosX, w.ui.origPosY = x, y @@ -236,8 +236,8 @@ func (w *window) SetSize(width, height int) { w.ui.setInitWindowSize(width, height) return } - ww := int(w.ui.toDeviceDependentPixel(float64(width))) - wh := int(w.ui.toDeviceDependentPixel(float64(height))) + ww := int(w.ui.toGLFWPixel(float64(width))) + wh := int(w.ui.toGLFWPixel(float64(height))) w.ui.setWindowSize(ww, wh, w.ui.isFullscreen()) }