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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*!
  2. * \file src/FontSDL.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <stdio.h>
  8. #include "global.h"
  9. #include "FontSDL.h"
  10. FontSDL::FontSDL() {
  11. mFont = NULL;
  12. mFontInit = false;
  13. mFontName = NULL;
  14. mFontTexture = 0;
  15. }
  16. FontSDL::~FontSDL() {
  17. if (mFont)
  18. TTF_CloseFont(mFont);
  19. if (mFontInit) {
  20. TTF_Quit();
  21. glDeleteTextures(1, &mFontTexture);
  22. }
  23. }
  24. int FontSDL::initialize() {
  25. assert(mFontInit == false);
  26. assert(mFontName != NULL);
  27. assert(mFontName[0] != '\0');
  28. if (TTF_Init() != 0) {
  29. printf("Could not initialize SDL-TTF!\n");
  30. return -1;
  31. }
  32. mFont = TTF_OpenFont(mFontName, 24);
  33. if (mFont == NULL) {
  34. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  35. return -2;
  36. }
  37. glGenTextures(1, &mFontTexture);
  38. mFontInit = true;
  39. return 0;
  40. }
  41. unsigned int FontSDL::widthText(float scale, std::string s) {
  42. assert(mFontInit == true);
  43. int w;
  44. if (TTF_SizeUTF8(mFont, s.c_str(), &w, NULL) != 0) {
  45. printf("TTF_SizeUTF8 Error: %s\n", TTF_GetError());
  46. return s.length() * 15 * scale;
  47. }
  48. return w * scale;
  49. }
  50. void FontSDL::drawText(unsigned int x, unsigned int y, float scale,
  51. const unsigned char color[4], std::string s) {
  52. assert(mFontInit == true);
  53. assert(s.length() > 0);
  54. SDL_Color col;
  55. col.r = color[0];
  56. col.g = color[1];
  57. col.b = color[2];
  58. col.a = color[3];
  59. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.c_str(), col);
  60. if (surface == NULL) {
  61. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  62. return;
  63. }
  64. int w = (int)((float)surface->w * scale);
  65. int h = (int)((float)surface->h * scale);
  66. GLenum textureFormat;
  67. if (surface->format->BytesPerPixel == 4) {
  68. if (surface->format->Rmask == 0x000000FF)
  69. textureFormat = GL_RGBA;
  70. else
  71. textureFormat = GL_BGRA_EXT;
  72. } else {
  73. if (surface->format->Rmask == 0x000000FF)
  74. textureFormat = GL_RGB;
  75. else
  76. textureFormat = GL_BGR_EXT;
  77. }
  78. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  79. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  80. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  81. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  82. SDL_FreeSurface(surface);
  83. GLuint xMin = x;
  84. GLuint yMin = y;
  85. GLuint xMax = xMin + w;
  86. GLuint yMax = yMin + h;
  87. glColor4ubv(color);
  88. glBegin(GL_QUADS);
  89. glTexCoord2f(0.0f, 0.0f);
  90. glVertex2i(xMin, yMin);
  91. glTexCoord2f(0.0f, 1.0f);
  92. glVertex2i(xMin, yMax);
  93. glTexCoord2f(1.0f, 1.0f);
  94. glVertex2i(xMax, yMax);
  95. glTexCoord2f(1.0f, 0.0f);
  96. glVertex2i(xMax, yMin);
  97. glEnd();
  98. }
  99. unsigned int FontSDL::heightText(float scale, unsigned int maxWidth, std::string s) {
  100. assert(mFontInit == true);
  101. assert(s.length() > 0);
  102. assert(maxWidth > 0);
  103. SDL_Color col;
  104. SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  105. if (surface == NULL) {
  106. printf("TTF_RenderUTF8_Blended_Wrapped Error: %s\n", TTF_GetError());
  107. return 0;
  108. }
  109. int h = surface->h * scale;
  110. SDL_FreeSurface(surface);
  111. return h;
  112. }
  113. void FontSDL::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  114. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  115. assert(mFontInit == true);
  116. assert(s.length() > 0);
  117. assert(maxWidth > 0);
  118. SDL_Color col;
  119. col.r = color[0];
  120. col.g = color[1];
  121. col.b = color[2];
  122. col.a = color[3];
  123. SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  124. if (surface == NULL) {
  125. printf("TTF_RenderUTF8_Blended_Wrapped Error: %s\n", TTF_GetError());
  126. return;
  127. }
  128. int w = (int)((float)surface->w * scale);
  129. int h = (int)((float)surface->h * scale);
  130. GLenum textureFormat;
  131. if (surface->format->BytesPerPixel == 4) {
  132. if (surface->format->Rmask == 0x000000FF)
  133. textureFormat = GL_RGBA;
  134. else
  135. textureFormat = GL_BGRA_EXT;
  136. } else {
  137. if (surface->format->Rmask == 0x000000FF)
  138. textureFormat = GL_RGB;
  139. else
  140. textureFormat = GL_BGR_EXT;
  141. }
  142. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  143. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  145. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  146. SDL_FreeSurface(surface);
  147. GLuint xMin = x;
  148. GLuint yMin = y;
  149. GLuint xMax = xMin + w;
  150. GLuint yMax = yMin + h;
  151. glColor4ubv(color);
  152. glBegin(GL_QUADS);
  153. glTexCoord2f(0.0f, 0.0f);
  154. glVertex2i(xMin, yMin);
  155. glTexCoord2f(0.0f, 1.0f);
  156. glVertex2i(xMin, yMax);
  157. glTexCoord2f(1.0f, 1.0f);
  158. glVertex2i(xMax, yMax);
  159. glTexCoord2f(1.0f, 0.0f);
  160. glVertex2i(xMax, yMin);
  161. glEnd();
  162. }