From ba07ca233439243647919164b9e30e7cc08b755f Mon Sep 17 00:00:00 2001 From: "wrenczes@gmail.com" Date: Sat, 23 Apr 2011 09:54:19 +0000 Subject: [PATCH] Improvement on my last change for reducing the formatted text caching: chopped it out altogether. I ran some profiling to see how much time on the psp it took to process the formatting on the description text (ie word wrapping it into several lines to fit on a card), and it ends up taking an average of 0.14 ms per card. For context, 60 fps is 16 ms. So clearly the formatting time is not the bottleneck here, and we gain no real performance caching the text, but lose memory due to pooling. So I cut it out entirely of the base class and we format on the fly during renders. I also profiled this after the fact, as we have an open bug on poor drawing performance on psp when in text mode - it turns out that in the AlternateRender() function, my numbers look like this: 10.2 seconds were spent in JBLFont::DrawString(); 0.5 seconds were spent in my new helper FormatText() call, so we know where the perf hit is now. I compared before & after on the psp with this mod, and the difference really isn't perceptible. (It's still juddery, but no worse than before.) I'll look at the DrawString() call next to see if we can make it any faster, although at first glance it looks like a pain. --- projects/mtg/include/CardPrimitive.h | 2 +- projects/mtg/include/MTGCard.h | 3 --- projects/mtg/src/CardGui.cpp | 24 +++++++++++++++++++++--- projects/mtg/src/CardPrimitive.cpp | 4 ++-- projects/mtg/src/GameStateMenu.cpp | 9 +++++++++ projects/mtg/src/MTGCard.cpp | 20 -------------------- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/projects/mtg/include/CardPrimitive.h b/projects/mtg/include/CardPrimitive.h index faac2c049..1057cf7f4 100644 --- a/projects/mtg/include/CardPrimitive.h +++ b/projects/mtg/include/CardPrimitive.h @@ -53,7 +53,7 @@ public: int has(int ability); void setText(const string& value); - const char * getText(); + const string& getText(); void addMagicText(string value); void addMagicText(string value, string zone); diff --git a/projects/mtg/include/MTGCard.h b/projects/mtg/include/MTGCard.h index f61800f79..ff6d10e77 100644 --- a/projects/mtg/include/MTGCard.h +++ b/projects/mtg/include/MTGCard.h @@ -29,7 +29,6 @@ protected: int mtgid; char rarity; char image_name[MTGCARD_NAME_SIZE]; - vector mFormattedText; int init(); public: @@ -46,8 +45,6 @@ public: //void setImageName( char * value); void setPrimitive(CardPrimitive * cp); - const vector& GetFormattedText(); - int getMTGId() const; int getId() const; char getRarity() const; diff --git a/projects/mtg/src/CardGui.cpp b/projects/mtg/src/CardGui.cpp index aff187fd2..990803f65 100644 --- a/projects/mtg/src/CardGui.cpp +++ b/projects/mtg/src/CardGui.cpp @@ -33,6 +33,18 @@ namespace { return cosf(2 * M_PI * (value - 35) / 256.0f); } + + void FormatText(std::string inText, std::vector& outFormattedText) + { + std::string::size_type found = inText.find_first_of("{}"); + while (found != string::npos) + { + inText[found] = '/'; + found = inText.find_first_of("{}", found + 1); + } + WFont * mFont = WResourceManager::Instance()->GetWFont(Fonts::MAGIC_FONT); + mFont->FormatText(inText, outFormattedText); + } } CardGui::CardGui(MTGCardInstance* card, float x, float y) : @@ -368,7 +380,10 @@ void CardGui::AlternateRender(MTGCard * card, const Pos& pos) // Write the description { font->SetScale(kWidthScaleFactor * pos.actZ); - const std::vector txt = card->GetFormattedText(); + + std::vector txt; + FormatText(card->data->getText(), txt); + unsigned i = 0; unsigned h = neofont ? 14 : 11; for (std::vector::const_iterator it = txt.begin(); it != txt.end(); ++it, ++i) @@ -577,7 +592,8 @@ void CardGui::TinyCropRender(MTGCard * card, const Pos& pos, JQuad * quad) renderer->RenderQuad(q.get(), x, pos.actY, pos.actT, scale, scale); } - const std::vector txt = card->GetFormattedText(); + std::vector txt; + FormatText(card->data->getText(), txt); size_t nbTextLines = txt.size(); //Render the image on top of that @@ -790,7 +806,9 @@ void CardGui::RenderCountersBig(const Pos& pos) WFont * font = WResourceManager::Instance()->GetWFont(Fonts::MAGIC_FONT); font->SetColor(ARGB((int)pos.actA, 0, 0, 0)); font->SetScale(kWidthScaleFactor * pos.actZ); - std::vector txt = card->GetFormattedText(); + + std::vector txt; + FormatText(card->data->getText(), txt); unsigned i = txt.size() + 1; Counter * c = NULL; for (int t = 0; t < card->counters->mCount; t++, i++) diff --git a/projects/mtg/src/CardPrimitive.cpp b/projects/mtg/src/CardPrimitive.cpp index 74f1fd96d..24d1762fa 100644 --- a/projects/mtg/src/CardPrimitive.cpp +++ b/projects/mtg/src/CardPrimitive.cpp @@ -218,9 +218,9 @@ void CardPrimitive::setText(const string& value) text = value; } -const char * CardPrimitive::getText() +const string& CardPrimitive::getText() { - return text.c_str(); + return text; } void CardPrimitive::addMagicText(string value) diff --git a/projects/mtg/src/GameStateMenu.cpp b/projects/mtg/src/GameStateMenu.cpp index 3b16b10e4..63e2552db 100644 --- a/projects/mtg/src/GameStateMenu.cpp +++ b/projects/mtg/src/GameStateMenu.cpp @@ -475,7 +475,16 @@ void GameStateMenu::Update(float dt) } if (primitivesLoadCounter < (int) (primitives.size())) { +#ifdef _DEBUG + int startTime = JGEGetTime(); +#endif MTGCollection()->load(primitives[primitivesLoadCounter].c_str()); +#if _DEBUG + int endTime = JGEGetTime(); + float elapsedTime = (endTime - startTime); + DebugTrace("Time elapsed while loading " << primitives[primitivesLoadCounter] << " : " << elapsedTime << " ms"); +#endif + primitivesLoadCounter++; break; } diff --git a/projects/mtg/src/MTGCard.cpp b/projects/mtg/src/MTGCard.cpp index 2b971092b..1572373ce 100644 --- a/projects/mtg/src/MTGCard.cpp +++ b/projects/mtg/src/MTGCard.cpp @@ -88,23 +88,3 @@ void MTGCard::setPrimitive(CardPrimitive * cp) { data = cp; } - -const vector& MTGCard::GetFormattedText() -{ - if (mFormattedText.empty()) - { - if (data != NULL) - { - std::string s = data->text; - std::string::size_type found = s.find_first_of("{}"); - while (found != string::npos) - { - s[found] = '/'; - found = s.find_first_of("{}", found + 1); - } - WFont * mFont = WResourceManager::Instance()->GetWFont(Fonts::MAGIC_FONT); - mFont->FormatText(s, mFormattedText); - } - } - return mFormattedText; -} \ No newline at end of file