Browse Source

Split Font subsystem from Window

Thomas Buck 10 years ago
parent
commit
93af7ceb65
20 changed files with 341 additions and 232 deletions
  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 View File

3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
     [ 20140605 ]
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
     * Created methods to convert strings of names to ActionEvents
9
     * Created methods to convert strings of names to ActionEvents
7
       and KeyboardButtons.
10
       and KeyboardButtons.
8
     * Simplified bind commands
11
     * Simplified bind commands

+ 0
- 2
README.md View File

10
 
10
 
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)
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
 ## Configuration
13
 ## Configuration
16
 
14
 
17
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.
15
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.

+ 3
- 6
TODO.md View File

11
     * Use shared_ptr, should make memory handling/segfaults much less of a problem
11
     * Use shared_ptr, should make memory handling/segfaults much less of a problem
12
         * Don't even new ... the data structures but use std::make_shared or allocate_shared?
12
         * Don't even new ... the data structures but use std::make_shared or allocate_shared?
13
         * Pass object references to all other objects that need it, completely remove gOpenRaider
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
     * Use std::strings
14
     * Use std::strings
15
+    * Rewrite Console and use operator << to write to the console
17
 * 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
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
 ## Changes
18
 ## Changes
21
 
19
 
30
 
28
 
31
 ## Future Features
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
 * When cutscene rendering is working, use TR4/5 style menu?
33
 * When cutscene rendering is working, use TR4/5 style menu?
37
 
34
 

+ 49
- 0
include/Font.h View File

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 View File

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 View File

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

+ 1
- 1
include/OpenRaider.h View File

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

+ 9
- 28
include/Window.h View File

10
 
10
 
11
 #include <ctime>
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
  * \brief Windowing interface
14
  * \brief Windowing interface
25
  */
15
  */
31
      */
21
      */
32
     virtual ~Window();
22
     virtual ~Window();
33
 
23
 
34
-    virtual void setDriver(const char *driver) = 0;
35
-
36
     virtual void setSize(unsigned int width, unsigned int height) = 0;
24
     virtual void setSize(unsigned int width, unsigned int height) = 0;
37
 
25
 
38
     virtual void setFullscreen(bool fullscreen) = 0;
26
     virtual void setFullscreen(bool fullscreen) = 0;
49
 
37
 
50
     virtual void swapBuffersGL() = 0;
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
     virtual int initializeGL();
46
     virtual int initializeGL();
53
 
47
 
54
     virtual void resizeGL();
48
     virtual void resizeGL();
57
 
51
 
58
     virtual void glExit2D();
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
 protected:
54
 protected:
74
     bool mInit;
55
     bool mInit;
75
     char *mDriver;
56
     char *mDriver;
76
     bool mFullscreen;
57
     bool mFullscreen;
77
     bool mMousegrab;
58
     bool mMousegrab;
78
-
79
-    bool mFontInit;
80
-    char *mFontName;
59
+    unsigned int mWidth;
60
+    unsigned int mHeight;
81
 };
61
 };
82
 
62
 
83
 Window &getWindow();
63
 Window &getWindow();
84
 
64
 
85
 #endif
65
 #endif
66
+

+ 1
- 17
include/WindowSDL.h View File

29
      */
29
      */
30
     virtual ~WindowSDL();
30
     virtual ~WindowSDL();
31
 
31
 
32
-    virtual void setDriver(const char *driver);
33
-
34
     virtual void setSize(unsigned int width, unsigned int height);
32
     virtual void setSize(unsigned int width, unsigned int height);
35
 
33
 
36
     virtual void setFullscreen(bool fullscreen);
34
     virtual void setFullscreen(bool fullscreen);
47
 
45
 
48
     virtual void swapBuffersGL();
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
 private:
48
 private:
60
-
61
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
49
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
62
     SDL_GLContext mGLContext; //!< The OpenGL Context
50
     SDL_GLContext mGLContext; //!< The OpenGL Context
63
 
51
 
64
-    TTF_Font *mFont;
65
-    unsigned int mFontTexture;
66
-
67
     bool mTextInput;
52
     bool mTextInput;
68
-
69
-    WindowString tempText;
70
 };
53
 };
71
 
54
 
72
 #endif
55
 #endif
56
+

+ 2
- 0
src/CMakeLists.txt View File

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

+ 2
- 1
src/Command.cpp View File

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

+ 10
- 8
src/Console.cpp View File

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

