Thomas Buck 10 anni fa
parent
commit
dded8c3512
7 ha cambiato i file con 79 aggiunte e 93 eliminazioni
  1. 2
    0
      ChangeLog
  2. 5
    5
      Makefile
  3. 2
    2
      README.md
  4. 3
    2
      include/SDLSystem.h
  5. 25
    51
      src/SDLSystem.cpp
  6. 1
    1
      src/Texture.cpp
  7. 41
    32
      test/GLString.cpp

+ 2
- 0
ChangeLog Vedi File

@@ -7,6 +7,8 @@
7 7
 
8 8
 	[ 20140210 ]
9 9
 	* Finished the Tomb Raider 1 Item/State definitions
10
+	* Ported to SDL2 and SDL2-TTF using the Migration Guide:
11
+	  https://wiki.libsdl.org/MigrationGuide
10 12
 
11 13
 	[ 20140209 ]
12 14
 	* Renamed OpenGLMesh to Mesh

+ 5
- 5
Makefile Vedi File

@@ -29,9 +29,9 @@ UNAME=$(shell uname -s)
29 29
 # -DUNICODE_SUPPORT			Add unicode/internation keyboard support
30 30
 # -DUSING_EMITTER_IN_GAME	Run particle test in game
31 31
 
32
-BASE_DEFS=$(shell sdl-config --cflags) -Iinclude -DUSING_EMITTER
32
+BASE_DEFS=$(shell sdl2-config --cflags) -Iinclude -DUSING_EMITTER
33 33
 
34
-BASE_LIBS=$(shell sdl-config --libs) -lz -lstdc++ \
34
+BASE_LIBS=$(shell sdl2-config --libs) -lz -lstdc++ \
35 35
 	-lpthread -lSDL_ttf
36 36
 
37 37
 # -DDEBUG_GL
@@ -362,9 +362,9 @@ TombRaider.test:
362 362
 GLString.test:
363 363
 	mkdir -p $(BUILD_TEST_DIR)
364 364
 	$(CC) $(FLAGS_ALL) $(WARNINGS) -Iinclude \
365
-	$(shell sdl-config --cflags) $(shell sdl-config --libs) \
366
-	$(GL_LIBS) $(GL_DEFS) -lSDL_ttf -lm -lstdc++ \
367
-	src/Texture.cpp src/GLString.cpp \
365
+	$(shell sdl2-config --cflags) $(shell sdl2-config --libs) \
366
+	$(GL_LIBS) $(GL_DEFS) -lSDL2_ttf -lm -lstdc++ \
367
+	src/Texture.cpp src/tga.cpp src/GLString.cpp \
368 368
 	test/GLString.cpp -o $(BUILD_TEST_DIR)/GLString.test
369 369
 
370 370
 #################################################################

+ 2
- 2
README.md Vedi File

