-
Notifications
You must be signed in to change notification settings - Fork 136
/
engo_glfw.go
520 lines (450 loc) · 13.1 KB
/
engo_glfw.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//go:build (darwin || linux || windows) && !ios && !android && !js && !sdl && !headless && !vulkan
// +build darwin linux windows
// +build !ios
// +build !android
// +build !js
// +build !sdl
// +build !headless
// +build !vulkan
package engo
import (
"io"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/EngoEngine/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
var (
// Window is the glfw.Window used for engo
Window *glfw.Window
// Gl is the current OpenGL context
Gl *gl.Context
cursorArrow *glfw.Cursor
cursorIBeam *glfw.Cursor
cursorCrosshair *glfw.Cursor
cursorHand *glfw.Cursor
cursorHResize *glfw.Cursor
cursorVResize *glfw.Cursor
scale = float32(1)
)
func init() {
runtime.LockOSThread()
}
// CreateWindow sets up the GLFW window and prepares the OpenGL surface for rendering
func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
CurrentBackEnd = BackEndGLFW
err := glfw.Init()
if err != nil {
log.Fatal(err)
}
if !opts.HeadlessMode {
cursorArrow = glfw.CreateStandardCursor(glfw.ArrowCursor)
cursorIBeam = glfw.CreateStandardCursor(glfw.IBeamCursor)
cursorCrosshair = glfw.CreateStandardCursor(glfw.CrosshairCursor)
cursorHand = glfw.CreateStandardCursor(glfw.HandCursor)
cursorHResize = glfw.CreateStandardCursor(glfw.HResizeCursor)
cursorVResize = glfw.CreateStandardCursor(glfw.VResizeCursor)
}
monitor := glfw.GetPrimaryMonitor()
var mode *glfw.VidMode
if monitor != nil {
mode = monitor.GetVideoMode()
} else {
// Initialize default values if no monitor is found
mode = &glfw.VidMode{
Width: 1,
Height: 1,
RedBits: 8,
GreenBits: 8,
BlueBits: 8,
RefreshRate: 60,
}
}
if fullscreen {
width = mode.Width
height = mode.Height
glfw.WindowHint(glfw.Decorated, 0)
} else {
monitor = nil
}
gameWidth = float32(width)
gameHeight = float32(height)
if opts.HeadlessMode {
glfw.WindowHint(glfw.Visible, glfw.False)
}
if opts.NotResizable {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.Samples, msaa)
if opts.HeadlessMode {
Gl = gl.NewContext()
return
}
Window, err = glfw.CreateWindow(width, height, title, monitor, nil)
if err != nil {
log.Fatal(err)
}
Window.MakeContextCurrent()
if !fullscreen {
Window.SetPos((mode.Width-width)/2, (mode.Height-height)/2)
}
SetVSync(opts.VSync)
Gl = gl.NewContext()
width, height = Window.GetSize()
windowWidth, windowHeight = float32(width), float32(height)
fw, fh := Window.GetFramebufferSize()
canvasWidth, canvasHeight = float32(fw), float32(fh)
if windowWidth <= canvasWidth && windowHeight <= canvasHeight {
scale = canvasWidth / windowWidth
}
Window.SetFramebufferSizeCallback(func(Window *glfw.Window, w, h int) {
Gl.Viewport(0, 0, w, h)
width, height = Window.GetSize()
windowWidth, windowHeight = float32(width), float32(width)
oldCanvasW, oldCanvasH := canvasWidth, canvasHeight
canvasWidth, canvasHeight = float32(w), float32(h)
ResizeXOffset += oldCanvasW - canvasWidth
ResizeYOffset += oldCanvasH - canvasHeight
if windowWidth <= canvasWidth && windowHeight <= canvasHeight {
scale = canvasWidth / windowWidth
}
})
Window.SetCursorPosCallback(func(Window *glfw.Window, x, y float64) {
Input.Mouse.X, Input.Mouse.Y = float32(x)/opts.GlobalScale.X, float32(y)/opts.GlobalScale.Y
if Input.Mouse.Action != Release && Input.Mouse.Action != Press {
Input.Mouse.Action = Move
}
})
Window.SetMouseButtonCallback(func(Window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
x, y := Window.GetCursorPos()
Input.Mouse.X, Input.Mouse.Y = float32(x)/(opts.GlobalScale.X), float32(y)/(opts.GlobalScale.Y)
// this is only valid because we use an internal structure that is
// 100% compatible with glfw3.h
Input.Mouse.Button = MouseButton(b)
Input.Mouse.Modifer = Modifier(m)
if a == glfw.Press {
Input.Mouse.Action = Press
} else {
Input.Mouse.Action = Release
}
})
Window.SetScrollCallback(func(Window *glfw.Window, xoff, yoff float64) {
Input.Mouse.ScrollX = float32(xoff)
Input.Mouse.ScrollY = float32(yoff)
})
Window.SetKeyCallback(func(Window *glfw.Window, k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey) {
key := Key(k)
if a == glfw.Press {
Input.keys.Set(key, true)
} else if a == glfw.Release {
Input.keys.Set(key, false)
}
Input.Modifier = Modifier(m)
})
Window.SetSizeCallback(func(w *glfw.Window, widthInt int, heightInt int) {
message := WindowResizeMessage{
OldWidth: int(windowWidth),
OldHeight: int(windowHeight),
NewWidth: widthInt,
NewHeight: heightInt,
}
windowWidth = float32(widthInt)
windowHeight = float32(heightInt)
// TODO: verify these for retina displays & verify if needed here
fw, fh := Window.GetFramebufferSize()
canvasWidth, canvasHeight = float32(fw), float32(fh)
if !opts.ScaleOnResize {
gameWidth, gameHeight = float32(widthInt), float32(heightInt)
}
Mailbox.Dispatch(message)
})
Window.SetCharCallback(func(Window *glfw.Window, char rune) {
Mailbox.Dispatch(TextMessage{char})
})
glfw.SetJoystickCallback(func(joy glfw.Joystick, event glfw.PeripheralEvent) {
Input.gamepads.mutex.Lock()
defer Input.gamepads.mutex.Unlock()
if event == glfw.Connected {
found := false
for _, gamepad := range Input.gamepads.gamepads {
if gamepad.id == joy.GetGUID() {
gamepad.connected = true
found = true
}
}
if !found {
for name, gamepad := range Input.gamepads.gamepads {
if !gamepad.connected && gamepad.id == "" { //gamepad was connected after registered
Input.gamepads.gamepads[name] = &Gamepad{
joystick: joy,
id: joy.GetGUID(),
connected: true,
}
}
}
}
} else if event == glfw.Disconnected {
for _, gamepad := range Input.gamepads.gamepads {
if gamepad.id == joy.GetGUID() {
gamepad.connected = false
}
}
}
})
Window.SetCloseCallback(func(Window *glfw.Window) {
Exit()
})
}
// DestroyWindow handles the termination of windows
func DestroyWindow() {
glfw.Terminate()
}
// SetTitle sets the title of the window
func SetTitle(title string) {
if opts.HeadlessMode {
log.Println("Title set to:", title)
} else {
Window.SetTitle(title)
}
}
// RunIteration runs one iteration per frame
func RunIteration() {
Time.Tick()
// First check for new keypresses
if !opts.HeadlessMode {
Input.update()
glfw.PollEvents()
}
// Then update the world and all Systems
currentUpdater.Update(Time.Delta())
// Lastly, forget keypresses and swap buffers
if !opts.HeadlessMode {
// reset values to avoid catching the same "signal" twice
Input.Mouse.ScrollX, Input.Mouse.ScrollY = 0, 0
Input.Mouse.Action = Neutral
Window.SwapBuffers()
}
}
// RunPreparation is called automatically when calling Open. It should only be called once.
func RunPreparation(defaultScene Scene) {
Time = NewClock()
SetScene(defaultScene, false)
}
func runLoop(defaultScene Scene, headless bool) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
closeEvent()
}()
RunPreparation(defaultScene)
ticker := time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
// Start tick, minimize the delta
Time.Tick()
for {
select {
case <-ticker.C:
RunIteration()
case <-resetLoopTicker:
ticker.Stop()
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
case <-closeGame:
ticker.Stop()
closeEvent()
return
}
}
}
// CursorPos returns the current cursor position
func CursorPos() (x, y float32) {
w, h := Window.GetCursorPos()
return float32(w), float32(h)
}
// WindowSize gets the current window size
func WindowSize() (w, h int) {
return Window.GetSize()
}
// WindowWidth gets the current window width
func WindowWidth() float32 {
return windowWidth
}
// WindowHeight gets the current window height
func WindowHeight() float32 {
return windowHeight
}
// CanvasWidth gets the width of the current OpenGL Framebuffer
func CanvasWidth() float32 {
return canvasWidth
}
// CanvasHeight gets the height of the current OpenGL Framebuffer
func CanvasHeight() float32 {
return canvasHeight
}
// CanvasScale gets the ratio of the canvas to the window sizes
func CanvasScale() float32 {
return scale
}
// SetCursor sets the pointer of the mouse to the defined standard cursor
func SetCursor(c Cursor) {
var cur *glfw.Cursor
switch c {
case CursorNone:
cur = nil // just for the documentation, this isn't required
case CursorArrow:
cur = cursorArrow
case CursorCrosshair:
cur = cursorCrosshair
case CursorHand:
cur = cursorHand
case CursorIBeam:
cur = cursorIBeam
case CursorHResize:
cur = cursorHResize
case CursorVResize:
cur = cursorVResize
}
Window.SetCursor(cur)
}
// SetVSync sets whether or not to use VSync
func SetVSync(enabled bool) {
opts.VSync = enabled
if opts.VSync {
glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
}
}
//SetCursorVisibility sets the visibility of the cursor.
//If true the cursor is visible, if false the cursor is not.
func SetCursorVisibility(visible bool) {
if visible {
glfw.GetCurrentContext().SetInputMode(glfw.CursorMode, glfw.CursorNormal)
} else {
glfw.GetCurrentContext().SetInputMode(glfw.CursorMode, glfw.CursorHidden)
}
}
// openFile is the desktop-specific way of opening a file
func openFile(url string) (io.ReadCloser, error) {
return os.Open(url)
}
// IsAndroidChrome tells if the browser is Chrome for android
func IsAndroidChrome() bool {
return false
}
// GetKeyName returns the string returned from the given Key. So you can write "Press W to move forward"
// and get a W for QWERTY and Z for AZERTY
func GetKeyName(k Key) string {
m := map[Key]glfw.Key{
KeyGrave: glfw.KeyGraveAccent,
KeyDash: glfw.KeyMinus,
KeyApostrophe: glfw.KeyApostrophe,
KeySemicolon: glfw.KeySemicolon,
KeyEquals: glfw.KeyEqual,
KeyComma: glfw.KeyComma,
KeyPeriod: glfw.KeyPeriod,
KeySlash: glfw.KeySlash,
KeyBackslash: glfw.KeyBackslash,
KeyBackspace: glfw.KeyBackspace,
KeyTab: glfw.KeyTab,
KeyCapsLock: glfw.KeyCapsLock,
KeySpace: glfw.KeySpace,
KeyEnter: glfw.KeyEnter,
KeyEscape: glfw.KeyEscape,
KeyInsert: glfw.KeyInsert,
KeyPrintScreen: glfw.KeyPrintScreen,
KeyDelete: glfw.KeyDelete,
KeyPageUp: glfw.KeyPageUp,
KeyPageDown: glfw.KeyPageDown,
KeyHome: glfw.KeyHome,
KeyEnd: glfw.KeyEnd,
KeyPause: glfw.KeyPause,
KeyScrollLock: glfw.KeyScrollLock,
KeyArrowLeft: glfw.KeyLeft,
KeyArrowRight: glfw.KeyRight,
KeyArrowDown: glfw.KeyDown,
KeyArrowUp: glfw.KeyUp,
KeyLeftBracket: glfw.KeyLeftBracket,
KeyLeftShift: glfw.KeyLeftShift,
KeyLeftControl: glfw.KeyLeftControl,
KeyLeftSuper: glfw.KeyLeftSuper,
KeyLeftAlt: glfw.KeyLeftAlt,
KeyRightBracket: glfw.KeyRightBracket,
KeyRightShift: glfw.KeyRightShift,
KeyRightControl: glfw.KeyRightControl,
KeyRightSuper: glfw.KeyRightSuper,
KeyRightAlt: glfw.KeyRightAlt,
KeyZero: glfw.Key0,
KeyOne: glfw.Key1,
KeyTwo: glfw.Key2,
KeyThree: glfw.Key3,
KeyFour: glfw.Key4,
KeyFive: glfw.Key5,
KeySix: glfw.Key6,
KeySeven: glfw.Key7,
KeyEight: glfw.Key8,
KeyNine: glfw.Key9,
KeyF1: glfw.KeyF1,
KeyF2: glfw.KeyF2,
KeyF3: glfw.KeyF3,
KeyF4: glfw.KeyF4,
KeyF5: glfw.KeyF5,
KeyF6: glfw.KeyF6,
KeyF7: glfw.KeyF7,
KeyF8: glfw.KeyF8,
KeyF9: glfw.KeyF9,
KeyF10: glfw.KeyF10,
KeyF11: glfw.KeyF11,
KeyF12: glfw.KeyF12,
KeyA: glfw.KeyA,
KeyB: glfw.KeyB,
KeyC: glfw.KeyC,
KeyD: glfw.KeyD,
KeyE: glfw.KeyE,
KeyF: glfw.KeyF,
KeyG: glfw.KeyG,
KeyH: glfw.KeyH,
KeyI: glfw.KeyI,
KeyJ: glfw.KeyJ,
KeyK: glfw.KeyK,
KeyL: glfw.KeyL,
KeyM: glfw.KeyM,
KeyN: glfw.KeyN,
KeyO: glfw.KeyO,
KeyP: glfw.KeyP,
KeyQ: glfw.KeyQ,
KeyR: glfw.KeyR,
KeyS: glfw.KeyS,
KeyT: glfw.KeyT,
KeyU: glfw.KeyU,
KeyV: glfw.KeyV,
KeyW: glfw.KeyW,
KeyX: glfw.KeyX,
KeyY: glfw.KeyY,
KeyZ: glfw.KeyZ,
KeyNumLock: glfw.KeyNumLock,
KeyNumMultiply: glfw.KeyKPMultiply,
KeyNumDivide: glfw.KeyKPDivide,
KeyNumAdd: glfw.KeyKPAdd,
KeyNumSubtract: glfw.KeyKPSubtract,
KeyNumZero: glfw.KeyKP0,
KeyNumOne: glfw.KeyKP1,
KeyNumTwo: glfw.KeyKP2,
KeyNumThree: glfw.KeyKP3,
KeyNumFour: glfw.KeyKP4,
KeyNumFive: glfw.KeyKP5,
KeyNumSix: glfw.KeyKP6,
KeyNumSeven: glfw.KeyKP7,
KeyNumEight: glfw.KeyKP8,
KeyNumNine: glfw.KeyKP9,
KeyNumDecimal: glfw.KeyKPDecimal,
KeyNumEnter: glfw.KeyKPEnter,
}
return glfw.GetKeyName(m[k], 0)
}