/
window_glfw.go
207 lines (173 loc) · 4.86 KB
/
window_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
//+build !android,!ios,!js
package hid
import (
"github.com/go-gl/glfw/v3.2/glfw"
"korok.io/korok/hid/gl"
"fmt"
"log"
"runtime"
)
var windowCallback WindowCallback
var inputCallback InputCallback
var Keys [1024]int
func init() {
runtime.LockOSThread()
}
func RegisterWindowCallback(callback WindowCallback) {
windowCallback = callback
}
func RegisterInputCallback(callback InputCallback) {
inputCallback = callback
}
func CreateWindow(option *WindowOptions) {
fmt.Println(glfw.GetVersionString())
// 初始化 glfw
err := glfw.Init()
defer glfw.Terminate()
if err != nil {
log.Fatal(err)
}
// 设置API版本兼容, 最低支持:3.2
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
// window resizable hint
if option.Resizable {
glfw.WindowHint(glfw.Resizable, glfw.True)
} else {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
// hide title-bar
if option.NoTitleBar {
glfw.WindowHint(glfw.Decorated, glfw.False)
} else {
glfw.WindowHint(glfw.Decorated, glfw.True)
}
monitor := glfw.GetPrimaryMonitor()
mode := &glfw.VidMode{
Width: 1,
Height: 1,
RedBits: 8,
GreenBits: 8,
BlueBits: 8,
RefreshRate: 60,
}
if monitor != nil {
mode = monitor.GetVideoMode()
}
// if full-screen ignore width and height
if option.FullScreen {
option.Width = mode.Width
option.Height = mode.Height
} else {
monitor = nil
}
// 创建窗口
window, err := glfw.CreateWindow(option.Width, option.Height, option.Title, monitor, nil)
if err != nil {
fmt.Println("fail window:", err)
return
}
defer window.Destroy()
if !option.FullScreen && mode.Width != 1 {
window.SetPos((mode.Width-option.Width)/2, (mode.Height-option.Height)/2)
}
// make the window's context current
window.MakeContextCurrent()
if option.NoVsync {
glfw.SwapInterval(0)
} else {
glfw.SwapInterval(1)
}
window.SetFramebufferSizeCallback(func(w *glfw.Window, width int, height int) {
gl.Viewport(0, 0, int32(width), int32(height))
})
// Handle input callback
window.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyEscape && action == glfw.Press {
w.SetShouldClose(true)
}
//if key >= 0 && key < 1024 {
// if action == glfw.Press {
// Keys[key] = gl.TRUE
// } else if action == glfw.Release {
// Keys[key] = gl.FALSE
// }
//}
if inputCallback != nil {
if action == glfw.Press {
inputCallback.OnKeyEvent(int(key), true)
} else if action == glfw.Release {
inputCallback.OnKeyEvent(int(key), false)
}
}
})
window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
if inputCallback != nil {
x, y := w.GetCursorPos()
pb := int(button)
if action == glfw.Press {
inputCallback.OnPointEvent(pb, true, float32(x), float32(y))
} else {
inputCallback.OnPointEvent(pb, false, float32(x), float32(y))
}
}
})
window.SetIconifyCallback(func(w *glfw.Window, iconified bool) {
if iconified {
windowCallback.OnPause()
} else {
windowCallback.OnResume()
}
})
window.SetFocusCallback(func(w *glfw.Window, focused bool) {
windowCallback.OnFocusChanged(focused)
})
window.SetSizeCallback(func(w *glfw.Window, width int, height int) {
windowCallback.OnResize(int32(width), int32(height))
// 此处应该判断是否全屏 TODO
})
// init openGL
// golang 版本必须调用此init方法来加载本地OpenGL指针,C原声不需要
// 见go-gl文档:https://github.com/go-gl/gl
if err := gl.Init(); err != nil {
fmt.Print(err)
return
}
// 读取本机的 OpenGL 版本
//version := gl.GoStr(gl.GetString(gl.VERSION))
//fmt.Println("OpenGL version", version)
// viewport size
w, h := window.GetFramebufferSize()
gl.Viewport(0, 0, int32(w), int32(h))
// DEBUG
// ========== Engine Start
windowCallback.OnCreate(float32(option.Width), float32(option.Height), float32(w)/float32(option.Width))
windowCallback.OnResize(int32(option.Width), int32(option.Height))
// ========== Engine End
// 全局配置
//gl.Enable(gl.DEPTH_TEST)
//gl.DepthFunc(gl.LESS)
if option.Clear[3] == 0 {
gl.ClearColor(1.0, 1.0, 1.0, 1.0)
} else {
gl.ClearColor(option.Clear[0], option.Clear[1], option.Clear[2], option.Clear[3])
}
// 如果窗口没有关闭,那么应该持续当前的循环
// main loop...
for !window.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
windowCallback.OnLoop()
// swap buffer
window.SwapBuffers()
// poll event
glfw.PollEvents()
// cursor should be update every frame!!
if inputCallback != nil {
x, y := window.GetCursorPos()
inputCallback.OnPointEvent(-1000, false, float32(x), float32(y))
}
}
windowCallback.OnDestroy()
}