diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-03-12 15:33:21 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-03-25 14:34:31 +0000 |
commit | 1aa382f08aadb0d4392b139204cb88df3685ab7e (patch) | |
tree | b63a75c0e79dfbdd06491b0ebe09d970475d1fc4 /src/gui | |
parent | f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6 (diff) | |
download | mana-1aa382f08aadb0d4392b139204cb88df3685ab7e.tar.gz mana-1aa382f08aadb0d4392b139204cb88df3685ab7e.tar.bz2 mana-1aa382f08aadb0d4392b139204cb88df3685ab7e.tar.xz mana-1aa382f08aadb0d4392b139204cb88df3685ab7e.zip |
Avoid string allocations during text rendering and sizing
Now a text chunk has a maximum length of 4k characters, but that should
be plenty of space since they're only single lines.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/truetypefont.cpp | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp index 167a66b2..adb49780 100644 --- a/src/gui/truetypefont.cpp +++ b/src/gui/truetypefont.cpp @@ -26,13 +26,22 @@ #include "resources/image.h" -#include "utils/stringutils.h" - #include <guichan/color.hpp> #include <guichan/exception.hpp> const unsigned int CACHE_SIZE = 256; +static const char *getSafeUtf8String(const std::string &text) +{ + static int UTF8_MAX_SIZE = 10; + + static char buf[4096]; + const int len = std::min(text.size(), sizeof(buf) - UTF8_MAX_SIZE); + memcpy(buf, text.c_str(), len); + memset(buf + len, 0, UTF8_MAX_SIZE); + return buf; +} + class TextChunk { public: @@ -62,7 +71,6 @@ class TextChunk const char *str = getSafeUtf8String(text); SDL_Surface *surface = TTF_RenderUTF8_Blended( font, str, sdlCol); - delete[] str; if (!surface) { @@ -180,7 +188,6 @@ int TrueTypeFont::getWidth(const std::string &text) const int w, h; const char *str = getSafeUtf8String(text); TTF_SizeUTF8(mFont, str, &w, &h); - delete[] str; return w; } |