瀏覽代碼

Fixed FontTTF Glyph Y Offset

Thomas Buck 10 年之前
父節點
當前提交
f529431d53
共有 3 個檔案被更改,包括 34 行新增75 行删除
  1. 3
    0
      ChangeLog.md
  2. 3
    0
      include/system/FontTTF.h
  3. 28
    75
      src/system/FontTTF.cpp

+ 3
- 0
ChangeLog.md 查看文件

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140107 ]
6
+    * Fixed problems with FontTTFs Glyph Baseline
7
+
5 8
     [ 20150106 ]
6 9
     * Removed SDL2-TTF Font implementation
7 10
     * Added new Font implementation using stb_truetype

+ 3
- 0
include/system/FontTTF.h 查看文件

@@ -47,6 +47,9 @@ class FontTTF {
47 47
   private:
48 48
     static int charIsMapped(int c);
49 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 54
     static unsigned char* fontData;
52 55
     static std::vector<FontMapTTF> maps;

+ 28
- 75
src/system/FontTTF.cpp 查看文件

@@ -15,7 +15,7 @@
15 15
 
16 16
 #define MAP_WIDTH 512
17 17
 #define MAP_HEIGHT 512
18
-#define FONT_SIZE 30
18
+#define FONT_SIZE 33
19 19
 #define MAP_NUM_CHARS 50
20 20
 
21 21
 FontMapTTF::FontMapTTF() : begin(-1), texture(-1), charInfo(nullptr) { }
@@ -159,56 +159,7 @@ unsigned int FontTTF::widthText(float scale, std::string s) {
159 159
 
160 160
 void FontTTF::drawText(unsigned int x, unsigned int y, float scale,
161 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 165
 unsigned int FontTTF::heightText(float scale, unsigned int maxWidth, std::string s) {
@@ -230,6 +181,12 @@ unsigned int FontTTF::heightText(float scale, unsigned int maxWidth, std::string
230 181
 
231 182
 void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
232 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 190
     glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
234 191
     std::vector<glm::vec2> vertices;
235 192
     std::vector<glm::vec2> uvs;
@@ -243,7 +200,7 @@ void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
243 200
         stbtt_aligned_quad quad;
244 201
         int tex = getQuad(s[i], &xpos, &ypos, &quad);
245 202
 
246
-        if (xpos > (x + maxWidth)) {
203
+        if (drawWrapped && (xpos > (x + maxWidth))) {
247 204
             xpos = x;
248 205
             ypos += FONT_SIZE * scale;
249 206
             if (s[i] != ' ')
@@ -261,28 +218,24 @@ void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
261 218
 
262 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 241
     vertexBuffer.bufferData(vertices);
@@ -301,7 +254,7 @@ int FontTTF::charIsMapped(int c) {
301 254
     if (c >= (MAP_NUM_CHARS / 2))
302 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 258
              << begin + MAP_NUM_CHARS - 1 << "..." << Log::endl;
306 259
 
307 260
     int p = maps.size();

Loading…
取消
儲存