Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FontSDL.cpp 5.0KB

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