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

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