+ 24
- 0
src/Font.cpp View File

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 View File

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 View File

9
 
9
 
10
 #include "global.h"
10
 #include "global.h"
11
 #include "Console.h"
11
 #include "Console.h"
12
-#include "Menu.h"
13
 #include "OpenRaider.h"
12
 #include "OpenRaider.h"
14
 #include "utils/strings.h"
13
 #include "utils/strings.h"
14
+#include "Window.h"
15
+#include "Menu.h"
15
 
16
 
16
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
17
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
17
 #include <dirent.h>
18
 #include <dirent.h>
162
 
163
 
163
 void Menu::displayMapList() {
164
 void Menu::displayMapList() {
164
     // Estimate displayable number of items
165
     // Estimate displayable number of items
165
-    int items = (getWindow().mHeight - 110) / 25;
166
+    int items = (getWindow().getHeight() - 110) / 25;
166
 
167
 
167
     // Select which part of the list to show
168
     // Select which part of the list to show
168
     int min, max;
169
     int min, max;
190
     for (int i = 0; i < (max - min); i++) {
191
     for (int i = 0; i < (max - min); i++) {
191
         char *map = mMapList[i + min];
192
         char *map = mMapList[i + min];
192
         if ((i + min) == (int)mCursor)
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
         else
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
         // Draw half-transparent *overlay*
202
         // Draw half-transparent *overlay*
202
         glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
203
         glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
203
         glDisable(GL_TEXTURE_2D);
204
         glDisable(GL_TEXTURE_2D);
204
-        glRecti(0, 0, getWindow().mWidth, getWindow().mHeight);
205
+        glRecti(0, 0, getWindow().getWidth(), getWindow().getHeight());
205
         glEnable(GL_TEXTURE_2D);
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
         // width of the drawn text to center it
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
         if (!mMapListFilled) {
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
         } else {
215
         } else {
215
             if (mMapList.size() == 0) {
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
             } else {
218
             } else {
218
                 // draw *play button* above list
219
                 // draw *play button* above list
219
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
220
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
220
                 glDisable(GL_TEXTURE_2D);
221
                 glDisable(GL_TEXTURE_2D);
221
                 glRecti(25, 25, 100, 75);
222
                 glRecti(25, 25, 100, 75);
222
                 glEnable(GL_TEXTURE_2D);
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
                 displayMapList();
226
                 displayMapList();
226
             }
227
             }
280
 }
281
 }
281
 
282
 
282
 void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
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
     if (released || (button != leftmouseKey))
286
     if (released || (button != leftmouseKey))
286
         return;
287
         return;

+ 6
- 3
src/OpenRaider.cpp View File

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

+ 7
- 6
src/Render.cpp View File

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

+ 19
- 0
src/Window.cpp View File

11
 
11
 
12
 #include "global.h"
12
 #include "global.h"
13
 #include "math/math.h"
13
 #include "math/math.h"
14
+#include "utils/strings.h"
14
 #include "Window.h"
15
 #include "Window.h"
15
 
16
 
16
 Window::~Window() {
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
 int Window::initializeGL() {
38
 int Window::initializeGL() {

+ 0
- 146
src/WindowSDL.cpp View File

24
     mTextInput = false;
24
     mTextInput = false;
25
     mWindow = NULL;
25
     mWindow = NULL;
26
     mGLContext = NULL;
26
     mGLContext = NULL;
27
-    mFontInit = false;
28
-    mFontName = NULL;
29
-    mFont = NULL;
30
-    mFontTexture = 0;
31
 
27
 
32
 #ifdef WIN32
28
 #ifdef WIN32
33
     setDriver("libGL32.dll");
29
     setDriver("libGL32.dll");
34
 #elif !defined(__APPLE__)
30
 #elif !defined(__APPLE__)
35
     setDriver("/usr/lib/libGL.so.1");
31
     setDriver("/usr/lib/libGL.so.1");
36
 #endif
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
 WindowSDL::~WindowSDL() {
35
 WindowSDL::~WindowSDL() {
50
         SDL_QuitSubSystem(SUBSYSTEMS_USED);
37
         SDL_QuitSubSystem(SUBSYSTEMS_USED);
51
         SDL_Quit();
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
 void WindowSDL::setSize(unsigned int width, unsigned int height) {
42
 void WindowSDL::setSize(unsigned int width, unsigned int height) {
510
     SDL_GL_SwapWindow(mWindow);
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 View File

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

Loading…
Cancel
Save