From b4e47fb41a19ca09c02bcd009b28cb7c3caa2256 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 22 Jan 2012 18:31:25 +0100 Subject: Use SDL_RWops directly on top of PhysFS This avoids the creation of a temporary buffer containing a complete file for the sole purpose of wrapping it up in an SDL_RWops. The necessary wrapper is by Ryan C. Gordon and is included in the PhysFS repository under 'extras'. Reviewed-by: Yohann Ferreira Conflicts: mana.files src/CMakeLists.txt src/resources/resourcemanager.cpp src/resources/soundeffect.cpp --- src/resources/resourcemanager.cpp | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) (limited to 'src/resources/resourcemanager.cpp') diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 3376394bc..c26526b97 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -35,6 +35,7 @@ #include "resources/spritedef.h" #include "utils/mkdir.h" +#include "utils/physfsrwops.h" #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include @@ -397,19 +399,16 @@ struct ResourceLoader ResourceManager *manager; std::string path; ResourceManager::loader fun; + static Resource *load(void *v) { if (!v) return nullptr; ResourceLoader *l = static_cast< ResourceLoader * >(v); - int fileSize; - if (!l->manager) + SDL_RWops *rw = PHYSFSRWOPS_openRead(l->path.c_str()); + if (!rw) return nullptr; - void *buffer = l->manager->loadFile(l->path, fileSize); - if (!buffer) - return nullptr; - Resource *res = l->fun(buffer, fileSize); - free(buffer); + Resource *res = l->fun(rw); return res; } }; @@ -451,16 +450,14 @@ struct DyedImageLoader d = new Dye(path.substr(p + 1)); path = path.substr(0, p); } - int fileSize; - void *buffer = l->manager->loadFile(path, fileSize); - if (!buffer) + SDL_RWops *rw = PHYSFSRWOPS_openRead(path.c_str()); + if (!rw) { delete d; return nullptr; } - Resource *res = d ? Image::load(buffer, fileSize, *d) - : Image::load(buffer, fileSize); - free(buffer); + Resource *res = d ? Image::load(rw, *d) + : Image::load(rw); delete d; return res; } @@ -695,18 +692,10 @@ void ResourceManager::saveTextFile(std::string path, std::string name, SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) { - int fileSize; - void *buffer = loadFile(filename, fileSize); - SDL_Surface *tmp = nullptr; - - if (buffer) - { - SDL_RWops *rw = SDL_RWFromMem(buffer, fileSize); - tmp = IMG_Load_RW(rw, 1); - ::free(buffer); - } - - return tmp; + SDL_Surface *surface = nullptr; + if (SDL_RWops *rw = PHYSFSRWOPS_openRead(filename.c_str())) + surface = IMG_Load_RW(rw, 1); + return surface; } void ResourceManager::scheduleDelete(SDL_Surface* surface) -- cgit v1.2.3-70-g09d2