Browse Source

GLUT windowing working more or less

Thomas Buck 9 years ago
parent
commit
0ac5aa293a
9 changed files with 372 additions and 45 deletions
  1. 3
    0
      ChangeLog.md
  2. 4
    3
      README.md
  3. 50
    0
      cmake/FindFreeGLUT.cmake
  4. 0
    2
      include/Window.h
  5. 11
    10
      include/WindowGLUT.h
  6. 0
    2
      include/WindowSDL.h
  7. 8
    8
      src/CMakeLists.txt
  8. 296
    14
      src/WindowGLUT.cpp
  9. 0
    6
      src/WindowSDL.cpp

+ 3
- 0
ChangeLog.md View File

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141008 ]
6
+    * Added freeGLUT windowing system to use if SDL2 is not available. Not yet working 100%.
7
+
5 8
     [ 20141007 ]
6 9
     * Set all RunTime vars to sensible defaults, so OpenRaider can try to start
7 10
       without a config file (a font is still required!)

+ 4
- 3
README.md View File

@@ -4,12 +4,10 @@
4 4
 
5 5
 This is a fork of the [OpenRaider](http://openraider.sourceforge.net) Open Source implementation of the classic Tomb Raider Game Engine. The original project was abandoned in 2003.
6 6
 
7
-This fork aims to get OpenRaider in a more usable state, maybe one day even being able to play the old Tomb Raider games flawlessly...
7
+This fork aims to get OpenRaider in a more usable state, maybe one day even being able to play the old Tomb Raider games flawlessly, while bringing the code to modern standards.
8 8
 
9 9
 Visit the IRC Channel `#OpenRaider` on `chat.freenode.net`. [Webchat](http://webchat.freenode.net/?channels=%23OpenRaider).
10 10
 
11
-If you just want to see OpenRaider doing something on your Mac, without installing any of the dependencies needed to build OpenRaider, grab the Mac App Bundle from the [most recent release](https://github.com/xythobuz/OpenRaider/releases).
12
-
13 11
 It seems as if OpenRaider will currently only work on Little-Endian platforms. This is not nice and will hopefully be fixed in the future!
14 12
 
15 13
 [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=xythobuz&url=https://github.com/xythobuz/OpenRaider&title=OpenRaider&language=&tags=github&category=software)
@@ -61,6 +59,8 @@ Linux Distributions will probably provide an ALUT library ready to install with
61 59
 
62 60
 Sound support is optional, however. If cmake can’t find OpenAL and ALUT, Audio will be deactivated. If you want to explicitly deactivate it, call cmake with `-DENABLE_AUDIO=NO`.
63 61
 
62
+There is also a rudimentary windowing implementation using freeGLUT. It will be used if GLUT is found instead of SDL2, or if you call cmake with `-DFORCE_GLUT=YES`. Be aware that your experience will not be as nice as with SDL2...
63
+
64 64
 ## Building
65 65
 
66 66
 Do a standard cmake out-of-source build to get a debug configuration and then run the resulting executable:
@@ -168,6 +168,7 @@ There are some included cmake scripts:
168 168
 * [FindSSE](https://gitorious.org/vc/vc/source/a1d8b9fc31060d870386613cc72319546c850b87:cmake/FindSSE.cmake)
169 169
 * [GetGitRevisionDescription.cmake](https://github.com/rpavlik/cmake-modules/blob/master/GetGitRevisionDescription.cmake)
170 170
 * [GetGitRevisionDescription.cmake.in](https://github.com/rpavlik/cmake-modules/blob/master/GetGitRevisionDescription.cmake.in)
171
+* [FindFreeGLUT](https://github.com/PDAL/PDAL/blob/master/cmake/modules/FindFreeGLUT.cmake)
171 172
 
172 173
 See the respective files in `cmake` for their licensing.
173 174
 

+ 50
- 0
cmake/FindFreeGLUT.cmake View File

@@ -0,0 +1,50 @@
1
+# Copyright (C) 2007-2009 LuaDist.
2
+# Created by Peter Kapec <kapecp@gmail.com>
3
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
4
+# For details see the COPYRIGHT file distributed with LuaDist.
5
+#	Note:
6
+#		Searching headers and libraries is very simple and is NOT as powerful as scripts
7
+#		distributed with CMake, because LuaDist defines directories to search for.
8
+#		Everyone is encouraged to contact the author with improvements. Maybe this file
9
+#		becomes part of CMake distribution sometimes.
10
+
11
+# - Find FreeGLUT
12
+# Find the native FreeGLUT headers and libraries.
13
+#
14
+#  FREEGLUT_INCLUDE_DIRS - where to find freeglut.h, etc.
15
+#  FREEGLUT_LIBRARIES    - List of libraries when using FreeGLUT.
16
+#  FREEGLUT_FOUND        - True if FreeGLUT found.
17
+
18
+# 2014-10-08 Thomas Buck <xythobuz@xythobuz.de>:
19
+#   Fix to work on Mac OS X
20
+
21
+# Look for the header file.
22
+FIND_PATH(FREEGLUT_INCLUDE_DIR NAMES GL/freeglut.h
23
+    PATHS /opt/local/include
24
+)
25
+
26
+# Look for the library.
27
+IF(APPLE)
28
+    # We don't want to use the GLUT shipping with OS X
29
+    FIND_LIBRARY(FREEGLUT_LIBRARY NAMES freeglut glut
30
+        PATHS /opt/local/lib
31
+        NO_DEFAULT_PATH
32
+    )
33
+ELSE(APPLE)
34
+    FIND_LIBRARY(FREEGLUT_LIBRARY NAMES freeglut)
35
+ENDIF(APPLE)
36
+
37
+# Handle the QUIETLY and REQUIRED arguments and set FREEGLUT_FOUND to TRUE if all listed variables are TRUE.
38
+INCLUDE(FindPackageHandleStandardArgs)
39
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(FREEGLUT DEFAULT_MSG FREEGLUT_LIBRARY FREEGLUT_INCLUDE_DIR)
40
+
41
+# Copy the results to the output variables.
42
+IF(FREEGLUT_FOUND)
43
+	SET(FREEGLUT_LIBRARIES ${FREEGLUT_LIBRARY})
44
+	SET(FREEGLUT_INCLUDE_DIRS ${FREEGLUT_INCLUDE_DIR})
45
+ELSE(FREEGLUT_FOUND)
46
+	SET(FREEGLUT_LIBRARIES)
47
+	SET(FREEGLUT_INCLUDE_DIRS)
48
+ENDIF(FREEGLUT_FOUND)
49
+
50
+MARK_AS_ADVANCED(FREEGLUT_INCLUDE_DIRS FREEGLUT_LIBRARIES)

+ 0
- 2
include/Window.h View File

@@ -32,8 +32,6 @@ public:
32 32
 
33 33
     virtual bool getTextInput() = 0;
34 34
 
35
-    virtual void delay(unsigned int ms) = 0;
36
-
37 35
     virtual void swapBuffersGL() = 0;
38 36
 
39 37
     virtual unsigned int getWidth();

+ 11
- 10
include/WindowGLUT.h View File

@@ -16,16 +16,8 @@
16 16
 class WindowGLUT : public Window {
17 17
 public:
18 18
 
19
-    /*!
20
-     * \brief Constructs an object of WindowGLUT
21
-     */
22 19
     WindowGLUT();
23 20
 
24
-    /*!
25
-     * \brief Deconstructs an object of WindowGLUT
26
-     */
27
-    virtual ~WindowGLUT();
28
-
29 21
     virtual void setSize(unsigned int width, unsigned int height);
30 22
 
31 23
     virtual void setFullscreen(bool fullscreen);
@@ -42,11 +34,20 @@ public:
42 34
 
43 35
     virtual bool getTextInput();
44 36
 
45
-    virtual void delay(unsigned int ms);
46
-
47 37
     virtual void swapBuffersGL();
48 38
 
49 39
 private:
40
+    static void reshapeCallback(int width, int height);
41
+    static void keyboardCallback(unsigned char key, int x, int y);
42
+    static void keyboardUpCallback(unsigned char key, int x, int y);
43
+    static void specialCallback(int key, int x, int y);
44
+    static void specialUpCallback(int key, int x, int y);
45
+    static void mouseCallback(int button, int state, int x, int y);
46
+    static void motionCallback(int x, int y);
47
+    static void mouseWheelCallback(int wheel, int direction, int x, int y);
48
+
49
+    static KeyboardButton convertAsciiButton(unsigned char key);
50
+    static KeyboardButton convertKeyCode(int key);
50 51
 };
51 52
 
52 53
 #endif

+ 0
- 2
include/WindowSDL.h View File

@@ -44,8 +44,6 @@ public:
44 44
 
45 45
     virtual bool getTextInput();
46 46
 
47
-    virtual void delay(unsigned int ms);
48
-
49 47
     virtual void swapBuffersGL();
50 48
 
51 49
 private:

+ 8
- 8
src/CMakeLists.txt View File

@@ -26,11 +26,11 @@ if (SDL2_FOUND AND NOT FORCE_GLUT)
26 26
     endif (SDL2TTF_FOUND)
27 27
 else (SDL2_FOUND AND NOT FORCE_GLUT)
28 28
     # Add freeglut Library
29
-    find_package (GLUT)
30
-    if (GLUT_FOUND)
31
-        include_directories (SYSTEM ${GLUT_INCLUDE_DIR})
32
-        set (LIBS ${LIBS} ${GLUT_LIBRARY})
33
-    endif (GLUT_FOUND)
29
+    find_package (FREEGLUT)
30
+    if (FREEGLUT_FOUND)
31
+        include_directories (SYSTEM ${FREEGLUT_INCLUDE_DIRS})
32
+        set (LIBS ${LIBS} ${FREEGLUT_LIBRARIES})
33
+    endif (FREEGLUT_FOUND)
34 34
 endif (SDL2_FOUND AND NOT FORCE_GLUT)
35 35
 
36 36
 # Add OpenAL Library
@@ -106,12 +106,12 @@ if (SDL2_FOUND AND NOT FORCE_GLUT)
106 106
         set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL_FONT")
107 107
     endif (SDL2TTF_FOUND)
108 108
 else (SDL2_FOUND AND NOT FORCE_GLUT)
109
-    if (GLUT_FOUND)
109
+    if (FREEGLUT_FOUND)
110 110
         set (SRCS ${SRCS} "WindowGLUT.cpp")
111 111
         set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_GLUT")
112
-    else (GLUT_FOUND)
112
+    else (FREEGLUT_FOUND)
113 113
         message (FATAL_ERROR "SDL2 or freeGLUT are required!")
114
-    endif (GLUT_FOUND)
114
+    endif (FREEGLUT_FOUND)
115 115
 endif (SDL2_FOUND AND NOT FORCE_GLUT)
116 116
 
117 117
 if (PNG_FOUND)

+ 296
- 14
src/WindowGLUT.cpp View File

@@ -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
 

+ 0
- 6
src/WindowSDL.cpp View File

@@ -460,12 +460,6 @@ bool WindowSDL::getTextInput() {
460 460
     return mTextInput;
461 461
 }
462 462
 
463
-void WindowSDL::delay(unsigned int ms) {
464
-    assert(mInit == true);
465
-
466
-    SDL_Delay(ms);
467
-}
468
-
469 463
 void WindowSDL::swapBuffersGL() {
470 464
     assert(mInit == true);
471 465
 

Loading…
Cancel
Save