From d003612c783c271285c437e47f4ab671405fa402 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 31 Aug 2012 22:18:06 +0200 Subject: [PATCH 1/1] added interpretation of gauges --- src/loader/Interpreter.cpp | 57 ++++++++++++++++++++++++++++++++++++++ src/loader/Interpreter.h | 7 +++++ 2 files changed, 64 insertions(+) diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index d5a61a6..f616c1a 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -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::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) { delete *i; } + for (vector::const_iterator i(gauges.begin()), end(gauges.end()); i != end; ++i) { + delete *i; + } for (vector::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::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::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") { diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h index ca9900c..cf08009 100644 --- a/src/loader/Interpreter.h +++ b/src/loader/Interpreter.h @@ -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 &ComplexAnimations() const { return complexAnimations; } const std::vector &Fonts() const { return fonts; } const std::vector &Frames() const { return frames; } + const std::vector &Gauges() const { return gauges; } const std::vector &Heroes() const { return heroes; } const std::vector &Images() const { return images; } const std::vector &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 complexAnimations; std::vector fonts; std::vector frames; + std::vector gauges; std::vector heroes; std::vector images; std::vector monsters; -- 2.39.2