summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorSteve Cotton <steve@s.cotton.clara.co.uk>2010-02-13 12:32:28 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-02-13 12:39:13 +0100
commit040e336fb70e331983f9537b2d0daa2d8a962fce (patch)
treedd6f4613f6d51f7cb3eb8a716e0807424d90f4bc /src/gui
parentcfc9b326d3c2ab3ea3f4187fa55e7a5645ee7d16 (diff)
downloadmana-040e336fb70e331983f9537b2d0daa2d8a962fce.tar.gz
mana-040e336fb70e331983f9537b2d0daa2d8a962fce.tar.bz2
mana-040e336fb70e331983f9537b2d0daa2d8a962fce.tar.xz
mana-040e336fb70e331983f9537b2d0daa2d8a962fce.zip
Optimise TrueTypeFont::getWidth
Use the cache created by TTF::drawString, drops the cost of TTF::getWidth from 5% of runtime to 0.5%. It increases the cost of calculating line-wrapping in the BrowserBox, but overall it's a saving, even with the BrowserBox recalculating on every redraw.
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/truetypefont.cpp17
-rw-r--r--src/gui/truetypefont.h3
2 files changed, 19 insertions, 1 deletions
diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp
index 5243a175..fe507489 100644
--- a/src/gui/truetypefont.cpp
+++ b/src/gui/truetypefont.cpp
@@ -1,6 +1,7 @@
/*
* The Mana World
* Copyright (C) 2004-2010 The Mana World Development Team
+ * Copyright (C) 2009 Aethyra Development Team
*
* This file is part of The Mana World.
*
@@ -42,6 +43,11 @@ class TextChunk
delete img;
}
+ bool operator==(const std::string &str) const
+ {
+ return (str == text);
+ }
+
bool operator==(const TextChunk &chunk) const
{
return (chunk.text == text && chunk.color == color);
@@ -162,6 +168,17 @@ void TrueTypeFont::drawString(gcn::Graphics *graphics,
int TrueTypeFont::getWidth(const std::string &text) const
{
+ for (CacheIterator i = mCache.begin(); i != mCache.end(); i++)
+ {
+ if ((*i) == text)
+ {
+ // Raise priority: move it to front
+ // Assumption is that TTF::draw will be called next
+ mCache.splice(mCache.begin(), mCache, i);
+ return i->img->getWidth();
+ }
+ }
+
int w, h;
TTF_SizeUTF8(mFont, text.c_str(), &w, &h);
return w;
diff --git a/src/gui/truetypefont.h b/src/gui/truetypefont.h
index 9211a7dc..45e1ec70 100644
--- a/src/gui/truetypefont.h
+++ b/src/gui/truetypefont.h
@@ -1,6 +1,7 @@
/*
* The Mana World
* Copyright (C) 2004-2010 The Mana World Development Team
+ * Copyright (C) 2009 Aethyra Development Team
*
* This file is part of The Mana World.
*
@@ -75,7 +76,7 @@ class TrueTypeFont : public gcn::Font
TTF_Font *mFont;
// Word surfaces cache
- std::list<TextChunk> mCache;
+ mutable std::list<TextChunk> mCache;
};
#endif