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 3.6KB

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