Browse Source

Port to SDL2

Thomas Buck 11 years ago
parent
commit
dded8c3512
7 changed files with 79 additions and 93 deletions
  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 View File

7
 
7
 
8
 	[ 20140210 ]
8
 	[ 20140210 ]
9
 	* Finished the Tomb Raider 1 Item/State definitions
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
 	[ 20140209 ]
13
 	[ 20140209 ]
12
 	* Renamed OpenGLMesh to Mesh
14
 	* Renamed OpenGLMesh to Mesh

+ 5
- 5
Makefile View File

29
 # -DUNICODE_SUPPORT			Add unicode/internation keyboard support
29
 # -DUNICODE_SUPPORT			Add unicode/internation keyboard support
30
 # -DUSING_EMITTER_IN_GAME	Run particle test in game
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
 	-lpthread -lSDL_ttf
35
 	-lpthread -lSDL_ttf
36
 
36
 
37
 # -DDEBUG_GL
37
 # -DDEBUG_GL
362
 GLString.test:
362
 GLString.test:
363
 	mkdir -p $(BUILD_TEST_DIR)
363
 	mkdir -p $(BUILD_TEST_DIR)
364
 	$(CC) $(FLAGS_ALL) $(WARNINGS) -Iinclude \
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
 	test/GLString.cpp -o $(BUILD_TEST_DIR)/GLString.test
368
 	test/GLString.cpp -o $(BUILD_TEST_DIR)/GLString.test
369
 
369
 
370
 #################################################################
370
 #################################################################

+ 2
- 2
README.md View File

32
 Basically, OpenRaider depends on the following:
32
 Basically, OpenRaider depends on the following:
33
 
33
 
34
 * OpenGL
34
 * OpenGL
35
-* SDL & SDL-TTF
35
+* SDL2 & SDL2-TTF
36
 * OpenAL & ALUT
36
 * OpenAL & ALUT
37
 * Posix Threads
37
 * Posix Threads
38
 * zlib
38
 * zlib
39
 
39
 
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:
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
 A similar command for the package manager of your favorite Linux Distribution should do the trick.
44
 A similar command for the package manager of your favorite Linux Distribution should do the trick.
45
 
45
 

+ 3
- 2
include/SDLSystem.h View File

8
 #ifndef _SDLSYSTEM_H_
8
 #ifndef _SDLSYSTEM_H_
9
 #define _SDLSYSTEM_H_
9
 #define _SDLSYSTEM_H_
10
 
10
 
11
-#include <SDL/SDL.h>
11
+#include <SDL2/SDL.h>
12
 #include <System.h>
12
 #include <System.h>
13
 
13
 
