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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * \file src/system/Font.cpp
  3. * \brief Font Interface
  4. *
  5. * \author xythobuz
  6. */
  7. #include <vector>
  8. #include "global.h"
  9. #include "Log.h"
  10. #include "utils/strings.h"
  11. #include "system/FontImGui.h"
  12. #include "system/FontTRLE.h"
  13. #include "system/FontTTF.h"
  14. #include "system/Shader.h"
  15. #include "system/Font.h"
  16. bool Font::isInit = false;
  17. std::string Font::fontName;
  18. bool Font::showFontBox = false;
  19. void Font::shutdown() {
  20. FontTRLE::shutdown();
  21. FontTTF::shutdown();
  22. }
  23. int Font::initialize(std::string font) {
  24. fontName = font;
  25. if (stringEndsWith(fontName, ".pc")) {
  26. return FontTRLE::initialize(fontName);
  27. } else if (stringEndsWith(fontName, ".ttf")) {
  28. return FontTTF::initialize(fontName);
  29. }
  30. if (font != "") {
  31. Log::get(LOG_ERROR) << "Unknown font file format: " << font << Log::endl;
  32. return -1;
  33. } else {
  34. return 0;
  35. }
  36. }
  37. unsigned int Font::widthText(float scale, std::string s) {
  38. if (stringEndsWith(fontName, ".pc")) {
  39. return FontTRLE::widthText(scale, s);
  40. } else if (stringEndsWith(fontName, ".ttf")) {
  41. return FontTTF::widthText(scale, s);
  42. } else {
  43. return FontImGui::widthText(scale, s);
  44. }
  45. }
  46. unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
  47. if (stringEndsWith(fontName, ".pc")) {
  48. return FontTRLE::heightText(scale, maxWidth, s);
  49. } else if (stringEndsWith(fontName, ".ttf")) {
  50. return FontTTF::heightText(scale, maxWidth, s);
  51. } else {
  52. return FontImGui::heightText(scale, maxWidth, s);
  53. }
  54. }
  55. void Font::drawText(unsigned int x, unsigned int y, float scale,
  56. glm::vec4 color, std::string s) {
  57. if (stringEndsWith(fontName, ".pc")) {
  58. FontTRLE::drawText(x, y, scale, color, s);
  59. } else if (stringEndsWith(fontName, ".ttf")) {
  60. FontTTF::drawText(x, y, scale, color, s);
  61. } else {
  62. FontImGui::drawText(x, y, scale, color, s);
  63. }
  64. if (showFontBox) {
  65. unsigned int w = widthText(scale, s);
  66. unsigned int h = heightText(scale, w + 100, s); // Should always be one line
  67. drawFontBox(x, y, w, h);
  68. }
  69. }
  70. void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  71. glm::vec4 color, unsigned int maxWidth, std::string s) {
  72. if (stringEndsWith(fontName, ".pc")) {
  73. FontTRLE::drawTextWrapped(x, y, scale, color, maxWidth, s);
  74. } else if (stringEndsWith(fontName, ".ttf")) {
  75. FontTTF::drawTextWrapped(x, y, scale, color, maxWidth, s);
  76. } else {
  77. FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
  78. }
  79. if (showFontBox) {
  80. unsigned int w = widthText(scale, s);
  81. if (w > maxWidth)
  82. w = maxWidth;
  83. unsigned int h = heightText(scale, w, s); // Should always be one line
  84. drawFontBox(x, y, w, h);
  85. }
  86. }
  87. void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
  88. glm::vec4 color, unsigned int width, std::string s) {
  89. drawText(x + ((width / 2) - (widthText(scale, s) / 2)), y, scale, color, s);
  90. }
  91. void Font::drawFontBox(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
  92. static ShaderBuffer vert, uv;
  93. std::vector<glm::vec2> vertices, uvs;
  94. vertices.emplace_back(x, y);
  95. vertices.emplace_back(x + w, y);
  96. vertices.emplace_back(x + w, y + h);
  97. vertices.emplace_back(x, y + h);
  98. vertices.emplace_back(x, y);
  99. uvs.emplace_back(0.0f, 0.0f);
  100. uvs.emplace_back(1.0f, 0.0f);
  101. uvs.emplace_back(1.0f, 1.0f);
  102. uvs.emplace_back(0.0f, 1.0f);
  103. uvs.emplace_back(0.0f, 0.0f);
  104. vert.bufferData(vertices);
  105. uv.bufferData(uvs);
  106. Shader::drawGL(vert, uv, glm::vec4(1.0f, 1.0f, 0.0f, 1.0f), TEXTURE_WHITE,
  107. TextureStorage::SYSTEM, gl::GL_LINE_STRIP);
  108. }