From: Daniel Karbach Date: Wed, 29 Aug 2012 22:01:08 +0000 (+0200) Subject: store pointers rather than objects in Interpreter X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=8cd1fa19b3697a78579ae254933bdcf7345ba9ff;p=l2e.git store pointers rather than objects in Interpreter to avoid problems due to reallocation --- diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 65db6a0..12dacb8 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -37,9 +37,18 @@ Interpreter::~Interpreter() { for (vector::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) { delete *i; } + for (vector::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) { + delete *i; + } for (vector::const_iterator i(images.begin()), end(images.end()); i != end; ++i) { SDL_FreeSurface(*i); } + for (vector::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) { + delete *i; + } + for (vector::const_iterator i(sprites.begin()), end(sprites.end()); i != end; ++i) { + delete *i; + } } @@ -67,7 +76,7 @@ Hero *Interpreter::GetHero(const std::string &name) { map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { if (i->second.IsCompatible(HERO)) { - return &heroes[i->second.index]; + return heroes[i->second.index]; } else { throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero"); } @@ -80,7 +89,7 @@ Monster *Interpreter::GetMonster(const std::string &name) { map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { if (i->second.IsCompatible(MONSTER)) { - return &monsters[i->second.index]; + return monsters[i->second.index]; } else { throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster"); } @@ -106,7 +115,7 @@ Sprite *Interpreter::GetSprite(const std::string &name) { map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { if (i->second.IsCompatible(SPRITE)) { - return &sprites[i->second.index]; + return sprites[i->second.index]; } else { throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite"); } @@ -260,17 +269,22 @@ Vector Interpreter::GetVector(const Value &v) { void Interpreter::ReadObject(const Definition &dfn) { if (dfn.TypeName() == "Hero") { - heroes.push_back(Hero()); - ReadHero(heroes.back(), *dfn.GetProperties()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, heroes.size() - 1))); + Hero *hero(new Hero); + int index(heroes.size()); + heroes.push_back(hero); + ReadHero(*hero, *dfn.GetProperties()); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, index))); } else if (dfn.TypeName() == "Monster") { - monsters.push_back(Monster()); - ReadMonster(monsters.back(), *dfn.GetProperties()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, monsters.size() - 1))); + Monster *monster(new Monster); + int index(monsters.size()); + monsters.push_back(monster); + ReadMonster(*monster, *dfn.GetProperties()); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, index))); } else if (dfn.TypeName() == "Sprite") { + Sprite *sprite(new Sprite); int index(sprites.size()); - sprites.push_back(Sprite()); - ReadSprite(sprites.back(), *dfn.GetProperties()); + sprites.push_back(sprite); + ReadSprite(*sprite, *dfn.GetProperties()); parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, index))); } else { throw Error("unhandled object type: " + dfn.TypeName()); diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h index a32b3ca..aea5fbe 100644 --- a/src/loader/Interpreter.h +++ b/src/loader/Interpreter.h @@ -107,11 +107,11 @@ private: std::map parsedDefinitions; std::vector animations; - std::vector heroes; + std::vector heroes; std::vector images; - std::vector monsters; + std::vector monsters; std::vector numbers; - std::vector sprites; + std::vector sprites; };