OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
FontTRLE.cpp
Go to the documentation of this file.
1 
8 #include <fstream>
9 #include <sstream>
10 
11 #include "global.h"
12 #include "TextureManager.h"
13 #include "utils/strings.h"
14 #include "system/Shader.h"
15 #include "system/FontTRLE.h"
16 
17 #define SCALING 2.0f
18 
19 bool FontTRLE::mFontInit = false;
20 unsigned int FontTRLE::mFontTexture = 0;
21 int FontTRLE::offsets[106][5];
24 
26 }
27 
28 int FontTRLE::initialize(std::string font) {
29  assert(stringEndsWith(font, ".pc") == true);
30 
31  shutdown();
32 
33  // Load .pc file...
34  std::ifstream file(font, std::ios::in | std::ios::binary);
35  unsigned char* pixels = new unsigned char[256 * 256 * 4];
36  if (!file.read((char*)pixels, 256 * 256 * 4)) {
37  delete [] pixels;
38  return -1;
39  }
40 
41  // Fix coloring
42  for (unsigned int i = 0; i < (256 * 256 * 4); i += 4) {
43  float y = (0.2126f * pixels[i + 2]);
44  y += (0.7152f * pixels[i + 1]);
45  y += (0.0722f * pixels[i]);
46  pixels[i] = pixels[i + 1] = pixels[i + 2] = (unsigned char)y;
47  }
48 
50  ColorMode::BGRA, 32,
52  delete [] pixels;
53 
54  // Try to load .lps file or use default glyph positions
55  std::string lpsFile = findAndReplace(font, ".pc", ".lps");
56  loadLPS(lpsFile);
57 
58  mFontInit = true;
59  return 0;
60 }
61 
62 void FontTRLE::loadLPS(std::string f) {
63  std::ifstream file(f);
64  if (!file) {
65  std::copy(&defaultOffsets[0][0], &defaultOffsets[0][0] + (106 * 5), &offsets[0][0]);
66  return;
67  }
68 
69  for (std::string line; std::getline(file, line);) {
70  std::istringstream stream(line);
71  int index;
72  stream >> index;
73  if (stream.get() != '=')
74  continue;
75  stream >> offsets[index][0];
76  if (stream.get() != ',')
77  continue;
78  stream >> offsets[index][1];
79  if (stream.get() != ',')
80  continue;
81  stream >> offsets[index][2];
82  if (stream.get() != ',')
83  continue;
84  stream >> offsets[index][3];
85  if (stream.get() != ',')
86  continue;
87  stream >> offsets[index][4];
88  }
89 }
90 
91 void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, unsigned int yDraw, float scale,
92  std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs) {
93  assert(mFontInit == true);
94 
95  float width = ((float)offsets[index][2]) * scale * SCALING;
96  float height = ((float)offsets[index][3]) * scale * SCALING;
97  float offset = ((float)offsets[index][4]) * scale * SCALING;
98 
99  // screen coordinates
100  float xMin = xDraw;
101  float yMin = yDraw + offset + (10.0f * scale * SCALING);
102  float xMax = xMin + width;
103  float yMax = yMin + height;
104 
105  // texture part
106  float txMin = ((float)offsets[index][0]) / 256.0f;
107  float txMax = ((float)(offsets[index][0] + offsets[index][2])) / 256.0f;
108  float tyMin = ((float)offsets[index][1]) / 256.0f;
109  float tyMax = ((float)(offsets[index][1] + offsets[index][3])) / 256.0f;
110 
111  vertices.push_back(glm::vec2(xMin, yMax));
112  vertices.push_back(glm::vec2(xMin, yMin));
113  vertices.push_back(glm::vec2(xMax, yMax));
114 
115  vertices.push_back(glm::vec2(xMax, yMin));
116  vertices.push_back(glm::vec2(xMax, yMax));
117  vertices.push_back(glm::vec2(xMin, yMin));
118 
119  uvs.push_back(glm::vec2(txMin, tyMax));
120  uvs.push_back(glm::vec2(txMin, tyMin));
121  uvs.push_back(glm::vec2(txMax, tyMax));
122 
123  uvs.push_back(glm::vec2(txMax, tyMin));
124  uvs.push_back(glm::vec2(txMax, tyMax));
125  uvs.push_back(glm::vec2(txMin, tyMin));
126 }
127 
128 unsigned int FontTRLE::widthText(float scale, std::string s) {
129  assert(mFontInit == true);
130  assert(s.length() > 0);
131 
132  unsigned int width = 0;
133  for (unsigned int i = 0; i < s.length(); i++) {
134  // index into offset table
135  int index = s[i] - '!';
136 
137  if (index == -1) // space
138  width += (unsigned int)(14.0f * scale * SCALING);
139 
140  if ((index < 0) || (index > 105))
141  continue; // skip unprintable chars
142 
143  width += (float)(offsets[index][2] + 1) * scale * SCALING; // glyph width
144  }
145 
146  return width;
147 }
148 
149 void FontTRLE::drawText(unsigned int x, unsigned int y, float scale,
150  glm::vec4 color, std::string s) {
151  assert(mFontInit == true);
152  assert(s.length() > 0);
153 
154  std::vector<glm::vec2> vertices;
155  std::vector<glm::vec2> uvs;
156  for (unsigned int i = 0; i < s.length(); i++) {
157  // index into offset table
158  int index = s[i] - '!';
159 
160  if (index == -1) // space
161  x += (unsigned int)(14.0f * scale * SCALING);
162 
163  if ((index < 0) || (index > 105))
164  continue; // skip unprintable chars
165 
166  writeChar((unsigned int)index, x, y, scale, vertices, uvs);
167  x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
168  }
169 
170  vertexBuffer.bufferData(vertices);
171  uvBuffer.bufferData(uvs);
173 }
174 
175 unsigned int FontTRLE::heightText(float scale, unsigned int maxWidth, std::string s) {
176  assert(mFontInit == true);
177  assert(s.length() > 0);
178 
179  unsigned int x = 0;
180  unsigned int yMax = 0;
181  unsigned int yReturn = 0;
182 
183  for (unsigned int i = 0; i < s.length(); i++) {
184  // index into offset table
185  int index = s[i] - '!';
186 
187  if (index == -1) // space
188  x += (unsigned int)(14.0f * scale * SCALING);
189 
190  if ((index < 0) || (index > 105))
191  continue; // skip unprintable chars
192 
193  if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
194  yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
195 
196  x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
197  if (x > maxWidth) {
198  // go to the next line
199  yReturn += yMax + 2;
200  yMax = 0;
201  x = (int)((float)(offsets[index][2] + 1) * scale * SCALING);
202  }
203  }
204 
205  return yReturn + yMax + 2;
206 }
207 
208 void FontTRLE::drawTextWrapped(unsigned int x, unsigned int y, float scale,
209  glm::vec4 color, unsigned int maxWidth, std::string s) {
210  assert(mFontInit == true);
211  assert(s.length() > 0);
212 
213  unsigned int xStart = x;
214  unsigned int yMax = 0;
215 
216  std::vector<glm::vec2> vertices;
217  std::vector<glm::vec2> uvs;
218  for (unsigned int i = 0; i < s.length(); i++) {
219  // index into offset table
220  int index = s[i] - '!';
221 
222  if (index == -1) // space
223  x += (unsigned int)(14.0f * scale * SCALING);
224 
225  if ((index < 0) || (index > 105))
226  continue; // skip unprintable chars
227 
228  if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
229  yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
230 
231  x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
232  if ((x - xStart) > maxWidth) {
233  // go to the next line
234  y += yMax + 2;
235  yMax = 0;
236  x = xStart;
237  } else {
238  x -= (int)((float)(offsets[index][2] + 1) * scale * SCALING);
239  }
240 
241  writeChar((unsigned int)index, x, y, scale, vertices, uvs);
242  x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
243  }
244 
245  vertexBuffer.bufferData(vertices);
246  uvBuffer.bufferData(uvs);
248 }
249 
250 int FontTRLE::defaultOffsets[106][5] = {
251  { 174, 52, 3, 12, -11 },
252  { 98, 58, 6, 4, -10 },
253  { 82, 26, 13, 11, -10 },
254  { 78, 38, 9, 13, -10 },
255  { 214, 13, 14, 11, -9 },
256  { 40, 26, 13, 11, -10 },
257  { 157, 57, 5, 6, -11 },
258  { 204, 39, 5, 15, -12 },
259  { 34, 40, 5, 15, -12 },
260  { 184, 59, 4, 4, -11 },
261  { 22, 40, 10, 10, -9 },
262  { 178, 59, 4, 4, -2 },
263  { 106, 60, 7, 2, -4 },
264  { 114, 60, 4, 3, -2 },
265  { 212, 38, 8, 14, -12 },
266  { 88, 49, 9, 9, -8 },
267  { 200, 55, 5, 9, -8 },
268  { 46, 52, 8, 9, -8 },
269  { 88, 38, 7, 10, -8 },
270  { 62, 40, 10, 10, -8 },
271  { 142, 48, 8, 11, -9 },
272  { 232, 50, 8, 10, -9 },
273  { 120, 47, 8, 11, -9 },
274  { 22, 51, 8, 10, -9 },
275  { 110, 49, 8, 10, -8 },
276  { 152, 57, 4, 7, -7 },
277  { 136, 57, 4, 9, -7 },
278  { 178, 40, 11, 9, -8 },
279  { 210, 53, 10, 6, -7 },
280  { 240, 40, 11, 9, -7 },
281  { 12, 39, 9, 12, -11 },
282  { 66, 13, 15, 13, -10 },
283  { 130, 13, 13, 12, -11 },
284  { 214, 25, 12, 12, -11 },
285  { 132, 35, 10, 12, -11 },
286  { 0, 26, 12, 12, -11 },
287  { 14, 26, 12, 12, -11 },
288  { 66, 27, 11, 12, -11 },
289  { 182, 27, 11, 12, -11 },
290  { 200, 13, 13, 12, -11 },
291  { 222, 54, 4, 12, -11 },
292  { 56, 52, 4, 15, -11 },
293  { 230, 15, 12, 12, -11 },
294  { 144, 35, 10, 12, -11 },
295  { 48, 13, 17, 12, -11 },
296  { 144, 13, 13, 12, -11 },
297  { 54, 26, 11, 12, -11 },
298  { 200, 26, 11, 12, -11 },
299  { 240, 0, 13, 14, -11 },
300  { 158, 13, 13, 12, -11 },
301  { 156, 35, 10, 12, -11 },
302  { 172, 13, 13, 12, -11 },
303  { 98, 13, 14, 12, -11 },
304  { 82, 13, 14, 12, -11 },
305  { 24, 13, 22, 12, -11 },
306  { 186, 13, 12, 13, -11 },
307  { 114, 13, 14, 12, -11 },
308  { 228, 28, 11, 12, -11 },
309  { 62, 60, 5, 3, -4 },
310  { 248, 59, 5, 3, -4 },
311  { 88, 59, 7, 3, -4 },
312  { 142, 60, 6, 2, -3 },
313  { 120, 59, 7, 3, -4 },
314  { 242, 59, 4, 4, -11 },
315  { 98, 49, 10, 8, -7 },
316  { 96, 35, 10, 13, -12 },
317  { 72, 52, 8, 8, -7 },
318  { 0, 39, 10, 11, -10 },
319  { 164, 52, 8, 8, -7 },
320  { 168, 38, 9, 13, -12 },
321  { 120, 35, 11, 11, -7 },
322  { 108, 35, 10, 13, -12 },
323  { 194, 27, 5, 11, -10 },
324  { 40, 51, 5, 15, -10 },
325  { 28, 26, 11, 13, -12 },
326  { 82, 52, 5, 12, -11 },
327  { 96, 26, 17, 8, -7 },
328  { 152, 48, 11, 8, -7 },
329  { 62, 51, 9, 8, -7 },
330  { 244, 15, 10, 12, -7 },
331  { 52, 39, 9, 12, -7 },
332  { 10, 52, 9, 8, -7 },
333  { 190, 52, 8, 8, -7 },
334  { 0, 51, 8, 10, -9 },
335  { 178, 50, 10, 8, -7 },
336  { 130, 48, 11, 8, -7 },
337  { 132, 26, 17, 8, -7 },
338  { 242, 50, 10, 8, -7 },
339  { 40, 38, 10, 12, -7 },
340  { 232, 41, 7, 8, -7 },
341  { 222, 41, 8, 12, -7 },
342  { 130, 57, 5, 8, -7 },
343  { 194, 39, 9, 12, -10 },
344  { 32, 56, 4, 11, -10 },
345  { 1, 14, 22, 11, -10 },
346  { 192, 0, 23, 13, -10 },
347  { 168, 0, 23, 12, -10 },
348  { 216, 0, 23, 12, -10 },
349  { 150, 26, 17, 8, -8 },
350  { 168, 26, 11, 11, -9 },
351  { 114, 26, 17, 8, -8 },
352  { 240, 28, 12, 11, -9 },
353  { 0, 0, 40, 12, -10 },
354  { 84, 0, 39, 11, -10 },
355  { 42, 0, 39, 11, -10 },
356  { 126, 0, 39, 11, -10 },
357 };
358 
#define SCALING
Definition: FontTRLE.cpp:17
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
String handling utilities.
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: FontTRLE.cpp:149
static int initialize(std::string font)
Definition: FontTRLE.cpp:28
static bool mFontInit
Definition: FontTRLE.h:40
OpenGL Shader Implementation.
static unsigned int widthText(float scale, std::string s)
Definition: FontTRLE.cpp:128
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: FontTRLE.cpp:175
static void loadLPS(std::string f)
Definition: FontTRLE.cpp:62
Included everywhere.
static void shutdown()
Definition: FontTRLE.cpp:25
bool stringEndsWith(std::string s, std::string suffix, bool casesensitive=false)
Definition: strings.cpp:32
#define assert(x)
Definition: global.h:124
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
Definition: Shader.cpp:278
static ShaderBuffer vertexBuffer
Definition: FontTRLE.h:47
static unsigned int mFontTexture
Definition: FontTRLE.h:41
static int offsets[106][5]
Definition: FontTRLE.h:44
static int defaultOffsets[106][5]
Definition: FontTRLE.h:45
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: FontTRLE.cpp:208
std::string findAndReplace(std::string s, std::string find, std::string replace)
Definition: strings.cpp:14
Texture Registry.
static const float scale
Definition: Sprite.cpp:15
static ShaderBuffer uvBuffer
Definition: FontTRLE.h:48
static void writeChar(unsigned int index, unsigned int xDraw, unsigned int yDraw, float scale, std::vector< glm::vec2 > &vertices, std::vector< glm::vec2 > &uvs)
Definition: FontTRLE.cpp:91
static int loadBufferSlot(unsigned char *image=nullptr, unsigned int width=256, unsigned int height=256, ColorMode mode=ColorMode::RGBA, unsigned int bpp=32, TextureStorage s=TextureStorage::GAME, int slot=-1, bool filter=true)
Loads Buffer as texture.
Tomb Raider Level Editor Font loader.