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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "RunTime.h"
  10. #include "FontManager.h"
  11. #include "Font.h"
  12. #include "FontTRLE.h"
  13. #ifdef USING_SDL_FONT
  14. #include "FontSDL.h"
  15. #endif
  16. FontManager::FontManager() {
  17. add(new FontTRLE(), ".pc");
  18. #ifdef USING_SDL_FONT
  19. add(new FontSDL(), ".ttf");
  20. #endif
  21. mFontInit = false;
  22. font = -1;
  23. // Default font path
  24. #ifdef USING_SDL_FONT
  25. setFont(getRunTime().getDataDir() + "/test.ttf");
  26. #else
  27. setFont(getRunTime().getDataDir() + "/font.pc");
  28. #endif
  29. }
  30. FontManager::~FontManager() {
  31. while (!fonts.empty()) {
  32. delete fonts.back();
  33. fonts.pop_back();
  34. }
  35. }
  36. void FontManager::add(Font *f, std::string e) {
  37. fonts.push_back(f);
  38. extensions.push_back(e);
  39. }
  40. int FontManager::initialize() {
  41. for (unsigned int i = 0; i < fonts.size(); i++) {
  42. if (stringEndsWith(mFontName, extensions.at(i))) {
  43. font = i;
  44. break;
  45. }
  46. }
  47. if (font == -1)
  48. return -1;
  49. mFontInit = true;
  50. fonts.at(font)->setFont(mFontName);
  51. return fonts.at(font)->initialize();
  52. }
  53. unsigned int FontManager::widthText(float scale, std::string s) {
  54. assert(font != -1);
  55. return fonts.at(font)->widthText(scale, s);
  56. }
  57. void FontManager::drawText(unsigned int x, unsigned int y, float scale,
  58. const unsigned char color[4], std::string s) {
  59. assert(font != -1);
  60. fonts.at(font)->drawText(x, y, scale, color, s);
  61. }
  62. unsigned int FontManager::heightText(float scale, unsigned int maxWidth, std::string s) {
  63. assert(font != -1);
  64. return fonts.at(font)->heightText(scale, maxWidth, s);
  65. }
  66. void FontManager::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  67. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  68. assert(font != -1);
  69. fonts.at(font)->drawTextWrapped(x, y, scale, color, maxWidth, s);
  70. }