]> git.localhorst.tv Git - l2e.git/commitdiff
added interpretation of gauges
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 31 Aug 2012 20:18:06 +0000 (22:18 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 31 Aug 2012 20:34:59 +0000 (22:34 +0200)
src/loader/Interpreter.cpp
src/loader/Interpreter.h

index d5a61a669a9bd177c023325cfec12031664395d1..f616c1a394a0f803d0fee0a828b5f53abf3c32b0 100644 (file)
@@ -14,6 +14,7 @@
 #include "../graphics/ComplexAnimation.h"
 #include "../graphics/Font.h"
 #include "../graphics/Frame.h"
+#include "../graphics/Gauge.h"
 #include "../graphics/SimpleAnimation.h"
 #include "../graphics/Sprite.h"
 
@@ -28,6 +29,7 @@ using battle::Stats;
 using graphics::Animation;
 using graphics::Font;
 using graphics::Frame;
+using graphics::Gauge;
 using graphics::ComplexAnimation;
 using graphics::SimpleAnimation;
 using graphics::Sprite;
@@ -53,6 +55,9 @@ Interpreter::~Interpreter() {
        for (vector<Frame *>::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<Gauge *>::const_iterator i(gauges.begin()), end(gauges.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
                SDL_FreeSurface(*i);
        }
@@ -128,6 +133,19 @@ Frame *Interpreter::GetFrame(const std::string &name) {
        }
 }
 
+Gauge *Interpreter::GetGauge(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == GAUGE) {
+                       return gauges[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Gauge");
+               }
+       } else {
+               throw Error("access to undefined Gauge " + name);
+       }
+}
+
 Hero *Interpreter::GetHero(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -327,6 +345,17 @@ Frame *Interpreter::GetFrame(const Value &v) {
        }
 }
 
+Gauge *Interpreter::GetGauge(const Value &v) {
+       if (v.IsLiteral()) {
+               Gauge *g(new Gauge);
+               ReadGauge(*g, *v.GetLiteral().GetProperties());
+               return g;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetGauge(v.GetIdentifier());
+       }
+}
+
 SDL_Surface *Interpreter::GetImage(const Value &v) {
        const char *file(GetString(v));
        SDL_Surface *image(IMG_Load(file));
@@ -427,6 +456,12 @@ void Interpreter::ReadObject(const Definition &dfn) {
                frames.push_back(frame);
                ReadFrame(*frame, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FRAME, index)));
+       } else if (dfn.TypeName() == "Gauge") {
+               Gauge *gauge(new Gauge);
+               int index(gauges.size());
+               gauges.push_back(gauge);
+               ReadGauge(*gauge, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, GAUGE, index)));
        } else if (dfn.TypeName() == "Hero") {
                Hero *hero(new Hero);
                int index(heroes.size());
@@ -528,6 +563,28 @@ void Interpreter::ReadFrame(Frame &f, const PropertyList &props) {
        }
 }
 
+void Interpreter::ReadGauge(Gauge &g, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "image") {
+                       g.SetSurface(GetImage(*i->second));
+               } else if (i->first == "full") {
+                       g.SetFullOffset(GetVector(*i->second));
+               } else if (i->first == "empty") {
+                       g.SetEmptyOffset(GetVector(*i->second));
+               } else if (i->first == "height") {
+                       g.SetHeight(GetNumber(*i->second));
+               } else if (i->first == "start") {
+                       g.SetStartWidth(GetNumber(*i->second));
+               } else if (i->first == "repeat") {
+                       g.SetRepeatWidth(GetNumber(*i->second));
+               } else if (i->first == "end") {
+                       g.SetEndWidth(GetNumber(*i->second));
+               } else {
+                       throw Error("unknown Gauge property: " + i->first);
+               }
+       }
+}
+
 void Interpreter::ReadHero(Hero &h, const PropertyList &props) {
        for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
                if (i->first == "name") {
index ca9900cb7ada7148801ec47f7a872c19345d0672..cf08009e2907618b042fd2ab72eafb964e5ced9f 100644 (file)
@@ -28,6 +28,7 @@ namespace graphics {
        class Animation;
        class Font;
        class Frame;
+       class Gauge;
        class SimpleAnimation;
        class Sprite;
 }
@@ -62,6 +63,7 @@ public:
        bool GetBoolean(const std::string &name) const;
        graphics::Font *GetFont(const std::string &name);
        graphics::Frame *GetFrame(const std::string &name);
+       graphics::Gauge *GetGauge(const std::string &name);
        battle::Hero *GetHero(const std::string &name);
        battle::Monster *GetMonster(const std::string &name);
        int GetNumber(const std::string &name) const;
@@ -75,6 +77,7 @@ public:
        const std::vector<graphics::ComplexAnimation *> &ComplexAnimations() const { return complexAnimations; }
        const std::vector<graphics::Font *> &Fonts() const { return fonts; }
        const std::vector<graphics::Frame *> &Frames() const { return frames; }
+       const std::vector<graphics::Gauge *> &Gauges() const { return gauges; }
        const std::vector<battle::Hero *> &Heroes() const { return heroes; }
        const std::vector<SDL_Surface *> &Images() const { return images; }
        const std::vector<battle::Monster *> &Monsters() const { return monsters; }
@@ -94,6 +97,7 @@ private:
        bool GetBoolean(const Value &);
        graphics::Font *GetFont(const Value &);
        graphics::Frame *GetFrame(const Value &);
+       graphics::Gauge *GetGauge(const Value &);
        SDL_Surface *GetImage(const Value &);
        int GetNumber(const Value &);
        battle::PartyLayout *GetPartyLayout(const Value &);
@@ -108,6 +112,7 @@ private:
        void ReadComplexAnimationFrame(graphics::ComplexAnimation::FrameProp &, const PropertyList &);
        void ReadFont(graphics::Font &, const PropertyList &);
        void ReadFrame(graphics::Frame &, const PropertyList &);
+       void ReadGauge(graphics::Gauge &, const PropertyList &);
        void ReadHero(battle::Hero &, const PropertyList &);
        void ReadMonster(battle::Monster &, const PropertyList &);
        void ReadPartyLayout(battle::PartyLayout &, const PropertyList &);
@@ -122,6 +127,7 @@ private:
                COMPLEX_ANIMATION,
                FONT,
                FRAME,
+               GAUGE,
                HERO,
                IMAGE,
                MONSTER,
@@ -147,6 +153,7 @@ private:
        std::vector<graphics::ComplexAnimation *> complexAnimations;
        std::vector<graphics::Font *> fonts;
        std::vector<graphics::Frame *> frames;
+       std::vector<graphics::Gauge *> gauges;
        std::vector<battle::Hero *> heroes;
        std::vector<SDL_Surface *> images;
        std::vector<battle::Monster *> monsters;