Browse Source

Started implementing FontTRLE

Thomas Buck 10 years ago
parent
commit
9c2ca51667
5 changed files with 322 additions and 2 deletions
  1. 3
    0
      ChangeLog.md
  2. 2
    2
      include/FontSDL.h
  3. 165
    0
      include/FontTRLE.h
  4. 1
    0
      src/FontSDL.cpp
  5. 151
    0
      src/FontTRLE.cpp

+ 3
- 0
ChangeLog.md View File

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140614 ]
6
+    * Started implementing TRLE Font loader
7
+
5 8
     [ 20140613 ]
6 9
     * Changed strange delete-guards
7 10
     * Moved some methods from World into the proper classes

+ 2
- 2
include/FontSDL.h View File

@@ -1,6 +1,6 @@
1 1
 /*!
2 2
  * \file include/FontSDL.h
3
- * \brief SDL windowing implementation
3
+ * \brief SDL Font implementation
4 4
  *
5 5
  * \author xythobuz
6 6
  */
@@ -13,7 +13,7 @@
13 13
 #include "Font.h"
14 14
 
15 15
 /*!
16
- * \brief SDL windowing implementation
16
+ * \brief SDL Font implementation
17 17
  */
18 18
 class FontSDL : public Font {
19 19
 public:

+ 165
- 0
include/FontTRLE.h View File

@@ -0,0 +1,165 @@
1
+/*!
2
+ * \file include/FontTRLE.h
3
+ * \brief Tomb Raider Level Editor Font loader
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _FONT_TRLE_H_
9
+#define _FONT_TRLE_H_
10
+
11
+#include "math/math.h"
12
+#include "Font.h"
13
+
14
+/*!
15
+ * \brief Tomb Raider Level Editor Font loader
16
+ */
17
+class FontTRLE : public Font {
18
+public:
19
+
20
+    /*!
21
+     * \brief Constructs an object of FontTRLE
22
+     */
23
+    FontTRLE();
24
+
25
+    /*!
26
+     * \brief Deconstructs an object of FontTRLE
27
+     */
28
+    virtual ~FontTRLE();
29
+
30
+    virtual int initialize();
31
+
32
+    virtual void writeString(FontString &s);
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:
38
+
39
+    void loadLPS(const char *file);
40
+    void writeChar(unsigned int index, unsigned int xDraw, FontString &s);
41
+
42
+    unsigned int mFontTexture;
43
+    FontString tempText;
44
+
45
+    unsigned int resolution1;
46
+    unsigned int resolution2;
47
+    int ruler1;
48
+    int ruler2;
49
+    int baselineAbs; // always zero?
50
+    vec_t spacing;
51
+    vec_t squashedTextFactor;
52
+
53
+    // 106 entries: (x, y, w, h, offset)
54
+    int offsets[106][5] = {
55
+        { 174,  52,   3,  12, -11 },
56
+        {  98,  58,   6,   4, -10 },
57
+        {  82,  26,  13,  11, -10 },
58
+        {  78,  38,   9,  13, -10 },
59
+        { 214,  13,  14,  11, -9  },
60
+        {  40,  26,  13,  11, -10 },
61
+        { 157,  57,   5,   6, -11 },
62
+        { 204,  39,   5,  15, -12 },
63
+        {  34,  40,   5,  15, -12 },
64
+        { 184,  59,   4,   4, -11 },
65
+        {  22,  40,  10,  10, -9  },
66
+        { 178,  59,   4,   4, -2  },
67
+        { 106,  60,   7,   2, -4  },
68
+        { 114,  60,   4,   3, -2  },
69
+        { 212,  38,   8,  14, -12 },
70
+        {  88,  49,   9,   9, -8  },
71
+        { 200,  55,   5,   9, -8  },
72
+        {  46,  52,   8,   9, -8  },
73
+        {  88,  38,   7,  10, -8  },
74
+        {  62,  40,  10,  10, -8  },
75
+        { 142,  48,   8,  11, -9  },
76
+        { 232,  50,   8,  10, -9  },
77
+        { 120,  47,   8,  11, -9  },
78
+        {  22,  51,   8,  10, -9  },
79
+        { 110,  49,   8,  10, -8  },
80
+        { 152,  57,   4,   7, -7  },
81
+        { 136,  57,   4,   9, -7  },
82
+        { 178,  40,  11,   9, -8  },
83
+        { 210,  53,  10,   6, -7  },
84
+        { 240,  40,  11,   9, -7  },
85
+        {  12,  39,   9,  12, -11 },
86
+        {  66,  13,  15,  13, -10 },
87
+        { 130,  13,  13,  12, -11 },
88
+        { 214,  25,  12,  12, -11 },
89
+        { 132,  35,  10,  12, -11 },
90
+        {   0,  26,  12,  12, -11 },
91
+        {  14,  26,  12,  12, -11 },
92
+        {  66,  27,  11,  12, -11 },
93
+        { 182,  27,  11,  12, -11 },
94
+        { 200,  13,  13,  12, -11 },
95
+        { 222,  54,   4,  12, -11 },
96
+        {  56,  52,   4,  15, -11 },
97
+        { 230,  15,  12,  12, -11 },
98
+        { 144,  35,  10,  12, -11 },
99
+        {  48,  13,  17,  12, -11 },
100
+        { 144,  13,  13,  12, -11 },
101
+        {  54,  26,  11,  12, -11 },
102
+        { 200,  26,  11,  12, -11 },
103
+        { 240,   0,  13,  14, -11 },
104
+        { 158,  13,  13,  12, -11 },
105
+        { 156,  35,  10,  12, -11 },
106
+        { 172,  13,  13,  12, -11 },
107
+        {  98,  13,  14,  12, -11 },
108
+        {  82,  13,  14,  12, -11 },
109
+        {  24,  13,  22,  12, -11 },
110
+        { 186,  13,  12,  13, -11 },
111
+        { 114,  13,  14,  12, -11 },
112
+        { 228,  28,  11,  12, -11 },
113
+        {  62,  60,   5,   3, -4  },
114
+        { 248,  59,   5,   3, -4  },
115
+        {  88,  59,   7,   3, -4  },
116
+        { 142,  60,   6,   2, -3  },
117
+        { 120,  59,   7,   3, -4  },
118
+        { 242,  59,   4,   4, -11 },
119
+        {  98,  49,  10,   8, -7  },
120
+        {  96,  35,  10,  13, -12 },
121
+        {  72,  52,   8,   8, -7  },
122
+        {   0,  39,  10,  11, -10 },
123
+        { 164,  52,   8,   8, -7  },
124
+        { 168,  38,   9,  13, -12 },
125
+        { 120,  35,  11,  11, -7  },
126
+        { 108,  35,  10,  13, -12 },
127
+        { 194,  27,   5,  11, -10 },
128
+        {  40,  51,   5,  15, -10 },
129
+        {  28,  26,  11,  13, -12 },
130
+        {  82,  52,   5,  12, -11 },
131
+        {  96,  26,  17,   8, -7  },
132
+        { 152,  48,  11,   8, -7  },
133
+        {  62,  51,   9,   8, -7  },
134
+        { 244,  15,   0,  12, -7  },
135
+        {  52,  39,   9,  12, -7  },
136
+        {  10,  52,   9,   8, -7  },
137
+        { 190,  52,   8,   8, -7  },
138
+        {   0,  51,   8,  10, -9  },
139
+        { 178,  50,  10,   8, -7  },
140
+        { 130,  48,  11,   8, -7  },
141
+        { 132,  26,  17,   8, -7  },
142
+        { 242,  50,  10,   8, -7  },
143
+        {  40,  38,  10,  12, -7  },
144
+        { 232,  41,   7,   8, -7  },
145
+        { 222,  41,   8,  12, -7  },
146
+        { 130,  57,   5,   8, -7  },
147
+        { 194,  39,   9,  12, -10 },
148
+        {  32,  56,   4,  11, -10 },
149
+        {   1,  14,  22,  11, -10 },
150
+        { 192,   0,  23,  13, -10 },
151
+        { 168,   0,  23,  12, -10 },
152
+        { 216,   0,  23,  12, -10 },
153
+        { 150,  26,  17,   8, -8  },
154
+        { 168,  26,  11,  11, -9  },
155
+        { 114,  26,  17,   8, -8  },
156
+        { 240,  28,  12,  11, -9  },
157
+        {   0,   0,  40,  12, -10 },
158
+        {  84,   0,  39,  11, -10 },
159
+        {  42,   0,  39,  11, -10 },
160
+        { 126,   0,  39,  11, -10 },
161
+    };
162
+};
163
+
164
+#endif
165
+

+ 1
- 0
src/FontSDL.cpp View File

@@ -58,6 +58,7 @@ int FontSDL::initialize() {
58 58
 }
59 59
 
60 60
 void FontSDL::writeString(FontString &s) {
61
+    assert(mFontInit == true);
61 62
     assert(s.text != NULL);
62 63
 
63 64
     SDL_Color color;

+ 151
- 0
src/FontTRLE.cpp View File

@@ -0,0 +1,151 @@
1
+/*!
2
+ * \file src/FontTRLE.cpp
3
+ * \brief SDL Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include <cstdio>
9
+#include <cstdarg>
10
+
11
+#include "global.h"
12
+#include "utils/strings.h"
13
+#include "FontTRLE.h"
14
+
15
+FontTRLE::FontTRLE() {
16
+    mFontInit = false;
17
+    mFontName = NULL;
18
+    mFontTexture = 0;
19
+
20
+    // TRLE defaults
21
+    resolution1 = 240;
22
+    resolution2 = 512;
23
+    ruler1 = -16;
24
+    ruler2 = -40;
25
+    baselineAbs = 0; // always zero?
26
+    spacing = 0.075f;
27
+    squashedTextFactor = 0.75f;
28
+    // offset table default is set in header
29
+
30
+    tempText.text = new char[256];
31
+    tempText.color[0] = 0xFF;
32
+    tempText.color[1] = 0xFF;
33
+    tempText.color[2] = 0xFF;
34
+    tempText.color[3] = 0xFF;
35
+    tempText.scale = 1.2f;
36
+    tempText.w = 0;
37
+    tempText.h = 0;
38
+}
39
+
40
+FontTRLE::~FontTRLE() {
41
+    delete [] tempText.text;
42
+    tempText.text = NULL;
43
+}
44
+
45
+int FontTRLE::initialize() {
46
+
47
+    // tmp debug
48
+    delete [] mFontName;
49
+    mFontName = bufferString("/Users/thomas/Downloads/TR Fonts/TR4 Official/Font.pc");
50
+
51
+    assert(mFontInit == false);
52
+    assert(mFontName != NULL);
53
+    assert(mFontName[0] != '\0');
54
+    assert(stringEndsWith(mFontName, ".pc") == true);
55
+
56
+    // Load .pc file into GL texture
57
+    unsigned char *pixels = NULL;
58
+    glGenTextures(1, &mFontTexture);
59
+    glBindTexture(GL_TEXTURE_2D, mFontTexture);
60
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
61
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
63
+
64
+    // Try to load .lps file, overwriting defaults
65
+    char *lpsFile = stringReplace(mFontName, ".pc", ".lps");
66
+    loadLPS(lpsFile);
67
+    delete [] lpsFile;
68
+
69
+    mFontInit = true;
70
+    return 0;
71
+}
72
+
73
+void FontTRLE::loadLPS(const char *file) {
74
+    //! \todo load lps file
75
+}
76
+
77
+void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, FontString &s) {
78
+/*
79
+    SDL_Color color;
80
+    color.r = (unsigned char)(s.color[0] * 255.0f);
81
+    color.g = (unsigned char)(s.color[1] * 255.0f);
82
+    color.b = (unsigned char)(s.color[2] * 255.0f);
83
+    color.a = (unsigned char)(s.color[3] * 255.0f);
84
+
85
+    glBindTexture(GL_TEXTURE_2D, mFontTexture);
86
+
87
+    GLuint xMin = s.x;
88
+    GLuint yMin = s.y;
89
+    GLuint xMax = xMin + s.w;
90
+    GLuint yMax = yMin + s.h;
91
+
92
+    glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
93
+
94
+    glBegin(GL_QUADS);
95
+    glTexCoord2f(0.0f, 0.0f);
96
+    glVertex2i(xMin, yMin);
97
+
98
+    glTexCoord2f(0.0f, 1.0f);
99
+    glVertex2i(xMin, yMax);
100
+
101
+    glTexCoord2f(1.0f, 1.0f);
102
+    glVertex2i(xMax, yMax);
103
+
104
+    glTexCoord2f(1.0f, 0.0f);
105
+    glVertex2i(xMax, yMin);
106
+    glEnd();
107
+*/
108
+}
109
+
110
+void FontTRLE::writeString(FontString &s) {
111
+    assert(mFontInit == true);
112
+    assert(s.text != NULL);
113
+
114
+    unsigned int x = s.x;
115
+
116
+    for (unsigned int i = 0; s.text[i] != '\0'; i++) {
117
+        // index into offset table
118
+        int index = s.text[i] - ' ';
119
+
120
+        if ((index < 0) || (index > 105))
121
+            continue; // skip unprintable chars
122
+
123
+        writeChar((unsigned int)index, x, s);
124
+        x += offsets[index][2]; // width
125
+    }
126
+
127
+/*
128
+    s.w = (int)((float)surface->w * s.scale);
129
+    s.h = (int)((float)surface->h * s.scale);
130
+*/
131
+}
132
+
133
+void FontTRLE::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
134
+    va_list args;
135
+    va_start(args, s);
136
+    vsnprintf(tempText.text, 256, s, args);
137
+    tempText.text[255] = '\0';
138
+    va_end(args);
139
+
140
+    tempText.scale = scale;
141
+    tempText.x = x;
142
+    tempText.y = y;
143
+    if (color) {
144
+        tempText.color[0] = color[0];
145
+        tempText.color[1] = color[1];
146
+        tempText.color[2] = color[2];
147
+        tempText.color[3] = color[3];
148
+    }
149
+    writeString(tempText);
150
+}
151
+

Loading…
Cancel
Save