]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
added interpretation of Font and Frame objects
[l2e.git] / src / loader / Interpreter.cpp
index 9f4b77dcbac93c20b43df8cb1d8017bbf14e5230..d5a61a669a9bd177c023325cfec12031664395d1 100644 (file)
@@ -12,6 +12,8 @@
 #include "../battle/Monster.h"
 #include "../battle/PartyLayout.h"
 #include "../graphics/ComplexAnimation.h"
+#include "../graphics/Font.h"
+#include "../graphics/Frame.h"
 #include "../graphics/SimpleAnimation.h"
 #include "../graphics/Sprite.h"
 
@@ -24,6 +26,8 @@ using battle::Monster;
 using battle::PartyLayout;
 using battle::Stats;
 using graphics::Animation;
+using graphics::Font;
+using graphics::Frame;
 using graphics::ComplexAnimation;
 using graphics::SimpleAnimation;
 using graphics::Sprite;
@@ -43,6 +47,12 @@ Interpreter::~Interpreter() {
        for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<Font *>::const_iterator i(fonts.begin()), end(fonts.end()); i != end; ++i) {
+               delete *i;
+       }
+       for (vector<Frame *>::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
                SDL_FreeSurface(*i);
        }
@@ -92,6 +102,32 @@ bool Interpreter::GetBoolean(const std::string &name) const {
        }
 }
 
+Font *Interpreter::GetFont(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == FONT) {
+                       return fonts[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Font");
+               }
+       } else {
+               throw Error("access to undefined Font " + name);
+       }
+}
+
+Frame *Interpreter::GetFrame(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == FRAME) {
+                       return frames[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Frame");
+               }
+       } else {
+               throw Error("access to undefined Frame " + name);
+       }
+}
+
 Hero *Interpreter::GetHero(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -269,6 +305,28 @@ bool Interpreter::GetBoolean(const Value &v) {
        }
 }
 
+Font *Interpreter::GetFont(const Value &v) {
+       if (v.IsLiteral()) {
+               Font *f(new Font);
+               ReadFont(*f, *v.GetLiteral().GetProperties());
+               return f;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetFont(v.GetIdentifier());
+       }
+}
+
+Frame *Interpreter::GetFrame(const Value &v) {
+       if (v.IsLiteral()) {
+               Frame *f(new Frame);
+               ReadFrame(*f, *v.GetLiteral().GetProperties());
+               return f;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetFrame(v.GetIdentifier());
+       }
+}
+
 SDL_Surface *Interpreter::GetImage(const Value &v) {
        const char *file(GetString(v));
        SDL_Surface *image(IMG_Load(file));
@@ -357,6 +415,18 @@ void Interpreter::ReadObject(const Definition &dfn) {
                complexAnimations.push_back(animation);
                ReadComplexAnimation(*animation, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COMPLEX_ANIMATION, index)));
+       } else if (dfn.TypeName() == "Font") {
+               Font *font(new Font);
+               int index(fonts.size());
+               fonts.push_back(font);
+               ReadFont(*font, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FONT, index)));
+       } else if (dfn.TypeName() == "Frame") {
+               Frame *frame(new Frame);
+               int index(frames.size());
+               frames.push_back(frame);
+               ReadFrame(*frame, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FRAME, index)));
        } else if (dfn.TypeName() == "Hero") {
                Hero *hero(new Hero);
                int index(heroes.size());
@@ -428,6 +498,36 @@ void Interpreter::ReadComplexAnimationFrame(ComplexAnimation::FrameProp &f, cons
        }
 }
 
+void Interpreter::ReadFont(Font &f, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "sprite") {
+                       f.SetSprite(GetSprite(*i->second));
+               } else if (i->first == "columnoffset") {
+                       f.SetColOffset(GetNumber(*i->second));
+               } else if (i->first == "rowoffset") {
+                       f.SetRowOffset(GetNumber(*i->second));
+               } else {
+                       throw Error("unknown Font property: " + i->first);
+               }
+       }
+}
+
+void Interpreter::ReadFrame(Frame &f, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "image") {
+                       f.SetSurface(GetImage(*i->second));
+               } else if (i->first == "border") {
+                       f.SetBorderSize(GetVector(*i->second));
+               } else if (i->first == "repeat") {
+                       f.SetRepeatSize(GetVector(*i->second));
+               } else if (i->first == "offset") {
+                       f.SetOffset(GetVector(*i->second));
+               } else {
+                       throw Error("unknown Frame 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") {