updated glfw code to fix scaling on macOS

This commit is contained in:
Thomas Friedel 2020-01-25 15:59:54 +01:00
parent 79fde3ee1d
commit c8ff508299
4 changed files with 32 additions and 10 deletions

View file

@ -5,7 +5,7 @@ import (
"runtime"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/backend/goglbackend"
)
@ -47,8 +47,9 @@ func main() {
log.Fatalf("Error loading canvas GL assets: %v", err)
}
window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) {
mx, my = xpos, ypos
var sx, sy float64 = 1, 1
window.SetCursorPosCallback(func(w *glfw.Window, xpos, ypos float64) {
mx, my = xpos*sx, ypos*sy
})
// initialize canvas with zero size, since size is set in main loop
@ -56,14 +57,20 @@ func main() {
for !window.ShouldClose() {
window.MakeContextCurrent()
// find window size and scaling
ww, wh := window.GetSize()
fbw, fbh := window.GetFramebufferSize()
sx = float64(fbw) / float64(ww)
sy = float64(fbh) / float64(wh)
glfw.PollEvents()
// set canvas size
ww, wh := window.GetSize()
backend.SetBounds(0, 0, ww, wh)
backend.SetBounds(0, 0, fbw, fbh)
// call the run function to do all the drawing
run(cv, float64(ww), float64(wh))
run(cv, float64(fbw), float64(fbh))
// swap back and front buffer
window.SwapBuffers()

View file

@ -10,7 +10,7 @@ import (
"time"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/backend/goglbackend"
)
@ -67,7 +67,8 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
gl.Enable(gl.MULTISAMPLE)
// load canvas GL backend
backend, err := goglbackend.New(0, 0, w, h, nil)
fbw, fbh := window.GetFramebufferSize()
backend, err := goglbackend.New(0, 0, fbw, fbh, nil)
if err != nil {
return nil, nil, fmt.Errorf("Error loading GoGL backend: %v", err)
}
@ -79,6 +80,8 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
}
var mx, my int
sx := float64(fbw) / float64(w)
sy := float64(fbh) / float64(h)
window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
if action == glfw.Press && wnd.MouseDown != nil {
@ -88,7 +91,7 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
}
})
window.SetCursorPosCallback(func(w *glfw.Window, xpos, ypos float64) {
mx, my = int(math.Round(xpos)), int(math.Round(ypos))
mx, my = int(math.Round(xpos*sx)), int(math.Round(ypos*sy))
if wnd.MouseMove != nil {
wnd.MouseMove(mx, my)
}
@ -111,10 +114,13 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
}
})
window.SetSizeCallback(func(w *glfw.Window, width, height int) {
fbw, fbh := window.GetFramebufferSize()
sx = float64(fbw) / float64(width)
sy = float64(fbh) / float64(height)
if wnd.SizeChange != nil {
wnd.SizeChange(width, height)
} else {
backend.SetBounds(0, 0, width, height)
backend.SetBounds(0, 0, fbw, fbh)
}
})
window.SetCloseCallback(func(w *glfw.Window) {
@ -169,3 +175,9 @@ func (wnd *Window) MainLoop(run func()) {
func (wnd *Window) Size() (int, int) {
return wnd.Window.GetSize()
}
// FramebufferSize returns the current width and height of
// the framebuffer
func (wnd *Window) FramebufferSize() (int, int) {
return wnd.Window.GetFramebufferSize()
}

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/tfriedel6/canvas
require (
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2
github.com/go-gl/glfw v0.0.0-20181014061658-691ee1b84c51
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/veandco/go-sdl2 v0.3.3
golang.org/x/exp v0.0.0-20181106170214-d68db9428509

2
go.sum
View file

@ -2,6 +2,8 @@ github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2 h1:78Hza2KHn2PX1jdydQnffa
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw v0.0.0-20181014061658-691ee1b84c51 h1:elGSwayRx7uAsfA5PnVKeTHh+AVsUTmas0CkHOw/DSk=
github.com/go-gl/glfw v0.0.0-20181014061658-691ee1b84c51/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 h1:b+9H1GAsx5RsjvDFLoS5zkNBzIQMuVKUYQDmxU3N5XE=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/veandco/go-sdl2 v0.3.3 h1:4/TirgB2MQ7oww3pM3Yfgf1YbChMlAQAmiCPe5koK0I=