|
@@ -5,8 +5,9 @@
|
5
|
5
|
* \author xythobuz
|
6
|
6
|
*/
|
7
|
7
|
|
8
|
|
-#include <cstdio>
|
9
|
|
-#include <ctime>
|
|
8
|
+#include <cstring>
|
|
9
|
+
|
|
10
|
+#include <GL/freeglut.h>
|
10
|
11
|
|
11
|
12
|
#include "global.h"
|
12
|
13
|
#include "RunTime.h"
|
|
@@ -14,6 +15,9 @@
|
14
|
15
|
#include "utils/strings.h"
|
15
|
16
|
#include "WindowGLUT.h"
|
16
|
17
|
|
|
18
|
+static int lastMouseX = 0;
|
|
19
|
+static int lastMouseY = 0;
|
|
20
|
+
|
17
|
21
|
WindowGLUT::WindowGLUT() {
|
18
|
22
|
mInit = false;
|
19
|
23
|
mWidth = DEFAULT_WIDTH;
|
|
@@ -23,11 +27,6 @@ WindowGLUT::WindowGLUT() {
|
23
|
27
|
mTextInput = false;
|
24
|
28
|
}
|
25
|
29
|
|
26
|
|
-WindowGLUT::~WindowGLUT() {
|
27
|
|
- if (mInit) {
|
28
|
|
- }
|
29
|
|
-}
|
30
|
|
-
|
31
|
30
|
void WindowGLUT::setSize(unsigned int width, unsigned int height) {
|
32
|
31
|
assert(width > 0);
|
33
|
32
|
assert(height > 0);
|
|
@@ -36,7 +35,7 @@ void WindowGLUT::setSize(unsigned int width, unsigned int height) {
|
36
|
35
|
mHeight = height;
|
37
|
36
|
|
38
|
37
|
if (mInit == true) {
|
39
|
|
- resizeGL();
|
|
38
|
+ glutReshapeWindow(width, height);
|
40
|
39
|
}
|
41
|
40
|
}
|
42
|
41
|
|
|
@@ -44,6 +43,10 @@ void WindowGLUT::setFullscreen(bool fullscreen) {
|
44
|
43
|
mFullscreen = fullscreen;
|
45
|
44
|
|
46
|
45
|
if (mInit == true) {
|
|
46
|
+ if (mFullscreen)
|
|
47
|
+ glutFullScreen();
|
|
48
|
+ else
|
|
49
|
+ glutLeaveFullScreen();
|
47
|
50
|
}
|
48
|
51
|
}
|
49
|
52
|
|
|
@@ -51,6 +54,10 @@ void WindowGLUT::setMousegrab(bool grab) {
|
51
|
54
|
mMousegrab = grab;
|
52
|
55
|
|
53
|
56
|
if (mInit == true) {
|
|
57
|
+ if (mMousegrab)
|
|
58
|
+ glutSetCursor(GLUT_CURSOR_NONE);
|
|
59
|
+ else
|
|
60
|
+ glutSetCursor(GLUT_CURSOR_INHERIT);
|
54
|
61
|
}
|
55
|
62
|
}
|
56
|
63
|
|
|
@@ -61,24 +68,40 @@ bool WindowGLUT::getMousegrab() {
|
61
|
68
|
int WindowGLUT::initialize() {
|
62
|
69
|
assert(mInit == false);
|
63
|
70
|
|
|
71
|
+ int argc = 1;
|
|
72
|
+ char *argv[] = { new char[11], nullptr };
|
|
73
|
+ strcpy(argv[0], "OpenRaider");
|
|
74
|
+ glutInit(&argc, argv);
|
|
75
|
+ glutInitWindowSize(mWidth, mHeight);
|
|
76
|
+ glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
|
|
77
|
+ glutCreateWindow(VERSION);
|
64
|
78
|
|
65
|
|
- mInit = true;
|
|
79
|
+ glutReshapeFunc(WindowGLUT::reshapeCallback);
|
|
80
|
+ glutKeyboardFunc(WindowGLUT::keyboardCallback);
|
|
81
|
+ glutKeyboardUpFunc(WindowGLUT::keyboardUpCallback);
|
|
82
|
+ glutSpecialFunc(WindowGLUT::specialCallback);
|
|
83
|
+ glutSpecialUpFunc(WindowGLUT::specialUpCallback);
|
|
84
|
+ glutMouseFunc(WindowGLUT::mouseCallback);
|
|
85
|
+ glutMotionFunc(WindowGLUT::motionCallback);
|
|
86
|
+ glutPassiveMotionFunc(WindowGLUT::motionCallback);
|
|
87
|
+ glutMouseWheelFunc(WindowGLUT::mouseWheelCallback);
|
66
|
88
|
|
|
89
|
+ delete [] argv[0];
|
67
|
90
|
|
|
91
|
+ mInit = true;
|
68
|
92
|
return 0;
|
69
|
93
|
}
|
70
|
94
|
|
71
|
95
|
void WindowGLUT::eventHandling() {
|
72
|
96
|
assert(mInit == true);
|
73
|
97
|
|
74
|
|
-
|
|
98
|
+ glutMainLoopEvent();
|
75
|
99
|
|
76
|
100
|
UI::eventsFinished();
|
77
|
101
|
}
|
78
|
102
|
|
79
|
103
|
void WindowGLUT::setTextInput(bool on) {
|
80
|
104
|
assert(mInit == true);
|
81
|
|
-
|
82
|
105
|
mTextInput = on;
|
83
|
106
|
}
|
84
|
107
|
|
|
@@ -87,11 +110,270 @@ bool WindowGLUT::getTextInput() {
|
87
|
110
|
return mTextInput;
|
88
|
111
|
}
|
89
|
112
|
|
90
|
|
-void WindowGLUT::delay(unsigned int ms) {
|
|
113
|
+void WindowGLUT::swapBuffersGL() {
|
91
|
114
|
assert(mInit == true);
|
|
115
|
+ glutSwapBuffers();
|
92
|
116
|
}
|
93
|
117
|
|
94
|
|
-void WindowGLUT::swapBuffersGL() {
|
95
|
|
- assert(mInit == true);
|
|
118
|
+void WindowGLUT::reshapeCallback(int width, int height) {
|
|
119
|
+ getWindow().resizeGL();
|
|
120
|
+}
|
|
121
|
+
|
|
122
|
+// Note that the escape, backspace, and delete keys are generated as an ASCII character.
|
|
123
|
+void WindowGLUT::keyboardCallback(unsigned char key, int x, int y) {
|
|
124
|
+ if (getWindow().getTextInput()) {
|
|
125
|
+ if ((key >= ' ') && (key <= '~')) {
|
|
126
|
+ char s[2] = { static_cast<char>(key), '\0' };
|
|
127
|
+ UI::handleText(s, false);
|
|
128
|
+ }
|
|
129
|
+ } else {
|
|
130
|
+ KeyboardButton b = convertAsciiButton(key);
|
|
131
|
+ UI::handleKeyboard(b, true);
|
|
132
|
+ }
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+void WindowGLUT::keyboardUpCallback(unsigned char key, int x, int y) {
|
|
136
|
+ if (!getWindow().getTextInput()) {
|
|
137
|
+ KeyboardButton b = convertAsciiButton(key);
|
|
138
|
+ UI::handleKeyboard(b, false);
|
|
139
|
+ }
|
|
140
|
+}
|
|
141
|
+
|
|
142
|
+void WindowGLUT::specialCallback(int key, int x, int y) {
|
|
143
|
+ KeyboardButton b = convertKeyCode(key);
|
|
144
|
+ UI::handleKeyboard(b, true);
|
|
145
|
+}
|
|
146
|
+
|
|
147
|
+void WindowGLUT::specialUpCallback(int key, int x, int y) {
|
|
148
|
+ KeyboardButton b = convertKeyCode(key);
|
|
149
|
+ UI::handleKeyboard(b, false);
|
|
150
|
+}
|
|
151
|
+
|
|
152
|
+void WindowGLUT::mouseCallback(int button, int state, int x, int y) {
|
|
153
|
+ KeyboardButton b;
|
|
154
|
+
|
|
155
|
+ switch (button) {
|
|
156
|
+ case GLUT_LEFT_BUTTON:
|
|
157
|
+ b = leftmouseKey;
|
|
158
|
+ break;
|
|
159
|
+
|
|
160
|
+ case GLUT_RIGHT_BUTTON:
|
|
161
|
+ b = rightmouseKey;
|
|
162
|
+ break;
|
|
163
|
+
|
|
164
|
+ case GLUT_MIDDLE_BUTTON:
|
|
165
|
+ b = middlemouseKey;
|
|
166
|
+ break;
|
|
167
|
+
|
|
168
|
+ default:
|
|
169
|
+ b = unknownKey;
|
|
170
|
+ break;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ UI::handleMouseClick(x, y, b, (state == GLUT_UP));
|
|
174
|
+}
|
|
175
|
+
|
|
176
|
+// The x and y callback parameters indicate the mouse location in window relative coordinates.
|
|
177
|
+void WindowGLUT::motionCallback(int x, int y) {
|
|
178
|
+ int xrel = x - lastMouseX;
|
|
179
|
+ int yrel = y - lastMouseY;
|
|
180
|
+ UI::handleMouseMotion(xrel, yrel, x, y);
|
|
181
|
+ lastMouseX = x;
|
|
182
|
+ lastMouseY = y;
|
|
183
|
+
|
|
184
|
+ if (getWindow().getMousegrab()) {
|
|
185
|
+ lastMouseX = getWindow().getWidth() / 2;
|
|
186
|
+ lastMouseY = getWindow().getHeight() / 2;
|
|
187
|
+ glutWarpPointer(lastMouseX, lastMouseY);
|
|
188
|
+ }
|
|
189
|
+}
|
|
190
|
+
|
|
191
|
+void WindowGLUT::mouseWheelCallback(int wheel, int direction, int x, int y) {
|
|
192
|
+ int xrel = 0, yrel = 0;
|
|
193
|
+
|
|
194
|
+ if (wheel == 0)
|
|
195
|
+ yrel = direction;
|
|
196
|
+ else
|
|
197
|
+ xrel = direction;
|
|
198
|
+
|
|
199
|
+ UI::handleMouseScroll(xrel, yrel);
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+KeyboardButton WindowGLUT::convertAsciiButton(unsigned char key) {
|
|
203
|
+ // Convert Uppercase to Lowercase
|
|
204
|
+ if ((key >= 'A') && (key <= 'Z'))
|
|
205
|
+ key = key - 'A' + 'a';
|
|
206
|
+
|
|
207
|
+ // Alphanumerics can be returned as is
|
|
208
|
+ if (((key >= '0') && (key <= '9'))
|
|
209
|
+ || ((key >= 'a') && (key <= 'z'))) {
|
|
210
|
+ return static_cast<KeyboardButton>(key);
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ //! \fixme GLUT requires keyboard layout? Currently US is hard coded
|
|
214
|
+ switch (key) {
|
|
215
|
+ case ' ':
|
|
216
|
+ return spaceKey;
|
|
217
|
+
|
|
218
|
+ case '!':
|
|
219
|
+ return oneKey;
|
|
220
|
+
|
|
221
|
+ case '@':
|
|
222
|
+ return twoKey;
|
|
223
|
+
|
|
224
|
+ case '#':
|
|
225
|
+ return threeKey;
|
|
226
|
+
|
|
227
|
+ case '$':
|
|
228
|
+ return fourKey;
|
|
229
|
+
|
|
230
|
+ case '%':
|
|
231
|
+ return fiveKey;
|
|
232
|
+
|
|
233
|
+ case '^':
|
|
234
|
+ return sixKey;
|
|
235
|
+
|
|
236
|
+ case '&':
|
|
237
|
+ return sevenKey;
|
|
238
|
+
|
|
239
|
+ case '*':
|
|
240
|
+ return eightKey;
|
|
241
|
+
|
|
242
|
+ case '(':
|
|
243
|
+ return nineKey;
|
|
244
|
+
|
|
245
|
+ case ')':
|
|
246
|
+ return zeroKey;
|
|
247
|
+
|
|
248
|
+ case '"':
|
|
249
|
+ case '\'':
|
|
250
|
+ return quoteKey;
|
|
251
|
+
|
|
252
|
+ case '+':
|
|
253
|
+ case '=':
|
|
254
|
+ return equalsKey;
|
|
255
|
+
|
|
256
|
+ case ',':
|
|
257
|
+ case '<':
|
|
258
|
+ return commaKey;
|
|
259
|
+
|
|
260
|
+ case '-':
|
|
261
|
+ case '_':
|
|
262
|
+ return minusKey;
|
|
263
|
+
|
|
264
|
+ case '.':
|
|
265
|
+ case '>':
|
|
266
|
+ return dotKey;
|
|
267
|
+
|
|
268
|
+ case '/':
|
|
269
|
+ case '?':
|
|
270
|
+ return slashKey;
|
|
271
|
+
|
|
272
|
+ case ':':
|
|
273
|
+ case ';':
|
|
274
|
+ return semicolonKey;
|
|
275
|
+
|
|
276
|
+ case '[':
|
|
277
|
+ case '{':
|
|
278
|
+ return leftbracketKey;
|
|
279
|
+
|
|
280
|
+ case ']':
|
|
281
|
+ case '}':
|
|
282
|
+ return rightbracketKey;
|
|
283
|
+
|
|
284
|
+ case '\\':
|
|
285
|
+ case '|':
|
|
286
|
+ return backslashKey;
|
|
287
|
+
|
|
288
|
+ case '`':
|
|
289
|
+ case '~':
|
|
290
|
+ return backquoteKey;
|
|
291
|
+
|
|
292
|
+ case '\t':
|
|
293
|
+ return tabKey;
|
|
294
|
+
|
|
295
|
+ case 8: // Backspace
|
|
296
|
+ return backspaceKey;
|
|
297
|
+
|
|
298
|
+ case '\r':
|
|
299
|
+ case '\n':
|
|
300
|
+ return enterKey;
|
|
301
|
+
|
|
302
|
+ case 27: // Escape
|
|
303
|
+ return escapeKey;
|
|
304
|
+
|
|
305
|
+ default:
|
|
306
|
+ return unknownKey;
|
|
307
|
+ }
|
|
308
|
+}
|
|
309
|
+
|
|
310
|
+KeyboardButton WindowGLUT::convertKeyCode(int key) {
|
|
311
|
+ switch (key) {
|
|
312
|
+ case GLUT_KEY_F1:
|
|
313
|
+ return f1Key;
|
|
314
|
+
|
|
315
|
+ case GLUT_KEY_F2:
|
|
316
|
+ return f2Key;
|
|
317
|
+
|
|
318
|
+ case GLUT_KEY_F3:
|
|
319
|
+ return f3Key;
|
|
320
|
+
|
|
321
|
+ case GLUT_KEY_F4:
|
|
322
|
+ return f4Key;
|
|
323
|
+
|
|
324
|
+ case GLUT_KEY_F5:
|
|
325
|
+ return f5Key;
|
|
326
|
+
|
|
327
|
+ case GLUT_KEY_F6:
|
|
328
|
+ return f6Key;
|
|
329
|
+
|
|
330
|
+ case GLUT_KEY_F7:
|
|
331
|
+ return f7Key;
|
|
332
|
+
|
|
333
|
+ case GLUT_KEY_F8:
|
|
334
|
+ return f8Key;
|
|
335
|
+
|
|
336
|
+ case GLUT_KEY_F9:
|
|
337
|
+ return f9Key;
|
|
338
|
+
|
|
339
|
+ case GLUT_KEY_F10:
|
|
340
|
+ return f10Key;
|
|
341
|
+
|
|
342
|
+ case GLUT_KEY_F11:
|
|
343
|
+ return f11Key;
|
|
344
|
+
|
|
345
|
+ case GLUT_KEY_F12:
|
|
346
|
+ return f12Key;
|
|
347
|
+
|
|
348
|
+ case GLUT_KEY_LEFT:
|
|
349
|
+ return leftKey;
|
|
350
|
+
|
|
351
|
+ case GLUT_KEY_UP:
|
|
352
|
+ return upKey;
|
|
353
|
+
|
|
354
|
+ case GLUT_KEY_RIGHT:
|
|
355
|
+ return rightKey;
|
|
356
|
+
|
|
357
|
+ case GLUT_KEY_DOWN:
|
|
358
|
+ return downKey;
|
|
359
|
+
|
|
360
|
+ case GLUT_KEY_PAGE_UP:
|
|
361
|
+ return pageupKey;
|
|
362
|
+
|
|
363
|
+ case GLUT_KEY_PAGE_DOWN:
|
|
364
|
+ return pagedownKey;
|
|
365
|
+
|
|
366
|
+ case GLUT_KEY_HOME:
|
|
367
|
+ return homeKey;
|
|
368
|
+
|
|
369
|
+ case GLUT_KEY_END:
|
|
370
|
+ return endKey;
|
|
371
|
+
|
|
372
|
+ case GLUT_KEY_INSERT:
|
|
373
|
+ return insertKey;
|
|
374
|
+
|
|
375
|
+ default:
|
|
376
|
+ return unknownKey;
|
|
377
|
+ }
|
96
|
378
|
}
|
97
|
379
|
|