ui: Introduce glfwScale

This commit is contained in:
Hajime Hoshi 2016-07-04 11:37:34 +09:00
parent cf093513be
commit 910e1b760c
4 changed files with 39 additions and 24 deletions

View File

@ -33,8 +33,6 @@ type userInterface struct {
width int width int
height int height int
scale float64 scale float64
deviceScale float64
framebufferScale float64
context *opengl.Context context *opengl.Context
funcs chan func() funcs chan func()
sizeChanged bool sizeChanged bool
@ -143,8 +141,6 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er
u.runOnMainThread(func() { u.runOnMainThread(func() {
m := glfw.GetPrimaryMonitor() m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode() v := m.GetVideoMode()
u.deviceScale = deviceScale()
u.framebufferScale = 1
if !u.setScreenSize(width, height, scale) { if !u.setScreenSize(width, height, scale) {
err = errors.New("ui: Fail to set the screen size") err = errors.New("ui: Fail to set the screen size")
return return
@ -152,24 +148,25 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er
u.window.SetTitle(title) u.window.SetTitle(title)
u.window.Show() u.window.Show()
x := (v.Width - int(float64(width)*u.windowScale())) / 2 w, h := u.glfwSize()
y := (v.Height - int(float64(height)*u.windowScale())) / 3 x := (v.Width - w) / 2
y := (v.Height - h) / 3
u.window.SetPos(x, y) u.window.SetPos(x, y)
}) })
return err return err
} }
func (u *userInterface) windowScale() float64 { func (u *userInterface) glfwSize() (int, int) {
return u.scale * u.deviceScale return int(float64(u.width) * u.scale * glfwScale()), int(float64(u.height) * u.scale * glfwScale())
} }
func (u *userInterface) actualScreenScale() float64 { func (u *userInterface) actualScreenScale() float64 {
return u.windowScale() * u.framebufferScale return u.scale * deviceScale()
} }
func (u *userInterface) pollEvents() error { func (u *userInterface) pollEvents() error {
glfw.PollEvents() glfw.PollEvents()
return currentInput.update(u.window, u.windowScale()) return currentInput.update(u.window, u.scale*glfwScale())
} }
func (u *userInterface) Update() (interface{}, error) { func (u *userInterface) Update() (interface{}, error) {
@ -261,7 +258,6 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool {
return false return false
} }
// u.scale should be set first since this affects windowScale().
origScale := u.scale origScale := u.scale
u.scale = scale u.scale = scale
@ -286,7 +282,8 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool {
window.SetFramebufferSizeCallback(nil) window.SetFramebufferSizeCallback(nil)
close(ch) close(ch)
}) })
window.SetSize(int(float64(width)*u.windowScale()), int(float64(height)*u.windowScale())) w, h := u.glfwSize()
window.SetSize(w, h)
event: event:
for { for {
@ -297,9 +294,6 @@ event:
default: default:
} }
} }
// This is usually 1, but sometimes more than 1 (e.g. Retina Mac)
fw, _ := window.GetFramebufferSize()
u.framebufferScale = float64(fw) / float64(width) / u.windowScale()
u.sizeChanged = true u.sizeChanged = true
return true return true
} }

View File

@ -21,3 +21,7 @@ func deviceScale() float64 {
// TODO: Implement this // TODO: Implement this
return 1 return 1
} }
func glfwScale() float64 {
return deviceScale()
}

View File

@ -18,8 +18,21 @@
package ui package ui
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework AppKit
//
// #import <AppKit/AppKit.h>
//
// static float scale() {
// NSScreen* primary = [[NSScreen screens] firstObject];
// return [primary backingScaleFactor];
// }
import "C"
func deviceScale() float64 { func deviceScale() float64 {
// The device scale is always 1 on Mac OS. return float64(C.scale())
// The frame buffer size is different from the window size. }
func glfwScale() float64 {
return 1 return 1
} }

View File

@ -30,3 +30,7 @@ func deviceScale() float64 {
dpi := int(C.getDPI()) dpi := int(C.getDPI())
return float64(dpi) / 96 return float64(dpi) / 96
} }
func glfwScale() float64 {
return deviceScale()
}