]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
moved most of the remaining data from main to test.l2s
[l2e.git] / src / loader / Interpreter.cpp
index 7c26aaafded4ae6ad86929521e1bc5ebc9fe3e26..5d3b9fccba1dd63722aa49d1c966d71af3c8e614 100644 (file)
@@ -244,6 +244,19 @@ PartyLayout *Interpreter::GetPartyLayout(const std::string &name) {
        }
 }
 
+const char *Interpreter::GetPath(const std::string &name) const {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == PATH) {
+                       return strings[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Path");
+               }
+       } else {
+               throw Error("access to undefined Path " + name);
+       }
+}
+
 Spell *Interpreter::GetSpell(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -273,7 +286,8 @@ Sprite *Interpreter::GetSprite(const std::string &name) {
 const char *Interpreter::GetString(const std::string &name) const {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.type == STRING) {
+               // TODO: enable path to string casting some time
+               if (i->second.type == STRING /* || i->second.type == PATH */) {
                        return strings[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to String");
@@ -348,6 +362,15 @@ void Interpreter::ReadLiteral(const Definition &dfn) {
                        numbers.push_back(dfn.GetLiteral()->GetNumber());
                        parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1)));
                        break;
+               case Literal::PATH:
+                       {
+                               char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
+                               std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
+                               str[dfn.GetLiteral()->GetString().size()] = '\0';
+                               strings.push_back(str);
+                       }
+                       parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PATH, strings.size() - 1)));
+                       break;
                case Literal::STRING:
                        {
                                char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
@@ -451,10 +474,16 @@ Ikari *Interpreter::GetIkari(const Value &v) {
 }
 
 SDL_Surface *Interpreter::GetImage(const Value &v) {
-       const char *file(GetString(v));
-       SDL_Surface *image(IMG_Load(file));
-       images.push_back(image);
-       return image;
+       string path(GetPath(v));
+       map<string, SDL_Surface *>::const_iterator i(imageCache.find(path));
+       if (i == imageCache.end()) {
+               SDL_Surface *image(IMG_Load(path.c_str()));
+               images.push_back(image);
+               imageCache.insert(make_pair(path, image));
+               return image;
+       } else {
+               return i->second;
+       }
 }
 
 Item *Interpreter::GetItem(const Value &v) {
@@ -488,6 +517,15 @@ PartyLayout *Interpreter::GetPartyLayout(const Value &v) {
        }
 }
 
+const char *Interpreter::GetPath(const Value &v) {
+       if (v.IsLiteral()) {
+               return v.GetLiteral().GetString().c_str();
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetPath(v.GetIdentifier());
+       }
+}
+
 const PropertyList *Interpreter::GetPropertyList(const Value &v) {
        if (v.IsLiteral()) {
                return v.GetLiteral().GetProperties();