From 040e336fb70e331983f9537b2d0daa2d8a962fce Mon Sep 17 00:00:00 2001 From: Steve Cotton Date: Sat, 13 Feb 2010 12:32:28 +0100 Subject: 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. --- src/gui/truetypefont.cpp | 17 +++++++++++++++++ src/gui/truetypefont.h | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 mCache; + mutable std::list mCache; }; #endif -- cgit v1.2.3-70-g09d2