Thomas Buck 10 lat temu
rodzic
commit
982261e7aa

+ 1
- 1
data/OpenRaider.ini Wyświetl plik

@@ -7,7 +7,7 @@ set audiodir   "$(basedir)/music"
7 7
 set datadir    "$(basedir)/data"
8 8
 set font       "$(datadir)/test.ttf"
9 9
 
10
- # Not needed for Mac OS X
10
+# Not needed for Mac OS X
11 11
 set gldriver   "/usr/lib/libGL.so.1"
12 12
 
13 13
 # Windowing

+ 40
- 0
include/Menu.h Wyświetl plik

@@ -0,0 +1,40 @@
1
+/*!
2
+ * \file include/Menu.h
3
+ * \brief Menu 'overlay'
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _MENU_H_
9
+#define _MENU_H_
10
+
11
+#include "Window.h"
12
+
13
+/*!
14
+ * \brief Menu 'overlay'
15
+ */
16
+class Menu {
17
+public:
18
+
19
+    /*!
20
+     * \brief Constructs an object of Menu
21
+     */
22
+    Menu();
23
+
24
+    /*!
25
+     * \brief Deconstructs an object of Menu
26
+     */
27
+    ~Menu();
28
+
29
+    void setVisible(bool visible);
30
+
31
+    bool isVisible();
32
+
33
+    void display();
34
+
35
+private:
36
+    bool mVisible;
37
+    WindowString mainText;
38
+};
39
+
40
+#endif

+ 5
- 3
include/OpenRaider.h Wyświetl plik

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "Menu.h"
13 14
 #include "Sound.h"
14 15
 #include "Window.h"
15 16
 
@@ -49,12 +50,13 @@ public:
49 50
 
50 51
     void run();
51 52
 
53
+    Window *mWindow;
54
+    Sound *mSound;
55
+    Menu *mMenu;
56
+
52 57
 private:
53 58
     bool mInit;
54 59
     bool mRunning;
55
-    Window *mWindow;
56
-
57
-    Sound *mSound;
58 60
 
59 61
     char *mBaseDir;
60 62
     char *mPakDir;

+ 16
- 2
include/Window.h Wyświetl plik

@@ -14,6 +14,8 @@ typedef struct {
14 14
     char *text;
15 15
     unsigned int x;
16 16
     unsigned int y;
17
+    int w;
18
+    int h;
17 19
     float scale;
18 20
     unsigned char color[3];
19 21
 } WindowString;
@@ -47,9 +49,9 @@ public:
47 49
 
48 50
     virtual int initializeGL();
49 51
 
50
-    virtual void resizeGL(unsigned int w, unsigned int h);
52
+    virtual void resizeGL();
51 53
 
52
-    virtual void glEnter2D(unsigned int width, unsigned int height);
54
+    virtual void glEnter2D();
53 55
 
54 56
     virtual void glExit2D();
55 57
 
@@ -58,6 +60,18 @@ public:
58 60
     virtual int initializeFont() = 0;
59 61
 
60 62
     virtual void writeString(WindowString *s) = 0;
63
+
64
+    unsigned int mWidth;
65
+    unsigned int mHeight;
66
+
67
+protected:
68
+    bool mInit;
69
+    char *mDriver;
70
+    bool mFullscreen;
71
+    bool mMousegrab;
72
+
73
+    bool mFontInit;
74
+    char *mFontName;
61 75
 };
62 76
 
63 77
 #endif

+ 1
- 8
include/WindowSDL.h Wyświetl plik

@@ -60,17 +60,10 @@ public:
60 60
     virtual void writeString(WindowString *s);
61 61
 
62 62
 private:
63
-    bool mInit;
64
-    char *mDriver;
65
-    unsigned int mWidth;
66
-    unsigned int mHeight;
67
-    bool mFullscreen;
68
-    bool mMousegrab;
63
+
69 64
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
70 65
     SDL_GLContext mGLContext; //!< The OpenGL Context
71 66
 
72
-    bool mFontInit;
73
-    char *mFontName;
74 67
     TTF_Font *mFont;
75 68
     GLuint mFontTexture;
76 69
 };

+ 2
- 0
include/main.h Wyświetl plik

@@ -7,6 +7,8 @@
7 7
 #ifndef _MAIN_H_
8 8
 #define _MAIN_H_
9 9
 