14
 /*!
14
 /*!
121
     bool mFullscreen;      //!< Current Fullscreen/Windowed mode
121
     bool mFullscreen;      //!< Current Fullscreen/Windowed mode
122
 
122
 
123
 private:
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
 #endif
128
 #endif

+ 25
- 51
src/SDLSystem.cpp View File

14
 #include <memory_test.h>
14
 #include <memory_test.h>
15
 #endif
15
 #endif
16
 
16
 
17
-#include <SDL/SDL_opengl.h>
17
+#include <SDL2/SDL_opengl.h>
18
 
18
 
19
 #include <MatMath.h>
19
 #include <MatMath.h>
20
 #include <SDLSystem.h>
20
 #include <SDLSystem.h>
51
 #endif
51
 #endif
52
 
52
 
53
 void SDLSystem::setGrabMouse(bool on) {
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
 void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscreen) {
57
 void SDLSystem::initVideo(unsigned int width, unsigned int height, bool fullscreen) {
83
     }
83
     }
84
 #endif
84
 #endif
85
 
85
 
86
-    flags |= SDL_OPENGL;
86
+    flags |= SDL_WINDOW_OPENGL;
87
 
87
 
88
     mFullscreen = fullscreen;
88
     mFullscreen = fullscreen;
89
     if (mFullscreen)
89
     if (mFullscreen)
90
-        flags |= SDL_FULLSCREEN;
90
+        flags |= SDL_WINDOW_FULLSCREEN;
91
 
91
 
92
-    SDL_ShowCursor(SDL_DISABLE);
93
     setGrabMouse(true); // Always grab mouse!
92
     setGrabMouse(true); // Always grab mouse!
94
 
93
 
95
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
94
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
97
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
96
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
98
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
97
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
99
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
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
     //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
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
 #ifdef UNICODE_SUPPORT
107
 #ifdef UNICODE_SUPPORT
106
     //@JML get Unicode value of key for international keyboard
108
     //@JML get Unicode value of key for international keyboard
115
 }
117
 }
116
 
118
 
117
 void SDLSystem::resize(unsigned int width, unsigned int height) {
119
 void SDLSystem::resize(unsigned int width, unsigned int height) {
118
-    int flags = 0;
119
-
120
     GLfloat aspect;
120
     GLfloat aspect;
121
 
121
 
122
     m_width = width;
122
     m_width = width;
139
     glMatrixMode(GL_MODELVIEW);
139
     glMatrixMode(GL_MODELVIEW);
140
     glLoadIdentity();
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
     // Resize context
142
     // Resize context
151
     resizeGL(width, height);
143
     resizeGL(width, height);
152
 }
144
 }
222
                     if (mkeys & KMOD_RALT)
214
                     if (mkeys & KMOD_RALT)
223
                         mod |= SYS_MOD_KEY_RALT;
215
                         mod |= SYS_MOD_KEY_RALT;
224
 
216
 
225
-                    if (mkeys & KMOD_LMETA)
217
+                    if (mkeys & KMOD_LGUI)
226
                         mod |= SYS_MOD_KEY_LMETA;
218
                         mod |= SYS_MOD_KEY_LMETA;
227
 
219
 
228
-                    if (mkeys & KMOD_RMETA)
220
+                    if (mkeys & KMOD_RGUI)
229
                         mod |= SYS_MOD_KEY_RMETA;
221
                         mod |= SYS_MOD_KEY_RMETA;
230
 
222
 
231
                     key = event.key.keysym.sym;
223
                     key = event.key.keysym.sym;
369
                         }
361
                         }
370
                     }
362
                     }
371
                     break;
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
                     break;
371
                     break;
376
             }
372
             }
377
         }
373
         }
394
 }
390
 }
395
 
391
 
396
 void SDLSystem::toggleFullscreen() {
392
 void SDLSystem::toggleFullscreen() {
397
-    int width = m_width;
398
-    int height = m_height;
399
     if (mWindow) {
393
     if (mWindow) {
400
         mFullscreen = !mFullscreen;
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
 void SDLSystem::swapBuffersGL() {
405
 void SDLSystem::swapBuffersGL() {
432
-    SDL_GL_SwapBuffers();
406
+    SDL_GL_SwapWindow(mWindow);
433
 }
407
 }
434
 
408
 

+ 1
- 1
src/Texture.cpp View File

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

+ 41
- 32
test/GLString.cpp View File

3
  * \brief Open GL rendering font/string Unit Test
3
  * \brief Open GL rendering font/string Unit Test
4
  *
4
  *
5
  * \author Mongoose
5
  * \author Mongoose
6
+ * \author xythobuz
6
  */
7
  */
7
 
8
 
8
 #include <math.h>
9
 #include <math.h>
9
-#include <SDL/SDL.h>
10
+#include <SDL2/SDL.h>
10
 #ifdef __APPLE__
11
 #ifdef __APPLE__
11
 #include <OpenGL/glu.h>
12
 #include <OpenGL/glu.h>
12
 #else
13
 #else
17
 #include <GLString.h>
18
 #include <GLString.h>
18
 
19
 
19
 GLString *TEXT;
20
 GLString *TEXT;
20
-SDL_Surface *SDL_WINDOW = NULL;
21
+SDL_Window *sdlWindow;
22
+SDL_GLContext glContext;
21
 Texture gTexture;
23
 Texture gTexture;
22
 
24
 
23
 void swap_buffers();
25
 void swap_buffers();
39
 }
41
 }
40
 
42
 
