/
grid.go
91 lines (70 loc) · 1.81 KB
/
grid.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package grid
import (
"fmt"
"log"
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)
const (
OpenGlVerMajor = 3
OpenGLVerMinor = 3
)
type Game struct {
Width int32
Height int32
Fullscreen bool
Resizable bool
Title string
Scene Scene
}
type Scene interface {
Setup()
Draw(dt float32)
Update(dt float32)
}
func Run(g *Game) error {
if g.Scene == nil {
return fmt.Errorf("Scene property of given Game struct is empty")
}
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
if err := glfw.Init(); err != nil {
return fmt.Errorf("glfw.Init failed: %s", err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.ContextVersionMajor, OpenGLVerMinor)
glfw.WindowHint(glfw.ContextVersionMinor, OpenGLVerMinor)
glfw.WindowHint(glfw.Resizable, glfw.False)
if g.Resizable {
glfw.WindowHint(glfw.Resizable, glfw.True)
}
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
window, err := glfw.CreateWindow(int(g.Width), int(g.Height), g.Title, nil, nil)
if err != nil {
return fmt.Errorf("glfw.CreateWindow failed: %s", err)
}
window.MakeContextCurrent()
// Initialize Glow
if err := gl.Init(); err != nil {
return fmt.Errorf("gl.Init failed:", err)
}
version := gl.GoStr(gl.GetString(gl.VERSION))
log.Println("gl.Init successful, OpenGL version:", version)
var previousTime, deltaTime, time float32
previousTime = float32(glfw.GetTime()) - 1.0/60.0
g.Scene.Setup()
for !window.ShouldClose() {
time = float32(glfw.GetTime())
deltaTime = time - previousTime
glfw.PollEvents()
gl.ClearColor(0.2, 0.3, 0.3, 0.5)
gl.Clear(gl.COLOR_BUFFER_BIT)
g.Scene.Update(deltaTime)
g.Scene.Draw(deltaTime)
previousTime = time
window.SwapBuffers()
}
return nil
}