summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-01-22 18:31:25 +0100
committerAndrei Karas <akaras@inbox.ru>2012-01-24 21:46:13 +0300
commitb4e47fb41a19ca09c02bcd009b28cb7c3caa2256 (patch)
treeeada17175623f6281a83aced143bb0940758a547 /src/resources
parent80f61e80228aa0fa617961a4ec30d954fcb15eba (diff)
downloadmv-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.gz
mv-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.bz2
mv-b4e47fb41a19ca09c02bcd009b28cb7c3caa2256.tar.xz
mv-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/resources')
-rw-r--r--src/resources/image.cpp7
-rw-r--r--src/resources/image.h15
-rw-r--r--src/resources/music.cpp5
-rw-r--r--src/resources/music.h5
-rw-r--r--src/resources/resourcemanager.cpp39
-rw-r--r--src/resources/resourcemanager.h4
-rw-r--r--src/resources/soundeffect.cpp8
-rw-r--r--src/resources/soundeffect.h5
8 files changed, 32 insertions, 56 deletions
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.