Browse Source

Fixed FontTTF Glyph Y Offset

Thomas Buck 10 years ago
parent
commit
f529431d53
3 changed files with 34 additions and 75 deletions
  1. 3
    0
      ChangeLog.md
  2. 3
    0
      include/system/FontTTF.h
  3. 28
    75
      src/system/FontTTF.cpp

+ 3
- 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
+    [ 20140107 ]
6
+    * Fixed problems with FontTTFs Glyph Baseline
7
+
5
     [ 20150106 ]
8
     [ 20150106 ]
6
     * Removed SDL2-TTF Font implementation
9
     * Removed SDL2-TTF Font implementation
7
     * Added new Font implementation using stb_truetype
10
     * Added new Font implementation using stb_truetype

+ 3
- 0
include/system/FontTTF.h View File

47
   private:
47
   private:
48
     static int charIsMapped(int c);
48
     static int charIsMapped(int c);
49
     static int getQuad(int c, float* xpos, float* ypos, stbtt_aligned_quad *quad);
49
     static int getQuad(int c, float* xpos, float* ypos, stbtt_aligned_quad *quad);
50
+    static void drawTextInternal(unsigned int x, unsigned int y, float scale,
51
+                                 const unsigned char color[4], unsigned int maxWidth, std::string s,
52
+                                 bool drawWrapped);
50
 
53
 
51
     static unsigned char* fontData;
54
     static unsigned char* fontData;
52
     static std::vector<FontMapTTF> maps;
55
     static std::vector<FontMapTTF> maps;

+ 28
- 75
src/system/FontTTF.cpp View File

15
 
15
 
16
 #define MAP_WIDTH 512
16
 #define MAP_WIDTH 512
17
 #define MAP_HEIGHT 512
17
 #define MAP_HEIGHT 512
18
-#define FONT_SIZE 30
18
+#define FONT_SIZE 33
19
 #define MAP_NUM_CHARS 50
19
 #define MAP_NUM_CHARS 50
20
 
20
 
21
 FontMapTTF::FontMapTTF() : begin(-1), texture(-1), charInfo(nullptr) { }
21
 FontMapTTF::FontMapTTF() : begin(-1), texture(-1), charInfo(nullptr) { }
159
 
159
 
160
 void FontTTF::drawText(unsigned int x, unsigned int y, float scale,
160
 void FontTTF::drawText(unsigned int x, unsigned int y, float scale,
161
                        const unsigned char color[4], std::string s) {
161
                        const unsigned char color[4], std::string s) {
162
-    glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
163
-    std::vector<glm::vec2> vertices;
164
-    std::vector<glm::vec2> uvs;
165
-    int texture = -1;
166
-    float xpos = x, ypos = y + (FONT_SIZE * scale);
167
-    for (int i = 0; i < s.length(); i++) {
168
-        if ((s[i] < 0x20) || (s[i] == 0x7F)) {
169
-            continue;
170
-        }
171
-
172
-        stbtt_aligned_quad quad;
173
-        int tex = getQuad(s[i], &xpos, &ypos, &quad);
174
-
175
-        if ((texture != tex) && (texture != -1)) {
176
-            vertexBuffer.bufferData(vertices);
177
-            uvBuffer.bufferData(uvs);
178
-            Shader::drawGL(vertexBuffer, uvBuffer, col, texture);
179
-            vertices.clear();
180
-            uvs.clear();
181
-        }
182
-
183
-        texture = tex;
184
-
185
-        glm::vec2 v1(quad.x0, quad.y0);
186
-        glm::vec2 v2(quad.x0, quad.y0 + ((quad.y1 - quad.y0) * scale));
187
-        glm::vec2 v3(quad.x0 + ((quad.x1 - quad.x0) * scale), quad.y0 + ((quad.y1 - quad.y0) * scale));
188
-        glm::vec2 v4(quad.x0 + ((quad.x1 - quad.x0) * scale), quad.y0);
189
-        glm::vec2 u1(quad.s0, quad.t0);
190
-        glm::vec2 u2(quad.s0, quad.t1);
191
-        glm::vec2 u3(quad.s1, quad.t1);
192
-        glm::vec2 u4(quad.s1, quad.t0);
193
-
194
-        vertices.push_back(v1);
195
-        vertices.push_back(v2);
196
-        vertices.push_back(v3);
197
-        vertices.push_back(v4);
198
-        vertices.push_back(v1);
199
-        vertices.push_back(v3);
200
-
201
-        uvs.push_back(u1);
202
-        uvs.push_back(u2);
203
-        uvs.push_back(u3);
204
-        uvs.push_back(u4);
205
-        uvs.push_back(u1);
206
-        uvs.push_back(u3);
207
-    }
208
-
209
-    vertexBuffer.bufferData(vertices);
210
-    uvBuffer.bufferData(uvs);
211
-    Shader::drawGL(vertexBuffer, uvBuffer, col, texture);
162
+    drawTextInternal(x, y, scale, color, 0, s, false);
212
 }
163
 }
