Try to use c++11 typeid for getFont

This commit is contained in:
Benau 2016-07-26 08:04:12 +08:00
parent 29185f679b
commit cefead119a
2 changed files with 16 additions and 8 deletions

View File

@ -52,17 +52,21 @@ void FontManager::loadFonts()
m_digit_ttf = new FaceTTF(stk_config->m_digit_ttf); m_digit_ttf = new FaceTTF(stk_config->m_digit_ttf);
// Now load fonts with settings of ttf file // Now load fonts with settings of ttf file
unsigned int font_loaded = 0;
RegularFace* regular = new RegularFace(m_normal_ttf); RegularFace* regular = new RegularFace(m_normal_ttf);
regular->init(); regular->init();
m_fonts.push_back(regular); m_fonts.push_back(regular);
m_font_type_map[std::type_index(typeid(RegularFace))] = font_loaded++;
BoldFace* bold = new BoldFace(m_normal_ttf); BoldFace* bold = new BoldFace(m_normal_ttf);
bold->init(); bold->init();
m_fonts.push_back(bold); m_fonts.push_back(bold);
m_font_type_map[std::type_index(typeid(BoldFace))] = font_loaded++;
DigitFace* digit = new DigitFace(m_digit_ttf); DigitFace* digit = new DigitFace(m_digit_ttf);
digit->init(); digit->init();
m_fonts.push_back(digit); m_fonts.push_back(digit);
m_font_type_map[std::type_index(typeid(DigitFace))] = font_loaded++;
} // loadFonts } // loadFonts
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -25,6 +25,8 @@
#include "utils/ptr_vector.hpp" #include "utils/ptr_vector.hpp"
#include <string> #include <string>
#include <typeindex>
#include <unordered_map>
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
@ -43,6 +45,8 @@ private:
FaceTTF* m_digit_ttf; FaceTTF* m_digit_ttf;
std::unordered_map<std::type_index, int> m_font_type_map;
public: public:
LEAK_CHECK() LEAK_CHECK()
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -53,10 +57,10 @@ public:
template <typename T> T* getFont() template <typename T> T* getFont()
{ {
T* out = NULL; T* out = NULL;
for (unsigned int i = 0; i < m_fonts.size(); i++) const unsigned int n = m_font_type_map[std::type_index(typeid(T))];
{ out = dynamic_cast<T*>(m_fonts.get(n));
out = dynamic_cast<T*>(m_fonts.get(i));
if (out != NULL) if (out != NULL)
{
return out; return out;
} }
Log::fatal("FontManager", "Can't get a font!"); Log::fatal("FontManager", "Can't get a font!");