Simple game engine with complete export to scripting language
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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "FontManager.h"
  2. #include "RenderDevice.h"
  3. // project includes
  4. #include "Utilities/Buffer.h"
  5. #include "Utilities/Log.h"
  6. #include "Utilities/format.h"
  7. // library includes
  8. #include "physfs.h"
  9. #include "FTGLTextureFont.h"
  10. // system includes
  11. #include <iostream>
  12. using namespace std;
  13. #include "FontManagerDefaultFont.h"
  14. namespace BlueCore
  15. {
  16. //------------------------------------------------------------------------------
  17. FontManager::FontManager(RenderDevice* device) :
  18. _Device(device)
  19. {
  20. // create default font
  21. FTFont *ftfont = new FTGLTextureFont (
  22. DefaultFontBytes,
  23. sizeof ( DefaultFontBytes ),
  24. false );
  25. ftfont->FaceSize( 16);
  26. _DefaultFont = new Font( ftfont, 0, device );
  27. if (_Device.valid())
  28. _Device->DeviceShutdownSignal.connect(this,
  29. &FontManager::DeviceShutdownSlot);
  30. clog << ">>> FontManager constructed..."<< endline;
  31. }
  32. //------------------------------------------------------------------------------
  33. FontManager::~FontManager()
  34. {
  35. destroy();
  36. clog << ">>> FontManager destructed..."<< endline;
  37. }
  38. //------------------------------------------------------------------------------
  39. void FontManager::DeviceShutdownSlot()
  40. {
  41. destroy();
  42. }
  43. //------------------------------------------------------------------------------
  44. void FontManager::destroy()
  45. {
  46. FontMap::iterator i;
  47. for (i = _fonts.begin(); i != _fonts.end(); i++)
  48. {
  49. if (i->second.valid())
  50. i->second->destroy();
  51. }
  52. _DefaultFont = 0;
  53. }
  54. //------------------------------------------------------------------------------
  55. Font *FontManager::loadFont(const std::string &filename, int size, bool hinting)
  56. {
  57. string key = format("%s-%d", filename.c_str(), size);
  58. // check if this font is already loaded
  59. FontMap::const_iterator result;
  60. result = _fonts.find(key);
  61. if (result != _fonts.end() && result->second.valid())
  62. {
  63. return result->second.pointer();
  64. }
  65. Font *font;
  66. PHYSFS_file *file = PHYSFS_openRead(filename.c_str() );
  67. if (file)
  68. {
  69. unsigned int file_size = PHYSFS_fileLength(file);
  70. unsigned char *buffer = new unsigned char[file_size];
  71. PHYSFS_read(file, buffer, 1, file_size);
  72. PHYSFS_close(file);
  73. // create the font
  74. FTFont *ftfont = new FTGLTextureFont ( buffer, file_size, hinting );
  75. ftfont->FaceSize(size);
  76. if (ftfont->Error() == 0)
  77. {
  78. font = new Font( ftfont, buffer, _Device );
  79. clog << ">>> Font '"<< filename << "' ("<< size << ") loaded"
  80. << endline;
  81. }
  82. else
  83. {
  84. delete ftfont;
  85. clog << "!!! Font '"<< filename << "' could not be loaded"
  86. << endline;
  87. font = new Font();
  88. }
  89. }
  90. else
  91. {
  92. clog << "!!! Font '"<< filename << "' not found"<< endline;
  93. font = new Font();
  94. }
  95. _fonts[key] = font;
  96. return font;
  97. }
  98. //------------------------------------------------------------------------------
  99. Font *FontManager::getDefaultFont()
  100. {
  101. return _DefaultFont.pointer();
  102. }
  103. //------------------------------------------------------------------------------
  104. Font::Font()
  105. {
  106. _Font = 0;
  107. _Buffer = 0;
  108. clog << ">>> Font constructed..."<< endline;
  109. }
  110. //------------------------------------------------------------------------------
  111. Font::Font(FTFont *font, unsigned char *buffer, RenderDevice* device) :
  112. _Font(font), _Buffer(buffer), _Device(device)
  113. {
  114. clog << ">>> Font constructed..."<< endline;
  115. }
  116. //------------------------------------------------------------------------------
  117. Font::~Font()
  118. {
  119. destroy();
  120. clog << ">>> Font destructed..."<< endline;
  121. }
  122. //------------------------------------------------------------------------------
  123. void Font::destroy()
  124. {
  125. delete _Font;
  126. delete [] _Buffer;
  127. clog << ">>> Font destroyed..."<< endline;
  128. }
  129. //------------------------------------------------------------------------------
  130. void Font::print(float x, float y, const std::string &text, int halign,
  131. int valign) const
  132. {
  133. if (_Font == 0)
  134. return;
  135. if (_Device.valid() == false)
  136. return;
  137. _Device->begin2D();
  138. if (x < 0.0)
  139. x += _Device->getViewportWidth();
  140. else if (x <= 1.0)
  141. x *= _Device->getViewportWidth();
  142. if (halign == 0)
  143. x -= _Font->Advance(text.c_str() ) / 2;
  144. else if (halign == -1)
  145. x -= _Font->Advance(text.c_str() );
  146. if (y < 0.0)
  147. y += _Device->getViewportHeight();
  148. else if (y <= 1.0)
  149. y *= _Device->getViewportHeight();
  150. y -= _Font->Descender();
  151. if (valign == 0)
  152. y -= _Font->LineHeight() / 2;
  153. else if (valign == -1)
  154. y -= _Font->LineHeight();
  155. glEnable(GL_TEXTURE_2D);
  156. glPushMatrix();
  157. glLoadIdentity();
  158. glTranslatef(x, y, 0);
  159. _Font->Render(text.c_str() );
  160. glPopMatrix();
  161. _Device->end2D();
  162. }
  163. } // namespace BlueCore