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();
m_text->FlushCache();
m_text->ReloadFonts();
m_app->GetEventQueue()->AddEvent(Event(EVENT_RELOAD_TEXTURES));
UpdateGroundSpotTextures();

View File

@ -191,17 +191,32 @@ CText::~CText()
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)
{
m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError());
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})
{
m_fonts[static_cast<Gfx::FontType>(type)] = MakeUnique<MultisizeFont>(fontLoader.GetFont(type));
@ -214,7 +229,10 @@ bool CText::Create()
FontType type = (*it).first;
CachedFont* cf = GetOrOpenFont(type, m_defaultSize);
if (cf == nullptr || cf->font == nullptr)
{
m_fonts = std::move(fonts);
return false;
}
}
return true;
@ -259,9 +277,9 @@ void CText::FlushCache()
}
}
//TODO: fix this
Destroy();
Create();
m_lastCachedFont = nullptr;
m_lastFontType = FONT_COMMON;
m_lastFontSize = 0;
}
int CText::GetTabSize()

View File

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