adjusts VideoMode and associated function to pull request change requests.

This commit is contained in:
David Linus Briemann 2018-05-04 18:03:25 +02:00
parent 167d972eed
commit 1bbbf26b31
3 changed files with 45 additions and 32 deletions

View File

@ -10,12 +10,17 @@ import (
"golang.org/x/image/font/basicfont" "golang.org/x/image/font/basicfont"
) )
type setting struct {
mode *pixelgl.VideoMode
monitor *pixelgl.Monitor
}
var ( var (
texts []*text.Text texts []*text.Text
staticText *text.Text staticText *text.Text
vmodes []*pixelgl.VideoMode settings []setting
activeMode pixelgl.VideoMode activeSetting *setting
activeMonitor *pixelgl.Monitor isFullScreen = false
) )
func run() { func run() {
@ -38,14 +43,19 @@ func run() {
for i := 0; i < len(monitors); i++ { for i := 0; i < len(monitors); i++ {
// Retrieve all video modes for a specific monitor. // Retrieve all video modes for a specific monitor.
modes := monitors[i].VideoModes() modes := monitors[i].VideoModes()
for j := 0; j < len(modes); j++ {
settings = append(settings, setting{
monitor: monitors[i],
mode: &modes[j],
})
}
vmodes = append(vmodes, modes...)
texts[i] = text.New(pixel.V(10+250*float64(i), -20), atlas) texts[i] = text.New(pixel.V(10+250*float64(i), -20), atlas)
texts[i].Color = colornames.Black texts[i].Color = colornames.Black
texts[i].WriteString(fmt.Sprintf("MONITOR %s\n\n", monitors[i].Name())) texts[i].WriteString(fmt.Sprintf("MONITOR %s\n\n", monitors[i].Name()))
for _, v := range modes { for _, v := range modes {
texts[i].WriteString(fmt.Sprintf("(%c) %dx%d @ %d hz, %d bpp\n", key, v.Width, v.Height, v.RefreshRate, v.BlueBits+v.RedBits+v.GreenBits)) texts[i].WriteString(fmt.Sprintf("(%c) %dx%d @ %d hz\n", key, v.Width, v.Height, v.RefreshRate))
key++ key++
} }
} }
@ -54,6 +64,8 @@ func run() {
staticText.Color = colornames.Black staticText.Color = colornames.Black
staticText.WriteString("ESC to exit\nW toggles windowed/fullscreen") staticText.WriteString("ESC to exit\nW toggles windowed/fullscreen")
activeSetting = &settings[0]
for !win.Closed() { for !win.Closed() {
win.Clear(colornames.Antiquewhite) win.Clear(colornames.Antiquewhite)
@ -67,27 +79,31 @@ func run() {
} }
if win.JustPressed(pixelgl.KeyW) { if win.JustPressed(pixelgl.KeyW) {
if activeMode.Monitor != nil { if isFullScreen {
// Switch to windowed and backup the correct monitor. // Switch to windowed and backup the correct monitor.
activeMonitor = activeMode.Monitor win.SetMonitor(nil)
activeMode.Monitor = nil isFullScreen = false
win.SetVideoMode(activeMode)
} else { } else {
// Switch to fullscreen. // Switch to fullscreen.
activeMode.Monitor = activeMonitor win.SetMonitor(activeSetting.monitor)
win.SetVideoMode(activeMode) isFullScreen = true
} }
win.SetBounds(pixel.R(0, 0, float64(activeSetting.mode.Width), float64(activeSetting.mode.Height)))
} }
input := win.Typed() input := win.Typed()
if len(input) > 0 { if len(input) > 0 {
key := int(input[0]) - 48 key := int(input[0]) - 48
fmt.Println(key) fmt.Println(key)
if key >= 0 && key < len(vmodes) { if key >= 0 && key < len(settings) {
activeMode = *vmodes[key] activeSetting = &settings[key]
activeMonitor = activeMode.Monitor
fmt.Println("change to:", activeMode.Width, activeMode.Height) if isFullScreen {
win.SetVideoMode(activeMode) win.SetMonitor(activeSetting.monitor)
} else {
win.SetMonitor(nil)
}
win.SetBounds(pixel.R(0, 0, float64(activeSetting.mode.Width), float64(activeSetting.mode.Height)))
} }
} }

View File

@ -10,13 +10,15 @@ type Monitor struct {
monitor *glfw.Monitor monitor *glfw.Monitor
} }
// VideoMode represents all properties of a video mode and is attached // VideoMode represents all properties of a video mode and is
// to a monitor if it is a fullscreen mode. // associated with a monitor if it is used in fullscreen mode.
type VideoMode struct { type VideoMode struct {
*glfw.VidMode // Width is the width of the vide mode in pixels.
// Monitor is a pointer to the monitor that owns this video mode. Width int
// If Monitor is nil the video mode is windowed. // Height is the height of the video mode in pixels.
Monitor *Monitor Height int
// RefreshRate holds the refresh rate of the associated monitor in Hz.
RefreshRate int
} }
// PrimaryMonitor returns the main monitor (usually the one with the taskbar and stuff). // PrimaryMonitor returns the main monitor (usually the one with the taskbar and stuff).
@ -106,15 +108,16 @@ func (m *Monitor) RefreshRate() (rate float64) {
} }
// VideoModes returns all available video modes for the monitor. // VideoModes returns all available video modes for the monitor.
func (m *Monitor) VideoModes() (vmodes []*VideoMode) { func (m *Monitor) VideoModes() (vmodes []VideoMode) {
var modes []*glfw.VidMode var modes []*glfw.VidMode
mainthread.Call(func() { mainthread.Call(func() {
modes = m.monitor.GetVideoModes() modes = m.monitor.GetVideoModes()
}) })
for _, mode := range modes { for _, mode := range modes {
vmodes = append(vmodes, &VideoMode{ vmodes = append(vmodes, VideoMode{
VidMode: mode, Width: mode.Width,
Monitor: m, Height: mode.Height,
RefreshRate: mode.RefreshRate,
}) })
} }
return return

View File

@ -424,9 +424,3 @@ func (w *Window) Clear(c color.Color) {
func (w *Window) Color(at pixel.Vec) pixel.RGBA { func (w *Window) Color(at pixel.Vec) pixel.RGBA {
return w.canvas.Color(at) return w.canvas.Color(at)
} }
// SetVideoMode applies the given video mode to this window.
func (w *Window) SetVideoMode(vm VideoMode) {
w.SetMonitor(vm.Monitor)
w.SetBounds(pixel.R(0, 0, float64(vm.Width), float64(vm.Height)))
}