From ccb63bd29e0cc7957be53cb40889496d04527d7a Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 12 Oct 2013 21:12:54 +0200 Subject: Fixed memory leaks in CText --- src/graphics/engine/text.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/graphics') diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 610bfcf..ffd2eb2 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -140,6 +140,12 @@ void CText::FlushCache() for (auto jt = mf->fonts.begin(); jt != mf->fonts.end(); ++jt) { CachedFont *f = (*jt).second; + for (auto ct = f->cache.begin(); ct != f->cache.end(); ++ct) + { + Texture tex; + tex.id = (*ct).second.id; + m_device->DestroyTexture(tex); + } f->cache.clear(); } } @@ -358,14 +364,22 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) CachedFont* cf = GetOrOpenFont(font, size); assert(cf != nullptr); - CharTexture tex; + Math::Point charSize; auto it = cf->cache.find(ch); if (it != cf->cache.end()) - tex = (*it).second; + { + charSize = (*it).second.charSize; + } else - tex = CreateCharTexture(ch, cf); + { + Math::IntPoint wndSize; + std::string text; + text.append({ch.c1, ch.c2, ch.c3}); + TTF_SizeUTF8(cf->font, text.c_str(), &wndSize.x, &wndSize.y); + charSize = m_engine->WindowToInterfaceSize(wndSize); + } - return tex.charSize.x * width; + return charSize.x * width; } @@ -896,7 +910,6 @@ CharTexture CText::CreateCharTexture(UTF8Char ch, CachedFont* font) if (! tex.Valid()) { m_error = "Texture create error"; - return texture; } else { -- cgit v1.2.3-1-g7c22