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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*!
  2. * \file src/FontSDL.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "FontSDL.h"
  9. FontSDL::FontSDL() {
  10. mFont = NULL;
  11. mFontInit = false;
  12. mFontName = NULL;
  13. mFontTexture = 0;
  14. tempText.text = new char[256];
  15. tempText.color[0] = 0xFF;
  16. tempText.color[1] = 0xFF;
  17. tempText.color[2] = 0xFF;
  18. tempText.color[3] = 0xFF;
  19. tempText.scale = 1.2f;
  20. tempText.w = 0;
  21. tempText.h = 0;
  22. }
  23. FontSDL::~FontSDL() {
  24. if (mFont)
  25. TTF_CloseFont(mFont);
  26. if (mFontInit)
  27. TTF_Quit();
  28. }
  29. int FontSDL::initialize() {
  30. assert(mFontInit == false);
  31. assert(mFontName != NULL);
  32. assert(mFontName[0] != '\0');
  33. if (TTF_Init() != 0) {
  34. printf("Could not initialize SDL-TTF!\n");
  35. return -1;
  36. }
  37. mFont = TTF_OpenFont(mFontName, 24);
  38. if (mFont == NULL) {
  39. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  40. return -2;
  41. }
  42. glGenTextures(1, &mFontTexture);
  43. mFontInit = true;
  44. return 0;
  45. }
  46. void FontSDL::writeString(FontString &s) {
  47. assert(mFontInit == true);
  48. assert(s.text != NULL);
  49. SDL_Color color;
  50. color.r = (unsigned char)(s.color[0] * 255.0f);
  51. color.g = (unsigned char)(s.color[1] * 255.0f);
  52. color.b = (unsigned char)(s.color[2] * 255.0f);
  53. color.a = (unsigned char)(s.color[3] * 255.0f);
  54. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.text, color);
  55. if (surface == NULL) {
  56. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  57. return;
  58. }
  59. s.w = (int)((float)surface->w * s.scale);
  60. s.h = (int)((float)surface->h * s.scale);
  61. GLenum textureFormat;
  62. if (surface->format->BytesPerPixel == 4) {
  63. if (surface->format->Rmask == 0x000000FF)
  64. textureFormat = GL_RGBA;
  65. else
  66. textureFormat = GL_BGRA_EXT;
  67. } else {
  68. if (surface->format->Rmask == 0x000000FF)
  69. textureFormat = GL_RGB;
  70. else
  71. textureFormat = GL_BGR_EXT;
  72. }
  73. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  76. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  77. GLuint xMin = s.x;
  78. GLuint yMin = s.y;
  79. GLuint xMax = xMin + s.w;
  80. GLuint yMax = yMin + s.h;
  81. glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
  82. glBegin(GL_QUADS);
  83. glTexCoord2f(0.0f, 0.0f);
  84. glVertex2i(xMin, yMin);
  85. glTexCoord2f(0.0f, 1.0f);
  86. glVertex2i(xMin, yMax);
  87. glTexCoord2f(1.0f, 1.0f);
  88. glVertex2i(xMax, yMax);
  89. glTexCoord2f(1.0f, 0.0f);
  90. glVertex2i(xMax, yMin);
  91. glEnd();
  92. SDL_FreeSurface(surface);
  93. }