]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Loader.cpp
ref and load images in l2o files
[l2e.git] / src / loader / Loader.cpp
index 1e0050dbc022a64c1d7896428caad0b152dac2f0..dbfbe3186fdd93e8a508110c082753197778cc3c 100644 (file)
@@ -5,6 +5,7 @@
 #include <fstream>
 #include <stdexcept>
 #include <utility>
+#include <SDL_image.h>
 
 using std::make_pair;
 using std::map;
@@ -57,6 +58,9 @@ void Loader::Load(const std::string &filePath) {
                LoadArrays(header->ident,
                                header->ArraysBegin(),
                                header->ArraysEnd());
+               LoadImages(header->ident,
+                               header->ImagesBegin(),
+                               header->ImagesEnd());
        } catch (...) {
                delete[] block;
                throw;
@@ -93,13 +97,27 @@ void Loader::LoadExternals(char *src, External *begin, External *end) {
        }
 }
 
+void Loader::LoadImages(char *src, Image *begin, Image *end) {
+       for (Image *i = begin; i != end; ++i) {
+               const string path(src + i->pathOffset);
+               SDL_Surface **dest = reinterpret_cast<SDL_Surface **>(src + i->destOffset);
+               std::map<string, SDL_Surface *>::const_iterator
+                               found(images.find(path));
+               if (found != images.end()) {
+                       *dest = found->second;
+               } else {
+                       SDL_Surface *loaded = IMG_Load(path.c_str());
+                       images.insert(make_pair(path, loaded));
+                       *dest = loaded;
+               }
+       }
+}
+
 void Loader::LoadObjects(char *src, Object *begin, Object *end) {
        for (Object *i = begin; i < end; i = i->Next()) {
                const TypeDescription &td =
                                TypeDescription::Get(i->typeId);
-               if (td.NeedsLinking()) {
-                       LoadObject(src, i->RawObject(), td);
-               }
+               LoadObject(src, i->RawObject(), td);
        }
 }
 
@@ -108,12 +126,15 @@ void Loader::LoadObject(char *src, char *object, const TypeDescription &td) {
                        i(td.FieldsBegin()), end(td.FieldsEnd());
                        i != end; ++i) {
                const FieldDescription &field = i->second;
-               if (!field.IsReferenced() && !field.IsAggregate()) {
-                       continue;
-               }
-               char **dest(reinterpret_cast<char **>(object + field.Offset()));
-               if (*dest) {
-                       *dest = src + *reinterpret_cast<unsigned int *>(dest);
+               if (field.IsReferenced() || field.IsAggregate()) {
+                       char **dest(reinterpret_cast<char **>(object + field.Offset()));
+                       if (*dest) {
+                               *dest = src + *reinterpret_cast<unsigned int *>(dest);
+                       }
+               } else {
+                       const TypeDescription &nestedType
+                                       = TypeDescription::Get(field.TypeId());
+                       LoadObject(src, object + field.Offset(), nestedType);
                }
        }
 }
@@ -123,8 +144,10 @@ void Loader::LoadArrays(char *src, Array *begin, Array *end) {
                if (i->ref) {
                        for (char *j = i->Data(), *end = i->Data() + i->size;
                                        j < end; j += sizeof(void *)) {
-                               *reinterpret_cast<char **>(j) =
-                                               src + *reinterpret_cast<unsigned int *>(j);
+                               unsigned int offset = *reinterpret_cast<unsigned int *>(j);
+                               if (offset) {
+                                       *reinterpret_cast<char **>(j) = src + offset;
+                               }
                        }
                } else {
                        const TypeDescription &td = TypeDescription::Get(i->typeId);