Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Font.cpp 2.4KB

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