Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Add display support Proof Of Concept #117

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
127 changes: 127 additions & 0 deletions examples/displays/borderless/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"fmt"

"github.com/jacekolszak/pixiq/colornames"
"github.com/jacekolszak/pixiq/glclear"
"github.com/jacekolszak/pixiq/glfw"
"github.com/jacekolszak/pixiq/mouse"
)

func main() {
// This example shows how to switch from windowed to full screen mode
glfw.StartMainThreadLoop(func(mainThreadLoop *glfw.MainThreadLoop) {
// Displays instance requires mainThreadLoop because accessing information
// about displays must be done from the main thread.
displays, err := glfw.Displays(mainThreadLoop)
if err != nil {
panic(err)
}

gl, err := glfw.NewOpenGL(mainThreadLoop)
if err != nil {
panic(err)
}
// Open standard window
screenWidth, screenHeight := 640, 360
win, err := gl.OpenWindow(screenWidth, screenHeight, glfw.Title("Press left mouse button to borderless fullscreen"))
if err != nil {
panic(err)
}

mouseState := mouse.New(win)

fullscreen := false

prepareScreen(gl, win)

for {
mouseState.Update()
if mouseState.JustReleased(mouse.Left) {
if !fullscreen {
display := currentDisplay(win, displays)
mode := display.VideoMode()
// Disable automatic iconify on focus loss.
win.SetAutoIconifyHint(false)
zoom := adjustZoom(mode, screenWidth, screenHeight)
fmt.Println(zoom)
// Turn on the full screen
win.EnterFullScreen(mode, zoom)
fullscreen = true
} else {
win.ExitFullScreen()
fullscreen = false
}
}
win.Draw()
if win.ShouldClose() {
break
}
}

})
}

func prepareScreen(gl *glfw.OpenGL, win *glfw.Window) {
clearTool := glclear.New(gl.Context())
clearTool.SetColor(colornames.White)
screen := win.Screen()
clearTool.Clear(screen)
clearTool.SetColor(colornames.Black)
clearTool.Clear(screen.Selection(270, 130).WithSize(100, 100))
}

func currentDisplay(win *glfw.Window, displays *glfw.DisplaysAPI) glfw.Display {
// TODO This functionality should be in a new package
highestArea := 0
all := displays.All()
bestDisplay := all[0]
for _, display := range all {
workarea := display.Workarea()
videoMode := display.VideoMode()
left := max(win.X(), workarea.X())
right := min(videoMode.Width()+workarea.X(), win.Width()+win.X())
top := max(win.Y(), workarea.Y())
bottom := min(videoMode.Height()+workarea.Y(), win.Height()+win.Y())
w := right - left
h := bottom - top
if w > 0 && h > 0 {
area := w * h
if area > highestArea {
bestDisplay = display
highestArea = area
}
}
}
return bestDisplay
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func adjustZoom(mode glfw.VideoMode, width, height int) (zoom int) {
zoom = 1
w := width
h := height
for mode.Width() > w && mode.Height() > h {
zoom++
w = width * zoom
h = height * zoom
}
if w > mode.Width() || h > mode.Height() {
zoom--
}
return zoom
}
74 changes: 74 additions & 0 deletions examples/displays/fullscreen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"fmt"
"github.com/jacekolszak/pixiq/clear"
"github.com/jacekolszak/pixiq/colornames"
"github.com/jacekolszak/pixiq/glfw"
)

