Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

use current monitor (instead of primary) for SetFullScreen() and ScreenResolution() #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions window/glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ type GlfwWindow struct {
lastCursorKey Cursor
}

func mini(x, y int) int {
if x < y {
return x
}
return y
}

func maxi(x, y int) int {
if x > y {
return x
}
return y
}

// Init initializes the GlfwWindow singleton with the specified width, height, and title.
func Init(width, height int, title string) error {

Expand Down Expand Up @@ -399,7 +413,7 @@ func (w *GlfwWindow) SetFullScreen(full bool) {
w.lastX, w.lastY = w.GetPos()
w.lastWidth, w.lastHeight = w.GetSize()
// Get size of primary monitor
mon := glfw.GetPrimaryMonitor()
mon := w.GetMonitor()
vmode := mon.GetVideoMode()
width := vmode.Width
height := vmode.Height
Expand Down Expand Up @@ -427,10 +441,39 @@ func (w *GlfwWindow) GetScale() (x float64, y float64) {
return w.scaleX, w.scaleY
}

// GetMonitor returns the window's best-guessed monitor (by max area).
// Implemented to allow putting the window in fullscreen mode
// on the same monitor that contains the window at the moment.
// So far glfw doesn't provide a proper method for getting the monitor containing the window.
// It only has GetWindowMonitor() which returns NULL unless the window is in fullscreen mode already.
// This is a workaround translated from a snippet in C located here: https://stackoverflow.com/a/31526753
func (w *GlfwWindow) GetMonitor() *glfw.Monitor {

wx, wy := w.GetPos()
ww, wh := w.GetSize()

bestOverlap := 0
bestMonitor := glfw.GetPrimaryMonitor()

for _, monitor := range glfw.GetMonitors() {
mode := monitor.GetVideoMode()
mx, my := monitor.GetPos()
mw, mh := mode.Width, mode.Height
overlap := maxi(0, mini(wx+ww, mx+mw)-maxi(wx, mx)) *
maxi(0, mini(wy+wh, my+mh)-maxi(wy, my))
if overlap > bestOverlap {
bestOverlap = overlap
bestMonitor = monitor
}
}

return bestMonitor
}

// ScreenResolution returns the screen resolution
func (w *GlfwWindow) ScreenResolution(p interface{}) (width, height int) {

mon := glfw.GetPrimaryMonitor()
mon := w.GetMonitor()
vmode := mon.GetVideoMode()
return vmode.Width, vmode.Height
}
Expand Down