First pass at reducing the overall memory footprint: moved GetFormattedText() out of CardPrimitives and into MTGCard. The idea being, only keep the formatted text around for cards that are actually in use.

Also fixed a subtle memory pooling issue in the RenderCountersBig() routine:  if you're in regular card display mode, the idea is that formatted text is only fetched if you flip into alternate render mode.  However, in this function, if counters are being drawn, it would fetch the formatted text in order to determine where to draw the counters, EVEN IF the counters count was zero.  So it had nothing to draw, but it meanwhile pooled the formatted strings into memory anyway.
This commit is contained in:
wrenczes@gmail.com
2011-04-22 16:04:41 +00:00
parent 58e340f30c
commit 224b140f9f
8 changed files with 167 additions and 159 deletions
-2
View File
@@ -13,7 +13,6 @@ using namespace std;
class CardPrimitive class CardPrimitive
{ {
protected: protected:
vector<string> ftdText;
string lcname; string lcname;
ManaCost manaCost; ManaCost manaCost;
@@ -84,7 +83,6 @@ public:
void getRestrictions(); void getRestrictions();
void setOtherRestrictions(string _restriction); void setOtherRestrictions(string _restriction);
void getOtherRestrictions(); void getOtherRestrictions();
const vector<string>& formattedText();
}; };
+4
View File
@@ -24,6 +24,7 @@ protected:
int mtgid; int mtgid;
char rarity; char rarity;
char image_name[MTGCARD_NAME_SIZE]; char image_name[MTGCARD_NAME_SIZE];
vector<string> mFormattedText;
int init(); int init();
public: public:
@@ -33,12 +34,15 @@ public:
MTGCard(); MTGCard();
MTGCard(int set_id); MTGCard(int set_id);
MTGCard(MTGCard * source); MTGCard(MTGCard * source);
virtual ~MTGCard();
void setMTGId(int id); void setMTGId(int id);
void setRarity(char _rarity); void setRarity(char _rarity);
//void setImageName( char * value); //void setImageName( char * value);
void setPrimitive(CardPrimitive * cp); void setPrimitive(CardPrimitive * cp);
const vector<string>& GetFormattedText();
int getMTGId() const; int getMTGId() const;
int getId() const; int getId() const;
char getRarity() const; char getRarity() const;
+131 -131
View File
@@ -24,43 +24,44 @@ struct Pos;
#include <list> #include <list>
using namespace std; using namespace std;
class MTGCardInstance: public CardPrimitive, public MTGCard, public Damageable { class MTGCardInstance: public CardPrimitive, public MTGCard, public Damageable
protected: {
int untapping; protected:
int nb_damages; int untapping;
string sample; int nb_damages;
int tapped; string sample;
int lifeOrig; int tapped;
MTGPlayerCards * belongs_to; int lifeOrig;
MTGCardInstance * getNextPartner(); MTGPlayerCards * belongs_to;
void initMTGCI(); MTGCardInstance * getNextPartner();
int addBlocker(MTGCardInstance * c); void initMTGCI();
int removeBlocker(MTGCardInstance * c); int addBlocker(MTGCardInstance * c);
int init(); int removeBlocker(MTGCardInstance * c);
public: int init();
int setAttacker(int value); public:
int setAttacker(int value);
int setDefenser(MTGCardInstance * c); int setDefenser(MTGCardInstance * c);
MTGGameZone * currentZone; MTGGameZone * currentZone;
Pos* view; Pos* view;
int X; int X;
int XX; int XX;
int alternateCostPaid[ManaCost::MANA_PAID_WITH_RETRACE + 1]; int alternateCostPaid[ManaCost::MANA_PAID_WITH_RETRACE + 1];
int paymenttype; int paymenttype;
int castMethod; /* Tells if the card reached its current zone by being cast or not (brought into the zone by an effect). non 0 == cast, 0 == not cast */ int castMethod; /* Tells if the card reached its current zone by being cast or not (brought into the zone by an effect). non 0 == cast, 0 == not cast */
int frozen; int frozen;
int sunburst; int sunburst;
int equipment; int equipment;
int auras; int auras;
bool wasDealtDamage; bool wasDealtDamage;
bool damageToOpponent; bool damageToOpponent;
bool damageToController; bool damageToController;
int reduxamount; int reduxamount;
int flanked; int flanked;
int regenerateTokens; int regenerateTokens;
int isToken; int isToken;
int origpower; int origpower;
int origtoughness; int origtoughness;
int isMultiColored; int isMultiColored;
int isBlackAndWhite; int isBlackAndWhite;
int isRedAndBlue; int isRedAndBlue;
int isBlackAndGreen; int isBlackAndGreen;
@@ -88,121 +89,120 @@ class MTGCardInstance: public CardPrimitive, public MTGCard, public Damageable {
int fresh; int fresh;
int MaxLevelUp; int MaxLevelUp;
Player * lastController; Player * lastController;
MTGGameZone * getCurrentZone(); MTGGameZone * getCurrentZone();
MTGGameZone * previousZone; MTGGameZone * previousZone;
MTGCardInstance * previous; MTGCardInstance * previous;
MTGCardInstance * next; MTGCardInstance * next;
int doDamageTest; int doDamageTest;
int summoningSickness; int summoningSickness;
ManaCost reducedCost; ManaCost reducedCost;
ManaCost increasedCost; ManaCost increasedCost;
ManaCost * getReducedManaCost(); ManaCost * getReducedManaCost();
ManaCost * getIncreasedManaCost(); ManaCost * getIncreasedManaCost();
bool matchesCastFilter(int castMethod); bool matchesCastFilter(int castMethod);
// The recommended method to test for summoning Sickness ! // The recommended method to test for summoning Sickness !
int hasSummoningSickness(); int hasSummoningSickness();
MTGCardInstance * changeController(Player * newcontroller); MTGCardInstance * changeController(Player * newcontroller);
Player * owner; Player * owner;
Counters * counters; Counters * counters;
int typeAsTarget(){return TARGET_CARD;} int typeAsTarget(){return TARGET_CARD;}
const string getDisplayName() const; const string getDisplayName() const;
MTGCardInstance * target; MTGCardInstance * target;
Targetable * backupTargets[MAX_TARGETS]; Targetable * backupTargets[MAX_TARGETS];
//types //types
void addType(char * type_text); void addType(char * type_text);
virtual void addType(int id); virtual void addType(int id);
void setType(const char * type_text); void setType(const char * type_text);
void setSubtype( string value); void setSubtype( string value);
int removeType(string value, int removeAll = 0); int removeType(string value, int removeAll = 0);
int removeType(int value, int removeAll = 0); int removeType(int value, int removeAll = 0);
//dangerranking is a hint to Ai which creatures are the ones it should be targetting for effects. //dangerranking is a hint to Ai which creatures are the ones it should be targetting for effects.
int DangerRanking(); int DangerRanking();
//Combat //Combat
bool blocked; //Blocked this turn or not? bool blocked; //Blocked this turn or not?
MTGCardInstance * defenser; MTGCardInstance * defenser;
list<MTGCardInstance *>blockers; list<MTGCardInstance *>blockers;
int attacker; int attacker;
int toggleDefenser(MTGCardInstance * opponent); int toggleDefenser(MTGCardInstance * opponent);
int raiseBlockerRankOrder(MTGCardInstance * blocker); int raiseBlockerRankOrder(MTGCardInstance * blocker);
//Returns rank of the card in blockers if it is a blocker of this (starting at 1), 0 otherwise //Returns rank of the card in blockers if it is a blocker of this (starting at 1), 0 otherwise
int getDefenserRank(MTGCardInstance * blocker); int getDefenserRank(MTGCardInstance * blocker);
int toggleAttacker(); int toggleAttacker();
MTGCardInstance * banding; // If belongs to a band when attacking MTGCardInstance * banding; // If belongs to a band when attacking
int canBlock(); int canBlock();
int canBlock(MTGCardInstance * opponent); int canBlock(MTGCardInstance * opponent);
int canAttack(); int canAttack();
int isAttacker(); int isAttacker();
MTGCardInstance * isDefenser(); MTGCardInstance * isDefenser();
int initAttackersDefensers(); int initAttackersDefensers();
MTGCardInstance * getNextOpponent(MTGCardInstance * previous=NULL); MTGCardInstance * getNextOpponent(MTGCardInstance * previous=NULL);
int nbOpponents(); int nbOpponents();
int stepPower(CombatStep step); int stepPower(CombatStep step);
int afterDamage(); int afterDamage();
int has(int ability); int has(int ability);
int cleanup(); int cleanup();
MTGCard * model; MTGCard * model;
MTGCardInstance(); MTGCardInstance();
MTGCardInstance(MTGCard * card, MTGPlayerCards * _belongs_to); MTGCardInstance(MTGCard * card, MTGPlayerCards * _belongs_to);
int regenerate(); int regenerate();
int triggerRegenerate(); int triggerRegenerate();
Player * controller(); Player * controller();
virtual ~MTGCardInstance(); virtual ~MTGCardInstance();
int bury(); int bury();
int destroy(); int destroy();
int addToToughness(int value);
int setToughness(int value);
int addToToughness(int value); vector<TargetChooser *>protections;
int setToughness(int value); int addProtection(TargetChooser * tc);
int removeProtection(TargetChooser *tc, int erase = 0);
int protectedAgainst(MTGCardInstance * card);
vector<TargetChooser *>protections; vector<TargetChooser *>canttarget;
int addProtection(TargetChooser * tc); int addCantBeTarget(TargetChooser * tc);
int removeProtection(TargetChooser *tc, int erase = 0); int removeCantBeTarget(TargetChooser *tc, int erase = 0);
int protectedAgainst(MTGCardInstance * card); int CantBeTargetby(MTGCardInstance * card);
vector<TargetChooser *>canttarget; vector<TargetChooser *>cantBeBlockedBys;
int addCantBeTarget(TargetChooser * tc); int addCantBeBlockedBy(TargetChooser * tc);
int removeCantBeTarget(TargetChooser *tc, int erase = 0); int removeCantBeBlockedBy(TargetChooser *tc, int erase = 0);
int CantBeTargetby(MTGCardInstance * card); int cantBeBlockedBy(MTGCardInstance * card);
vector<TargetChooser *>cantBeBlockedBys; void copy(MTGCardInstance * card);
int addCantBeBlockedBy(TargetChooser * tc);
int removeCantBeBlockedBy(TargetChooser *tc, int erase = 0);
int cantBeBlockedBy(MTGCardInstance * card);
void copy(MTGCardInstance * card); void setUntapping();
int isUntapping();
int isTapped();
void untap();
void tap();
void attemptUntap();
void setUntapping(); void eventattacked();
int isUntapping(); void eventattackedAlone();
int isTapped(); void eventattackednotblocked();
void untap(); void eventattackedblocked(MTGCardInstance * opponent);
void tap(); void eventblocked(MTGCardInstance * opponent);
void attemptUntap();
void eventattacked(); int isInPlay();
void eventattackedAlone(); JSample * getSample();
void eventattackednotblocked();
void eventattackedblocked(MTGCardInstance * opponent);
void eventblocked(MTGCardInstance * opponent);
int isInPlay(); JQuadPtr getIcon();
JSample * getSample();
JQuadPtr getIcon(); ostream& toString(ostream&) const;
ostream& toString(ostream&) const; static MTGCardInstance AnyCard;
static MTGCardInstance NoCard;
static MTGCardInstance AnyCard; static MTGCardInstance ExtraRules[2];
static MTGCardInstance NoCard;
static MTGCardInstance ExtraRules[2];
}; };
-2
View File
@@ -1409,7 +1409,6 @@ int AIPlayerBaka::computeActions()
//am im not interupting my own spell, or the stack contains nothing. //am im not interupting my own spell, or the stack contains nothing.
{ {
findingCard = true; findingCard = true;
CardDescriptor cd;
ManaCost * icurrentMana = getPotentialMana(); ManaCost * icurrentMana = getPotentialMana();
bool ipotential = false; bool ipotential = false;
if (icurrentMana->getConvertedCost()) if (icurrentMana->getConvertedCost())
@@ -1454,7 +1453,6 @@ int AIPlayerBaka::computeActions()
} }
else if(p == this && g->mLayers->stackLayer()->count(0, NOT_RESOLVED) == 0) else if(p == this && g->mLayers->stackLayer()->count(0, NOT_RESOLVED) == 0)
{ //standard actions { //standard actions
CardDescriptor cd;
switch (currentGamePhase) switch (currentGamePhase)
{ {
case Constants::MTG_PHASE_FIRSTMAIN: case Constants::MTG_PHASE_FIRSTMAIN:
+4 -4
View File
@@ -368,7 +368,7 @@ void CardGui::AlternateRender(MTGCard * card, const Pos& pos)
// Write the description // Write the description
{ {
font->SetScale(kWidthScaleFactor * pos.actZ); font->SetScale(kWidthScaleFactor * pos.actZ);
const std::vector<string> txt = card->data->formattedText(); const std::vector<string> txt = card->GetFormattedText();
unsigned i = 0; unsigned i = 0;
unsigned h = neofont ? 14 : 11; unsigned h = neofont ? 14 : 11;
for (std::vector<string>::const_iterator it = txt.begin(); it != txt.end(); ++it, ++i) for (std::vector<string>::const_iterator it = txt.begin(); it != txt.end(); ++it, ++i)
@@ -577,7 +577,7 @@ void CardGui::TinyCropRender(MTGCard * card, const Pos& pos, JQuad * quad)
renderer->RenderQuad(q.get(), x, pos.actY, pos.actT, scale, scale); renderer->RenderQuad(q.get(), x, pos.actY, pos.actT, scale, scale);
} }
const std::vector<string> txt = card->data->formattedText(); const std::vector<string> txt = card->GetFormattedText();
size_t nbTextLines = txt.size(); size_t nbTextLines = txt.size();
//Render the image on top of that //Render the image on top of that
@@ -785,12 +785,12 @@ void CardGui::RenderBig(MTGCard* card, const Pos& pos)
void CardGui::RenderCountersBig(const Pos& pos) void CardGui::RenderCountersBig(const Pos& pos)
{ {
// Write Named Counters // Write Named Counters
if (card->counters) if (card->counters && card->counters->mCount > 0)
{ {
WFont * font = WResourceManager::Instance()->GetWFont(Fonts::MAGIC_FONT); WFont * font = WResourceManager::Instance()->GetWFont(Fonts::MAGIC_FONT);
font->SetColor(ARGB((int)pos.actA, 0, 0, 0)); font->SetColor(ARGB((int)pos.actA, 0, 0, 0));
font->SetScale(kWidthScaleFactor * pos.actZ); font->SetScale(kWidthScaleFactor * pos.actZ);
std::vector<string> txt = card->formattedText(); std::vector<string> txt = card->GetFormattedText();
unsigned i = txt.size() + 1; unsigned i = txt.size() + 1;
Counter * c = NULL; Counter * c = NULL;
for (int t = 0; t < card->counters->mCount; t++, i++) for (int t = 0; t < card->counters->mCount; t++, i++)
-17
View File
@@ -61,23 +61,6 @@ int CardPrimitive::init()
return 1; return 1;
} }
const vector<string>& CardPrimitive::formattedText()
{
if (ftdText.empty())
{
std::string s = 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, ftdText);
}
return ftdText;
}
bool CardPrimitive::isCreature() bool CardPrimitive::isCreature()
{ {
return hasSubtype(Subtypes::TYPE_CREATURE); return hasSubtype(Subtypes::TYPE_CREATURE);
+2 -1
View File
@@ -251,10 +251,11 @@ void GameApp::Create()
JSoundSystem::GetInstance()->SetSfxVolume(options[Options::SFXVOLUME].number); JSoundSystem::GetInstance()->SetSfxVolume(options[Options::SFXVOLUME].number);
JSoundSystem::GetInstance()->SetMusicVolume(options[Options::MUSICVOLUME].number); JSoundSystem::GetInstance()->SetMusicVolume(options[Options::MUSICVOLUME].number);
DebugTrace("size of MTGCardInstance: " << sizeof(MTGCardInstance));
DebugTrace("size of MTGCard: "<< sizeof(MTGCard)); DebugTrace("size of MTGCard: "<< sizeof(MTGCard));
DebugTrace("size of CardPrimitive: "<< sizeof(CardPrimitive)); DebugTrace("size of CardPrimitive: "<< sizeof(CardPrimitive));
DebugTrace("size of ExtraCost: " << sizeof(ExtraCost)); DebugTrace("size of ExtraCost: " << sizeof(ExtraCost));
DebugTrace("Size of ManaCost: " << sizeof(ManaCost)); DebugTrace("size of ManaCost: " << sizeof(ManaCost));
LOG("Game Creation Done."); LOG("Game Creation Done.");
} }
+25 -1
View File
@@ -23,9 +23,9 @@ MTGCard::MTGCard(int set_id)
init(); init();
setId = set_id; setId = set_id;
} }
MTGCard::MTGCard(MTGCard * source) MTGCard::MTGCard(MTGCard * source)
{ {
strcpy(image_name, source->image_name); strcpy(image_name, source->image_name);
rarity = source->rarity; rarity = source->rarity;
mtgid = source->mtgid; mtgid = source->mtgid;
@@ -33,6 +33,10 @@ MTGCard::MTGCard(MTGCard * source)
data = source->data; data = source->data;
} }
MTGCard::~MTGCard()
{
}
int MTGCard::init() int MTGCard::init()
{ {
setId = 0; setId = 0;
@@ -82,3 +86,23 @@ void MTGCard::setPrimitive(CardPrimitive * cp)
{ {
data = cp; data = cp;
} }
const vector<string>& 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;
}