uidriver/glfw: Bug fix: Unscale the size for framebuffers on Linux/UNIX

Fixes #1307
This commit is contained in:
Hajime Hoshi 2020-09-19 00:56:39 +09:00
parent 97607f5779
commit 40e35fa047
5 changed files with 27 additions and 18 deletions

View File

@ -333,8 +333,9 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) {
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
} }
cx, cy = window.GetCursorPos() cx, cy = window.GetCursorPos()
cx = i.ui.toDeviceIndependentPixel(cx) // TODO: This is tricky. Rename the function?
cy = i.ui.toDeviceIndependentPixel(cy) cx = i.ui.fromGLFWMonitorPixel(cx)
cy = i.ui.fromGLFWMonitorPixel(cy)
return nil return nil
}) })

View File

@ -22,6 +22,7 @@ package glfw
import ( import (
"fmt" "fmt"
"image" "image"
"math"
"os" "os"
"runtime" "runtime"
"sync" "sync"
@ -143,12 +144,12 @@ func initialize() error {
panic("glfw: glfw.CreateWindow must not return nil") 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.window = w
theUI.initMonitor = currentMonitor(w) theUI.initMonitor = currentMonitor(w)
v := theUI.initMonitor.GetVideoMode() v := theUI.initMonitor.GetVideoMode()
theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width))) theUI.initFullscreenWidthInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Width)))
theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height))) theUI.initFullscreenHeightInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Height)))
theUI.window.Destroy() theUI.window.Destroy()
theUI.window = nil theUI.window = nil
@ -398,8 +399,8 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
var w, h int var w, h int
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
v := currentMonitor(u.window).GetVideoMode() v := currentMonitor(u.window).GetVideoMode()
w = int(u.toDeviceIndependentPixel(float64(v.Width))) w = int(u.fromGLFWMonitorPixel(float64(v.Width)))
h = int(u.toDeviceIndependentPixel(float64(v.Height))) h = int(u.fromGLFWMonitorPixel(float64(v.Height)))
return nil return nil
}) })
return w, h return w, h
@ -792,17 +793,19 @@ func (u *UserInterface) updateSize() {
if u.isFullscreen() { if u.isFullscreen() {
v := currentMonitor(u.window).GetVideoMode() v := currentMonitor(u.window).GetVideoMode()
ww, wh := v.Width, v.Height ww, wh := v.Width, v.Height
w = u.toDeviceIndependentPixel(float64(ww)) w = u.fromGLFWMonitorPixel(float64(ww))
h = u.toDeviceIndependentPixel(float64(wh)) h = u.fromGLFWMonitorPixel(float64(wh))
} else { } else {
// Instead of u.windowWidth and u.windowHeight, use the actual window size here. // 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 // On Windows, the specified size at SetSize and the actual window size might not
// match (#1163). // match (#1163).
ww, wh := u.window.GetSize() ww, wh := u.window.GetSize()
// TODO: Is this correct? w = u.fromGLFWPixel(float64(ww))
w = u.toDeviceIndependentPixel(float64(ww)) h = u.fromGLFWPixel(float64(wh))
h = u.toDeviceIndependentPixel(float64(wh))
} }
// On Linux/UNIX, further adjusting is required (#1307).
w = math.Ceil(u.toFramebufferPixel(w))
h = math.Ceil(u.toFramebufferPixel(h))
return nil return nil
}) })
u.context.Layout(w, h) u.context.Layout(w, h)
@ -1152,7 +1155,7 @@ func (u *UserInterface) SetInitFocused(focused bool) {
} }
func (u *UserInterface) monitorPosition() (int, int) { func (u *UserInterface) monitorPosition() (int, int) {
// TODO: toDeviceIndependentPixel might be required. // TODO: fromGLFWMonitorPixel might be required.
return currentMonitor(u.window).GetPos() return currentMonitor(u.window).GetPos()
} }

View File

@ -45,7 +45,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/glfw" "github.com/hajimehoshi/ebiten/internal/glfw"
) )
func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
return x return x
} }

View File

@ -22,8 +22,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/glfw" "github.com/hajimehoshi/ebiten/internal/glfw"
) )
// toDeviceIndependentPixel must be called from the main thread. // fromGLFWMonitorPixel must be called from the main thread.
func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
return x / u.deviceScaleFactor() return x / u.deviceScaleFactor()
} }
@ -37,6 +37,11 @@ func (u *UserInterface) toGLFWPixel(x float64) float64 {
return x 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) { func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
return x, y return x, y
} }

View File

@ -99,8 +99,8 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
return nil return nil
} }
// toDeviceIndependentPixel must be called from the main thread. // fromGLFWMonitorPixel must be called from the main thread.
func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
return x / u.deviceScaleFactor() return x / u.deviceScaleFactor()
} }