41
 void event_display(int width, int height) {
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
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
47
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45
     glLoadIdentity();
48
     glLoadIdentity();
65
     glDisable(GL_CULL_FACE);
68
     glDisable(GL_CULL_FACE);
66
     glEnable(GL_BLEND);
69
     glEnable(GL_BLEND);
67
     glEnable(GL_TEXTURE_2D);
70
     glEnable(GL_TEXTURE_2D);
68
-    glColor3f(0.75, 0.5, 1.0);
71
+    glColor3f(0.5f, 0.7f, 1.0f);
69
 
72
 
70
     glEnterMode2d(width, height);
73
     glEnterMode2d(width, height);
71
     TEXT->Render();
74
     TEXT->Render();
76
 }
79
 }
77
 
80
 
78
 void swap_buffers() {
81
 void swap_buffers() {
79
-    SDL_GL_SwapBuffers();
82
+    SDL_GL_SwapWindow(sdlWindow);
80
 }
83
 }
81
 
84
 
82
 
85
 
83
 void shutdown_gl() {
86
 void shutdown_gl() {
87
+    SDL_GL_DeleteContext(glContext);
88
+    SDL_DestroyWindow(sdlWindow);
84
     SDL_Quit();
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
     // Setup GL
93
     // Setup GL
92
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
94
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
93
     glShadeModel(GL_SMOOTH);
95
     glShadeModel(GL_SMOOTH);
95
     glDepthFunc(GL_LESS);
97
     glDepthFunc(GL_LESS);
96
     glEnable(GL_BLEND);
98
     glEnable(GL_BLEND);
97
     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
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
     // Mongoose 2002.01.01, Texture setup
106
     // Mongoose 2002.01.01, Texture setup
102
     gTexture.reset();
107
     gTexture.reset();
106
     gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32);  // ASCII
111
     gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32);  // ASCII
107
 
112
 
108
     TEXT->Init(4);
113
     TEXT->Init(4);
109
-    i = TEXT->glPrintf((width/2)-50, height/2-32, "OpenRaider");
114
+    i = TEXT->glPrintf(50, 50, "OpenRaider GLString");
110
     if (i) {
115
     if (i) {
111
         printf(errorText, i);
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
     if (i) {
119
     if (i) {
115
         printf(errorText, i);
120
         printf(errorText, i);
116
     }
121
     }
117
     TEXT->Scale(1.2f);
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
     if (i) {
124
     if (i) {
120
         printf(errorText, i);
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
     if (i) {
128
     if (i) {
124
         printf(errorText, i);
129
         printf(errorText, i);
125
     }
130
     }
164
     }
169
     }
165
 #endif
170
 #endif
166
 
171
 
167
-    flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
172
+    flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
168
 
173
 
169
     if (fullscreen) {
174
     if (fullscreen) {
170
-        flags |= SDL_FULLSCREEN;
175
+        flags |= SDL_WINDOW_FULLSCREEN;
171
         SDL_ShowCursor(SDL_DISABLE);
176
         SDL_ShowCursor(SDL_DISABLE);
172
     }
177
     }
173
 
178
 
176
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
181
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
177
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
182
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
178
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
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
     // Init rendering
188
     // Init rendering
184
-    init_gl(width, height);
189
+    init_gl();
190
+    event_resize(width, height);
191
+    init_text();
185
 
192
 
186
     for (;;) {
193
     for (;;) {
187
         // Pause for 10-20 ms
194
         // Pause for 10-20 ms
220
                     if (mkeys & KMOD_RALT)
227
                     if (mkeys & KMOD_RALT)
221
                         mod |= KMOD_RALT;
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
                     key = event.key.keysym.sym;
236
                     key = event.key.keysym.sym;
230
 
237
 
234
                             exit(0);
241
                             exit(0);
235
 #ifdef __APPLE__
242
 #ifdef __APPLE__
236
                         case 113: // q
243
                         case 113: // q
237
-                            if ((mod & KMOD_RMETA) || (mod & KMOD_LMETA))
244
+                            if ((mod & KMOD_RGUI) || (mod & KMOD_LGUI))
238
                                 exit(0);
245
                                 exit(0);
239
                             break;
246
                             break;
240
 #endif
247
 #endif
241
-                        case 114: // r
242
-                            break;
243
                     }
248
                     }
244
                     break;
249
                     break;
245
                 case SDL_KEYUP:
250
                 case SDL_KEYUP:
246
                     break;
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
                     break;
261
                     break;
253
             }
262
             }
254
         }
263
         }

Loading…
Cancel
Save