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.

FontManager.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. * \file src/FontManager.cpp
  3. * \brief Font Manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "utils/strings.h"
  9. #include "FontManager.h"
  10. #include "Font.h"
  11. #include "FontTRLE.h"
  12. #ifdef USING_SDL_FONT
  13. #include "FontSDL.h"
  14. #endif
  15. FontManager::FontManager() {
  16. add(new FontTRLE(), ".pc");
  17. #ifdef USING_SDL_FONT
  18. add(new FontSDL(), ".ttf");
  19. #endif
  20. mFontInit = false;
  21. font = -1;
  22. }
  23. FontManager::~FontManager() {
  24. while (!fonts.empty()) {
  25. delete fonts.back();
  26. fonts.pop_back();
  27. }
  28. }
  29. void FontManager::add(Font *f, std::string e) {
  30. fonts.push_back(f);
  31. extensions.push_back(e);
  32. }
  33. int FontManager::initialize() {
  34. for (unsigned int i = 0; i < fonts.size(); i++) {
  35. if (stringEndsWith(mFontName, extensions.at(i))) {
  36. font = i;
  37. break;
  38. }
  39. }
  40. if (font == -1)
  41. return -1;
  42. mFontInit = true;
  43. fonts.at(font)->setFont(mFontName);
  44. return fonts.at(font)->initialize();
  45. }
  46. unsigned int FontManager::widthText(float scale, std::string s) {
  47. assert(font != -1);
  48. return fonts.at(font)->widthText(scale, s);
  49. }
  50. void FontManager::drawText(unsigned int x, unsigned int y, float scale,
  51. const unsigned char color[4], std::string s) {
  52. assert(font != -1);
  53. fonts.at(font)->drawText(x, y, scale, color, s);
  54. }
  55. unsigned int FontManager::heightText(float scale, unsigned int maxWidth, std::string s) {
  56. assert(font != -1);
  57. return fonts.at(font)->heightText(scale, maxWidth, s);
  58. }
  59. void FontManager::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  60. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  61. assert(font != -1);
  62. fonts.at(font)->drawTextWrapped(x, y, scale, color, maxWidth, s);
  63. }