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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*!
  2. * \file src/FontTRLE.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <fstream>
  8. #include "global.h"
  9. #include "utils/strings.h"
  10. #include "FontTRLE.h"
  11. FontTRLE::FontTRLE() {
  12. mFontInit = false;
  13. mFontName = NULL;
  14. mFontTexture = 0;
  15. // TRLE defaults
  16. resolution1 = 240;
  17. resolution2 = 512;
  18. ruler1 = -16;
  19. ruler2 = -40;
  20. baselineAbs = 0; // always zero?
  21. spacing = 0.075f;
  22. squashedTextFactor = 0.75f;
  23. // offset table default is set in header
  24. tempText.text = new char[256];
  25. tempText.color[0] = 0xFF;
  26. tempText.color[1] = 0xFF;
  27. tempText.color[2] = 0xFF;
  28. tempText.color[3] = 0xFF;
  29. tempText.scale = 1.2f;
  30. tempText.w = 0;
  31. tempText.h = 0;
  32. }
  33. FontTRLE::~FontTRLE() {
  34. delete [] tempText.text;
  35. tempText.text = NULL;
  36. }
  37. int FontTRLE::initialize() {
  38. // TODO font coloring not working when .pc has color?!?!
  39. // tmp debug
  40. delete [] mFontName;
  41. mFontName = bufferString("/Users/thomas/Downloads/TR Fonts/TR4 Official/Font.pc");
  42. //mFontName = bufferString("/Users/thomas/Downloads/TR Fonts/TR2 Web/Font.pc");
  43. assert(mFontInit == false);
  44. assert(mFontName != NULL);
  45. assert(mFontName[0] != '\0');
  46. assert(stringEndsWith(mFontName, ".pc") == true);
  47. // Load .pc file...
  48. std::ifstream file(mFontName, std::ios::in | std::ios::binary);
  49. unsigned char *pixels = new unsigned char[256 * 256 * 4];
  50. if (!file.read((char *)pixels, 256 * 256 * 4)) {
  51. delete [] pixels;
  52. return -1;
  53. }
  54. // ...into GL texture
  55. glGenTextures(1, &mFontTexture);
  56. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  59. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
  60. delete [] pixels;
  61. // Try to load .lps file, overwriting default glyph positions
  62. char *lpsFile = stringReplace(mFontName, ".pc", ".lps");
  63. loadLPS(lpsFile);
  64. delete [] lpsFile;
  65. mFontInit = true;
  66. return 0;
  67. }
  68. void FontTRLE::loadLPS(const char *file) {
  69. // TODO load lps file
  70. // if baselineAbs != 0
  71. // all offset[4] + baselineAbs;
  72. }
  73. #define SCALING 2.0f
  74. void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, FontString &s) {
  75. int width = (int)(((vec_t)offsets[index][2]) * s.scale * SCALING);
  76. int height = (int)(((vec_t)offsets[index][3]) * s.scale * SCALING);
  77. int offset = (int)(((vec_t)offsets[index][4]) * s.scale * SCALING);
  78. // screen coordinates
  79. int xMin = xDraw;
  80. int yMin = s.y + offset + (int)(10.0f * s.scale * SCALING);
  81. int xMax = xMin + width;
  82. int yMax = yMin + height;
  83. // texture part
  84. vec_t txMin = ((vec_t)offsets[index][0]) / 256.0f;
  85. vec_t txMax = ((vec_t)(offsets[index][0] + offsets[index][2])) / 256.0f;
  86. vec_t tyMin = ((vec_t)offsets[index][1]) / 256.0f;
  87. vec_t tyMax = ((vec_t)(offsets[index][1] + offsets[index][3])) / 256.0f;
  88. // draw
  89. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  90. glColor4f(s.color[0], s.color[1], s.color[2], s.color[3]);
  91. glBegin(GL_QUADS);
  92. glTexCoord2f(txMin, tyMin);
  93. glVertex2i(xMin, yMin);
  94. glTexCoord2f(txMin, tyMax);
  95. glVertex2i(xMin, yMax);
  96. glTexCoord2f(txMax, tyMax);
  97. glVertex2i(xMax, yMax);
  98. glTexCoord2f(txMax, tyMin);
  99. glVertex2i(xMax, yMin);
  100. glEnd();
  101. }
  102. void FontTRLE::writeString(FontString &s) {
  103. assert(mFontInit == true);
  104. assert(s.text != NULL);
  105. unsigned int x = s.x;
  106. for (unsigned int i = 0; s.text[i] != '\0'; i++) {
  107. // index into offset table
  108. int index = s.text[i] - '!';
  109. if (index == -1) // space
  110. x += (unsigned int)(14.0f * s.scale * SCALING);
  111. if ((index < 0) || (index > 105))
  112. continue; // skip unprintable chars
  113. writeChar((unsigned int)index, x, s);
  114. x += (int)((vec_t)(offsets[index][2] + 1) * s.scale * SCALING); // width
  115. }
  116. // TODO scaling?!
  117. s.w = x;
  118. /*
  119. s.w = (int)((float)surface->w * s.scale * SCALING);
  120. s.h = (int)((float)surface->h * s.scale * SCALING);
  121. */
  122. }
  123. void FontTRLE::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
  124. va_list args;
  125. va_start(args, s);
  126. vsnprintf(tempText.text, 256, s, args);
  127. tempText.text[255] = '\0';
  128. va_end(args);
  129. tempText.scale = scale;
  130. tempText.x = x;
  131. tempText.y = y;
  132. if (color) {
  133. tempText.color[0] = color[0];
  134. tempText.color[1] = color[1];
  135. tempText.color[2] = color[2];
  136. tempText.color[3] = color[3];
  137. }
  138. writeString(tempText);
  139. }