Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FontTRLE.cpp 11KB

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