Browse Source

Added FontManager

Thomas Buck 10 years ago
parent
commit
48b2985506
13 changed files with 155 additions and 77 deletions
  1. 4
    0
      ChangeLog.md
  2. 4
    2
      README.md
  3. 4
    0
      TODO.md
  4. 2
    1
      include/Font.h
  5. 42
    0
      include/FontManager.h
  6. 0
    5
      include/FontSDL.h
  7. 0
    4
      include/FontTRLE.h
  8. 10
    12
      src/CMakeLists.txt
  9. 22
    0
      src/Font.cpp
  10. 65
    0
      src/FontManager.cpp
  11. 0
    22
      src/FontSDL.cpp
  12. 0
    21
      src/FontTRLE.cpp
  13. 2
    10
      src/main.cpp

+ 4
- 0
ChangeLog.md View File

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20140615 ]
6
+    * Added FontManager, selecting Font library to use depending on
7
+      the file extension of the font file specified in the config.
8
+
5
     [ 20140614 ]
9
     [ 20140614 ]
6
     * Implemented TRLE Font loader
10
     * Implemented TRLE Font loader
7
 
11
 

+ 4
- 2
README.md View File

117
 
117
 
118
 ### Tomb Raider Level Editor Font support
118
 ### Tomb Raider Level Editor Font support
119
 
119
 
120
-OpenRaider can read Font.pc files used by the TRLE. If the fonts glyph positions match the TR4 defaults, only a Font.pc file is required. If the positions differ, you also need a [Leikkuri/Cutter](http://trep.trlevel.de/en/downloads.html) preset file (.lps).
120
+OpenRaider can read Font.pc files used by the TRLE. If the fonts glyph positions match the TR4 defaults, only a Font.pc file is required. If the positions differ, you also need a [Leikkuri/Cutter](http://trep.trlevel.de/en/downloads.html) preset file (`.lps`).
121
 
121
 
122
-TRLE Font support will automatically be built if SDL2-TTF is not available. Just change the font file path in your config file.
122
+TRLE Font support will automatically be built and used when selecting a font in your config file ending with `.pc`.
123
+
124
+If a `.lps` is required, give it the same name as your font file.
123
 
125
 
124
 I’ve made [a small writeup on my website](http://xythobuz.de/2014_06_14_trle_font.html) about this.
126
 I’ve made [a small writeup on my website](http://xythobuz.de/2014_06_14_trle_font.html) about this.
125
 
127
 

+ 4
- 0
TODO.md View File

26
 * Support SSE with other compilers than Clang (src/CMakeLists.txt)
26
 * Support SSE with other compilers than Clang (src/CMakeLists.txt)
27
 * Visual C++ compiler flags? (CMakeLists.txt)
27
 * Visual C++ compiler flags? (CMakeLists.txt)
28
 
28
 
29
+## Bugs
30
+
31
+* Menu Header “OpenRaider” not visible when using FontSDL?
32
+
29
 ## Future Features
33
 ## Future Features
30
 
34
 
31
 * Use only assets from old TR games, not even depending on a font file?
35
 * Use only assets from old TR games, not even depending on a font file?

+ 2
- 1
include/Font.h View File

36
     virtual void writeString(FontString &s) = 0;
36
     virtual void writeString(FontString &s) = 0;
37
 
37
 
38
     virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
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;
39
+        __attribute__((format(printf, 6, 0)));
40
 
40
 
41
 protected:
41
 protected:
42
     bool mFontInit;
42
     bool mFontInit;
43
     char *mFontName;
43
     char *mFontName;
44
+    FontString tempText;
44
 };
45
 };
45
 
46
 
46
 Font &getFont();
47
 Font &getFont();

+ 42
- 0
include/FontManager.h View File

1
+/*!
2
+ * \file include/FontManager.h
3
+ * \brief Font manager
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _FONT_MANAGER_H_
9
+#define _FONT_MANAGER_H_
10
+
11
+#include <vector>
12
+
13
+#include "Font.h"
14
+
15
+class FontManager : public Font {
16
+public:
17
+
18
+    /*!
19
+     * \brief Constructs an object of FontManager
20
+     */
21
+    FontManager();
22
+
23
+    /*!
24
+     * \brief Deconstructs an object of FontManager
25
+     */
26
+    virtual ~FontManager();
27
+
28
+    virtual int initialize();
29
+
30
+    virtual void writeString(FontString &s);
31
+
32
+private:
33
+
34
+    void add(Font *f, const char *e);
35
+
36
+    int font;
37
+    std::vector<Font *> fonts;
38
+    std::vector<const char *> extensions;
39
+};
40
+
41
+#endif
42
+

+ 0
- 5
include/FontSDL.h View File

32
 
32
 
33
     virtual void writeString(FontString &s);
33
     virtual void writeString(FontString &s);
34
 
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:
35
 private:
39
     TTF_Font *mFont;
36
     TTF_Font *mFont;
40
     unsigned int mFontTexture;
37
     unsigned int mFontTexture;
41
-
42
-    FontString tempText;
43
 };
38
 };
44
 
39
 
45
 #endif
40
 #endif

