浏览代码

Split Font subsystem from Window

Thomas Buck 10 年前
父节点
当前提交
93af7ceb65
共有 20 个文件被更改,包括 341 次插入232 次删除
  1. 3
    0
      ChangeLog.md
  2. 0
    2
      README.md
  3. 3
    6
      TODO.md
  4. 49
    0
      include/Font.h
  5. 46
    0
      include/FontSDL.h
  6. 2
    2
      include/Menu.h
  7. 1
    1
      include/OpenRaider.h
  8. 9
    28
      include/Window.h
  9. 1
    17
      include/WindowSDL.h
  10. 2
    0
      src/CMakeLists.txt
  11. 2
    1
      src/Command.cpp
  12. 10
    8
      src/Console.cpp
  13. 24
    0
      src/Font.cpp
  14. 138
    0
      src/FontSDL.cpp
  15. 13
    12
      src/Menu.cpp
  16. 6
    3
      src/OpenRaider.cpp
  17. 7
    6
      src/Render.cpp
  18. 19
    0
      src/Window.cpp
  19. 0
    146
      src/WindowSDL.cpp
  20. 6
    0
      src/main.cpp

+ 3
- 0
ChangeLog.md 查看文件

@@ -3,6 +3,9 @@
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5 5
     [ 20140605 ]
6
+    * Split Font/Text-Rendering Subsystem from Windowing System
7
+    * Slight improvements on Window Interface/Implementation differences
8
+    * Window width/height finally private too
6 9
     * Created methods to convert strings of names to ActionEvents
7 10
       and KeyboardButtons.
8 11
     * Simplified bind commands

+ 0
- 2
README.md 查看文件

@@ -10,8 +10,6 @@ It seems as if OpenRaider will currently only work on Little-Endian platforms. T
10 10
 
