From e224a077737739a895fe533c9cce93783621d8e9 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 19 May 2015 14:49:19 +0300 Subject: Add fast way for draw not changed text strings. TextChunk with colors and image stored inside draw object. If string or color changed old string image moved to cache. New string image generated or moved from cache. Use new way in drawing string in label. --- src/gui/fonts/font.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'src/gui/fonts/font.cpp') diff --git a/src/gui/fonts/font.cpp b/src/gui/fonts/font.cpp index 27628aec5..58528f383 100644 --- a/src/gui/fonts/font.cpp +++ b/src/gui/fonts/font.cpp @@ -73,6 +73,7 @@ #include "resources/image.h" #include "resources/imagehelper.h" +#include "utils/delete2.h" #include "utils/files.h" #include "utils/paths.h" #include "utils/sdlcheckutils.h" @@ -316,7 +317,7 @@ void Font::doClean() TextChunkList *const cache = &mCache[f]; const size_t size = static_cast(cache->size); #ifdef DEBUG_FONT_COUNTERS - logger->log("ptr: %d, size: %d", f, size); + logger->log("ptr: %u, size: %ld", f, size); #endif if (size > CACHE_SIZE_SMALL3) { @@ -367,3 +368,72 @@ const TextChunkList *Font::getCache() const { return mCache; } + +void Font::generate(TextChunk &chunk) +{ + const std::string &text = chunk.text; + if (text.empty()) + return; + + const unsigned char chr = text[0]; + TextChunkList *const cache = &mCache[chr]; + Color &col = chunk.color; + Color &col2 = chunk.color2; + const int oldAlpha = col.a; + col.a = 255; + + TextChunkSmall key(text, col, col2); + std::map &search = cache->search; + std::map::iterator i = search.find(key); + if (i != search.end()) + { + TextChunk *const chunk2 = (*i).second; + cache->moveToFirst(chunk2); + //search.erase(key); + cache->remove(chunk2); + chunk.img = chunk2->img; +// logger->log("cached image: " + chunk.text); + } + else + { + if (cache->size >= CACHE_SIZE) + { +#ifdef DEBUG_FONT_COUNTERS + mDeleteCounter ++; +#endif + cache->removeBack(); + } +#ifdef DEBUG_FONT_COUNTERS + mCreateCounter ++; +#endif + const float alpha = static_cast(chunk.color.a) / 255.0F; + chunk.generate(mFont, alpha); +// logger->log("generate image: " + chunk.text); + } + col.a = oldAlpha; +} + +void Font::insertChunk(TextChunk *const chunk) +{ + if (!chunk || chunk->text.empty() || !chunk->img) + return; +// logger->log("insert chunk: text=%s, color: %d,%d,%d", +// chunk->text.c_str(), chunk->color.r, chunk->color.g, chunk->color.b); + const unsigned char chr = chunk->text[0]; + TextChunkList *const cache = &mCache[chr]; + + std::map &search = cache->search; + std::map::iterator i + = search.find(TextChunkSmall(chunk->text, + chunk->color, chunk->color2)); + if (i != search.end()) + { + delete2(chunk->img); + return; + } + + TextChunk *const chunk2 = new TextChunk(chunk->text, + chunk->color, chunk->color2, chunk->textFont); + chunk2->img = chunk->img; + cache->insertFirst(chunk2); +} -- cgit v1.2.3-70-g09d2