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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. font = -1;
  21. }
  22. FontManager::~FontManager() {
  23. while (!fonts.empty()) {
  24. delete fonts.back();
  25. fonts.pop_back();
  26. }
  27. }
  28. void FontManager::add(Font *f, std::string e) {
  29. fonts.push_back(f);
  30. extensions.push_back(e);
  31. }
  32. int FontManager::initialize() {
  33. for (unsigned int i = 0; i < fonts.size(); i++) {
  34. if (stringEndsWith(mFontName, extensions.at(i))) {
  35. font = i;
  36. break;
  37. }
  38. }
  39. if (font == -1)
  40. return -1;
  41. fonts.at(font)->setFont(mFontName);
  42. return fonts.at(font)->initialize();
  43. }
  44. unsigned int FontManager::widthText(float scale, std::string s) {
  45. assert(font != -1);
  46. return fonts.at(font)->widthText(scale, s);
  47. }
  48. void FontManager::drawText(unsigned int x, unsigned int y, float scale,
  49. const unsigned char color[4], std::string s) {
  50. assert(font != -1);
  51. fonts.at(font)->drawText(x, y, scale, color, s);
  52. }
  53. unsigned int FontManager::heightText(float scale, unsigned int maxWidth, std::string s) {
  54. assert(font != -1);
  55. return fonts.at(font)->heightText(scale, maxWidth, s);
  56. }
  57. void FontManager::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  58. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  59. assert(font != -1);
  60. fonts.at(font)->drawTextWrapped(x, y, scale, color, maxWidth, s);
  61. }