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.4KB

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