Try to improve font reloading

pyro-refactor
MrSimbax 2020-07-19 16:07:27 +02:00
parent 56a8c5eb48
commit 7e6782a1be
3 changed files with 29 additions and 8 deletions

View File

@ -408,6 +408,7 @@ void CEngine::ReloadAllTextures()
{ {
FlushTextureCache(); FlushTextureCache();
m_text->FlushCache(); m_text->FlushCache();
m_text->ReloadFonts();
m_app->GetEventQueue()->AddEvent(Event(EVENT_RELOAD_TEXTURES)); m_app->GetEventQueue()->AddEvent(Event(EVENT_RELOAD_TEXTURES));
UpdateGroundSpotTextures(); UpdateGroundSpotTextures();

View File

@ -191,17 +191,32 @@ CText::~CText()
bool CText::Create() bool CText::Create()
{ {
CFontLoader fontLoader;
if (!fontLoader.Init())
{
GetLogger()->Warn("Error on parsing fonts config file: failed to open file\n");
}
if (TTF_Init() != 0) if (TTF_Init() != 0)
{ {
m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError()); m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError());
return false; return false;
} }
if (!ReloadFonts())
{
return false;
}
return true;
}
bool CText::ReloadFonts()
{
CFontLoader fontLoader;
if (!fontLoader.Init())
{
GetLogger()->Warn("Error on parsing fonts config file: failed to open file\n");
}
// Backup previous fonts
auto fonts = std::move(m_fonts);
m_fonts.clear();
for (auto type : {FONT_COMMON, FONT_STUDIO, FONT_SATCOM}) for (auto type : {FONT_COMMON, FONT_STUDIO, FONT_SATCOM})
{ {
m_fonts[static_cast<Gfx::FontType>(type)] = MakeUnique<MultisizeFont>(fontLoader.GetFont(type)); m_fonts[static_cast<Gfx::FontType>(type)] = MakeUnique<MultisizeFont>(fontLoader.GetFont(type));
@ -214,8 +229,11 @@ bool CText::Create()
FontType type = (*it).first; FontType type = (*it).first;
CachedFont* cf = GetOrOpenFont(type, m_defaultSize); CachedFont* cf = GetOrOpenFont(type, m_defaultSize);
if (cf == nullptr || cf->font == nullptr) if (cf == nullptr || cf->font == nullptr)
{
m_fonts = std::move(fonts);
return false; return false;
} }
}
return true; return true;
} }
@ -259,9 +277,9 @@ void CText::FlushCache()
} }
} }
//TODO: fix this m_lastCachedFont = nullptr;
Destroy(); m_lastFontType = FONT_COMMON;
Create(); m_lastFontSize = 0;
} }
int CText::GetTabSize() int CText::GetTabSize()

View File

@ -256,6 +256,8 @@ public:
//! Flushes cached textures //! Flushes cached textures
void FlushCache(); void FlushCache();
//! Try to load new font files
bool ReloadFonts();
//@{ //@{
//! Tab size management //! Tab size management