Bläddra i källkod

Merge pull request #39 from exploser/fix-application-viewport

Fix Application to use framebuffer size
Daniel Salvadori 7 år sedan
förälder
incheckning
bac84a668d
3 ändrade filer med 25 tillägg och 7 borttagningar
  1. 3 3
      util/application/application.go
  2. 21 4
      window/glfw.go
  3. 1 0
      window/window.go

+ 3 - 3
util/application/application.go

@@ -548,15 +548,15 @@ func (app *Application) Quit() {
 // OnWindowResize is default handler for window resize events.
 func (app *Application) OnWindowResize() {
 
-	// Get window size and sets the viewport to the same size
-	width, height := app.win.Size()
+	// Get framebuffer size and sets the viewport accordingly
+	width, height := app.win.FramebufferSize()
 	app.gl.Viewport(0, 0, int32(width), int32(height))
 
 	// Sets perspective camera aspect ratio
 	aspect := float32(width) / float32(height)
 	app.camPersp.SetAspect(aspect)
 
-	// Sets the GUI root panel size to the size of the screen
+	// Sets the GUI root panel size to the size of the framebuffer
 	if app.guiroot != nil {
 		app.guiroot.SetSize(float32(width), float32(height))
 	}

+ 21 - 4
window/glfw.go

@@ -38,6 +38,8 @@ type glfwWindow struct {
 	lastY           int
 	lastWidth       int
 	lastHeight      int
+	scaleX          float64
+	scaleY          float64
 }
 
 // glfw manager singleton
@@ -120,6 +122,10 @@ func (m *glfwManager) CreateWindow(width, height int, title string, fullscreen b
 	w.mgr = m
 	w.Dispatcher.Initialize()
 
+	fbw, fbh := w.FramebufferSize()
+	w.scaleX = float64(fbw) / float64(width)
+	w.scaleY = float64(fbh) / float64(height)
+
 	// Set key callback to dispatch event
 	win.SetKeyCallback(func(x *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
 
@@ -155,12 +161,14 @@ func (m *glfwManager) CreateWindow(width, height int, title string, fullscreen b
 	win.SetMouseButtonCallback(func(x *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
 
 		xpos, ypos := x.GetCursorPos()
+
 		w.mouseEv.W = w
 		w.mouseEv.Button = MouseButton(button)
 		w.mouseEv.Action = Action(action)
 		w.mouseEv.Mods = ModifierKey(mods)
-		w.mouseEv.Xpos = float32(xpos)
-		w.mouseEv.Ypos = float32(ypos)
+		w.mouseEv.Xpos = float32(xpos * w.scaleX)
+		w.mouseEv.Ypos = float32(ypos * w.scaleY)
+
 		if action == glfw.Press {
 			w.Dispatch(OnMouseDown, &w.mouseEv)
 			return
@@ -174,9 +182,12 @@ func (m *glfwManager) CreateWindow(width, height int, title string, fullscreen b
 	// Set window size callback to dispatch event
 	win.SetSizeCallback(func(x *glfw.Window, width int, height int) {
 
+		fbw, fbh := x.GetFramebufferSize()
 		w.sizeEv.W = w
 		w.sizeEv.Width = width
 		w.sizeEv.Height = height
+		w.scaleX = float64(fbw) / float64(width)
+		w.scaleY = float64(fbh) / float64(height)
 		w.Dispatch(OnWindowSize, &w.sizeEv)
 	})
 
@@ -193,8 +204,8 @@ func (m *glfwManager) CreateWindow(width, height int, title string, fullscreen b
 	win.SetCursorPosCallback(func(x *glfw.Window, xpos float64, ypos float64) {
 
 		w.cursorEv.W = w
-		w.cursorEv.Xpos = float32(xpos)
-		w.cursorEv.Ypos = float32(ypos)
+		w.cursorEv.Xpos = float32(xpos * w.scaleX)
+		w.cursorEv.Ypos = float32(ypos * w.scaleY)
 		w.Dispatch(OnCursor, &w.cursorEv)
 	})
 
@@ -288,6 +299,12 @@ func (w *glfwWindow) FramebufferSize() (width int, height int) {
 	return w.win.GetFramebufferSize()
 }
 
+// Scale returns this window's DPI scale factor (FramebufferSize / Size)
+func (w *glfwWindow) Scale() (x float64, y float64) {
+
+	return w.scaleX, w.scaleY
+}
+
 // SetSize sets the size, in screen coordinates, of the client area of this window
 func (w *glfwWindow) SetSize(width int, height int) {
 

+ 1 - 0
window/window.go

@@ -26,6 +26,7 @@ type IWindow interface {
 	MakeContextCurrent()
 	FramebufferSize() (width int, height int)
 	Size() (width int, height int)
+	Scale() (x float64, y float64)
 	SetSize(width int, height int)
 	Pos() (xpos, ypos int)
 	SetPos(xpos, ypos int)