diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-01-22 18:31:25 +0100 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-01-24 21:46:13 +0300 |
commit | b4e47fb41a19ca09c02bcd009b28cb7c3caa2256 (patch) | |
tree | eada17175623f6281a83aced143bb0940758a547 /src | |
parent | 80f61e80228aa0fa617961a4ec30d954fcb15eba (diff) | |
download | manaplus-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.gz manaplus-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.bz2 manaplus-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.xz manaplus-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/resources/image.cpp | 7 | ||||
-rw-r--r-- | src/resources/image.h | 15 | ||||
-rw-r--r-- | src/resources/music.cpp | 5 | ||||
-rw-r--r-- | src/resources/music.h | 5 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 39 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 4 | ||||
-rw-r--r-- | src/resources/soundeffect.cpp | 8 | ||||
-rw-r--r-- | src/resources/soundeffect.h | 5 | ||||
-rw-r--r-- | src/utils/physfsrwops.h | 88 |
11 files changed, 124 insertions, 56 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fd56c9cba..d2c003559 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -452,6 +452,8 @@ SET(SRCS utils/mathutils.h utils/paths.cpp utils/paths.h + utils/physfsrwops.cpp + utils/physfsrwops.h utils/process.cpp utils/process.h utils/stringutils.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 6948bfcc4..b39145c63 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -459,6 +459,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ utils/mkdir.h \ utils/paths.cpp \ utils/paths.h \ + utils/physfsrwops.cpp \ + utils/physfsrwops.h \ utils/process.cpp \ utils/process.h \ utils/specialfolder.cpp \ diff --git a/src/resources/image.cpp b/src/resources/image.cpp index d94967631..9e9124ab6 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -119,10 +119,8 @@ Image::~Image() unload(); } -Resource *Image::load(void *buffer, unsigned bufferSize) +Resource *Image::load(SDL_RWops *rw) { - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); SDL_Surface *tmpImage = IMG_Load_RW(rw, 1); if (!tmpImage) @@ -137,9 +135,8 @@ Resource *Image::load(void *buffer, unsigned bufferSize) return image; } -Resource *Image::load(void *buffer, unsigned bufferSize, Dye const &dye) +Resource *Image::load(SDL_RWops *rw, Dye const &dye) { - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); SDL_Surface *tmpImage = IMG_Load_RW(rw, 1); if (!tmpImage) diff --git a/src/resources/image.h b/src/resources/image.h index a9f5722cd..d22ed4be2 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -66,28 +66,25 @@ class Image : public Resource virtual ~Image(); /** - * Loads an image from a buffer in memory. + * Loads an image from an SDL_RWops structure. * - * @param buffer The memory buffer containing the image data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the image from. * * @return <code>NULL</code> if an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** - * Loads an image from a buffer in memory and recolors it. + * Loads an image from an SDL_RWops structure and recolors it. * - * @param buffer The memory buffer containing the image data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the image from. * @param dye The dye used to recolor the image. * * @return <code>NULL</code> if an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize, - Dye const &dye); + static Resource *load(SDL_RWops *rw, Dye const &dye); /** * Loads an image from an SDL surface. diff --git a/src/resources/music.cpp b/src/resources/music.cpp index 6e752ab60..7bdd51d36 100644 --- a/src/resources/music.cpp +++ b/src/resources/music.cpp @@ -38,11 +38,8 @@ Music::~Music() Mix_FreeChunk(mChunk); } -Resource *Music::load(void *buffer, unsigned bufferSize) +Resource *Music::load(SDL_RWops *rw) { - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - // Use Mix_LoadMUS to load the raw music data //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1); diff --git a/src/resources/music.h b/src/resources/music.h index 428c02572..6df63b2ac 100644 --- a/src/resources/music.h +++ b/src/resources/music.h @@ -41,13 +41,12 @@ class Music : public Resource /** * Loads a music from a buffer in memory. * - * @param buffer The memory buffer containing the music data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the music data from. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** * Plays the music. 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 <physfs.h> #include <SDL_image.h> @@ -42,6 +43,7 @@ #include <fstream> #include <iostream> #include <sstream> +#include <zlib.h> #include <sys/stat.h> #include <sys/time.h> @@ -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) diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index f0146b8b4..7b61e2eaa 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -37,7 +37,9 @@ class Music; class Resource; class SoundEffect; class SpriteDef; + struct SDL_Surface; +struct SDL_RWops; /** * A class for loading and managing resources. @@ -48,7 +50,7 @@ class ResourceManager public: - typedef Resource *(*loader)(void *, unsigned); + typedef Resource *(*loader)(SDL_RWops *); typedef Resource *(*generator)(void *); ResourceManager(); diff --git a/src/resources/soundeffect.cpp b/src/resources/soundeffect.cpp index eaa323bd6..6a3a980a7 100644 --- a/src/resources/soundeffect.cpp +++ b/src/resources/soundeffect.cpp @@ -31,14 +31,10 @@ SoundEffect::~SoundEffect() Mix_FreeChunk(mChunk); } -Resource *SoundEffect::load(void *buffer, unsigned bufferSize) +Resource *SoundEffect::load(SDL_RWops *rw) { - if (!buffer) + if (!rw) return nullptr; - - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - // Load the music data and free the RWops structure Mix_Chunk *tmpSoundEffect = Mix_LoadWAV_RW(rw, 1); diff --git a/src/resources/soundeffect.h b/src/resources/soundeffect.h index 91ca3bb59..0df7f50d5 100644 --- a/src/resources/soundeffect.h +++ b/src/resources/soundeffect.h @@ -41,13 +41,12 @@ class SoundEffect : public Resource /** * Loads a sample from a buffer in memory. * - * @param buffer The memory buffer containing the sample data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the sample data from. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** * Plays the sample. diff --git a/src/utils/physfsrwops.h b/src/utils/physfsrwops.h new file mode 100644 index 000000000..406fba6f6 --- /dev/null +++ b/src/utils/physfsrwops.h @@ -0,0 +1,88 @@ +/* + * This code provides a glue layer between PhysicsFS and Simple Directmedia + * Layer's (SDL) RWops i/o abstraction. + * + * License: this code is public domain. I make no warranty that it is useful, + * correct, harmless, or environmentally safe. + * + * This particular file may be used however you like, including copying it + * verbatim into a closed-source project, exploiting it commercially, and + * removing any trace of my name from the source (although I hope you won't + * do that). I welcome enhancements and corrections to this file, but I do + * not require you to send me patches if you make changes. This code has + * NO WARRANTY. + * + * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license. + * Please see LICENSE.txt in the root of the source tree. + * + * SDL falls under the LGPL license. You can get SDL at http://www.libsdl.org/ + * + * This file was written by Ryan C. Gordon. (icculus@icculus.org). + */ + +#ifndef _INCLUDE_PHYSFSRWOPS_H_ +#define _INCLUDE_PHYSFSRWOPS_H_ + +#include "physfs.h" +#include "SDL.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Open a platform-independent filename for reading, and make it accessible + * via an SDL_RWops structure. The file will be closed in PhysicsFS when the + * RWops is closed. PhysicsFS should be configured to your liking before + * opening files through this method. + * + * @param filename File to open in platform-independent notation. + * @return A valid SDL_RWops structure on success, NULL on error. Specifics + * of the error can be gleaned from PHYSFS_getLastError(). + */ +__EXPORT__ SDL_RWops *PHYSFSRWOPS_openRead(const char *fname); + +/** + * Open a platform-independent filename for writing, and make it accessible + * via an SDL_RWops structure. The file will be closed in PhysicsFS when the + * RWops is closed. PhysicsFS should be configured to your liking before + * opening files through this method. + * + * @param filename File to open in platform-independent notation. + * @return A valid SDL_RWops structure on success, NULL on error. Specifics + * of the error can be gleaned from PHYSFS_getLastError(). + */ +__EXPORT__ SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname); + +/** + * Open a platform-independent filename for appending, and make it accessible + * via an SDL_RWops structure. The file will be closed in PhysicsFS when the + * RWops is closed. PhysicsFS should be configured to your liking before + * opening files through this method. + * + * @param filename File to open in platform-independent notation. + * @return A valid SDL_RWops structure on success, NULL on error. Specifics + * of the error can be gleaned from PHYSFS_getLastError(). + */ +__EXPORT__ SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname); + +/** + * Make a SDL_RWops from an existing PhysicsFS file handle. You should + * dispose of any references to the handle after successful creation of + * the RWops. The actual PhysicsFS handle will be destroyed when the + * RWops is closed. + * + * @param handle a valid PhysicsFS file handle. + * @return A valid SDL_RWops structure on success, NULL on error. Specifics + * of the error can be gleaned from PHYSFS_getLastError(). + */ +__EXPORT__ SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle); + +#ifdef __cplusplus +} +#endif + +#endif /* include-once blocker */ + +/* end of physfsrwops.h ... */ + |