Skip to content

Commit

Permalink
updated glfw code to fix scaling on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
tfriedel6 committed Jan 25, 2020
1 parent 79fde3e commit c8ff508
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
19 changes: 13 additions & 6 deletions examples/glfw/glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -47,23 +47,30 @@ 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
cv := canvas.New(backend)

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()
Expand Down
20 changes: 16 additions & 4 deletions glfwcanvas/glfwcanvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit c8ff508

Please sign in to comment.