func main() {
// This example shows how to open a window in a fullscreen mode
glfw.StartMainThreadLoop(func(mainThreadLoop *glfw.MainThreadLoop) {
// Displays instance requires mainThreadLoop because accessing information
// about displays must be done from the main thread.
displays, err := glfw.Displays(mainThreadLoop)
if err != nil {
panic(err)
}
// Get Primary display. This is usually the display where elements like the Windows task bar
// or the OS X menu bar is located.
primary, ok := displays.Primary()
if !ok {
panic("no displays found")
}
// get current video mode which is usually the best one to pick (unfortunately not on MacOS)
videoMode := primary.VideoMode()
// try to find the window zoom which will give screen size close enough to requested 640x360
zoom := adjustZoom(videoMode, 640, 360)
fmt.Printf("Adjusted zoom=%d\n", zoom)

gl, err := glfw.NewOpenGL(mainThreadLoop)
if err != nil {
panic(err)
}

win, err := gl.OpenFullScreenWindow(videoMode, glfw.Zoom(zoom))
if err != nil {
panic(err)
}

prepareScreen(win)

// Show full screen for 3 seconds
fmt.Println("Refresh rate is", videoMode.RefreshRate())
for x := 0; x < videoMode.RefreshRate()*3; x++ {
win.Draw() // blocks until VSync
}
win.Close()
})
}

// Adjusts the zoom of window based on the VideoMode and recommended screen size
func adjustZoom(mode glfw.VideoMode, width, height int) (zoom int) {
zoom = 1
w := width
h := height
for mode.Width() > w && mode.Height() > h {
zoom++
w = width * zoom
h = height * zoom
}
if w > mode.Width() || h > mode.Height() {
zoom--
}
return zoom
}

func prepareScreen(win *glfw.Window) {
screen := win.Screen()
clearTool := clear.New()
clearTool.SetColor(colornames.Lightgray)
clearTool.Clear(screen)
screen.SetColor(100, 100, colornames.Black)
}
28 changes: 28 additions & 0 deletions examples/displays/list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"github.com/jacekolszak/pixiq/glfw"
)

func main() {
// This example shows how to list all displays
glfw.StartMainThreadLoop(func(mainThreadLoop *glfw.MainThreadLoop) {
// Displays instance requires mainThreadLoop because accessing information
// about displays must be done from the main thread.
displays, err := glfw.Displays(mainThreadLoop)
if err != nil {
panic(err)
}

all := displays.All()
for _, display := range all {
fmt.Println("Name:", display.Name())
physicalSize := display.PhysicalSize()
fmt.Printf("Phyical size: %d mm x %d mm\n", physicalSize.Width(), physicalSize.Height())
videoMode := display.VideoMode()
fmt.Printf("Current resolution: %d x %d, %d Hz\n", videoMode.Width(), videoMode.Height(), videoMode.RefreshRate())
fmt.Println()
}
})
}
54 changes: 54 additions & 0 deletions examples/windows/resize/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"github.com/jacekolszak/pixiq/clear"
"github.com/jacekolszak/pixiq/colornames"
"github.com/jacekolszak/pixiq/glfw"
"github.com/jacekolszak/pixiq/mouse"
"log"
)

func main() {
// This example shows how to open a window in a fullscreen mode
glfw.StartMainThreadLoop(func(mainThreadLoop *glfw.MainThreadLoop) {
gl, err := glfw.NewOpenGL(mainThreadLoop)
if err != nil {
panic(err)
}

width := 121
height := 101
zoom := 5
win, err := gl.OpenWindow(width, height, glfw.Zoom(zoom), glfw.Title("Scroll the mouse wheel to zoom in/out"), glfw.Resizable(true))
if err != nil {
panic(err)
}

mouseState := mouse.New(win)
for {
screen := win.Screen()
clearTool := clear.New()
clearTool.SetColor(colornames.Lightgray)
clearTool.Clear(screen)
screen.SetColor(screen.Width()/2, screen.Height()/2, colornames.Black)

mouseState.Update()
if mouseState.Scroll().Y() > 0 {
zoom++
if err := win.Resize(width, height, zoom); err != nil {
log.Printf("Resize failed: %v", err)
}
}
if mouseState.Scroll().Y() < 0 && zoom > 5 {
zoom--
if err := win.Resize(width, height, zoom); err != nil {
log.Printf("Resize failed: %v", err)
}
}
win.Draw()
if win.ShouldClose() {
break
}
}
})
}
Loading