/
window.go
92 lines (77 loc) · 1.92 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
package glfw
//#include <GLFW/glfw3.h>
import "C"
import (
"sync"
"unsafe"
)
type Window struct {
w *C.GLFWwindow
closeCB CloseFunc
focusCB FocusFunc
resizeCB ResizeFunc
framebufferResizeCB ResizeFunc
keyCB KeyFunc
charCB CharFunc
mouseMoveCB MouseMoveFunc
mouseButtonCB MouseButtonFunc
scrollCB ScrollFunc
}
var windows = struct {
sync.Mutex
m map[*C.GLFWwindow]*Window
}{m: map[*C.GLFWwindow]*Window{}}
func NewWindow(width, height int, title string) *Window {
win := C.glfwCreateWindow(C.int(width), C.int(height), C.CString(title), nil, nil)
if win == nil {
panic("GLFW: Failed to create window.")
}
w := &Window{w: win}
C.glfwSetWindowUserPointer(win, unsafe.Pointer(w))
windows.Lock()
windows.m[win] = w
windows.Unlock()
return w
}
func (w *Window) Destroy() {
C.glfwDestroyWindow(w.w)
windows.Lock()
delete(windows.m, w.w)
windows.Unlock()
}
func (w *Window) Hide() { C.glfwHideWindow(w.w) }
func (w *Window) Show() { C.glfwShowWindow(w.w) }
func (w *Window) SetTitle(s string) {
C.glfwSetWindowTitle(w.w, C.CString(s))
}
func (w *Window) Size() (width, height int) {
var wid, hei C.int
C.glfwGetWindowSize(w.w, &wid, &hei)
return int(wid), int(hei)
}
func (w *Window) Move(x, y int) {
C.glfwSetWindowPos(w.w, C.int(x), C.int(y))
}
func (w *Window) FramebufferSize() (width, height int) {
var wid, hei C.int
C.glfwGetFramebufferSize(w.w, &wid, &hei)
return int(wid), int(hei)
}
func (w *Window) ShouldClose() bool {
return C.glfwWindowShouldClose(w.w) == C.GL_TRUE
}
func (w *Window) SetShouldClose(b bool) {
v := C.GL_FALSE
if b {
v = C.GL_TRUE
}
C.glfwSetWindowShouldClose(w.w, C.int(v))
}
func (w *Window) MousePosition() (float64, float64) {
var x, y C.double
C.glfwGetCursorPos(w.w, &x, &y)
return float64(x), float64(y)
}
func (w *Window) SwapBuffers() {
C.glfwSwapBuffers(w.w)
}