-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.go
214 lines (177 loc) · 4.76 KB
/
window.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package gofb
import (
"runtime"
"time"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
// Window represents window object
type Window struct {
Width int
Height int
Fullscreen bool
window *glfw.Window
running bool
lastFrameTime time.Time
frameDeltaMs int64
globalElapsedMs int64
framesCount int64
inputPress []bool
cursorPos Vec2
}
// NewWindow create new OpenGL window
func NewWindow(name string, width int, height int, fullscreen bool) *Window {
runtime.LockOSThread()
if err := glfw.Init(); err != nil {
panic(err)
}
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
var monitor *glfw.Monitor
if fullscreen {
monitor = glfw.GetPrimaryMonitor()
}
window, err := glfw.CreateWindow(width, height, name, monitor, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
panic(err)
}
w := Window{Width: width, Height: height, Fullscreen: fullscreen, window: window}
w.set2DProjection()
w.lastFrameTime = time.Now()
w.inputPress = make([]bool, 505)
window.SetKeyCallback(w.keyCallback)
w.cursorPos = NewVec2(0, 0)
window.SetCursorPosCallback(w.cursorPositionCallback)
window.SetMouseButtonCallback(w.mouseButtonCallback)
w.running = true
return &w
}
// Clear screen
func (w *Window) Clear(c Color) {
c.glClear()
gl.Clear(gl.COLOR_BUFFER_BIT)
}
// StartFrame needs to be at the start of the render loop
func (w *Window) StartFrame() {
w.frameDeltaMs = time.Since(w.lastFrameTime).Milliseconds()
w.globalElapsedMs += w.frameDeltaMs
w.lastFrameTime = time.Now()
}
// FinalizeFrame will swap buffers and poll for events
// This function needs to be called at the end of the render loop
func (w *Window) FinalizeFrame() {
w.framesCount++
w.window.SwapBuffers()
glfw.PollEvents()
}
// GetDeltaTimeMs return number of milliseconds elapsed when rendering last frame
func (w *Window) GetDeltaTimeMs() int64 {
return w.frameDeltaMs
}
// GetTotalElapsedMs return number of milliseconds elapsed since application started
func (w *Window) GetTotalElapsedMs() int64 {
return w.globalElapsedMs
}
// GetFPS get average frames per second
func (w *Window) GetFPS() float32 {
return float32(w.framesCount) / float32(w.globalElapsedMs) * 1000
}
// IsInput check if input was pressed
func (w *Window) IsInput(inputCode int) bool {
return w.inputPress[inputCode]
}
// SetKeyCallback set custom keyboard callback function
func (w *Window) SetKeyCallback(cbfun glfw.KeyCallback) {
w.window.SetKeyCallback(cbfun)
}
// ResetKeyCallback reset back original keyboardcallback
func (w *Window) ResetKeyCallback() {
w.window.SetKeyCallback(w.keyCallback)
}
// GetCursorPos get mouse cursor position
func (w *Window) GetCursorPos() Vec2 {
return w.cursorPos
}
// IsRunning check if application is running
func (w *Window) IsRunning() bool {
return !w.window.ShouldClose() && w.running
}
// Destroy will close window
func (w *Window) Destroy() {
defer glfw.Terminate()
}
// Stop running application
func (w *Window) Stop() {
w.running = false
}
func (w *Window) keyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch action {
case glfw.Repeat:
case glfw.Press:
w.inputPress[key] = true
break
default:
w.inputPress[key] = false
}
w.setModKey(mods)
}
func (w *Window) cursorPositionCallback(window *glfw.Window, x float64, y float64) {
w.cursorPos.X = int(x)
w.cursorPos.Y = int(y)
}
func (w *Window) mouseButtonCallback(window *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
switch action {
case glfw.Repeat:
case glfw.Press:
w.inputPress[w.glfwMouseToInput(button)] = true
break
default:
w.inputPress[w.glfwMouseToInput(button)] = false
}
w.setModKey(mods)
}
func (w *Window) glfwMouseToInput(button glfw.MouseButton) int {
switch button {
case glfw.MouseButtonLeft:
return MouseButtonLeft
case glfw.MouseButtonMiddle:
return MouseButtonMiddle
case glfw.MouseButtonRight:
return MouseButtonRight
}
return InputUnknown
}
func (w *Window) setModKey(mods glfw.ModifierKey) {
w.inputPress[KeyAlt] = false
w.inputPress[KeyControl] = false
w.inputPress[KeyShift] = false
w.inputPress[KeySuper] = false
switch mods {
case glfw.ModAlt:
w.inputPress[KeyAlt] = true
break
case glfw.ModControl:
w.inputPress[KeyControl] = true
break
case glfw.ModShift:
w.inputPress[KeyShift] = true
break
case glfw.ModSuper:
w.inputPress[KeySuper] = true
break
}
}
func (w *Window) set2DProjection() {
gl.Disable(gl.DEPTH_TEST)
gl.Enable(gl.TEXTURE)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(0, float64(w.Width), float64(w.Height), 0, 0, 1)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
}