213
 
164
 
214
 unsigned int FontTTF::heightText(float scale, unsigned int maxWidth, std::string s) {
165
 unsigned int FontTTF::heightText(float scale, unsigned int maxWidth, std::string s) {
230
 
181
 
231
 void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
182
 void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
232
                               const unsigned char color[4], unsigned int maxWidth, std::string s) {
183
                               const unsigned char color[4], unsigned int maxWidth, std::string s) {
184
+    drawTextInternal(x, y, scale, color, maxWidth, s, true);
185
+}
186
+
187
+void FontTTF::drawTextInternal(unsigned int x, unsigned int y, float scale,
188
+                               const unsigned char color[4], unsigned int maxWidth, std::string s,
189
+                               bool drawWrapped) {
233
     glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
190
     glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
234
     std::vector<glm::vec2> vertices;
191
     std::vector<glm::vec2> vertices;
235
     std::vector<glm::vec2> uvs;
192
     std::vector<glm::vec2> uvs;
243
         stbtt_aligned_quad quad;
200
         stbtt_aligned_quad quad;
244
         int tex = getQuad(s[i], &xpos, &ypos, &quad);
201
         int tex = getQuad(s[i], &xpos, &ypos, &quad);
245
 
202
 
246
-        if (xpos > (x + maxWidth)) {
203
+        if (drawWrapped && (xpos > (x + maxWidth))) {
247
             xpos = x;
204
             xpos = x;
248
             ypos += FONT_SIZE * scale;
205
             ypos += FONT_SIZE * scale;
249
             if (s[i] != ' ')
206
             if (s[i] != ' ')
261
 
218
 
262
         texture = tex;
219
         texture = tex;
263
 
220
 
264
-        glm::vec2 v1(quad.x0, quad.y0);
265
-        glm::vec2 v2(quad.x0, quad.y0 + ((quad.y1 - quad.y0) * scale));
266
-        glm::vec2 v3(quad.x0 + ((quad.x1 - quad.x0) * scale), quad.y0 + ((quad.y1 - quad.y0) * scale));
267
-        glm::vec2 v4(quad.x0 + ((quad.x1 - quad.x0) * scale), quad.y0);
268
-        glm::vec2 u1(quad.s0, quad.t0);
269
-        glm::vec2 u2(quad.s0, quad.t1);
270
-        glm::vec2 u3(quad.s1, quad.t1);
271
-        glm::vec2 u4(quad.s1, quad.t0);
272
-
273
-        vertices.push_back(v1);
274
-        vertices.push_back(v2);
275
-        vertices.push_back(v3);
276
-        vertices.push_back(v4);
277
-        vertices.push_back(v1);
278
-        vertices.push_back(v3);
279
-
280
-        uvs.push_back(u1);
281
-        uvs.push_back(u2);
282
-        uvs.push_back(u3);
283
-        uvs.push_back(u4);
284
-        uvs.push_back(u1);
285
-        uvs.push_back(u3);
221
+        float xmin = quad.x0;
222
+        float xmax = quad.x0 + ((quad.x1 - quad.x0) * scale);
223
+        float ymin = quad.y1;
224
+        float ymax = quad.y1 + ((quad.y0 - quad.y1) * scale);
225
+
226
+        vertices.emplace_back(xmin, ymin);
227
+        vertices.emplace_back(xmin, ymax);
228
+        vertices.emplace_back(xmax, ymax);
229
+        vertices.emplace_back(xmax, ymin);
230
+        vertices.emplace_back(xmin, ymin);
231
+        vertices.emplace_back(xmax, ymax);
232
+
233
+        uvs.emplace_back(quad.s0, quad.t1);
234
+        uvs.emplace_back(quad.s0, quad.t0);
235
+        uvs.emplace_back(quad.s1, quad.t0);
236
+        uvs.emplace_back(quad.s1, quad.t1);
237
+        uvs.emplace_back(quad.s0, quad.t1);
238
+        uvs.emplace_back(quad.s1, quad.t0);
286
     }
239
     }
287
 
240
 
288
     vertexBuffer.bufferData(vertices);
241
     vertexBuffer.bufferData(vertices);
301
     if (c >= (MAP_NUM_CHARS / 2))
254
     if (c >= (MAP_NUM_CHARS / 2))
302
         begin -= (MAP_NUM_CHARS / 2);
255
         begin -= (MAP_NUM_CHARS / 2);
303
 
256
 
304
-    getLog() << "Unmapped character " << c << ", new map from " << begin << " to "
257
+    getLog() << "Unmapped character '" << char(c) << "', new map from " << begin << " to "
305
              << begin + MAP_NUM_CHARS - 1 << "..." << Log::endl;
258
              << begin + MAP_NUM_CHARS - 1 << "..." << Log::endl;
306
 
259
 
307
     int p = maps.size();
260
     int p = maps.size();

Loading…
Cancel
Save