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.

FontSDL.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*!
  2. * \file src/FontSDL.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "FontSDL.h"
  9. FontSDL::FontSDL() {
  10. mFont = NULL;
  11. mFontInit = false;
  12. mFontName = NULL;
  13. mFontTexture = 0;
  14. tempText.text = new char[256];
  15. tempText.color[0] = 0xFF;
  16. tempText.color[1] = 0xFF;
  17. tempText.color[2] = 0xFF;
  18. tempText.color[3] = 0xFF;
  19. tempText.scale = 1.2f;
  20. tempText.w = 0;
  21. tempText.h = 0;
  22. }
  23. FontSDL::~FontSDL() {
  24. if (mFont)
  25. TTF_CloseFont(mFont);
  26. if (mFontInit)
  27. TTF_Quit();
  28. if (tempText.text)
  29. delete [] tempText.text;
  30. }
  31. int FontSDL::initialize() {
  32. assert(mFontInit == false);
  33. assert(mFontName != NULL);
  34. assert(mFontName[0] != '\0');
  35. if (TTF_Init() != 0) {
  36. printf("Could not initialize SDL-TTF!\n");
  37. return -1;
  38. }
  39. mFont = TTF_OpenFont(mFontName, 24);
  40. if (mFont == NULL) {
  41. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  42. return -2;
  43. }
  44. glGenTextures(1, &mFontTexture);
  45. mFontInit = true;
  46. return 0;
  47. }
  48. void FontSDL::writeString(FontString &s) {
  49. assert(s.text != NULL);
  50. SDL_Color color;
  51. color.r = (unsigned char)(s.color[0] * 255.0f);
  52. color.g = (unsigned char)(s.color[1] * 255.0f);
  53. color.b = (unsigned char)(s.color[2] * 255.0f);
  54. color.a = (unsigned char)(s.color[3] * 255.0f);
  55. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.text, color);
  56. if (surface == NULL) {
  57. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  58. return;
  59. }
  60. s.w = (int)((float)surface->w * s.scale);
  61. s.h = (int)((float)surface->h * s.scale);
  62. GLenum textureFormat;
  63. if (surface->format->BytesPerPixel == 4) {
  64. if (surface->format->Rmask == 0x000000FF)
  65. textureFormat = GL_RGBA;
  66. else
  67. textureFormat = GL_BGRA_EXT;
  68. } else {
  69. if (surface->format->Rmask == 0x000000FF)
  70. textureFormat = GL_RGB;
  71. else
  72. textureFormat = GL_BGR_EXT;
  73. }
  74. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  77. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  78. GLuint xMin = s.x;
  79. GLuint yMin = s.y;
  80. GLuint xMax = xMin + s.w;
  81. GLuint yMax = yMin + s.h;
  82. glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
  83. glBegin(GL_QUADS);
  84. glTexCoord2f(0.0f, 0.0f);
  85. glVertex2i(xMin, yMin);
  86. glTexCoord2f(0.0f, 1.0f);
  87. glVertex2i(xMin, yMax);
  88. glTexCoord2f(1.0f, 1.0f);
  89. glVertex2i(xMax, yMax);
  90. glTexCoord2f(1.0f, 0.0f);
  91. glVertex2i(xMax, yMin);
  92. glEnd();
  93. SDL_FreeSurface(surface);
  94. }
  95. void FontSDL::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
  96. va_list args;
  97. va_start(args, s);
  98. vsnprintf(tempText.text, 256, s, args);
  99. tempText.text[255] = '\0';
  100. va_end(args);
  101. tempText.scale = scale;
  102. tempText.x = x;
  103. tempText.y = y;
  104. if (color) {
  105. tempText.color[0] = color[0];
  106. tempText.color[1] = color[1];
  107. tempText.color[2] = color[2];
  108. tempText.color[3] = color[3];
  109. }
  110. writeString(tempText);
  111. }