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.

Font.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/FontImGui.h"
  11. #include "system/FontTRLE.h"
  12. #include "system/FontTTF.h"
  13. #include "system/Font.h"
  14. bool Font::isInit = false;
  15. std::string Font::fontName;
  16. void Font::shutdown() {
  17. FontTRLE::shutdown();
  18. FontTTF::shutdown();
  19. }
  20. int Font::initialize(std::string font) {
  21. fontName = font;
  22. if (stringEndsWith(fontName, ".pc")) {
  23. return FontTRLE::initialize(fontName);
  24. } else if (stringEndsWith(fontName, ".ttf")) {
  25. return FontTTF::initialize(fontName);
  26. }
  27. if (font != "") {
  28. Log::get(LOG_ERROR) << "Unknown font file format: " << font << Log::endl;
  29. return -1;
  30. } else {
  31. return 0;
  32. }
  33. }
  34. unsigned int Font::widthText(float scale, std::string s) {
  35. if (stringEndsWith(fontName, ".pc")) {
  36. return FontTRLE::widthText(scale, s);
  37. } else if (stringEndsWith(fontName, ".ttf")) {
  38. return FontTTF::widthText(scale, s);
  39. } else {
  40. return FontImGui::widthText(scale, s);
  41. }
  42. }
  43. unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
  44. if (stringEndsWith(fontName, ".pc")) {
  45. return FontTRLE::heightText(scale, maxWidth, s);
  46. } else if (stringEndsWith(fontName, ".ttf")) {
  47. return FontTTF::heightText(scale, maxWidth, s);
  48. } else {
  49. return FontImGui::heightText(scale, maxWidth, s);
  50. }
  51. }
  52. void Font::drawText(unsigned int x, unsigned int y, float scale,
  53. const unsigned char color[4], std::string s) {
  54. if (stringEndsWith(fontName, ".pc")) {
  55. FontTRLE::drawText(x, y, scale, color, s);
  56. } else if (stringEndsWith(fontName, ".ttf")) {
  57. FontTTF::drawText(x, y, scale, color, s);
  58. } else {
  59. FontImGui::drawText(x, y, scale, color, s);
  60. }
  61. }
  62. void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  63. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  64. if (stringEndsWith(fontName, ".pc")) {
  65. FontTRLE::drawTextWrapped(x, y, scale, color, maxWidth, s);
  66. } else if (stringEndsWith(fontName, ".ttf")) {
  67. FontTTF::drawTextWrapped(x, y, scale, color, maxWidth, s);
  68. } else {
  69. FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
  70. }
  71. }
  72. void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
  73. const unsigned char color[4], unsigned int width, std::string s) {
  74. drawText(x + ((width / 2) - (widthText(scale, s) / 2)), y, scale, color, s);
  75. }