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.

Font.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*!
  2. * \file src/system/Font.cpp
  3. * \brief Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Log.h"
  9. #include "utils/strings.h"
  10. #include "system/Window.h"
  11. #include "system/Font.h"
  12. #include "system/FontImGui.h"
  13. #include "system/FontTRLE.h"
  14. #ifdef USING_SDL_FONT
  15. #include "system/FontSDL.h"
  16. #endif
  17. bool Font::isInit = false;
  18. std::string Font::fontName;
  19. void Font::shutdown() {
  20. FontTRLE::shutdown();
  21. #ifdef USING_SDL_FONT
  22. FontSDL::shutdown();
  23. #endif
  24. }
  25. int Font::initialize(std::string font) {
  26. fontName = font;
  27. if (stringEndsWith(fontName, ".pc")) {
  28. return FontTRLE::initialize(fontName);
  29. #ifdef USING_SDL_FONT
  30. } else if (stringEndsWith(fontName, ".ttf")) {
  31. return FontSDL::initialize(fontName);
  32. #endif
  33. }
  34. if (font != "") {
  35. getLog() << "Unknown font file format: " << font << Log::endl;
  36. return -1;
  37. } else {
  38. return 0;
  39. }
  40. }
  41. unsigned int Font::widthText(float scale, std::string s) {
  42. if (stringEndsWith(fontName, ".pc")) {
  43. return FontTRLE::widthText(scale, s);
  44. #ifdef USING_SDL_FONT
  45. } else if (stringEndsWith(fontName, ".ttf")) {
  46. return FontSDL::widthText(scale, s);
  47. #endif
  48. } else {
  49. return FontImGui::widthText(scale, s);
  50. }
  51. }
  52. unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
  53. if (stringEndsWith(fontName, ".pc")) {
  54. return FontTRLE::heightText(scale, maxWidth, s);
  55. #ifdef USING_SDL_FONT
  56. } else if (stringEndsWith(fontName, ".ttf")) {
  57. return FontSDL::heightText(scale, maxWidth, s);
  58. #endif
  59. } else {
  60. return FontImGui::heightText(scale, maxWidth, s);
  61. }
  62. }
  63. void Font::drawText(unsigned int x, unsigned int y, float scale,
  64. const unsigned char color[4], std::string s) {
  65. if (stringEndsWith(fontName, ".pc")) {
  66. FontTRLE::drawText(x, y, scale, color, s);
  67. #ifdef USING_SDL_FONT
  68. } else if (stringEndsWith(fontName, ".ttf")) {
  69. FontSDL::drawText(x, y, scale, color, s);
  70. #endif
  71. } else {
  72. FontImGui::drawText(x, y, scale, color, s);
  73. }
  74. }
  75. void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  76. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  77. if (stringEndsWith(fontName, ".pc")) {
  78. FontTRLE::drawTextWrapped(x, y, scale, color, maxWidth, s);
  79. #ifdef USING_SDL_FONT
  80. } else if (stringEndsWith(fontName, ".ttf")) {
  81. FontSDL::drawTextWrapped(x, y, scale, color, maxWidth, s);
  82. #endif
  83. } else {
  84. FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
  85. }
  86. }
  87. void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
  88. const unsigned char color[4], unsigned int width, std::string s) {
  89. drawText(x + ((width / 2) - (widthText(scale, s) / 2)), y, scale, color, s);
  90. }