X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FLoader.cpp;h=dbfbe3186fdd93e8a508110c082753197778cc3c;hb=1970312e983541d32d4ff73c81b8d90156a7bb99;hp=9c8739aabee00191584852973971f203fc8afa5e;hpb=45bb35881a10720ae26701ddf075f756419cd627;p=l2e.git diff --git a/src/loader/Loader.cpp b/src/loader/Loader.cpp index 9c8739a..dbfbe31 100644 --- a/src/loader/Loader.cpp +++ b/src/loader/Loader.cpp @@ -5,6 +5,7 @@ #include #include #include +#include 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(src + i->destOffset); + std::map::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,25 +126,35 @@ 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(object + field.Offset())); - if (*dest) { - *dest = src + *reinterpret_cast(dest); + if (field.IsReferenced() || field.IsAggregate()) { + char **dest(reinterpret_cast(object + field.Offset())); + if (*dest) { + *dest = src + *reinterpret_cast(dest); + } + } else { + const TypeDescription &nestedType + = TypeDescription::Get(field.TypeId()); + LoadObject(src, object + field.Offset(), nestedType); } } } void Loader::LoadArrays(char *src, Array *begin, Array *end) { for (Array *i = begin; i < end; i = i->Next()) { - if (!i->ref) { - continue; - } - for (char *j = i->Data(), *end = i->Data() + i->size; - j < end; j += sizeof(void *)) { - *reinterpret_cast(j) = - src + *reinterpret_cast(j); + if (i->ref) { + for (char *j = i->Data(), *end = i->Data() + i->size; + j < end; j += sizeof(void *)) { + unsigned int offset = *reinterpret_cast(j); + if (offset) { + *reinterpret_cast(j) = src + offset; + } + } + } else { + const TypeDescription &td = TypeDescription::Get(i->typeId); + for (char *j = i->Data(), *end = i->Data() + i->size; + j < end; j += td.Size()) { + LoadObject(src, j, td); + } } } }