Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontTRLE.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*!
  2. * \file src/FontTRLE.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <stdexcept>
  11. #include "global.h"
  12. #include "TextureManager.h"
  13. #include "utils/strings.h"
  14. #include "system/Window.h"
  15. #include "system/FontTRLE.h"
  16. #define SCALING 2.0f
  17. bool FontTRLE::mFontInit = false;
  18. unsigned int FontTRLE::mFontTexture = 0;
  19. int FontTRLE::offsets[106][5];
  20. void FontTRLE::shutdown() {
  21. }
  22. int FontTRLE::initialize(std::string font) {
  23. assert(stringEndsWith(font, ".pc") == true);
  24. shutdown();
  25. // Load .pc file...
  26. std::ifstream file(font, std::ios::in | std::ios::binary);
  27. unsigned char* pixels = new unsigned char[256 * 256 * 4];
  28. if (!file.read((char*)pixels, 256 * 256 * 4)) {
  29. delete [] pixels;
  30. return -1;
  31. }
  32. // Fix coloring
  33. for (unsigned int i = 0; i < (256 * 256 * 4); i += 4) {
  34. float y = (0.2126f * pixels[i + 2]);
  35. y += (0.7152f * pixels[i + 1]);
  36. y += (0.0722f * pixels[i]);
  37. pixels[i] = pixels[i + 1] = pixels[i + 2] = (unsigned char)y;
  38. }
  39. mFontTexture = getTextureManager().loadBufferSlot(pixels, 256, 256,
  40. TextureManager::ColorMode::BGRA, 32, TextureManager::TextureStorage::SYSTEM);
  41. delete [] pixels;
  42. // Try to load .lps file or use default glyph positions
  43. std::string lpsFile = findAndReplace(font, ".pc", ".lps");
  44. loadLPS(lpsFile);
  45. mFontInit = true;
  46. return 0;
  47. }
  48. void FontTRLE::loadLPS(std::string f) {
  49. std::ifstream file(f);
  50. if (!file) {
  51. std::copy(&defaultOffsets[0][0], &defaultOffsets[0][0] + (106 * 5), &offsets[0][0]);
  52. return;
  53. }
  54. for (std::string line; std::getline(file, line);) {
  55. std::istringstream stream(line);
  56. int index;
  57. stream >> index;
  58. if (stream.get() != '=')
  59. continue;
  60. stream >> offsets[index][0];
  61. if (stream.get() != ',')
  62. continue;
  63. stream >> offsets[index][1];
  64. if (stream.get() != ',')
  65. continue;
  66. stream >> offsets[index][2];
  67. if (stream.get() != ',')
  68. continue;
  69. stream >> offsets[index][3];
  70. if (stream.get() != ',')
  71. continue;
  72. stream >> offsets[index][4];
  73. }
  74. }
  75. void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, unsigned int yDraw, float scale,
  76. std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs) {
  77. assert(mFontInit == true);
  78. float width = ((float)offsets[index][2]) * scale * SCALING;
  79. float height = ((float)offsets[index][3]) * scale * SCALING;
  80. float offset = ((float)offsets[index][4]) * scale * SCALING;
  81. // screen coordinates
  82. float xMin = xDraw;
  83. float yMin = yDraw + offset + (10.0f * scale * SCALING);
  84. float xMax = xMin + width;
  85. float yMax = yMin + height;
  86. // texture part
  87. float txMin = ((float)offsets[index][0]) / 256.0f;
  88. float txMax = ((float)(offsets[index][0] + offsets[index][2])) / 256.0f;
  89. float tyMin = ((float)offsets[index][1]) / 256.0f;
  90. float tyMax = ((float)(offsets[index][1] + offsets[index][3])) / 256.0f;
  91. vertices.push_back(glm::vec2(xMin, yMax));
  92. vertices.push_back(glm::vec2(xMin, yMin));
  93. vertices.push_back(glm::vec2(xMax, yMax));
  94. vertices.push_back(glm::vec2(xMax, yMin));
  95. vertices.push_back(glm::vec2(xMax, yMax));
  96. vertices.push_back(glm::vec2(xMin, yMin));
  97. uvs.push_back(glm::vec2(txMin, tyMax));
  98. uvs.push_back(glm::vec2(txMin, tyMin));
  99. uvs.push_back(glm::vec2(txMax, tyMax));
  100. uvs.push_back(glm::vec2(txMax, tyMin));
  101. uvs.push_back(glm::vec2(txMax, tyMax));
  102. uvs.push_back(glm::vec2(txMin, tyMin));
  103. }
  104. unsigned int FontTRLE::widthText(float scale, std::string s) {
  105. assert(mFontInit == true);
  106. assert(s.length() > 0);
  107. unsigned int width = 0;
  108. for (unsigned int i = 0; i < s.length(); i++) {
  109. // index into offset table
  110. int index = s[i] - '!';
  111. if (index == -1) // space
  112. width += (unsigned int)(14.0f * scale * SCALING);
  113. if ((index < 0) || (index > 105))
  114. continue; // skip unprintable chars
  115. width += (float)(offsets[index][2] + 1) * scale * SCALING; // glyph width
  116. }
  117. return width;
  118. }
  119. void FontTRLE::drawText(unsigned int x, unsigned int y, float scale,
  120. const unsigned char color[4], std::string s) {
  121. assert(mFontInit == true);
  122. assert(s.length() > 0);
  123. std::vector<glm::vec2> vertices;
  124. std::vector<glm::vec2> uvs;
  125. for (unsigned int i = 0; i < s.length(); i++) {
  126. // index into offset table
  127. int index = s[i] - '!';
  128. if (index == -1) // space
  129. x += (unsigned int)(14.0f * scale * SCALING);
  130. if ((index < 0) || (index > 105))
  131. continue; // skip unprintable chars
  132. writeChar((unsigned int)index, x, y, scale, vertices, uvs);
  133. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  134. }
  135. glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
  136. Window::drawTextGL(vertices, uvs, col, mFontTexture);
  137. }
  138. unsigned int FontTRLE::heightText(float scale, unsigned int maxWidth, std::string s) {
  139. assert(mFontInit == true);
  140. assert(s.length() > 0);
  141. unsigned int x = 0;
  142. unsigned int yMax = 0;
  143. unsigned int yReturn = 0;
  144. for (unsigned int i = 0; i < s.length(); i++) {
  145. // index into offset table
  146. int index = s[i] - '!';
  147. if (index == -1) // space
  148. x += (unsigned int)(14.0f * scale * SCALING);
  149. if ((index < 0) || (index > 105))
  150. continue; // skip unprintable chars
  151. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  152. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  153. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  154. if (x > maxWidth) {
  155. // go to the next line
  156. yReturn += yMax + 2;
  157. yMax = 0;
  158. x = (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  159. }
  160. }
  161. return yReturn + yMax + 2;
  162. }
  163. void FontTRLE::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  164. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  165. assert(mFontInit == true);
  166. assert(s.length() > 0);
  167. unsigned int xStart = x;
  168. unsigned int yMax = 0;
  169. std::vector<glm::vec2> vertices;
  170. std::vector<glm::vec2> uvs;
  171. for (unsigned int i = 0; i < s.length(); i++) {
  172. // index into offset table
  173. int index = s[i] - '!';
  174. if (index == -1) // space
  175. x += (unsigned int)(14.0f * scale * SCALING);
  176. if ((index < 0) || (index > 105))
  177. continue; // skip unprintable chars
  178. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  179. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  180. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  181. if ((x - xStart) > maxWidth) {
  182. // go to the next line
  183. y += yMax + 2;
  184. yMax = 0;
  185. x = xStart;
  186. } else {
  187. x -= (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  188. }
  189. writeChar((unsigned int)index, x, y, scale, vertices, uvs);
  190. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  191. }
  192. glm::vec4 col(color[0] / 256.0f, color[1] / 256.0f, color[2] / 256.0f, color[3] / 256.0f);
  193. Window::drawTextGL(vertices, uvs, col, mFontTexture);
  194. }
  195. int FontTRLE::defaultOffsets[106][5] = {
  196. { 174, 52, 3, 12, -11 },
  197. { 98, 58, 6, 4, -10 },
  198. { 82, 26, 13, 11, -10 },
  199. { 78, 38, 9, 13, -10 },
  200. { 214, 13, 14, 11, -9 },
  201. { 40, 26, 13, 11, -10 },
  202. { 157, 57, 5, 6, -11 },
  203. { 204, 39, 5, 15, -12 },
  204. { 34, 40, 5, 15, -12 },
  205. { 184, 59, 4, 4, -11 },
  206. { 22, 40, 10, 10, -9 },
  207. { 178, 59, 4, 4, -2 },
  208. { 106, 60, 7, 2, -4 },
  209. { 114, 60, 4, 3, -2 },
  210. { 212, 38, 8, 14, -12 },
  211. { 88, 49, 9, 9, -8 },
  212. { 200, 55, 5, 9, -8 },
  213. { 46, 52, 8, 9, -8 },
  214. { 88, 38, 7, 10, -8 },
  215. { 62, 40, 10, 10, -8 },
  216. { 142, 48, 8, 11, -9 },
  217. { 232, 50, 8, 10, -9 },
  218. { 120, 47, 8, 11, -9 },
  219. { 22, 51, 8, 10, -9 },
  220. { 110, 49, 8, 10, -8 },
  221. { 152, 57, 4, 7, -7 },
  222. { 136, 57, 4, 9, -7 },
  223. { 178, 40, 11, 9, -8 },
  224. { 210, 53, 10, 6, -7 },
  225. { 240, 40, 11, 9, -7 },
  226. { 12, 39, 9, 12, -11 },
  227. { 66, 13, 15, 13, -10 },
  228. { 130, 13, 13, 12, -11 },
  229. { 214, 25, 12, 12, -11 },
  230. { 132, 35, 10, 12, -11 },
  231. { 0, 26, 12, 12, -11 },
  232. { 14, 26, 12, 12, -11 },
  233. { 66, 27, 11, 12, -11 },
  234. { 182, 27, 11, 12, -11 },
  235. { 200, 13, 13, 12, -11 },
  236. { 222, 54, 4, 12, -11 },
  237. { 56, 52, 4, 15, -11 },
  238. { 230, 15, 12, 12, -11 },
  239. { 144, 35, 10, 12, -11 },
  240. { 48, 13, 17, 12, -11 },
  241. { 144, 13, 13, 12, -11 },
  242. { 54, 26, 11, 12, -11 },
  243. { 200, 26, 11, 12, -11 },
  244. { 240, 0, 13, 14, -11 },
  245. { 158, 13, 13, 12, -11 },
  246. { 156, 35, 10, 12, -11 },
  247. { 172, 13, 13, 12, -11 },
  248. { 98, 13, 14, 12, -11 },
  249. { 82, 13, 14, 12, -11 },
  250. { 24, 13, 22, 12, -11 },
  251. { 186, 13, 12, 13, -11 },
  252. { 114, 13, 14, 12, -11 },
  253. { 228, 28, 11, 12, -11 },
  254. { 62, 60, 5, 3, -4 },
  255. { 248, 59, 5, 3, -4 },
  256. { 88, 59, 7, 3, -4 },
  257. { 142, 60, 6, 2, -3 },
  258. { 120, 59, 7, 3, -4 },
  259. { 242, 59, 4, 4, -11 },
  260. { 98, 49, 10, 8, -7 },
  261. { 96, 35, 10, 13, -12 },
  262. { 72, 52, 8, 8, -7 },
  263. { 0, 39, 10, 11, -10 },
  264. { 164, 52, 8, 8, -7 },
  265. { 168, 38, 9, 13, -12 },
  266. { 120, 35, 11, 11, -7 },
  267. { 108, 35, 10, 13, -12 },
  268. { 194, 27, 5, 11, -10 },
  269. { 40, 51, 5, 15, -10 },
  270. { 28, 26, 11, 13, -12 },
  271. { 82, 52, 5, 12, -11 },
  272. { 96, 26, 17, 8, -7 },
  273. { 152, 48, 11, 8, -7 },
  274. { 62, 51, 9, 8, -7 },
  275. { 244, 15, 10, 12, -7 },
  276. { 52, 39, 9, 12, -7 },
  277. { 10, 52, 9, 8, -7 },
  278. { 190, 52, 8, 8, -7 },
  279. { 0, 51, 8, 10, -9 },
  280. { 178, 50, 10, 8, -7 },
  281. { 130, 48, 11, 8, -7 },
  282. { 132, 26, 17, 8, -7 },
  283. { 242, 50, 10, 8, -7 },
  284. { 40, 38, 10, 12, -7 },
  285. { 232, 41, 7, 8, -7 },
  286. { 222, 41, 8, 12, -7 },
  287. { 130, 57, 5, 8, -7 },
  288. { 194, 39, 9, 12, -10 },
  289. { 32, 56, 4, 11, -10 },
  290. { 1, 14, 22, 11, -10 },
  291. { 192, 0, 23, 13, -10 },
  292. { 168, 0, 23, 12, -10 },
  293. { 216, 0, 23, 12, -10 },
  294. { 150, 26, 17, 8, -8 },
  295. { 168, 26, 11, 11, -9 },
  296. { 114, 26, 17, 8, -8 },
  297. { 240, 28, 12, 11, -9 },
  298. { 0, 0, 40, 12, -10 },
  299. { 84, 0, 39, 11, -10 },
  300. { 42, 0, 39, 11, -10 },
  301. { 126, 0, 39, 11, -10 },
  302. };