瀏覽代碼

TRLE Font support nearly working

Thomas Buck 10 年之前
父節點
當前提交
9f9987d016
共有 1 個檔案被更改,包括 53 行新增33 行删除
  1. 53
    33
      src/FontTRLE.cpp

+ 53
- 33
src/FontTRLE.cpp 查看文件

@@ -5,8 +5,7 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include <cstdio>
9
-#include <cstdarg>
8
+#include <fstream>
10 9
 
11 10
 #include "global.h"
12 11
 #include "utils/strings.h"
@@ -43,25 +42,37 @@ FontTRLE::~FontTRLE() {
43 42
 }
44 43
 
45 44
 int FontTRLE::initialize() {
45
+    // TODO font coloring not working when .pc has color?!?!
46
+
46 47
 
47 48
     // tmp debug
48 49
     delete [] mFontName;
49 50
     mFontName = bufferString("/Users/thomas/Downloads/TR Fonts/TR4 Official/Font.pc");
51
+    //mFontName = bufferString("/Users/thomas/Downloads/TR Fonts/TR2 Web/Font.pc");
52
+
50 53
 
51 54
     assert(mFontInit == false);
52 55
     assert(mFontName != NULL);
53 56
     assert(mFontName[0] != '\0');
54 57
     assert(stringEndsWith(mFontName, ".pc") == true);
55 58
 
56
-    // Load .pc file into GL texture
57
-    unsigned char *pixels = NULL;
59
+    // Load .pc file...
60
+    std::ifstream file(mFontName, std::ios::in | std::ios::binary);
61
+    unsigned char *pixels = new unsigned char[256 * 256 * 4];
62
+    if (!file.read((char *)pixels, 256 * 256 * 4)) {
63
+        delete [] pixels;
64
+        return -1;
65
+    }
66
+
67
+    // ...into GL texture
58 68
     glGenTextures(1, &mFontTexture);
59 69
     glBindTexture(GL_TEXTURE_2D, mFontTexture);
60 70
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
61 71
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62 72
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
73
+    delete [] pixels;
63 74
 
64
-    // Try to load .lps file, overwriting defaults
75
+    // Try to load .lps file, overwriting default glyph positions
65 76
     char *lpsFile = stringReplace(mFontName, ".pc", ".lps");
66 77
     loadLPS(lpsFile);
67 78
     delete [] lpsFile;
@@ -71,40 +82,44 @@ int FontTRLE::initialize() {
71 82
 }
72 83
 
73 84
 void FontTRLE::loadLPS(const char *file) {
74
-    //! \todo load lps file
85
+    // TODO load lps file
86
+
87
+    // if baselineAbs != 0
88
+    //     all offset[4] + baselineAbs;
75 89
 }
76 90
 
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);
91
+#define SCALING 2.0f
84 92
 
93
+void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, FontString &s) {
94
+    int width = (int)(((vec_t)offsets[index][2]) * s.scale * SCALING);
95
+    int height = (int)(((vec_t)offsets[index][3]) * s.scale * SCALING);
96
+    int offset = (int)(((vec_t)offsets[index][4]) * s.scale * SCALING);
97
+
98
+    // screen coordinates
99
+    int xMin = xDraw;
100
+    int yMin = s.y + offset + (int)(10.0f * s.scale * SCALING);
101
+    int xMax = xMin + width;
102
+    int yMax = yMin + height;
103
+
104
+    // texture part
105
+    vec_t txMin = ((vec_t)offsets[index][0]) / 256.0f;
106
+    vec_t txMax = ((vec_t)(offsets[index][0] + offsets[index][2])) / 256.0f;
107
+    vec_t tyMin = ((vec_t)offsets[index][1]) / 256.0f;
108
+    vec_t tyMax = ((vec_t)(offsets[index][1] + offsets[index][3])) / 256.0f;
109
+
110
+    // draw
85 111
     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
-
112
+    glColor4f(s.color[0], s.color[1], s.color[2], s.color[3]);
94 113
     glBegin(GL_QUADS);
95
-    glTexCoord2f(0.0f, 0.0f);
114
+    glTexCoord2f(txMin, tyMin);
96 115
     glVertex2i(xMin, yMin);
97
-
98
-    glTexCoord2f(0.0f, 1.0f);
116
+    glTexCoord2f(txMin, tyMax);
99 117
     glVertex2i(xMin, yMax);
100
-
101
-    glTexCoord2f(1.0f, 1.0f);
118
+    glTexCoord2f(txMax, tyMax);
102 119
     glVertex2i(xMax, yMax);
103
-
104
-    glTexCoord2f(1.0f, 0.0f);
120
+    glTexCoord2f(txMax, tyMin);
105 121
     glVertex2i(xMax, yMin);
106 122
     glEnd();
107
-*/
108 123
 }
109 124
 
110 125
 void FontTRLE::writeString(FontString &s) {
@@ -115,18 +130,23 @@ void FontTRLE::writeString(FontString &s) {
115 130
 
116 131
     for (unsigned int i = 0; s.text[i] != '\0'; i++) {
117 132
         // index into offset table
118
-        int index = s.text[i] - ' ';
133
+        int index = s.text[i] - '!';
134
+
135
+        if (index == -1) // space
136
+            x += (unsigned int)(14.0f * s.scale * SCALING);
119 137
 
120 138
         if ((index < 0) || (index > 105))
121 139
             continue; // skip unprintable chars
122 140
 
123 141
         writeChar((unsigned int)index, x, s);
124
-        x += offsets[index][2]; // width
142
+        x += (int)((vec_t)(offsets[index][2] + 1) * s.scale * SCALING); // width
125 143
     }
126 144
 
145
+    // TODO scaling?!
146
+    s.w = x;
127 147
 /*
128
-    s.w = (int)((float)surface->w * s.scale);
129
-    s.h = (int)((float)surface->h * s.scale);
148
+    s.w = (int)((float)surface->w * s.scale * SCALING);
149
+    s.h = (int)((float)surface->h * s.scale * SCALING);
130 150
 */
131 151
 }
132 152
 

Loading…
取消
儲存