/
run.go
57 lines (47 loc) · 1.17 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ui
import (
glfw "github.com/go-gl/glfw3"
"log"
"runtime"
)
var mainc chan<- func()
// Run should be called by your main function, and will block
// indefinitely. This is necessary because some platforms expect calls
// from the main thread.
func Run() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// ensure we have at least 2 procs, due to the thread conditions we
// have to work with.
procs := runtime.GOMAXPROCS(0)
if procs < 2 {
runtime.GOMAXPROCS(2)
}
glfw.Init()
defer glfw.Terminate()
setupGlfw()
mainchan := make(chan func(), 32)
mainc = mainchan
for {
select {
case fn := <-mainchan:
fn()
default:
glfw.WaitEvents()
}
}
}
func setupGlfw() {
glfw.SetErrorCallback(onGlfwError)
glfw.WindowHint(glfw.Resizable, 1)
glfw.WindowHint(glfw.Visible, 1)
glfw.WindowHint(glfw.ClientApi, glfw.OpenglApi)
glfw.WindowHint(glfw.OpenglForwardCompatible, 1)
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
glfw.WindowHint(glfw.OpenglDebugContext, 1)
}
func onGlfwError(code glfw.ErrorCode, desc string) {
log.Println("glfw: " + desc)
}