]> git.localhorst.tv Git - l2e.git/commitdiff
store pointers rather than objects in Interpreter
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 29 Aug 2012 22:01:08 +0000 (00:01 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 29 Aug 2012 22:01:08 +0000 (00:01 +0200)
to avoid problems due to reallocation

src/loader/Interpreter.cpp
src/loader/Interpreter.h

index 65db6a00400bccb30329fa40e3325d97a6ca33cc..12dacb89cd8ab85fe92f3b6bf9f23e0df0a8180d 100644 (file)
@@ -37,9 +37,18 @@ Interpreter::~Interpreter() {
        for (vector<Animation *>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
                SDL_FreeSurface(*i);
        }
+       for (vector<Monster *>::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) {
+               delete *i;
+       }
+       for (vector<Sprite *>::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<string, ParsedDefinition>::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<string, ParsedDefinition>::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<string, ParsedDefinition>::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<int> 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());
index a32b3cad6d4c8a90cd8401f25c868955c2dd23fc..aea5fbe85712433c963cf5f1aeb8016f59fdbc0b 100644 (file)
@@ -107,11 +107,11 @@ private:
        std::map<std::string, ParsedDefinition> parsedDefinitions;
 
        std::vector<graphics::Animation *> animations;
-       std::vector<battle::Hero> heroes;
+       std::vector<battle::Hero *> heroes;
        std::vector<SDL_Surface *> images;
-       std::vector<battle::Monster> monsters;
+       std::vector<battle::Monster *> monsters;
        std::vector<int> numbers;
-       std::vector<graphics::Sprite> sprites;
+       std::vector<graphics::Sprite *> sprites;
 
 };