diff --git a/internal/uidriver/glfw/input.go b/internal/uidriver/glfw/input.go index beef9bf75..19c48fd6b 100644 --- a/internal/uidriver/glfw/input.go +++ b/internal/uidriver/glfw/input.go @@ -333,8 +333,9 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) { i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press } cx, cy = window.GetCursorPos() - cx = i.ui.toDeviceIndependentPixel(cx) - cy = i.ui.toDeviceIndependentPixel(cy) + // TODO: This is tricky. Rename the function? + cx = i.ui.fromGLFWMonitorPixel(cx) + cy = i.ui.fromGLFWMonitorPixel(cy) return nil }) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 03ddf439d..c782e9cd1 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -22,6 +22,7 @@ package glfw import ( "fmt" "image" + "math" "os" "runtime" "sync" @@ -143,12 +144,12 @@ func initialize() error { panic("glfw: glfw.CreateWindow must not return nil") } - // Create a window and set it: this affects toDeviceIndependentPixel and deviceScaleFactor. + // Create a window and set it: this affects fromGLFWMonitorPixel and deviceScaleFactor. theUI.window = w theUI.initMonitor = currentMonitor(w) v := theUI.initMonitor.GetVideoMode() - theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width))) - theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height))) + theUI.initFullscreenWidthInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Width))) + theUI.initFullscreenHeightInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Height))) theUI.window.Destroy() theUI.window = nil @@ -398,8 +399,8 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) { var w, h int _ = u.t.Call(func() error { v := currentMonitor(u.window).GetVideoMode() - w = int(u.toDeviceIndependentPixel(float64(v.Width))) - h = int(u.toDeviceIndependentPixel(float64(v.Height))) + w = int(u.fromGLFWMonitorPixel(float64(v.Width))) + h = int(u.fromGLFWMonitorPixel(float64(v.Height))) return nil }) return w, h @@ -792,17 +793,19 @@ func (u *UserInterface) updateSize() { if u.isFullscreen() { v := currentMonitor(u.window).GetVideoMode() ww, wh := v.Width, v.Height - w = u.toDeviceIndependentPixel(float64(ww)) - h = u.toDeviceIndependentPixel(float64(wh)) + w = u.fromGLFWMonitorPixel(float64(ww)) + h = u.fromGLFWMonitorPixel(float64(wh)) } else { // Instead of u.windowWidth and u.windowHeight, use the actual window size here. // On Windows, the specified size at SetSize and the actual window size might not // match (#1163). ww, wh := u.window.GetSize() - // TODO: Is this correct? - w = u.toDeviceIndependentPixel(float64(ww)) - h = u.toDeviceIndependentPixel(float64(wh)) + w = u.fromGLFWPixel(float64(ww)) + h = u.fromGLFWPixel(float64(wh)) } + // On Linux/UNIX, further adjusting is required (#1307). + w = math.Ceil(u.toFramebufferPixel(w)) + h = math.Ceil(u.toFramebufferPixel(h)) return nil }) u.context.Layout(w, h) @@ -1152,7 +1155,7 @@ func (u *UserInterface) SetInitFocused(focused bool) { } func (u *UserInterface) monitorPosition() (int, int) { - // TODO: toDeviceIndependentPixel might be required. + // TODO: fromGLFWMonitorPixel might be required. return currentMonitor(u.window).GetPos() } diff --git a/internal/uidriver/glfw/ui_darwin.go b/internal/uidriver/glfw/ui_darwin.go index 436cd4578..33ebd0943 100644 --- a/internal/uidriver/glfw/ui_darwin.go +++ b/internal/uidriver/glfw/ui_darwin.go @@ -45,7 +45,7 @@ import ( "github.com/hajimehoshi/ebiten/internal/glfw" ) -func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { +func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 { return x } diff --git a/internal/uidriver/glfw/ui_unix.go b/internal/uidriver/glfw/ui_unix.go index c9ff3c23d..05e821adb 100644 --- a/internal/uidriver/glfw/ui_unix.go +++ b/internal/uidriver/glfw/ui_unix.go @@ -22,8 +22,8 @@ import ( "github.com/hajimehoshi/ebiten/internal/glfw" ) -// toDeviceIndependentPixel must be called from the main thread. -func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { +// fromGLFWMonitorPixel must be called from the main thread. +func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 { return x / u.deviceScaleFactor() } @@ -37,6 +37,11 @@ func (u *UserInterface) toGLFWPixel(x float64) float64 { return x } +// toFramebufferPixel must be called from the main thread. +func (u *UserInterface) toFramebufferPixel(x float64) float64 { + return x / u.deviceScaleFactor() +} + func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { return x, y } diff --git a/internal/uidriver/glfw/ui_windows.go b/internal/uidriver/glfw/ui_windows.go index 2aeb4410f..ad1e2a35e 100644 --- a/internal/uidriver/glfw/ui_windows.go +++ b/internal/uidriver/glfw/ui_windows.go @@ -99,8 +99,8 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { return nil } -// toDeviceIndependentPixel must be called from the main thread. -func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { +// fromGLFWMonitorPixel must be called from the main thread. +func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 { return x / u.deviceScaleFactor() }