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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. orAssert(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. orAssert(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. orAssert(mFontInit == true);
  107. orAssert(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. glm::vec4 color, std::string s) {
  122. orAssert(mFontInit == true);
  123. orAssert(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. Shader::drawGL(vertexBuffer, uvBuffer, color, mFontTexture);
  139. }
  140. unsigned int FontTRLE::heightText(float scale, unsigned int maxWidth, std::string s) {
  141. orAssert(mFontInit == true);
  142. orAssert(s.length() > 0);
  143. unsigned int x = 0;
  144. unsigned int yMax = 0;
  145. unsigned int yReturn = 0;
  146. for (unsigned int i = 0; i < s.length(); i++) {
  147. // index into offset table
  148. int index = s[i] - '!';
  149. if (index == -1) // space
  150. x += (unsigned int)(14.0f * scale * SCALING);
  151. if ((index < 0) || (index > 105))
  152. continue; // skip unprintable chars
  153. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  154. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  155. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  156. if (x > maxWidth) {
  157. // go to the next line
  158. yReturn += yMax + 2;
  159. yMax = 0;
  160. x = (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  161. }
  162. }
  163. return yReturn + yMax + 2;
  164. }
  165. void FontTRLE::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  166. glm::vec4 color, unsigned int maxWidth, std::string s) {
  167. orAssert(mFontInit == true);
  168. orAssert(s.length() > 0);
  169. unsigned int xStart = x;
  170. unsigned int yMax = 0;
  171. std::vector<glm::vec2> vertices;
  172. std::vector<glm::vec2> uvs;
  173. for (unsigned int i = 0; i < s.length(); i++) {
  174. // index into offset table
  175. int index = s[i] - '!';
  176. if (index == -1) // space
  177. x += (unsigned int)(14.0f * scale * SCALING);
  178. if ((index < 0) || (index > 105))
  179. continue; // skip unprintable chars
  180. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  181. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  182. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  183. if ((x - xStart) > maxWidth) {
  184. // go to the next line
  185. y += yMax + 2;
  186. yMax = 0;
  187. x = xStart;
  188. } else {
  189. x -= (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  190. }
  191. writeChar((unsigned int)index, x, y, scale, vertices, uvs);
  192. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  193. }
  194. vertexBuffer.bufferData(vertices);
  195. uvBuffer.bufferData(uvs);
  196. Shader::drawGL(vertexBuffer, uvBuffer, color, mFontTexture);
  197. }
  198. int FontTRLE::defaultOffsets[106][5] = {
  199. { 174, 52, 3, 12, -11 },
  200. { 98, 58, 6, 4, -10 },
  201. { 82, 26, 13, 11, -10 },
  202. { 78, 38, 9, 13, -10 },
  203. { 214, 13, 14, 11, -9 },
  204. { 40, 26, 13, 11, -10 },
  205. { 157, 57, 5, 6, -11 },
  206. { 204, 39, 5, 15, -12 },
  207. { 34, 40, 5, 15, -12 },
  208. { 184, 59, 4, 4, -11 },
  209. { 22, 40, 10, 10, -9 },
  210. { 178, 59, 4, 4, -2 },
  211. { 106, 60, 7, 2, -4 },
  212. { 114, 60, 4, 3, -2 },
  213. { 212, 38, 8, 14, -12 },
  214. { 88, 49, 9, 9, -8 },
  215. { 200, 55, 5, 9, -8 },
  216. { 46, 52, 8, 9, -8 },
  217. { 88, 38, 7, 10, -8 },
  218. { 62, 40, 10, 10, -8 },
  219. { 142, 48, 8, 11, -9 },
  220. { 232, 50, 8, 10, -9 },
  221. { 120, 47, 8, 11, -9 },
  222. { 22, 51, 8, 10, -9 },
  223. { 110, 49, 8, 10, -8 },
  224. { 152, 57, 4, 7, -7 },
  225. { 136, 57, 4, 9, -7 },
  226. { 178, 40, 11, 9, -8 },
  227. { 210, 53, 10, 6, -7 },
  228. { 240, 40, 11, 9, -7 },
  229. { 12, 39, 9, 12, -11 },
  230. { 66, 13, 15, 13, -10 },
  231. { 130, 13, 13, 12, -11 },
  232. { 214, 25, 12, 12, -11 },
  233. { 132, 35, 10, 12, -11 },
  234. { 0, 26, 12, 12, -11 },
  235. { 14, 26, 12, 12, -11 },
  236. { 66, 27, 11, 12, -11 },
  237. { 182, 27, 11, 12, -11 },
  238. { 200, 13, 13, 12, -11 },
  239. { 222, 54, 4, 12, -11 },
  240. { 56, 52, 4, 15, -11 },
  241. { 230, 15, 12, 12, -11 },
  242. { 144, 35, 10, 12, -11 },
  243. { 48, 13, 17, 12, -11 },
  244. { 144, 13, 13, 12, -11 },
  245. { 54, 26, 11, 12, -11 },
  246. { 200, 26, 11, 12, -11 },
  247. { 240, 0, 13, 14, -11 },
  248. { 158, 13, 13, 12, -11 },
  249. { 156, 35, 10, 12, -11 },
  250. { 172, 13, 13, 12, -11 },
  251. { 98, 13, 14, 12, -11 },
  252. { 82, 13, 14, 12, -11 },
  253. { 24, 13, 22, 12, -11 },
  254. { 186, 13, 12, 13, -11 },
  255. { 114, 13, 14, 12, -11 },
  256. { 228, 28, 11, 12, -11 },
  257. { 62, 60, 5, 3, -4 },
  258. { 248, 59, 5, 3, -4 },
  259. { 88, 59, 7, 3, -4 },
  260. { 142, 60, 6, 2, -3 },
  261. { 120, 59, 7, 3, -4 },
  262. { 242, 59, 4, 4, -11 },
  263. { 98, 49, 10, 8, -7 },
  264. { 96, 35, 10, 13, -12 },
  265. { 72, 52, 8, 8, -7 },
  266. { 0, 39, 10, 11, -10 },
  267. { 164, 52, 8, 8, -7 },
  268. { 168, 38, 9, 13, -12 },
  269. { 120, 35, 11, 11, -7 },
  270. { 108, 35, 10, 13, -12 },
  271. { 194, 27, 5, 11, -10 },
  272. { 40, 51, 5, 15, -10 },
  273. { 28, 26, 11, 13, -12 },
  274. { 82, 52, 5, 12, -11 },
  275. { 96, 26, 17, 8, -7 },
  276. { 152, 48, 11, 8, -7 },
  277. { 62, 51, 9, 8, -7 },
  278. { 244, 15, 10, 12, -7 },
  279. { 52, 39, 9, 12, -7 },
  280. { 10, 52, 9, 8, -7 },
  281. { 190, 52, 8, 8, -7 },
  282. { 0, 51, 8, 10, -9 },
  283. { 178, 50, 10, 8, -7 },
  284. { 130, 48, 11, 8, -7 },
  285. { 132, 26, 17, 8, -7 },
  286. { 242, 50, 10, 8, -7 },
  287. { 40, 38, 10, 12, -7 },
  288. { 232, 41, 7, 8, -7 },
  289. { 222, 41, 8, 12, -7 },
  290. { 130, 57, 5, 8, -7 },
  291. { 194, 39, 9, 12, -10 },
  292. { 32, 56, 4, 11, -10 },
  293. { 1, 14, 22, 11, -10 },
  294. { 192, 0, 23, 13, -10 },
  295. { 168, 0, 23, 12, -10 },
  296. { 216, 0, 23, 12, -10 },
  297. { 150, 26, 17, 8, -8 },
  298. { 168, 26, 11, 11, -9 },
  299. { 114, 26, 17, 8, -8 },
  300. { 240, 28, 12, 11, -9 },
  301. { 0, 0, 40, 12, -10 },
  302. { 84, 0, 39, 11, -10 },
  303. { 42, 0, 39, 11, -10 },
  304. { 126, 0, 39, 11, -10 },
  305. };