CachedFont* CText::GetOrOpenFont(FontType font, float size) { Math::IntPoint windowSize = m_engine->GetWindowSize(); int pointSize = static_cast<int>(size * (windowSize.Length() / REFERENCE_SIZE.Length())); if (m_lastCachedFont != nullptr) { if (m_lastFontType == font && m_lastFontSize == pointSize) return m_lastCachedFont; } auto it = m_fonts.find(font); if (it == m_fonts.end()) { m_error = std::string("Invalid font type ") + StrUtils::ToString<int>(static_cast<int>(font)); return nullptr; } MultisizeFont* mf = (*it).second; auto jt = mf->fonts.find(pointSize); if (jt != mf->fonts.end()) { m_lastCachedFont = (*jt).second; m_lastFontType = font; m_lastFontSize = pointSize; return m_lastCachedFont; } m_lastCachedFont = new CachedFont(); SDL_RWops* file = CResourceManager::GetSDLFileHandler(mf->fileName); if(file == nullptr) { m_error = std::string("Unable to open file"); return nullptr; } m_lastCachedFont->font = TTF_OpenFontRW(file, 1, pointSize); if (m_lastCachedFont->font == nullptr) m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError()); mf->fonts[pointSize] = m_lastCachedFont; return m_lastCachedFont; }
CachedFont* CText::GetOrOpenFont(FontType font, float size) { Math::IntPoint windowSize = m_engine->GetWindowSize(); int pointSize = static_cast<int>(size * (windowSize.Length() / REFERENCE_SIZE.Length())); if (m_lastCachedFont != nullptr) { if (m_lastFontType == font && m_lastFontSize == pointSize) return m_lastCachedFont; } auto it = m_fonts.find(font); if (it == m_fonts.end()) { m_error = std::string("Invalid font type ") + StrUtils::ToString<int>(static_cast<int>(font)); return nullptr; } MultisizeFont* mf = (*it).second; auto jt = mf->fonts.find(pointSize); if (jt != mf->fonts.end()) { m_lastCachedFont = (*jt).second; m_lastFontType = font; m_lastFontSize = pointSize; return m_lastCachedFont; } std::string path = CApplication::GetInstance().GetDataFilePath(DIR_FONT, mf->fileName); m_lastCachedFont = new CachedFont(); m_lastCachedFont->font = TTF_OpenFont(path.c_str(), pointSize); if (m_lastCachedFont->font == nullptr) m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError()); mf->fonts[pointSize] = m_lastCachedFont; return m_lastCachedFont; }