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.5KB

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