11 11
 [![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)
12 12
 
13
-<script data-gittip-username="xythobuz" data-gittip-widget="button" src="//gttp.co/v1.js"></script>
14
-
15 13
 ## Configuration
16 14
 
17 15
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.

+ 3
- 6
TODO.md 查看文件

@@ -11,11 +11,9 @@ There are these DebugModel, DebugMap flags...?
11 11
     * Use shared_ptr, should make memory handling/segfaults much less of a problem
12 12
         * Don't even new ... the data structures but use std::make_shared or allocate_shared?
13 13
         * Pass object references to all other objects that need it, completely remove gOpenRaider
14
-    * Use streams for (file) I/O
15
-        * Does not need strtok() anymore
16 14
     * Use std::strings
15
+    * Rewrite Console and use operator << to write to the console
17 16
 * SDL_TTF 2.0.12+ can do line breaks, use it: http://stackoverflow.com/questions/17847818/how-to-do-line-breaks-and-line-wrapping-with-sdl-ttf/18418688#18418688
18
-* Rewrite Console using std::strings and use operator << to write to the console
19 17
 
20 18
 ## Changes
21 19
 
@@ -30,8 +28,7 @@ There are these DebugModel, DebugMap flags...?
30 28
 
31 29
 ## Future Features
32 30
 
33
-* Use only assets from old TR games?
34
-* [PCX image reading](http://bespin.org/~qz/pc-gpe/pcx.txt) for eg. TR2 built-in menu background
35
-    * Cut TGA image reader, currently only used for menu background?!
31
+* Use only assets from old TR games, not even depending on a font file?
32
+* Cut TGA image reader, currently only used for menu background?!
36 33
 * When cutscene rendering is working, use TR4/5 style menu?
37 34
 

+ 49
- 0
include/Font.h 查看文件

@@ -0,0 +1,49 @@
1
+/*!
2
+ * \file include/Font.h
3
+ * \brief Font interface
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _FONT_H_
9
+#define _FONT_H_
10
+
11
+typedef struct {
12
+    char *text;
13
+    unsigned int x;
14
+    unsigned int y;
15
+    int w;
16
+    int h;
17
+    float scale;
18
+    float color[4];
19
+} FontString;
20
+
21
+/*!
22
+ * \brief Font interface
23
+ */
24
+class Font {
25
+public:
26
+
27
+    /*!
28
+     * \brief Deconstructs an object of Font
29
+     */
30
+    virtual ~Font();
31
+
32
+    virtual void setFont(const char *font);
33
+
34
+    virtual int initialize() = 0;
35
+
36
+    virtual void writeString(FontString &s) = 0;
37
+
38
+    virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
39
+        __attribute__((format(printf, 6, 0))) = 0;
40
+
41
+protected:
42
+    bool mFontInit;
43
+    char *mFontName;
44
+};
45
+
46
+Font &getFont();
47
+
48
+#endif
49
+

+ 46
- 0
include/FontSDL.h 查看文件

@@ -0,0 +1,46 @@
1
+/*!
2
+ * \file include/FontSDL.h
3
+ * \brief SDL windowing implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _FONT_SDL_H_
9
+#define _FONT_SDL_H_
10
+
11
+#include "SDL_ttf.h"
12
+
13
+#include "Font.h"
14
+
15
+/*!
16
+ * \brief SDL windowing implementation
17
+ */
18
+class FontSDL : public Font {
19
+public:
20
+
21
+    /*!
22
+     * \brief Constructs an object of FontSDL
23
+     */
24
+    FontSDL();
25
+
26
+    /*!
27
+     * \brief Deconstructs an object of FontSDL
28
+     */
29
+    virtual ~FontSDL();
30
+
31
+    virtual int initialize();
32
+
33
+    virtual void writeString(FontString &s);
34
+
35
+    virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
36
+        __attribute__((format(printf, 6, 0)));
37
+
38
+private:
39
+    TTF_Font *mFont;
40
+    unsigned int mFontTexture;
41
+
42
+    FontString tempText;
43
+};
44
+
45
+#endif
46
+

+ 2
- 2
include/Menu.h 查看文件

@@ -8,7 +8,7 @@
8 8
 #ifndef _MENU_H_
9 9
 #define _MENU_H_
10 10
 
11
-#include "Window.h"
11
+#include "Font.h"
12 12
 
13 13
 /*!
14 14
  * \brief Menu 'overlay'
@@ -50,7 +50,7 @@ private:
50 50
     unsigned int mCursor;
51 51
     unsigned int mMin;
52 52
 
53
-    WindowString mainText;
53
+    FontString mainText;
54 54
 
55 55
     bool mMapListFilled;
56 56
     bool mFirstPass;

+ 1
- 1
include/OpenRaider.h 查看文件

@@ -13,10 +13,10 @@
13 13
 #include <vector>
14 14
 
15 15
 #include "Console.h"
16
+#include "Font.h"
16 17
 #include "Game.h"
17 18
 #include "Menu.h"
18 19
 #include "Sound.h"
19
-#include "Window.h"
20 20
 
21 21
 /*!
22 22
  * \brief Main Game Singleton

+ 9
- 28
include/Window.h 查看文件

@@ -10,16 +10,6 @@
10 10
 
11 11
 #include <ctime>
12 12
 
13
-typedef struct {
14
-    char *text;
15
-    unsigned int x;
16
-    unsigned int y;
17
-    int w;
18
-    int h;
19
-    float scale;
20
-    float color[4];
21
-} WindowString;
22
-
23 13
 /*!
24 14
  * \brief Windowing interface
25 15
  */
@@ -31,8 +21,6 @@ public:
31 21
      */
32 22
     virtual ~Window();
33 23
 
34
-    virtual void setDriver(const char *driver) = 0;
35
-
36 24
     virtual void setSize(unsigned int width, unsigned int height) = 0;
37 25
 
38 26
     virtual void setFullscreen(bool fullscreen) = 0;
@@ -49,6 +37,12 @@ public:
49 37
 
50 38
     virtual void swapBuffersGL() = 0;
51 39
 
40
+    virtual void setDriver(const char *driver);
41
+
42
+    virtual unsigned int getWidth();
43
+
44
+    virtual unsigned int getHeight();
45
+
52 46
     virtual int initializeGL();
53 47
 
54 48
     virtual void resizeGL();
@@ -57,29 +51,16 @@ public:
57 51
 
58 52
     virtual void glExit2D();
59 53
 
60
-    virtual void setFont(const char *font) = 0;
61
-
62
-    virtual int initializeFont() = 0;
63
-
64
-    virtual void writeString(WindowString &s) = 0;
65
-
66
-    virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
67
-        __attribute__((format(printf, 6, 0))) = 0;
68
-
69
-    //! \fixme should be private
70
-    unsigned int mWidth;
71
-    unsigned int mHeight;
72
-
73 54
 protected:
74 55
     bool mInit;
75 56
     char *mDriver;
76 57
     bool mFullscreen;
77 58
     bool mMousegrab;
78
-
79
-    bool mFontInit;
80
-    char *mFontName;
59
+    unsigned int mWidth;
60
+    unsigned int mHeight;
81 61
 };
82 62
 
83 63
 Window &getWindow();
84 64
 
85 65
 #endif
66
+

+ 1
- 17
include/WindowSDL.h 查看文件

@@ -29,8 +29,6 @@ public:
29 29
      */
30 30
     virtual ~WindowSDL();
31 31
 
32
-    virtual void setDriver(const char *driver);
33
-
34 32
     virtual void setSize(unsigned int width, unsigned int height);
35 33
 
36 34
     virtual void setFullscreen(bool fullscreen);
@@ -47,26 +45,12 @@ public:
47 45
 
48 46
     virtual void swapBuffersGL();
49 47
 
50
-    virtual void setFont(const char *font);
51
-
52
-    virtual int initializeFont();
53
-
54
-    virtual void writeString(WindowString &s);
55
-
56
-    virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
57
-        __attribute__((format(printf, 6, 0)));
58
-
59 48
 private:
60
-
61 49
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
62 50
     SDL_GLContext mGLContext; //!< The OpenGL Context
63 51
 
64
-    TTF_Font *mFont;
65
-    unsigned int mFontTexture;
66
-
67 52
     bool mTextInput;
68
-
69
-    WindowString tempText;
70 53
 };