+ 0
- 4
include/FontTRLE.h View File

31
 
31
 
32
     virtual void writeString(FontString &s);
32
     virtual void writeString(FontString &s);
33
 
33
 
34
-    virtual void drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...)
35
-        __attribute__((format(printf, 6, 0)));
36
-
37
 private:
34
 private:
38
 
35
 
39
     void loadLPS(const char *f);
36
     void loadLPS(const char *f);
40
     void writeChar(unsigned int index, unsigned int xDraw, FontString &s);
37
     void writeChar(unsigned int index, unsigned int xDraw, FontString &s);
41
 
38
 
42
     unsigned int mFontTexture;
39
     unsigned int mFontTexture;
43
-    FontString tempText;
44
 
40
 
45
     // 106 entries: (x, y, w, h, offset)
41
     // 106 entries: (x, y, w, h, offset)
46
     int offsets[106][5] = {
42
     int offsets[106][5] = {

+ 10
- 12
src/CMakeLists.txt View File

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} "Font.cpp")
47
+set (SRCS ${SRCS} "FontManager.cpp")
48
+set (SRCS ${SRCS} "FontTRLE.cpp")
47
 set (SRCS ${SRCS} "Game.cpp")
49
 set (SRCS ${SRCS} "Game.cpp")
48
 set (SRCS ${SRCS} "main.cpp")
50
 set (SRCS ${SRCS} "main.cpp")
49
 set (SRCS ${SRCS} "Menu.cpp")
51
 set (SRCS ${SRCS} "Menu.cpp")
73
 endif (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
75
 endif (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
74
 
76
 
75
 # Select available Windowing library
77
 # Select available Windowing library
76
-if (SDL2_FOUND AND SDL2TTF_FOUND)
78
+if (SDL2_FOUND)
77
     set (SRCS ${SRCS} "WindowSDL.cpp")
79
     set (SRCS ${SRCS} "WindowSDL.cpp")
78
-    set (SRCS ${SRCS} "FontSDL.cpp")
79
     set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL")
80
     set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL")
80
-else (SDL2_FOUND AND SDL2TTF_FOUND)
81
-    if (SDL2_FOUND)
82
-        set (SRCS ${SRCS} "WindowSDL.cpp")
83
-        set (SRCS ${SRCS} "FontTRLE.cpp")
84
-        set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL")
85
-        set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_TRLE")
86
-    else (SDL2_FOUND)
87
-        message (FATAL_ERROR "SDL2 is required at the moment!")
88
-    endif (SDL2_FOUND)
89
-endif (SDL2_FOUND AND SDL2TTF_FOUND)
81
+    if (SDL2TTF_FOUND)
82
+        set (SRCS ${SRCS} "FontSDL.cpp")
83
+        set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL_FONT")
84
+    endif (SDL2TTF_FOUND)
85
+else (SDL2_FOUND)
86
+    message (FATAL_ERROR "SDL2 is required at the moment!")
87
+endif (SDL2_FOUND)
90
 
88
 
91
 #################################################################
89
 #################################################################
92
 
90
 

+ 22
- 0
src/Font.cpp View File

12
 Font::~Font() {
12
 Font::~Font() {
13
     delete [] mFontName;
13
     delete [] mFontName;
14
     mFontName = NULL;
14
     mFontName = NULL;
15
+
16
+    delete [] tempText.text;
17
+    tempText.text = NULL;
15
 }
18
 }
16
 
19
 
17
 void Font::setFont(const char *font) {
20
 void Font::setFont(const char *font) {
22
     mFontName = fullPath(font, 0);
25
     mFontName = fullPath(font, 0);
23
 }
26
 }
24
 
27
 
28
+void Font::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
29
+    va_list args;
30
+    va_start(args, s);
31
+    vsnprintf(tempText.text, 256, s, args);
32
+    tempText.text[255] = '\0';
33
+    va_end(args);
34
+
35
+    tempText.scale = scale;
36
+    tempText.x = x;
37
+    tempText.y = y;
38
+    if (color) {
39
+        tempText.color[0] = color[0];
40
+        tempText.color[1] = color[1];
41
+        tempText.color[2] = color[2];
42
+        tempText.color[3] = color[3];
43
+    }
44
+    writeString(tempText);
45
+}
46
+

+ 65
- 0
src/FontManager.cpp View File

1
+/*!
2
+ * \file src/FontManager.cpp
3
+ * \brief SDL Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/strings.h"
10
+#include "FontManager.h"
11
+#include "Font.h"
12
+#include "FontSDL.h"
13
+#include "FontTRLE.h"
14
+
15
+FontManager::FontManager() {
16
+    add(new FontTRLE(), ".pc");
17
+
18
+#ifdef USING_SDL_FONT
19
+    add(new FontSDL(), ".ttf");
20
+#endif
21
+
22
+    font = -1;
23
+
24
+    tempText.text = new char[256];
25
+    tempText.color[0] = 0xFF;
26
+    tempText.color[1] = 0xFF;
27
+    tempText.color[2] = 0xFF;
28
+    tempText.color[3] = 0xFF;
29
+    tempText.scale = 1.2f;
30
+    tempText.w = 0;
31
+    tempText.h = 0;
32
+}
33
+
34
+FontManager::~FontManager() {
35
+    while (!fonts.empty()) {
36
+        delete fonts.back();
37
+        fonts.pop_back();
38
+    }
39
+}
40
+
41
+void FontManager::add(Font *f, const char *e) {
42
+    fonts.push_back(f);
43
+    extensions.push_back(e);
44
+}
45
+
46
+int FontManager::initialize() {
47
+    for (unsigned int i = 0; i < fonts.size(); i++) {
48
+        if (stringEndsWith(mFontName, extensions.at(i))) {
49
+            font = i;
50
+            break;
51
+        }
52
+    }
53
+
54
+    if (font == -1)
55
+        return -1;
56
+
57
+    fonts.at(font)->setFont(mFontName);
58
+    return fonts.at(font)->initialize();
59
+}
60
+
61
+void FontManager::writeString(FontString &s) {
62
+    assert(font != -1);
63
+    fonts.at(font)->writeString(s);
64
+}
65
+

+ 0
- 22
src/FontSDL.cpp View File

30
 
30
 
31
     if (mFontInit)
31
     if (mFontInit)
32
         TTF_Quit();
32
         TTF_Quit();
33
-
34
-    delete [] tempText.text;
35
-    tempText.text = NULL;
36
 }
33
 }
37
 
34
 
38
 int FontSDL::initialize() {
35
 int FontSDL::initialize() {
118
     SDL_FreeSurface(surface);
115
     SDL_FreeSurface(surface);
119
 }
116
 }
120
 
117
 
121
-void FontSDL::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
122
-    va_list args;
123
-    va_start(args, s);
124
-    vsnprintf(tempText.text, 256, s, args);
125
-    tempText.text[255] = '\0';
126
-    va_end(args);
127
-
128
-    tempText.scale = scale;
129
-    tempText.x = x;
130
-    tempText.y = y;
131
-    if (color) {
132
-        tempText.color[0] = color[0];
133
-        tempText.color[1] = color[1];
134
-        tempText.color[2] = color[2];
135
-        tempText.color[3] = color[3];
136
-    }
137
-    writeString(tempText);
138
-}
139
-

+ 0
- 21
src/FontTRLE.cpp View File

28
 }
28
 }
29
 
29
 
30
 FontTRLE::~FontTRLE() {
30
 FontTRLE::~FontTRLE() {
31
-    delete [] tempText.text;
32
-    tempText.text = NULL;
33
 }
31
 }
34
 
32
 
35
 int FontTRLE::initialize() {
33
 int FontTRLE::initialize() {
167
 */
165
 */
168
 }
