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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "TextureManager.h"
  10. #include "system/Window.h"
  11. #include "system/FontSDL.h"
  12. bool FontSDL::mFontInit = false;
  13. TTF_Font* FontSDL::mFont = nullptr;
  14. unsigned int FontSDL::mFontTexture = -1;
  15. void FontSDL::shutdown() {
  16. if (mFont != nullptr)
  17. TTF_CloseFont(mFont);
  18. mFont = nullptr;
  19. if (mFontInit) {
  20. TTF_Quit();
  21. mFontInit = false;
  22. }
  23. }
  24. int FontSDL::initialize(std::string font) {
  25. if (!mFontInit) {
  26. if (TTF_Init() != 0) {
  27. std::cout << "Could not initialize SDL-TTF!" << std::endl;
  28. return -1;
  29. }
  30. // Reserve slot
  31. mFontTexture = getTextureManager().loadBufferSlot(nullptr, 256, 256,
  32. TextureManager::ColorMode::RGBA, 32, TextureManager::TextureStorage::SYSTEM);
  33. mFontInit = true;
  34. }
  35. if (mFont != nullptr)
  36. TTF_CloseFont(mFont);
  37. mFont = TTF_OpenFont(font.c_str(), 24);
  38. if (mFont == nullptr) {
  39. std::cout << "TTF_OpenFont Error: " << TTF_GetError() << std::endl;
  40. return -2;
  41. }
  42. return 0;
  43. }
  44. unsigned int FontSDL::widthText(float scale, std::string s) {
  45. assert(mFontInit == true);
  46. assert(mFont != nullptr);
  47. int w;
  48. if (TTF_SizeUTF8(mFont, s.c_str(), &w, nullptr) != 0) {
  49. std::cout << "TTF_SizeUTF8 Error: " << TTF_GetError() << std::endl;
  50. return s.length() * 15 * scale;
  51. }
  52. return w * scale;
  53. }
  54. void FontSDL::drawText(unsigned int x, unsigned int y, float scale,
  55. const unsigned char color[4], std::string s) {
  56. assert(mFontInit == true);
  57. assert(mFont != nullptr);
  58. assert(s.length() > 0);
  59. SDL_Color col;
  60. col.r = color[0];
  61. col.g = color[1];
  62. col.b = color[2];
  63. col.a = color[3];
  64. SDL_Surface* surface = TTF_RenderUTF8_Blended(mFont, s.c_str(), col);
  65. if (surface == nullptr) {
  66. std::cout << "TTF_RenderUTF8_Blended Error: " << TTF_GetError() << std::endl;
  67. return;
  68. }
  69. int w = (int)((float)surface->w * scale);
  70. int h = (int)((float)surface->h * scale);
  71. TextureManager::ColorMode textureFormat;
  72. unsigned int bpp = 0;
  73. if (surface->format->BytesPerPixel == 4) {
  74. if (surface->format->Rmask == 0x000000FF)
  75. textureFormat = TextureManager::ColorMode::RGBA;
  76. else
  77. textureFormat = TextureManager::ColorMode::BGRA;
  78. bpp = 32;
  79. } else {
  80. if (surface->format->Rmask == 0x000000FF)
  81. textureFormat = TextureManager::ColorMode::RGB;
  82. else
  83. textureFormat = TextureManager::ColorMode::BGR;
  84. bpp = 24;
  85. }
  86. getTextureManager().loadBufferSlot(static_cast<unsigned char*>(surface->pixels),
  87. surface->w, surface->h, textureFormat, bpp,
  88. TextureManager::TextureStorage::SYSTEM, mFontTexture);
  89. SDL_FreeSurface(surface);
  90. std::vector<glm::vec2> vertices;
  91. std::vector<glm::vec2> uvs;
  92. vertices.push_back(glm::vec2(x, y + h));
  93. vertices.push_back(glm::vec2(x, y));
  94. vertices.push_back(glm::vec2(x + w, y + h));
  95. vertices.push_back(glm::vec2(x + w, y));
  96. vertices.push_back(glm::vec2(x + w, y + h));
  97. vertices.push_back(glm::vec2(x, y));
  98. uvs.push_back(glm::vec2(0.0f, 1.0f));
  99. uvs.push_back(glm::vec2(0.0f, 0.0f));
  100. uvs.push_back(glm::vec2(1.0f, 1.0f));
  101. uvs.push_back(glm::vec2(1.0f, 0.0f));
  102. uvs.push_back(glm::vec2(1.0f, 1.0f));
  103. uvs.push_back(glm::vec2(0.0f, 0.0f));
  104. Window::drawTextGL(vertices, uvs, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), mFontTexture);
  105. }
  106. unsigned int FontSDL::heightText(float scale, unsigned int maxWidth, std::string s) {
  107. assert(mFontInit == true);
  108. assert(mFont != nullptr);
  109. assert(s.length() > 0);
  110. assert(maxWidth > 0);
  111. SDL_Color col;
  112. SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  113. if (surface == nullptr) {
  114. std::cout << "TTF_RenderUTF8_Blended_Wrapped Error: " << TTF_GetError() << std::endl;
  115. return 0;
  116. }
  117. int h = surface->h * scale;
  118. SDL_FreeSurface(surface);
  119. return h;
  120. }
  121. void FontSDL::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  122. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  123. assert(mFontInit == true);
  124. assert(mFont != nullptr);
  125. assert(s.length() > 0);
  126. assert(maxWidth > 0);
  127. SDL_Color col;
  128. col.r = color[0];
  129. col.g = color[1];
  130. col.b = color[2];
  131. col.a = color[3];
  132. SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(mFont, s.c_str(), col, maxWidth);
  133. if (surface == nullptr) {
  134. std::cout << "TTF_RenderUTF8_Blended_Wrapped Error: " << TTF_GetError() << std::endl;
  135. return;
  136. }
  137. int w = (int)((float)surface->w * scale);
  138. int h = (int)((float)surface->h * scale);
  139. TextureManager::ColorMode textureFormat;
  140. unsigned int bpp = 0;
  141. if (surface->format->BytesPerPixel == 4) {
  142. if (surface->format->Rmask == 0x000000FF)
  143. textureFormat = TextureManager::ColorMode::RGBA;
  144. else
  145. textureFormat = TextureManager::ColorMode::BGRA;
  146. bpp = 32;
  147. } else {
  148. if (surface->format->Rmask == 0x000000FF)
  149. textureFormat = TextureManager::ColorMode::RGB;
  150. else
  151. textureFormat = TextureManager::ColorMode::BGR;
  152. bpp = 24;
  153. }
  154. getTextureManager().loadBufferSlot(static_cast<unsigned char*>(surface->pixels),
  155. surface->w, surface->h, textureFormat, bpp,
  156. TextureManager::TextureStorage::SYSTEM, mFontTexture);
  157. SDL_FreeSurface(surface);
  158. std::vector<glm::vec2> vertices;
  159. std::vector<glm::vec2> uvs;
  160. vertices.push_back(glm::vec2(x, y + h));
  161. vertices.push_back(glm::vec2(x, y));
  162. vertices.push_back(glm::vec2(x + w, y + h));
  163. vertices.push_back(glm::vec2(x + w, y));
  164. vertices.push_back(glm::vec2(x + w, y + h));
  165. vertices.push_back(glm::vec2(x, y));
  166. uvs.push_back(glm::vec2(0.0f, 1.0f));
  167. uvs.push_back(glm::vec2(0.0f, 0.0f));
  168. uvs.push_back(glm::vec2(1.0f, 1.0f));
  169. uvs.push_back(glm::vec2(1.0f, 0.0f));
  170. uvs.push_back(glm::vec2(1.0f, 1.0f));
  171. uvs.push_back(glm::vec2(0.0f, 0.0f));
  172. Window::drawTextGL(vertices, uvs, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), mFontTexture);
  173. }