71 54
 
72 55
 #endif
56
+

+ 2
- 0
src/CMakeLists.txt 查看文件

@@ -43,6 +43,7 @@ set (SRCS ${SRCS} "Camera.cpp")
43 43
 set (SRCS ${SRCS} "Command.cpp")
44 44
 set (SRCS ${SRCS} "Console.cpp")
45 45
 set (SRCS ${SRCS} "Entity.cpp")
46
+set (SRCS ${SRCS} "Font.cpp")
46 47
 set (SRCS ${SRCS} "Game.cpp")
47 48
 set (SRCS ${SRCS} "main.cpp")
48 49
 set (SRCS ${SRCS} "Menu.cpp")
@@ -74,6 +75,7 @@ endif (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
74 75
 # Select available Windowing library
75 76
 if (SDL2_FOUND AND SDL2TTF_FOUND)
76 77
     set (SRCS ${SRCS} "WindowSDL.cpp")
78
+    set (SRCS ${SRCS} "FontSDL.cpp")
77 79
     set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL")
78 80
 else (SDL2_FOUND AND SDL2TTF_FOUND)
79 81
     # Currently only SDL2 support

+ 2
- 1
src/Command.cpp 查看文件

@@ -12,6 +12,7 @@
12 12
 #include "global.h"
13 13
 #include "Console.h"
14 14
 #include "Entity.h"
15
+#include "Font.h"
15 16
 #include "Game.h"
16 17
 #include "math/math.h"
17 18
 #include "Menu.h"
@@ -682,7 +683,7 @@ int OpenRaider::set(std::istream &command) {
682 683
         const char *value = temp.c_str();
683 684
         char *quotes = stringReplace(value, "\"", "");
684 685
         char *tmp = expandDirectoryNames(quotes);
685
-        getWindow().setFont(tmp);
686
+        getFont().setFont(tmp);
686 687
         delete [] tmp;
687 688
         delete [] quotes;
688 689
     } else {

+ 10
- 8
src/Console.cpp 查看文件

@@ -8,10 +8,12 @@
8 8
 #include <iostream>
9 9
 
10 10
 #include "global.h"
11
-#include "Console.h"
11
+#include "Font.h"
12 12
 #include "OpenRaider.h"
13 13
 #include "utils/strings.h"
14 14
 #include "utils/time.h"
15
+#include "Window.h"
16
+#include "Console.h"
15 17
 
16 18
 #define INPUT_BUFFER_SIZE 255
17 19
 
@@ -75,8 +77,8 @@ void Console::print(const char *s, ...) {
75 77
 }
76 78
 
77 79
 #define LINE_GEOMETRY(window) unsigned int firstLine = 35; \
78
-        unsigned int lastLine = (window.mHeight / 2) - 55; \
79
-        unsigned int inputLine = (window.mHeight / 2) - 30; \
80
+        unsigned int lastLine = (window.getHeight() / 2) - 55; \
81
+        unsigned int inputLine = (window.getHeight() / 2) - 30; \
80 82
         unsigned int lineSteps = 20; \
81 83
         unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
82 84
         while (((lineCount * lineSteps) + firstLine) < inputLine) { \
@@ -93,7 +95,7 @@ void Console::display() {
93 95
         // Draw half-transparent *overlay*
94 96
         glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
95 97
         glDisable(GL_TEXTURE_2D);
96
-        glRecti(0, 0, getWindow().mWidth, getWindow().mHeight / 2);
98
+        glRecti(0, 0, getWindow().getWidth(), getWindow().getHeight() / 2);
97 99
         glEnable(GL_TEXTURE_2D);
98 100
 
99 101
         int scrollIndicator;
@@ -103,7 +105,7 @@ void Console::display() {
103 105
             scrollIndicator = 100;
104 106
         }
105 107
 
106
-        getWindow().drawText(10, 10, 0.70f, OR_BLUE,
108
+        getFont().drawText(10, 10, 0.70f, OR_BLUE,
107 109
                 "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
108 110
 
109 111
         // Draw output log
@@ -117,15 +119,15 @@ void Console::display() {
117 119
             historyOffset = mHistory.size() - lineCount;
118 120
         }
119 121
         for (int i = 0; i < end; i++) {
120
-            getWindow().drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
122
+            getFont().drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
121 123
                     0.75f, OR_BLUE, "%s", mHistory[i + historyOffset - mLineOffset]);
122 124
         }
123 125
 
124 126
         // Draw current input
125 127
         if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
126
-            getWindow().drawText(10, inputLine, 0.75f, OR_BLUE, "> %s", mInputBuffer);
128
+            getFont().drawText(10, inputLine, 0.75f, OR_BLUE, "> %s", mInputBuffer);
127 129
         } else {
128
-            getWindow().drawText(10, inputLine, 0.75f, OR_BLUE, ">");
130
+            getFont().drawText(10, inputLine, 0.75f, OR_BLUE, ">");
129 131
         }
130 132
 
131 133
         //! \todo display the current mPartialInput. The UTF-8 segfaults SDL-TTF, somehow?

+ 24
- 0
src/Font.cpp 查看文件

@@ -0,0 +1,24 @@
1
+/*!
2
+ * \file src/Font.cpp
3
+ * \brief Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/strings.h"
10
+#include "Font.h"
11
+
12
+Font::~Font() {
13
+    if (mFontName)
14
+        delete [] mFontName;
15
+}
16
+
17
+void Font::setFont(const char *font) {
18
+    assert(font != NULL);
19
+    assert(font[0] != '\0');
20
+    assert(mFontInit == false);
21
+
22
+    mFontName = fullPath(font, 0);
23
+}
24
+

+ 138
- 0
src/FontSDL.cpp 查看文件

@@ -0,0 +1,138 @@
1
+/*!
2
+ * \file src/FontSDL.cpp
3
+ * \brief SDL Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "FontSDL.h"
10
+
11
+FontSDL::FontSDL() {
12
+    mFont = NULL;
13
+    mFontInit = false;
14
+    mFontName = NULL;
15
+    mFontTexture = 0;
16
+
17
+    tempText.text = new char[256];
18
+    tempText.color[0] = 0xFF;
19
+    tempText.color[1] = 0xFF;
20
+    tempText.color[2] = 0xFF;
21
+    tempText.color[3] = 0xFF;
22
+    tempText.scale = 1.2f;
23
+    tempText.w = 0;
24
+    tempText.h = 0;
25
+}
26
+
27
+FontSDL::~FontSDL() {
28
+    if (mFont)
29
+        TTF_CloseFont(mFont);
30
+
31
+    if (mFontInit)
32
+        TTF_Quit();
33
+
34
+    if (tempText.text)
35
+        delete [] tempText.text;
36
+}
37
+
38
+int FontSDL::initialize() {
39
+    assert(mFontInit == false);
40
+    assert(mFontName != NULL);
41
+    assert(mFontName[0] != '\0');
42
+
43
+    if (TTF_Init() != 0) {
44
+        printf("Could not initialize SDL-TTF!\n");
45
+        return -1;
46
+    }
47
+
48
+    mFont = TTF_OpenFont(mFontName, 24);
49
+    if (mFont == NULL) {
50
+        printf("TTF_OpenFont Error: %s\n", TTF_GetError());
51
+        return -2;
52
+    }
53
+
54
+    glGenTextures(1, &mFontTexture);
55
+
56
+    mFontInit = true;
57
+    return 0;
58
+}
59
+
60
+void FontSDL::writeString(FontString &s) {
61
+    assert(s.text != NULL);
62
+
63
+    SDL_Color color;
64
+    color.r = (unsigned char)(s.color[0] * 255.0f);
65
+    color.g = (unsigned char)(s.color[1] * 255.0f);
66
+    color.b = (unsigned char)(s.color[2] * 255.0f);
67
+    color.a = (unsigned char)(s.color[3] * 255.0f);
68
+
69
+    SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.text, color);
70
+    if (surface == NULL) {
71
+        printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
72
+        return;
73
+    }
74
+
75
+    s.w = (int)((float)surface->w * s.scale);
76
+    s.h = (int)((float)surface->h * s.scale);
77
+
78
+    GLenum textureFormat;
79
+    if (surface->format->BytesPerPixel == 4) {
80
+        if (surface->format->Rmask == 0x000000FF)
81
+            textureFormat = GL_RGBA;
82
+        else
83
+            textureFormat = GL_BGRA_EXT;
84
+    } else {
85
+        if (surface->format->Rmask == 0x000000FF)
86
+            textureFormat = GL_RGB;
87
+        else
88
+            textureFormat = GL_BGR_EXT;
89
+    }
90
+
91
+    glBindTexture(GL_TEXTURE_2D, mFontTexture);
92
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94
+    glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
95
+
96
+    GLuint xMin = s.x;
97
+    GLuint yMin = s.y;
98
+    GLuint xMax = xMin + s.w;
99
+    GLuint yMax = yMin + s.h;
100
+
101
+    glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
102
+
103
+    glBegin(GL_QUADS);
104
+    glTexCoord2f(0.0f, 0.0f);
105
+    glVertex2i(xMin, yMin);
106
+
107
+    glTexCoord2f(0.0f, 1.0f);
108
+    glVertex2i(xMin, yMax);
109
+
110
+    glTexCoord2f(1.0f, 1.0f);
111
+    glVertex2i(xMax, yMax);
112
+
113
+    glTexCoord2f(1.0f, 0.0f);
114
+    glVertex2i(xMax, yMin);
115
+    glEnd();
116
+
117
+    SDL_FreeSurface(surface);
118
+}
119
+
120
+void FontSDL::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
121
+    va_list args;
122
+    va_start(args, s);
123
+    vsnprintf(tempText.text, 256, s, args);
124
+    tempText.text[255] = '\0';
125
+    va_end(args);
126
+
127
+    tempText.scale = scale;
128
+    tempText.x = x;
129
+    tempText.y = y;
130
+    if (color) {
131
+        tempText.color[0] = color[0];
132
+        tempText.color[1] = color[1];
133
+        tempText.color[2] = color[2];
134
+        tempText.color[3] = color[3];
135
+    }
136
+    writeString(tempText);
137
+}
138
+

+ 13
- 12
src/Menu.cpp 查看文件

@@ -9,9 +9,10 @@
9 9
 
10 10
 #include "global.h"
11 11
 #include "Console.h"
12
-#include "Menu.h"
13 12
 #include "OpenRaider.h"
14 13
 #include "utils/strings.h"
14
+#include "Window.h"
15
+#include "Menu.h"
15 16
 
16 17
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
17 18
 #include <dirent.h>
@@ -162,7 +163,7 @@ void Menu::fillMapList() {
162 163
 
163 164
 void Menu::displayMapList() {
164 165
     // Estimate displayable number of items
165
-    int items = (getWindow().mHeight - 110) / 25;
166
+    int items = (getWindow().getHeight() - 110) / 25;
166 167
 
167 168
     // Select which part of the list to show
168 169
     int min, max;
@@ -190,9 +191,9 @@ void Menu::displayMapList() {
190 191
     for (int i = 0; i < (max - min); i++) {
191 192
         char *map = mMapList[i + min];
192 193
         if ((i + min) == (int)mCursor)
193
-            getWindow().drawText(25, 100 + (25 * i), 0.75f, RED, "%s", map);
194
+            getFont().drawText(25, 100 + (25 * i), 0.75f, RED, "%s", map);
194 195
         else
195
-            getWindow().drawText(25, 100 + (25 * i), 0.75f, OR_BLUE, "%s", map);
196
+            getFont().drawText(25, 100 + (25 * i), 0.75f, OR_BLUE, "%s", map);
196 197
     }
197 198
 }
198 199
 
@@ -201,26 +202,26 @@ void Menu::display() {
201 202
         // Draw half-transparent *overlay*
202 203
         glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
203 204
         glDisable(GL_TEXTURE_2D);
204
-        glRecti(0, 0, getWindow().mWidth, getWindow().mHeight);
205
+        glRecti(0, 0, getWindow().getWidth(), getWindow().getHeight());
205 206
         glEnable(GL_TEXTURE_2D);
206 207
 
207
-        // Draw heading text, using WindowString so we can get the
208
+        // Draw heading text, using FontString so we can get the
208 209
         // width of the drawn text to center it
209
-        mainText.x = (getWindow().mWidth / 2) - (mainText.w / 2);
210
-        getWindow().writeString(mainText);
210
+        mainText.x = (getWindow().getWidth() / 2) - (mainText.w / 2);
211
+        getFont().writeString(mainText);
211 212
 
212 213
         if (!mMapListFilled) {
213
-            getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, OR_BLUE, "Generating map list...");
214
+            getFont().drawText(25, (getWindow().getHeight() / 2) - 20, 0.75f, OR_BLUE, "Generating map list...");
214 215
         } else {
215 216
             if (mMapList.size() == 0) {
216
-                getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, RED, "No maps found! See README.md");
217
+                getFont().drawText(25, (getWindow().getHeight() / 2) - 20, 0.75f, RED, "No maps found! See README.md");
217 218
             } else {
218 219
                 // draw *play button* above list
219 220
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
220 221
                 glDisable(GL_TEXTURE_2D);
221 222
                 glRecti(25, 25, 100, 75);
222 223
                 glEnable(GL_TEXTURE_2D);
223
-                getWindow().drawText(40, 35, 0.75f, BLACK, "Play");
224
+                getFont().drawText(40, 35, 0.75f, BLACK, "Play");
224 225
 
225 226
                 displayMapList();
226 227
             }
@@ -280,7 +281,7 @@ void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
280 281
 }
281 282
 
282 283
 void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
283
-    int items = (getWindow().mHeight - 110) / 25;
284
+    int items = (getWindow().getHeight() - 110) / 25;
284 285
 
285 286
     if (released || (button != leftmouseKey))
286 287
         return;

+ 6
- 3
src/OpenRaider.cpp 查看文件

@@ -17,6 +17,7 @@
17 17
 #include "TombRaider.h"
18 18
 #include "utils/strings.h"
19 19
 #include "utils/time.h"
20
+#include "Window.h"
20 21
 #include "OpenRaider.h"
21 22
 
22 23
 OpenRaider::OpenRaider() {
@@ -60,12 +61,14 @@ int OpenRaider::initialize() {
60 61
         return -2;
61 62
     }
62 63
 
63
-    error = getWindow().initializeFont();
64
+    // Initialize Font
65
+    error = getFont().initialize();
64 66
     if (error != 0) {
65 67
         printf("Could not initialize Font (%d)!\n", error);
66 68
         return -3;
67 69
     }
68 70
 
71
+    // Initialize Sound
69 72
     error = getSound().initialize();
70 73
     if (error != 0) {
71 74
         printf("Could not initialize Sound (%d)!\n", error);
@@ -117,13 +120,13 @@ void OpenRaider::frame() {
117 120
 
118 121
     // Draw FPS counter
119 122
     if (mFPS)
120
-        getWindow().drawText(10, getWindow().mHeight - 20, 0.5f, OR_BLUE, "%dFPS", fps);
123
+        getFont().drawText(10, getWindow().getHeight() - 20, 0.5f, OR_BLUE, "%dFPS", fps);
121 124
 
122 125
 #ifdef DEBUG
123 126
     // Draw debug infos
124 127
     if (getGame().isLoaded() && (!getMenu().isVisible())) {
125 128
         for (int i = 0; i < 3; i++) {
126
-            getWindow().drawText(10, getWindow().mHeight - ((4 - i) * 20), 0.5f, OR_BLUE, "%.2f (%.2f)",
129
+            getFont().drawText(10, getWindow().getHeight() - ((4 - i) * 20), 0.5f, OR_BLUE, "%.2f (%.2f)",
127 130
                 getGame().getLara().getPos(i) / 256.0f, getGame().getLara().getAngle(i));
128 131
         }
129 132
     }

+ 7
- 6
src/Render.cpp 查看文件

@@ -18,6 +18,7 @@
18 18
 #include "Render.h"
19 19
 #include "utils/strings.h"
20 20
 #include "utils/tga.h"
21
+#include "Window.h"
21 22
 
22 23
 Render::Render() {
23 24
     mSkyMesh = -1;
@@ -37,7 +38,7 @@ Render::~Render() {
37 38
 
38 39
 void Render::screenShot(char *filenameBase)
39 40
 {
40
-    int sz = getWindow().mWidth * getWindow().mHeight;
41
+    int sz = getWindow().getWidth() * getWindow().getHeight();
41 42
     unsigned char *image = new unsigned char[sz * 3];
42 43
     char *filename = NULL;
43 44
     static int count = 0;
@@ -58,9 +59,9 @@ void Render::screenShot(char *filenameBase)
58 59
     }
59 60
 
60 61
     // Capture frame buffer
61
-    glReadPixels(0, 0, getWindow().mWidth, getWindow().mHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
62
+    glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
62 63
 
63
-    tgaSaveFilename(image, getWindow().mWidth, getWindow().mHeight, 0, "%s", filename);
64
+    tgaSaveFilename(image, getWindow().getWidth(), getWindow().getHeight(), 0, "%s", filename);
64 65
     printf("Took screenshot '%s'.\n", filename);
65 66
 
66 67
     delete [] filename;
@@ -488,10 +489,10 @@ void Render::drawLoadScreen()
488 489
     float x = 0.0f, y = 0.0f, z = -160.0f;
489 490
     float w, h;
490 491
 
491
-    if (getWindow().mWidth < getWindow().mHeight)
492
-        w = h = (float)getWindow().mWidth;
492
+    if (getWindow().getWidth() < getWindow().getHeight())
493
+        w = h = (float)getWindow().getWidth();
493 494
     else
494
-        w = h = (float)getWindow().mHeight;
495
+        w = h = (float)getWindow().getHeight();
495 496
 
496 497
 
497 498
     if (mTexture.getTextureCount() <= 0)

+ 19
- 0
src/Window.cpp 查看文件

@@ -11,9 +11,28 @@
11 11
 
12 12
 #include "global.h"
13 13
 #include "math/math.h"
14
+#include "utils/strings.h"
14 15
 #include "Window.h"
15 16
 
16 17
 Window::~Window() {
18
+    if (mDriver)
19
+        delete [] mDriver;
20
+}
21
+
22
+unsigned int Window::getWidth() {
23
+    return mWidth;
24
+}
25
+
26
+unsigned int Window::getHeight() {
27
+    return mHeight;
28
+}
29
+
30
+void Window::setDriver(const char *driver) {
31
+    assert(driver != NULL);
32
+    assert(driver[0] != '\0');
33
+    assert(mInit == false);
34
+
35
+    mDriver = bufferString("%s", driver);
17 36
 }
18 37
 
19 38
 int Window::initializeGL() {

+ 0
- 146
src/WindowSDL.cpp 查看文件

@@ -24,25 +24,12 @@ WindowSDL::WindowSDL() {
24 24
     mTextInput = false;
25 25
     mWindow = NULL;
26 26
     mGLContext = NULL;
27
-    mFontInit = false;
28
-    mFontName = NULL;
29
-    mFont = NULL;
30
-    mFontTexture = 0;
31 27
 
32 28
 #ifdef WIN32
33 29
     setDriver("libGL32.dll");
34 30
 #elif !defined(__APPLE__)
35 31
     setDriver("/usr/lib/libGL.so.1");
36 32
 #endif
37
-
38
-    tempText.text = new char[256];
39
-    tempText.color[0] = 0xFF;
40
-    tempText.color[1] = 0xFF;
41
-    tempText.color[2] = 0xFF;
42
-    tempText.color[3] = 0xFF;
43
-    tempText.scale = 1.2f;
44
-    tempText.w = 0;
45
-    tempText.h = 0;
46 33
 }
47 34
 
48 35
 WindowSDL::~WindowSDL() {
@@ -50,29 +37,6 @@ WindowSDL::~WindowSDL() {
50 37
         SDL_QuitSubSystem(SUBSYSTEMS_USED);
51 38
         SDL_Quit();
52 39
     }
53
-
54
-    if (mFont)
55
-        TTF_CloseFont(mFont);
56
-
57
-    if (mFontInit)
58
-        TTF_Quit();
59
-
60
-    if (mDriver)
61
-        delete [] mDriver;
62
-
63
-    if (mFontName)
64
-        delete [] mFontName;
65
-
66
-    if (tempText.text)
67
-        delete [] tempText.text;
68
-}
69
-
70
-void WindowSDL::setDriver(const char *driver) {
71
-    assert(driver != NULL);
72
-    assert(driver[0] != '\0');
73
-    assert(mInit == false);
74
-
75
-    mDriver = bufferString("%s", driver);
76 40
 }
77 41
 
78 42
 void WindowSDL::setSize(unsigned int width, unsigned int height) {
@@ -510,113 +474,3 @@ void WindowSDL::swapBuffersGL() {
510 474
     SDL_GL_SwapWindow(mWindow);
511 475
 }
512 476
 
513
-void WindowSDL::setFont(const char *font) {
514
-    assert(font != NULL);
515
-    assert(font[0] != '\0');
516
-    assert(mFontInit == false);
517
-
518
-    mFontName = fullPath(font, 0);
519
-}
520
-
521
-int WindowSDL::initializeFont() {
522
-    assert(mFontInit == false);
523
-    assert(mFontName != NULL);
524
-    assert(mFontName[0] != '\0');
525
-
526
-    if (TTF_Init() != 0) {
527
-        printf("Could not initialize SDL-TTF!\n");
528
-        return -1;
529
-    }
530
-
531
-    mFont = TTF_OpenFont(mFontName, 24);
532
-    if (mFont == NULL) {
533
-        printf("TTF_OpenFont Error: %s\n", TTF_GetError());
534
-        return -2;
535
-    }
536
-
537
-    glGenTextures(1, &mFontTexture);
538
-
539
-    mFontInit = true;
540
-    return 0;
541
-}
542
-
543
-void WindowSDL::writeString(WindowString &s) {
544
-    assert(s.text != NULL);
545
-    assert(mInit == true);
546
-
547
-    SDL_Color color;
548
-    color.r = (unsigned char)(s.color[0] * 255.0f);
549
-    color.g = (unsigned char)(s.color[1] * 255.0f);
550
-    color.b = (unsigned char)(s.color[2] * 255.0f);
551
-    color.a = (unsigned char)(s.color[3] * 255.0f);
552
-
553
-    SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.text, color);
554
-    if (surface == NULL) {
555
-        printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
556
-        return;
557
-    }
558
-
559
-    s.w = (int)((float)surface->w * s.scale);
560
-    s.h = (int)((float)surface->h * s.scale);
561
-
562
-    GLenum textureFormat;
563
-    if (surface->format->BytesPerPixel == 4) {
564
-        if (surface->format->Rmask == 0x000000FF)
565
-            textureFormat = GL_RGBA;
566
-        else
567
-            textureFormat = GL_BGRA_EXT;
568
-    } else {
569
-        if (surface->format->Rmask == 0x000000FF)
570
-            textureFormat = GL_RGB;
571
-        else
572
-            textureFormat = GL_BGR_EXT;
573
-    }
574
-
575
-    glBindTexture(GL_TEXTURE_2D, mFontTexture);
576
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
577
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
578
-    glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
579
-
580
-    GLuint xMin = s.x;
581
-    GLuint yMin = s.y;
582
-    GLuint xMax = xMin + s.w;
583
-    GLuint yMax = yMin + s.h;
584
-
585
-    glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
586
-
587
-    glBegin(GL_QUADS);
588
-        glTexCoord2f(0.0f, 0.0f);
589
-        glVertex2i(xMin, yMin);
590
-
591
-        glTexCoord2f(0.0f, 1.0f);
592
-        glVertex2i(xMin, yMax);
593
-
594
-        glTexCoord2f(1.0f, 1.0f);
595
-        glVertex2i(xMax, yMax);
596
-
597
-        glTexCoord2f(1.0f, 0.0f);
598
-        glVertex2i(xMax, yMin);
599
-    glEnd();
600
-
601
-    SDL_FreeSurface(surface);
602
-}
603
-
604
-void WindowSDL::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
605
-    va_list args;
606
-    va_start(args, s);
607
-    vsnprintf(tempText.text, 256, s, args);
608
-    tempText.text[255] = '\0';
609
-    va_end(args);
610
-
611
-    tempText.scale = scale;
612
-    tempText.x = x;
613
-    tempText.y = y;
614
-    if (color) {
615
-        tempText.color[0] = color[0];
616
-        tempText.color[1] = color[1];
617
-        tempText.color[2] = color[2];
618
-        tempText.color[3] = color[3];
619
-    }
620
-    writeString(tempText);
621
-}
622
-

+ 6
- 0
src/main.cpp 查看文件

@@ -28,6 +28,7 @@
28 28
 
29 29
 #ifdef USING_SDL
30 30
 #include "WindowSDL.h"
31
+#include "FontSDL.h"
31 32
 #else
32 33
 #error No Windowing Library selected!
33 34
 #endif
@@ -48,6 +49,7 @@ SoundNull gSound;
48 49
 
49 50
 #ifdef USING_SDL
50 51
 WindowSDL gWindow;
52
+FontSDL gFont;
51 53
 #endif
52 54
 
53 55
 Camera &getCamera() {
@@ -58,6 +60,10 @@ Console &getConsole() {
58 60
     return gConsole;
59 61
 }
60 62
 
63
+Font &getFont() {
64
+    return gFont;
65
+}
66
+
61 67
 Game &getGame() {
62 68
     return gGame;
63 69
 }

正在加载...
取消
保存