10
+#include "OpenRaider.h"
11
+
10 12
 extern OpenRaider *gOpenRaider; //!< Main Game Singleton
11 13
 
12 14
 /*!

+ 2
- 0
include/utils/strings.h Wyświetl plik

@@ -12,6 +12,8 @@
12 12
 #include <cstdarg>
13 13
 #include <vector>
14 14
 
15
+char *stringRemoveQuotes(const char *s);
16
+
15 17
 char *stringReplace(const char *s, const char *search, const char *replace);
16 18
 
17 19
 void printStringVector(std::vector<char *> *args);

+ 1
- 0
src/CMakeLists.txt Wyświetl plik

@@ -32,6 +32,7 @@ set (LIBS ${LIBS} ${ZLIB_LIBRARIES})
32 32
 
33 33
 # Set Source files
34 34
 set (SRCS ${SRCS} "main.cpp")
35
+set (SRCS ${SRCS} "Menu.cpp")
35 36
 set (SRCS ${SRCS} "OpenRaider.cpp")
36 37
 set (SRCS ${SRCS} "Sound.cpp")
37 38
 set (SRCS ${SRCS} "Window.cpp")

+ 53
- 0
src/Menu.cpp Wyświetl plik

@@ -0,0 +1,53 @@
1
+/*!
2
+ * \file src/Menu.cpp
3
+ * \brief Menu 'overlay'
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifdef __APPLE__
9
+#include <OpenGL/gl.h>
10
+#include <OpenGL/glu.h>
11
+#else
12
+#include <GL/gl.h>
13
+#include <GL/glu.h>
14
+#endif
15
+
16
+#include "config.h"
17
+#include "main.h"
18
+#include "Menu.h"
19
+#include "utils/strings.h"
20
+
21
+Menu::Menu() {
22
+    mVisible = false;
23
+    mainText.text = bufferString(VERSION);
24
+    mainText.color[0] = 0xFF;
25
+    mainText.color[1] = 0xFF;
26
+    mainText.color[2] = 0xFF;
27
+    mainText.scale = 1.2f;
28
+    mainText.w = 0;
29
+    mainText.h = 0;
30
+}
31
+
32
+Menu::~Menu() {
33
+
34
+}
35
+
36
+void Menu::setVisible(bool visible) {
37
+    mVisible = visible;
38
+}
39
+
40
+bool Menu::isVisible() {
41
+    return mVisible;
42
+}
43
+
44
+void Menu::display() {
45
+    Window *window = gOpenRaider->mWindow;
46
+
47
+    if (mVisible) {
48
+        mainText.x = (window->mWidth / 2) - (mainText.w / 2);
49
+        mainText.y = 10;
50
+        window->writeString(&mainText);
51
+    }
52
+}
53
+

+ 52
- 52
src/OpenRaider.cpp Wyświetl plik

@@ -26,11 +26,15 @@ OpenRaider::OpenRaider() {
26 26
     mAudioDir = NULL;
27 27
     mDataDir = NULL;
28 28
 
29
+    mMenu = new Menu();
29 30
     mSound = new Sound();
30 31
     mWindow = new WindowSDL();
31 32
 }
32 33
 
33 34
 OpenRaider::~OpenRaider() {
35
+    if (mMenu)
36
+        delete mMenu;
37
+
34 38
     if (mSound)
35 39
         delete mSound;
36 40
 
@@ -171,6 +175,18 @@ char *OpenRaider::expandDirectoryNames(const char *s) {
171 175
     return NULL;
172 176
 }
173 177
 
178
+#define CHANGE_DIR_WITH_EXPANSION(a) do {     \
179
+    char *quotes = stringRemoveQuotes(value); \
180
+    char *tmp = expandDirectoryNames(quotes); \
181
+    if (tmp == NULL) {                        \
182
+        a = fullPath(quotes, 0);              \
183
+    } else {                                  \
184
+        a = fullPath(tmp, 0);                 \
185
+        delete [] tmp;                        \
186
+    }                                         \
187
+    delete [] quotes;                         \
188
+} while(false)
189
+
174 190
 int OpenRaider::set(const char *var, const char *value) {
175 191
     if (strcmp(var, "size") == 0) {
176 192
         // value has format like "\"1024x768\""
@@ -204,49 +220,27 @@ int OpenRaider::set(const char *var, const char *value) {
204 220
         }
205 221
         mSound->setVolume(vol);
206 222
     } else if (strcmp(var, "mouse_x") == 0) {
207
-        // TODO mouse support
223
+        float sense = 1.0f;
224
+        if (sscanf(value, "%f", &sense) != 1) {
225
+            printf("set-mouse_x-Error: Invalid value (%s)\n", value);
226
+            return -6;
227
+        }
228
+        //! \todo mouse support
208 229
     } else if (strcmp(var, "mouse_y") == 0) {
209
-        // TODO mouse support
210
-    } else if (strcmp(var, "basedir") == 0) {
211
-        char *quotes = stringReplace(value, "\"", "");
212
-        char *tmp = expandDirectoryNames(quotes);
213
-        if (tmp == NULL) {
214
-            mBaseDir = fullPath(quotes, 0);
215
-        } else {
216
-            mBaseDir = fullPath(tmp, 0);
217
-            delete [] tmp;
230
+        float sense = 1.0f;
231
+        if (sscanf(value, "%f", &sense) != 1) {
232
+            printf("set-mouse_y-Error: Invalid value (%s)\n", value);
233
+            return -7;
218 234
         }
219
-        delete [] quotes;
235
+        //! \todo mouse support
236
+    } else if (strcmp(var, "basedir") == 0) {
237
+        CHANGE_DIR_WITH_EXPANSION(mBaseDir);
220 238
     } else if (strcmp(var, "pakdir") == 0) {
221
-        char *quotes = stringReplace(value, "\"", "");
222
-        char *tmp = expandDirectoryNames(quotes);
223
-        if (tmp == NULL) {
224
-            mPakDir = fullPath(quotes, 0);
225
-        } else {
226
-            mPakDir = fullPath(tmp, 0);
227
-            delete [] tmp;
228
-        }
229
-        delete [] quotes;
239
+        CHANGE_DIR_WITH_EXPANSION(mPakDir);
230 240
     } else if (strcmp(var, "audiodir") == 0) {
231
-        char *quotes = stringReplace(value, "\"", "");
232
-        char *tmp = expandDirectoryNames(quotes);
233
-        if (tmp == NULL) {
234
-            mAudioDir = fullPath(quotes, 0);
235
-        } else {
236
-            mAudioDir = fullPath(tmp, 0);
237
-            delete [] tmp;
238
-        }
239
-        delete [] quotes;
241
+        CHANGE_DIR_WITH_EXPANSION(mAudioDir);
240 242
     } else if (strcmp(var, "datadir") == 0) {
241
-        char *quotes = stringReplace(value, "\"", "");
242
-        char *tmp = expandDirectoryNames(quotes);
243
-        if (tmp == NULL) {
244
-            mDataDir = fullPath(quotes, 0);
245
-        } else {
246
-            mDataDir = fullPath(tmp, 0);
247
-            delete [] tmp;
248
-        }
249
-        delete [] quotes;
243
+        CHANGE_DIR_WITH_EXPANSION(mDataDir);
250 244
     } else if (strcmp(var, "font") == 0) {
251 245
         char *quotes = stringReplace(value, "\"", "");
252 246
         char *tmp = expandDirectoryNames(quotes);
@@ -266,19 +260,23 @@ int OpenRaider::set(const char *var, const char *value) {
266 260
 }
267 261
 
268 262
 int OpenRaider::bind(const char *action, const char *key) {
269
-    if (strcmp(action, "console") == 0) {
263
+    const char *tmp = action;
264
+    if (action[0] == '+')
265
+        tmp++;
270 266
 
271
-    } else if (strcmp(action, "forward") == 0) {
267
+    if (strcmp(tmp, "console") == 0) {
272 268
 
273
-    } else if (strcmp(action, "backward") == 0) {
269
+    } else if (strcmp(tmp, "forward") == 0) {
274 270
 
275
-    } else if (strcmp(action, "jump") == 0) {
271
+    } else if (strcmp(tmp, "backward") == 0) {
276 272
 
277
-    } else if (strcmp(action, "crouch") == 0) {
273
+    } else if (strcmp(tmp, "jump") == 0) {
278 274
 
279
-    } else if (strcmp(action, "left") == 0) {
275
+    } else if (strcmp(tmp, "crouch") == 0) {
280 276
 
281
-    } else if (strcmp(action, "right") == 0) {
277
+    } else if (strcmp(tmp, "left") == 0) {
278
+
279
+    } else if (strcmp(tmp, "right") == 0) {
282 280
 
283 281
     } else {
284 282
         printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
@@ -307,6 +305,8 @@ int OpenRaider::initialize() {
307 305
     if (mSound->initialize() != 0)
308 306
         return -4;
309 307
 
308
+    mMenu->setVisible(true);
309
+
310 310
     mInit = true;
311 311
 
312 312
     return 0;
@@ -322,15 +322,15 @@ void OpenRaider::run() {
322 322
 
323 323
         mWindow->eventHandling();
324 324
 
325
+        // Temp Debug
326
+        glClearColor(0.25f, 0.75f, 0.25f, 1.0f);
325 327
         glClear(GL_COLOR_BUFFER_BIT);
326 328
 
327
-        WindowString s;
328
-        s.text = bufferString("This text is not fixed-width...");
329
-        s.x = 100;
330
-        s.y = 100;
331
-        s.scale = 1.5f;
332
-        s.color[0] = s.color[1] = s.color[2] = 0xFF;
333
-        mWindow->writeString(&s);
329
+        mWindow->glEnter2D();
330
+
331
+        mMenu->display();
332
+
333
+        mWindow->glExit2D();
334 334
 
335 335
         mWindow->swapBuffersGL();
336 336
 

+ 10
- 12
src/Window.cpp Wyświetl plik

@@ -25,13 +25,14 @@ Window::~Window() {
25 25
 
26 26
 int Window::initializeGL() {
27 27
     // Print driver support information
28
-    printf("GL Vendor  : %s\n", glGetString(GL_VENDOR));
29
-    printf("GL Renderer: %s\n", glGetString(GL_RENDERER));
30
-    printf("GL Version : %s\n", glGetString(GL_VERSION));
28
+    //printf("GL Vendor  : %s\n", glGetString(GL_VENDOR));
29
+    //printf("GL Renderer: %s\n", glGetString(GL_RENDERER));
30
+    //printf("GL Version : %s\n", glGetString(GL_VERSION));
31 31
 
32 32
     // Testing for goodies
33 33
     const char *s = (const char *)glGetString(GL_EXTENSIONS);
34 34
     if ((s != NULL) && (s[0] != '\0')) {
35
+        //! \todo MultiTexture flag
35 36
         //if (strstr(s, "GL_ARB_multitexture"))
36 37
             //mFlags |= Render::fMultiTexture;
37 38
     }
@@ -95,28 +96,25 @@ int Window::initializeGL() {
95 96
     return 0;
96 97
 }
97 98
 
98
-void Window::resizeGL(unsigned int w, unsigned int h) {
99
+void Window::resizeGL() {
99 100
     float fovY = 45.0f;
100 101
     float clipNear = 4.0f;
101 102
     float clipFar = 4000.0f;
102 103
 
103
-    assert(w > 0);
104
-    assert(h > 0);
105
-
106
-    glViewport(0, 0, w, h);
104
+    glViewport(0, 0, mWidth, mHeight);
107 105
     glMatrixMode(GL_PROJECTION);
108 106
     glLoadIdentity();
109 107
 
110 108
     // Adjust clipping
111 109
     GLfloat fH = tanf(fovY * OR_PI / 360.0f) * clipNear;
112
-    GLfloat fW = fH * ((GLfloat)w) / ((GLfloat)h);
110
+    GLfloat fW = fH * ((GLfloat)mWidth) / ((GLfloat)mHeight);
113 111
     glFrustum(-fW, fW, -fH, fH, clipNear, clipFar);
114 112
 
115 113
     glMatrixMode(GL_MODELVIEW);
116 114
     glLoadIdentity();
117 115
 }
118 116
 
119
-void Window::glEnter2D(unsigned int width, unsigned int height) {
117
+void Window::glEnter2D() {
120 118
     glPushAttrib(GL_ENABLE_BIT);
121 119
     glDisable(GL_DEPTH_TEST);
122 120
     glDisable(GL_CULL_FACE);
@@ -126,13 +124,13 @@ void Window::glEnter2D(unsigned int width, unsigned int height) {
126 124
     glEnable(GL_BLEND);
127 125
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
128 126
 
129
-    glViewport(0, 0, width, height);
127
+    glViewport(0, 0, mWidth, mHeight);
130 128
 
131 129
     glMatrixMode(GL_PROJECTION);
132 130
     glPushMatrix();
133 131
     glLoadIdentity();
134 132
 
135
-    glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.0, 1.0);
133
+    glOrtho(0.0, (GLdouble)mWidth, (GLdouble)mHeight, 0.0, 0.0, 1.0);
136 134
 
137 135
     glMatrixMode(GL_MODELVIEW);
138 136
     glPushMatrix();

+ 16
- 8
src/WindowSDL.cpp Wyświetl plik

@@ -70,7 +70,7 @@ void WindowSDL::setSize(unsigned int width, unsigned int height) {
70 70
 
71 71
     if (mInit == true) {
72 72
         SDL_SetWindowSize(mWindow, mWidth, mHeight);
73
-        resizeGL(mWidth, mHeight);
73
+        resizeGL();
74 74
     }
75 75
 }
76 76
 
@@ -250,6 +250,13 @@ void WindowSDL::writeString(WindowString *s) {
250 250
         return;
251 251
     }
252 252
 
253
+    if (TTF_SizeUTF8(mFont, s->text, &s->w, &s->h) != 0) {
254
+        printf("TTF_SizeUTF8 Error: %s\n", TTF_GetError());
255
+        // Don't need to abort
256
+    }
257
+    s->w = (int)((float)s->w * s->scale);
258
+    s->h = (int)((float)s->h * s->scale);
259
+
253 260
     GLenum textureFormat;
254 261
     if (surface->format->BytesPerPixel == 4) {
255 262
         if (surface->format->Rmask == 0x000000FF)
@@ -265,24 +272,25 @@ void WindowSDL::writeString(WindowString *s) {
265 272
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
266 273
     glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
267 274
 
268
-    glEnter2D(mWidth, mHeight);
275
+    GLuint xMin = s->x;
276
+    GLuint yMin = s->y;
277
+    GLuint xMax = xMin + (int)((float)surface->w * s->scale);
278
+    GLuint yMax = yMin + (int)((float)surface->h * s->scale);
269 279
 
270 280
     glBegin(GL_QUADS);
271 281
         glTexCoord2f(0.0f, 0.0f);
272
-        glVertex2i(s->x, s->y);
282
+        glVertex2i(xMin, yMin);
273 283
 
274 284
         glTexCoord2f(0.0f, 1.0f);
275
-        glVertex2i(s->x, (int)((float)surface->h * s->scale));
285
+        glVertex2i(xMin, yMax);
276 286
 
277 287
         glTexCoord2f(1.0f, 1.0f);
278
-        glVertex2i((int)((float)surface->w * s->scale), (int)((float)surface->h * s->scale));
288
+        glVertex2i(xMax, yMax);
279 289
 
280 290
         glTexCoord2f(1.0f, 0.0f);
281
-        glVertex2i((int)((float)surface->w * s->scale), s->y);
291
+        glVertex2i(xMax, yMin);
282 292
     glEnd();
283 293
 
284
-    glExit2D();
285
-
286 294
     SDL_FreeSurface(surface);
287 295
 }
288 296
 

+ 0
- 1
src/main.cpp Wyświetl plik

@@ -10,7 +10,6 @@
10 10
 #include <cstring>
11 11
 
12 12
 #include "config.h"
13
-#include "OpenRaider.h"
14 13
 #include "main.h"
15 14
 
16 15
 OpenRaider *gOpenRaider = NULL;

+ 13
- 0
src/utils/strings.cpp Wyświetl plik

@@ -17,6 +17,19 @@
17 17
 
18 18
 #include "utils/strings.h"
19 19
 
20
+char *stringRemoveQuotes(const char *s) {
21
+    size_t length = strlen(s);
22
+    if ((s[0] == '"') && (s[length - 1] == '"')) {
23
+        char *buf = new char[length - 1];
24
+        for (size_t i = 1; i < (length - 1); i++)
25
+            buf[i - 1] = s[i];
26
+        buf[length - 2] = '\0';
27
+        return buf;
28
+    } else {
29
+        return bufferString("%s", s);
30
+    }
31
+}
32
+
20 33
 char *stringReplace(const char *s, const char *search, const char *replace) {
21 34
     char *tmp = strstr(s, search);
22 35
     if (tmp == NULL)

Ładowanie…
Anuluj
Zapisz