Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FontSDL.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "system/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,
  84. textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  85. SDL_FreeSurface(surface);
  86. GLuint xMin = x;
  87. GLuint yMin = y;
  88. GLuint xMax = xMin + w;
  89. GLuint yMax = yMin + h;
  90. glColor4ubv(color);
  91. glBegin(GL_QUADS);
  92. glTexCoord2f(0.0f, 0.0f);
  93. glVertex2i(xMin, yMin);
  94. glTexCoord2f(0.0f, 1.0f);
  95. glVertex2i(xMin, yMax);
  96. glTexCoord2f(1.0f, 1.0f);
  97. glVertex2i(xMax, yMax);
  98. glTexCoord2f(1.0f, 0.0f);
  99. glVertex2i(xMax, yMin);
  100. glEnd();
  101. }
  102. unsigned int FontSDL::heightText(float scale, unsigned int maxWidth, std::string s) {
  103. assert(mFontInit == true);
  104. assert(mFont != nullptr);
  105. assert(s.length() > 0);
  106. assert(maxWidth > 0);
  107. SDL_Color col;
  108. SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  109. if (surface == nullptr) {
  110. std::cout << "TTF_RenderUTF8_Blended_Wrapped Error: " << TTF_GetError() << std::endl;
  111. return 0;
  112. }
  113. int h = surface->h * scale;
  114. SDL_FreeSurface(surface);
  115. return h;
  116. }
  117. void FontSDL::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  118. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  119. assert(mFontInit == true);
  120. assert(mFont != nullptr);
  121. assert(s.length() > 0);
  122. assert(maxWidth > 0);
  123. SDL_Color col;
  124. col.r = color[0];
  125. col.g = color[1];
  126. col.b = color[2];
  127. col.a = color[3];
  128. SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  129. if (surface == nullptr) {
  130. std::cout << "TTF_RenderUTF8_Blended_Wrapped Error: " << TTF_GetError() << std::endl;
  131. return;
  132. }
  133. int w = (int)((float)surface->w * scale);
  134. int h = (int)((float)surface->h * scale);
  135. GLenum textureFormat;
  136. if (surface->format->BytesPerPixel == 4) {
  137. if (surface->format->Rmask == 0x000000FF)
  138. textureFormat = GL_RGBA;
  139. else
  140. textureFormat = GL_BGRA_EXT;
  141. } else {
  142. if (surface->format->Rmask == 0x000000FF)
  143. textureFormat = GL_RGB;
  144. else
  145. textureFormat = GL_BGR_EXT;
  146. }
  147. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  150. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0,
  151. textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  152. SDL_FreeSurface(surface);
  153. GLuint xMin = x;
  154. GLuint yMin = y;
  155. GLuint xMax = xMin + w;
  156. GLuint yMax = yMin + h;
  157. glColor4ubv(color);
  158. glBegin(GL_QUADS);
  159. glTexCoord2f(0.0f, 0.0f);
  160. glVertex2i(xMin, yMin);
  161. glTexCoord2f(0.0f, 1.0f);
  162. glVertex2i(xMin, yMax);
  163. glTexCoord2f(1.0f, 1.0f);
  164. glVertex2i(xMax, yMax);
  165. glTexCoord2f(1.0f, 0.0f);
  166. glVertex2i(xMax, yMin);
  167. glEnd();
  168. }