166
 }
169
 
167
 
170
-void FontTRLE::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
171
-    va_list args;
172
-    va_start(args, s);
173
-    vsnprintf(tempText.text, 256, s, args);
174
-    tempText.text[255] = '\0';
175
-    va_end(args);
176
-
177
-    tempText.scale = scale;
178
-    tempText.x = x;
179
-    tempText.y = y;
180
-    if (color) {
181
-        tempText.color[0] = color[0];
182
-        tempText.color[1] = color[1];
183
-        tempText.color[2] = color[2];
184
-        tempText.color[3] = color[3];
185
-    }
186
-    writeString(tempText);
187
-}
188
-

+ 2
- 10
src/main.cpp View File

12
 #include "global.h"
12
 #include "global.h"
13
 #include "Camera.h"
13
 #include "Camera.h"
14
 #include "Console.h"
14
 #include "Console.h"
15
+#include "FontManager.h"
15
 #include "Game.h"
16
 #include "Game.h"
16
 #include "Menu.h"
17
 #include "Menu.h"
17
 #include "OpenRaider.h"
18
 #include "OpenRaider.h"
28
 
29
 
29
 #ifdef USING_SDL
30
 #ifdef USING_SDL
30
 #include "WindowSDL.h"
31
 #include "WindowSDL.h"
31
-#ifdef USING_TRLE
32
-#include "FontTRLE.h"
33
-#else
34
-#include "FontSDL.h"
35
-#endif
36
 #else
32
 #else
37
 #error No Windowing Library selected!
33
 #error No Windowing Library selected!
38
 #endif
34
 #endif
39
 
35
 
40
 Camera gCamera;
36
 Camera gCamera;
41
 Console gConsole;
37
 Console gConsole;
38
+FontManager gFont;
42
 Game gGame;
39
 Game gGame;
43
 Menu gMenu;
40
 Menu gMenu;
44
 OpenRaider gOpenRaider;
41
 OpenRaider gOpenRaider;
53
 
50
 
54
 #ifdef USING_SDL
51
 #ifdef USING_SDL
55
 WindowSDL gWindow;
52
 WindowSDL gWindow;
56
-#ifdef USING_TRLE
57
-FontTRLE gFont;
58
-#else
59
-FontSDL gFont;
60
-#endif
61
 #endif
53
 #endif
62
 
54
 
63
 Camera &getCamera() {
55
 Camera &getCamera() {

Loading…
Cancel
Save