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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  15. FontSDL::~FontSDL() {
  16. if (mFont)
  17. TTF_CloseFont(mFont);
  18. if (mFontInit)
  19. TTF_Quit();
  20. }
  21. int FontSDL::initialize() {
  22. assert(mFontInit == false);
  23. assert(mFontName != NULL);
  24. assert(mFontName[0] != '\0');
  25. if (TTF_Init() != 0) {
  26. printf("Could not initialize SDL-TTF!\n");
  27. return -1;
  28. }
  29. mFont = TTF_OpenFont(mFontName, 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. void FontSDL::writeString(FontString &s) {
  39. assert(mFontInit == true);
  40. assert(s.text != NULL);
  41. SDL_Color color;
  42. color.r = s.color[0];
  43. color.g = s.color[1];
  44. color.b = s.color[2];
  45. color.a = s.color[3];
  46. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s.text, color);
  47. if (surface == NULL) {
  48. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  49. return;
  50. }
  51. s.w = (int)((float)surface->w * s.scale);
  52. s.h = (int)((float)surface->h * s.scale);
  53. GLenum textureFormat;
  54. if (surface->format->BytesPerPixel == 4) {
  55. if (surface->format->Rmask == 0x000000FF)
  56. textureFormat = GL_RGBA;
  57. else
  58. textureFormat = GL_BGRA_EXT;
  59. } else {
  60. if (surface->format->Rmask == 0x000000FF)
  61. textureFormat = GL_RGB;
  62. else
  63. textureFormat = GL_BGR_EXT;
  64. }
  65. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  66. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  67. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  68. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  69. GLuint xMin = s.x;
  70. GLuint yMin = s.y;
  71. GLuint xMax = xMin + s.w;
  72. GLuint yMax = yMin + s.h;
  73. glColor4ubv(s.color);
  74. glBegin(GL_QUADS);
  75. glTexCoord2f(0.0f, 0.0f);
  76. glVertex2i(xMin, yMin);
  77. glTexCoord2f(0.0f, 1.0f);
  78. glVertex2i(xMin, yMax);
  79. glTexCoord2f(1.0f, 1.0f);
  80. glVertex2i(xMax, yMax);
  81. glTexCoord2f(1.0f, 0.0f);
  82. glVertex2i(xMax, yMin);
  83. glEnd();
  84. SDL_FreeSurface(surface);
  85. }