@@ -32,14 +32,14 @@ A more or less recent [Doxygen documentation](http://xythobuz.github.io/OpenRaid
32 32
 Basically, OpenRaider depends on the following:
33 33
 
34 34
 * OpenGL
35
-* SDL & SDL-TTF
35
+* SDL2 & SDL2-TTF
36 36
 * OpenAL & ALUT
37 37
 * Posix Threads
38 38
 * zlib
39 39
 
40 40
 On Mac OS X 10.9 with [XCode](https://developer.apple.com/xcode/) and [MacPorts](http://www.macports.org) installed, the following should be enough to get all dependencies that are available as port:
41 41
 
42
-    sudo port install zlib cmake libsdl libsdl_ttf
42
+    sudo port install zlib cmake libsdl2 libsdl2_ttf
43 43
 
44 44
 A similar command for the package manager of your favorite Linux Distribution should do the trick.
45 45
 

+ 3
- 2
include/SDLSystem.h Vedi File

@@ -8,7 +8,7 @@
8 8
 #ifndef _SDLSYSTEM_H_
9 9
 #define _SDLSYSTEM_H_
10 10
 
11
-#include <SDL/SDL.h>
11
+#include <SDL2/SDL.h>
12 12
 #include <System.h>
13 13
 
14 14
 /*!
@@ -121,7 +121,8 @@ protected:
121 121
     bool mFullscreen;      //!< Current Fullscreen/Windowed mode
122 122
 
123 123
 private:
124
-    SDL_Surface *mWindow;  //!< This is the pointer to the SDL surface
124
+    SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
125
+    SDL_GLContext mGLContext; //!< The OpenGL Context
125 126
 };
126 127
 
127 128
 #endif

+ 25
- 51
src/SDLSystem.cpp Vedi File

@@ -14,7 +14,7 @@
14 14
 #include <memory_test.h>
15 15
 #endif
16 16
 
17
-#include <SDL/SDL_opengl.h>
17
+#include <SDL2/SDL_opengl.h>
18 18
 
19 19
 #include <MatMath.h>
20 20
 #include <SDLSystem.h>
@@ -51,7 +51,7 @@ void SDLSystem::bindKeyCommand(const char *cmd, int key, int event) {
51 51
 #endif
52 52
 
53 53
 void SDLSystem::setGrabMouse(bool on) {
54
-    SDL_WM_GrabInput(on ? SDL_GRAB_ON : SDL_GRAB_OFF);
54
+    SDL_SetRelativeMouseMode(on ? SDL_TRUE : SDL_FALSE);
55 55
 }
56 56
 
57 57
 void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscreen) {
@@ -83,13 +83,12 @@ void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscre
83 83
     }
84 84
 #endif
85 85
 
86
-    flags |= SDL_OPENGL;
86
+    flags |= SDL_WINDOW_OPENGL;
87 87
 
88 88
     mFullscreen = fullscreen;
89 89
     if (mFullscreen)
90
-        flags |= SDL_FULLSCREEN;
90
+        flags |= SDL_WINDOW_FULLSCREEN;
91 91
 
92
-    SDL_ShowCursor(SDL_DISABLE);
93 92
     setGrabMouse(true); // Always grab mouse!
94 93
 
95 94
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
@@ -97,10 +96,13 @@ void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscre
97 96
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
98 97
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
99 98
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
100
-    mWindow = SDL_SetVideoMode(width, height, 16, flags);
101
-    SDL_WM_SetCaption(VERSION, VERSION);
99
+
100
+    mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
101
+            width, height, flags);
102
+    mGLContext = SDL_GL_CreateContext(mWindow);
103
+
102 104
     //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
103
-    SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
105
+    //SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
104 106
 
105 107
 #ifdef UNICODE_SUPPORT
106 108
     //@JML get Unicode value of key for international keyboard
@@ -115,8 +117,6 @@ void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscre
115 117
 }
116 118
 
117 119
 void SDLSystem::resize(unsigned int width, unsigned int height) {
118
-    int flags = 0;
119
-
120 120
     GLfloat aspect;
121 121
 
122 122
     m_width = width;
@@ -139,14 +139,6 @@ void SDLSystem::resize(unsigned int width, unsigned int height) {
139 139
     glMatrixMode(GL_MODELVIEW);
140 140
     glLoadIdentity();
141 141
 
142
-    // Resize window
143
-    flags |= SDL_OPENGL;
144
-
145
-    if (mFullscreen)
146
-        flags |= SDL_FULLSCREEN;
147
-
148
-    mWindow = SDL_SetVideoMode(width, height, 16, flags);
149
-
150 142
     // Resize context
151 143
     resizeGL(width, height);
152 144
 }
@@ -222,10 +214,10 @@ void SDLSystem::runGame() {
222 214
                     if (mkeys & KMOD_RALT)
223 215
                         mod |= SYS_MOD_KEY_RALT;
224 216
 
225
-                    if (mkeys & KMOD_LMETA)
217
+                    if (mkeys & KMOD_LGUI)
226 218
                         mod |= SYS_MOD_KEY_LMETA;
227 219
 
228
-                    if (mkeys & KMOD_RMETA)
220
+                    if (mkeys & KMOD_RGUI)
229 221
                         mod |= SYS_MOD_KEY_RMETA;
230 222
 
231 223
                     key = event.key.keysym.sym;
@@ -369,9 +361,13 @@ void SDLSystem::runGame() {
369 361
                         }
370 362
                     }
371 363
                     break;
372
-                case SDL_VIDEORESIZE:
373
-                    resizeGL(event.resize.w, event.resize.h);
374
-                    gameFrame();
364
+                case SDL_WINDOWEVENT:
365
+                    switch(event.window.event) {
366
+                        case SDL_WINDOWEVENT_RESIZED:
367
+                            resizeGL(event.window.data1, event.window.data2);
368
+                            gameFrame();
369
+                            break;
370
+                    }
375 371
                     break;
376 372
             }
377 373
         }
@@ -394,41 +390,19 @@ void SDLSystem::shutdown(int i) {
394 390
 }
395 391
 
396 392
 void SDLSystem::toggleFullscreen() {
397
-    int width = m_width;
398
-    int height = m_height;
399 393
     if (mWindow) {
400 394
         mFullscreen = !mFullscreen;
401 395
 
402
-        // SDL_WM_ToggleFullScreen does not work on all platforms
403
-        // eg. Mac OS X
404
-        // SDL_WM_ToggleFullScreen(mWindow);
405
-
406
-        // I added a mFullscreen flag to this class. Then I modified it's
407
-        // resize() method to use the SDL_FULLSCREEN flag in the
408
-        // SetVideoMode() call based on the mFullscreen flag.
409
-        // Then, I modified this method to find out an available
410
-        // resolution for the fullscreen mode.
411
-        // Now you can see something when switching to Fullscreen,
412
-        // but it's full of graphical glitches...? I don't know...
413
-        // -- xythobuz 2013-12-31
414
-
415
-        if (mFullscreen) {
416
-            SDL_Rect **dimensions = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
417
-            if (dimensions == NULL) {
418
-                printf("Can't enter fullscreen!\n");
419
-                mFullscreen = false;
420
-            } else if (dimensions != (SDL_Rect **)-1) {
421
-                //! \fixme Don't just use first available resolution...
422
-                width = dimensions[0]->w;
423
-                height = dimensions[0]->h;
424
-            }
425
-        }
396
+        if (mFullscreen)
397
+            SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
398
+        else
399
+            SDL_SetWindowFullscreen(mWindow, 0);
426 400
 
427
-        resize(width, height);
401
+        // resize(width, height); // not needed with desktop fullscreen
428 402
     }
429 403
 }
430 404
 
431 405
 void SDLSystem::swapBuffersGL() {
432
-    SDL_GL_SwapBuffers();
406
+    SDL_GL_SwapWindow(mWindow);
433 407
 }
434 408
 

+ 1
- 1
src/Texture.cpp Vedi File

@@ -37,7 +37,7 @@
37 37
 #include <memory_test.h>
38 38
 #endif
39 39
 
40
-#include <SDL/SDL_ttf.h>
40
+#include <SDL2/SDL_ttf.h>
41 41
 
42 42
 #ifdef __APPLE__
43 43
 #include <OpenGL/gl.h>

+ 41
- 32
test/GLString.cpp Vedi File

@@ -3,10 +3,11 @@
3 3
  * \brief Open GL rendering font/string Unit Test
4 4
  *
5 5
  * \author Mongoose
6
+ * \author xythobuz
6 7
  */
7 8
 
8 9
 #include <math.h>
9
-#include <SDL/SDL.h>
10
+#include <SDL2/SDL.h>
10 11
 #ifdef __APPLE__
11 12
 #include <OpenGL/glu.h>
12 13
 #else
@@ -17,7 +18,8 @@
17 18
 #include <GLString.h>
18 19
 
19 20
 GLString *TEXT;
20
-SDL_Surface *SDL_WINDOW = NULL;
21
+SDL_Window *sdlWindow;
22
+SDL_GLContext glContext;
21 23
 Texture gTexture;
22 24
 
23 25
 void swap_buffers();
@@ -39,7 +41,8 @@ void event_resize(int width, int height) {
39 41
 }
40 42
 
41 43
 void event_display(int width, int height) {
42
-    static float x = 0.0f, y = 0.0f, z = -150.0f, r = 0.0f;
44
+    static float r = 0.0f;
45
+    float x = 0.0f, y = 0.0f, z = -150.0f;
43 46
 
44 47
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45 48
     glLoadIdentity();
@@ -65,7 +68,7 @@ void event_display(int width, int height) {
65 68
     glDisable(GL_CULL_FACE);
66 69
     glEnable(GL_BLEND);
67 70
     glEnable(GL_TEXTURE_2D);
68
-    glColor3f(0.75, 0.5, 1.0);
71
+    glColor3f(0.5f, 0.7f, 1.0f);
69 72
 
70 73
     glEnterMode2d(width, height);
71 74
     TEXT->Render();
@@ -76,18 +79,17 @@ void event_display(int width, int height) {
76 79
 }
77 80
 
78 81
 void swap_buffers() {
79
-    SDL_GL_SwapBuffers();
82
+    SDL_GL_SwapWindow(sdlWindow);
80 83
 }
81 84
 
82 85
 
83 86
 void shutdown_gl() {
87
+    SDL_GL_DeleteContext(glContext);
88
+    SDL_DestroyWindow(sdlWindow);
84 89
     SDL_Quit();
85 90
 }
86 91
 
87
-void init_gl(unsigned int width, unsigned int height) {
88
-    int i;
89
-    const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
90
-
92
+void init_gl() {
91 93
     // Setup GL
92 94
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
93 95
     glShadeModel(GL_SMOOTH);
@@ -95,8 +97,11 @@ void init_gl(unsigned int width, unsigned int height) {
95 97
     glDepthFunc(GL_LESS);
96 98
     glEnable(GL_BLEND);
97 99
     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
100
+}
98 101
 
99
-    event_resize(width, height);
102
+void init_text() {
103
+    int i;
104
+    const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
100 105
 
101 106
     // Mongoose 2002.01.01, Texture setup
102 107
     gTexture.reset();
@@ -106,20 +111,20 @@ void init_gl(unsigned int width, unsigned int height) {
106 111
     gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32);  // ASCII
107 112
 
108 113
     TEXT->Init(4);
109
-    i = TEXT->glPrintf((width/2)-50, height/2-32, "OpenRaider");
114
+    i = TEXT->glPrintf(50, 50, "OpenRaider GLString");
110 115
     if (i) {
111 116
         printf(errorText, i);
112 117
     }
113
-    i = TEXT->glPrintf((width/2)-50, height/2, "GLString");
118
+    i = TEXT->glPrintf(50, 100, "Unit Test by Mongoose");
114 119
     if (i) {
115 120
         printf(errorText, i);
116 121
     }
117 122
     TEXT->Scale(1.2f);
118
-    i = TEXT->glPrintf((width/2)-100, height/2+32, "Unit Test by Mongoose");
123
+    i = TEXT->glPrintf(50, 150, "ported to SDL2 & TTF");
119 124
     if (i) {
120 125
         printf(errorText, i);
121 126
     }
122
-    i = TEXT->glPrintf((width/2)-100, height/2+64, "ported to TTF by xythobuz");
127
+    i = TEXT->glPrintf(50, 200, "by xythobuz");
123 128
     if (i) {
124 129
         printf(errorText, i);
125 130
     }
@@ -164,10 +169,10 @@ void init_gl(unsigned int width, unsigned int height) {
164 169
     }
165 170
 #endif
166 171
 
167
-    flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
172
+    flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
168 173
 
169 174
     if (fullscreen) {
170
-        flags |= SDL_FULLSCREEN;
175
+        flags |= SDL_WINDOW_FULLSCREEN;
171 176
         SDL_ShowCursor(SDL_DISABLE);
172 177
     }
173 178
 
@@ -176,12 +181,14 @@ void init_gl(unsigned int width, unsigned int height) {
176 181
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
177 182
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
178 183
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
179
-    SDL_WINDOW = SDL_SetVideoMode(width, height, 16, flags);
180
-    SDL_WM_SetCaption("GLString Test", "GLString Test");
181
-    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
184
+
185
+    sdlWindow = SDL_CreateWindow("GLString Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
186
+    glContext = SDL_GL_CreateContext(sdlWindow);
182 187
 
183 188
     // Init rendering
184
-    init_gl(width, height);
189
+    init_gl();
190
+    event_resize(width, height);
191
+    init_text();
185 192
 
186 193
     for (;;) {
187 194
         // Pause for 10-20 ms
@@ -220,11 +227,11 @@ void init_gl(unsigned int width, unsigned int height) {
220 227
                     if (mkeys & KMOD_RALT)
221 228
                         mod |= KMOD_RALT;
222 229
 
223
-                    if (mkeys & KMOD_LMETA)
224
-                        mod |= KMOD_LMETA;
230
+                    if (mkeys & KMOD_LGUI)
231
+                        mod |= KMOD_LGUI;
225 232
 
226
-                    if (mkeys & KMOD_RMETA)
227
-                        mod |= KMOD_RMETA;
233
+                    if (mkeys & KMOD_RGUI)
234
+                        mod |= KMOD_RGUI;
228 235
 
229 236
                     key = event.key.keysym.sym;
230 237
 
@@ -234,21 +241,23 @@ void init_gl(unsigned int width, unsigned int height) {
234 241
                             exit(0);
235 242
 #ifdef __APPLE__
236 243
                         case 113: // q
237
-                            if ((mod & KMOD_RMETA) || (mod & KMOD_LMETA))
244
+                            if ((mod & KMOD_RGUI) || (mod & KMOD_LGUI))
238 245
                                 exit(0);
239 246
                             break;
240 247
 #endif
241
-                        case 114: // r
242
-                            break;
243 248
                     }
244 249
                     break;
245 250
                 case SDL_KEYUP:
246 251
                     break;
247
-                case SDL_VIDEORESIZE:
248
-                    width = event.resize.w;
249
-                    height = event.resize.h;
250
-                    event_resize(width, height);
251
-                    event_display(width, height);
252
+                case SDL_WINDOWEVENT:
253
+                    switch(event.window.event) {
254
+                        case SDL_WINDOWEVENT_RESIZED:
255
+                            width = event.window.data1;
256
+                            height = event.window.data2;
257
+                            event_resize(width, height);
258
+                            event_display(width, height);
259
+                            break;
260
+                    }
252 261
                     break;
253 262
             }
254 263
         }

Loading…
Annulla
Salva