-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
glfw_notwindows.go
306 lines (256 loc) · 6.13 KB
/
glfw_notwindows.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
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows
// +build !js
package glfw
import (
"image"
"sync"
"github.com/go-gl/glfw/v3.3/glfw"
)
type windows map[*glfw.Window]*Window
var (
theWindows = windows{}
windowsM sync.Mutex
)
func (w windows) add(win *glfw.Window) *Window {
if win == nil {
return nil
}
ww := &Window{win}
windowsM.Lock()
w[win] = ww
windowsM.Unlock()
return ww
}
func (w windows) remove(win *glfw.Window) {
windowsM.Lock()
delete(w, win)
windowsM.Unlock()
}
func (w windows) get(win *glfw.Window) *Window {
if win == nil {
return nil
}
windowsM.Lock()
ww := w[win]
windowsM.Unlock()
return ww
}
type Monitor struct {
m *glfw.Monitor
}
func (m *Monitor) GetPos() (x, y int) {
return m.m.GetPos()
}
func (m *Monitor) GetVideoMode() *VidMode {
v := m.m.GetVideoMode()
if v == nil {
return nil
}
return &VidMode{
Width: v.Width,
Height: v.Height,
RedBits: v.RedBits,
GreenBits: v.GreenBits,
BlueBits: v.BlueBits,
RefreshRate: v.RefreshRate,
}
}
type Window struct {
w *glfw.Window
}
func (w *Window) Destroy() {
w.w.Destroy()
theWindows.remove(w.w)
}
func (w *Window) GetAttrib(attrib Hint) int {
return w.w.GetAttrib(glfw.Hint(attrib))
}
func (w *Window) SetAttrib(attrib Hint, value int) {
w.w.SetAttrib(glfw.Hint(attrib), value)
}
func (w *Window) GetCursorPos() (x, y float64) {
return w.w.GetCursorPos()
}
func (w *Window) GetInputMode(mode InputMode) int {
return w.w.GetInputMode(glfw.InputMode(mode))
}
func (w *Window) GetKey(key Key) Action {
return Action(w.w.GetKey(glfw.Key(key)))
}
func (w *Window) GetMonitor() *Monitor {
m := w.w.GetMonitor()
if m == nil {
return nil
}
return &Monitor{m}
}
func (w *Window) GetMouseButton(button MouseButton) Action {
return Action(w.w.GetMouseButton(glfw.MouseButton(button)))
}
func (w *Window) GetPos() (x, y int) {
return w.w.GetPos()
}
func (w *Window) GetSize() (width, height int) {
return w.w.GetSize()
}
func (w *Window) MakeContextCurrent() {
w.w.MakeContextCurrent()
}
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
var gcb glfw.CharModsCallback
if cbfun != nil {
gcb = func(window *glfw.Window, char rune, mods glfw.ModifierKey) {
cbfun(theWindows.get(window), char, ModifierKey(mods))
}
}
w.w.SetCharModsCallback(gcb)
return nil // TODO
}
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
var gcb glfw.FramebufferSizeCallback
if cbfun != nil {
gcb = func(window *glfw.Window, width int, height int) {
cbfun(theWindows.get(window), width, height)
}
}
w.w.SetFramebufferSizeCallback(gcb)
return nil // TODO
}
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
var gcb glfw.ScrollCallback
if cbfun != nil {
gcb = func(window *glfw.Window, xoff float64, yoff float64) {
cbfun(theWindows.get(window), xoff, yoff)
}
}
w.w.SetScrollCallback(gcb)
return nil // TODO
}
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
var gcb glfw.SizeCallback
if cbfun != nil {
gcb = func(window *glfw.Window, width, height int) {
cbfun(theWindows.get(window), width, height)
}
}
w.w.SetSizeCallback(gcb)
return nil // TODO
}
func (w *Window) SetIcon(images []image.Image) {
w.w.SetIcon(images)
}
func (w *Window) SetInputMode(mode InputMode, value int) {
w.w.SetInputMode(glfw.InputMode(mode), value)
}
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
var m *glfw.Monitor
if monitor != nil {
m = monitor.m
}
w.w.SetMonitor(m, xpos, ypos, width, height, refreshRate)
}
func (w *Window) SetPos(xpos, ypos int) {
w.w.SetPos(xpos, ypos)
}
func (w *Window) SetSize(width, height int) {
w.w.SetSize(width, height)
}
func (w *Window) SetTitle(title string) {
w.w.SetTitle(title)
}
func (w *Window) ShouldClose() bool {
return w.w.ShouldClose()
}
func (w *Window) Show() {
w.w.Show()
}
func (w *Window) SwapBuffers() {
w.w.SwapBuffers()
}
func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
var gm *glfw.Monitor
if monitor != nil {
gm = monitor.m
}
var gw *glfw.Window
if share != nil {
gw = share.w
}
w, err := glfw.CreateWindow(width, height, title, gm, gw)
if err != nil {
return nil, err
}
return theWindows.add(w), nil
}
func (j Joystick) GetAxes() []float32 {
return glfw.Joystick(j).GetAxes()
}
func (j Joystick) GetButtons() []Action {
var bs []Action
for _, b := range glfw.Joystick(j).GetButtons() {
bs = append(bs, Action(b))
}
return bs
}
func GetMonitors() []*Monitor {
ms := []*Monitor{}
for _, m := range glfw.GetMonitors() {
if m != nil {
ms = append(ms, &Monitor{m})
} else {
ms = append(ms, nil)
}
}
return ms
}
func GetPrimaryMonitor() *Monitor {
m := glfw.GetPrimaryMonitor()
if m == nil {
return nil
}
return &Monitor{m}
}
func Init() error {
return glfw.Init()
}
func (j Joystick) Present() bool {
return glfw.Joystick(j).Present()
}
func PollEvents() {
glfw.PollEvents()
}
func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) {
var gcb func(monitor *glfw.Monitor, event glfw.PeripheralEvent)
if cbfun != nil {
gcb = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
var m *Monitor
if monitor != nil {
m = &Monitor{monitor}
}
cbfun(m, PeripheralEvent(event))
}
}
glfw.SetMonitorCallback(gcb)
}
func SwapInterval(interval int) {
glfw.SwapInterval(interval)
}
func Terminate() {
glfw.Terminate()
}
func WindowHint(target Hint, hint int) {
glfw.WindowHint(glfw.Hint(target), hint)
}