#include <resourcemanager.h>
Definition at line 41 of file resourcemanager.h.
Public Types | |
typedef Resource *(* | loader )(void *, unsigned) |
typedef Resource *(* | generator )(void *) |
Public Member Functions | |
ResourceManager () | |
~ResourceManager () | |
Destructor. | |
bool | setWriteDir (const std::string &path) |
Sets the write directory. | |
bool | addToSearchPath (const std::string &path, bool append) |
Adds a directory or archive to the search path. | |
void | searchAndAddArchives (const std::string &path, const std::string &ext, bool append) |
Searches for zip files and adds them to the search path. | |
bool | mkdir (const std::string &path) |
Creates a directory in the write path. | |
bool | exists (const std::string &path) |
Checks whether the given file or directory exists in the search path. | |
bool | isDirectory (const std::string &path) |
Checks whether the given path is a directory. | |
std::string | getPath (const std::string &file) |
Returns the real path to a file. | |
Resource * | get (const std::string &idPath, generator fun, void *data) |
Creates a resource and adds it to the resource map. | |
Resource * | load (const std::string &path, loader fun) |
Loads a resource from a file and adds it to the resource map. | |
bool | copyFile (const std::string &src, const std::string &dst) |
Copies a file from one place to another (useful for extracting raw files from a zip archive, for example). | |
Image * | getImage (const std::string &idPath) |
Convenience wrapper around ResourceManager::get for loading images. | |
Music * | getMusic (const std::string &idPath) |
Convenience wrapper around ResourceManager::get for loading songs. | |
SoundEffect * | getSoundEffect (const std::string &idPath) |
Convenience wrapper around ResourceManager::get for loading samples. | |
ImageSet * | getImageSet (const std::string &imagePath, int w, int h) |
Creates a image set based on the image referenced by the given path and the supplied sprite sizes. | |
SpriteDef * | getSprite (const std::string &path, int variant=0) |
Creates a sprite definition based on a given path and the supplied variant. | |
void | release (Resource *) |
Releases a resource, placing it in the set of orphaned resources. | |
void * | loadFile (const std::string &fileName, int &fileSize) |
Allocates data into a buffer pointer for raw data loading. | |
std::vector< std::string > | loadTextFile (const std::string &fileName) |
Retrieves the contents of a text file. | |
SDL_Surface * | loadSDLSurface (const std::string &filename) |
Loads the given filename as an SDL surface. | |
Static Public Member Functions | |
static ResourceManager * | getInstance () |
Returns an instance of the class, creating one if it does not already exist. | |
static void | deleteInstance () |
Deletes the class instance if it exists. | |
Private Types | |
typedef std::map< std::string, Resource * > | Resources |
typedef Resources::iterator | ResourceIterator |
Private Member Functions | |
void | cleanOrphans () |
Static Private Member Functions | |
static void | cleanUp (Resource *resource) |
Deletes the resource after logging a cleanup message. | |
Private Attributes | |
Resources | mResources |
Resources | mOrphanedResources |
time_t | mOldestOrphan |
Static Private Attributes | |
static ResourceManager * | instance = NULL |
Friends | |
class | Resource |
typedef Resource*(* ResourceManager::loader)(void *, unsigned) |
typedef Resource*(* ResourceManager::generator)(void *) |
typedef std::map<std::string, Resource*> ResourceManager::Resources [private] |
Definition at line 216 of file resourcemanager.h.
typedef Resources::iterator ResourceManager::ResourceIterator [private] |
Definition at line 217 of file resourcemanager.h.
ResourceManager::ResourceManager | ( | ) |
Definition at line 42 of file resourcemanager.cpp.
ResourceManager::~ResourceManager | ( | ) |
Destructor.
Cleans up remaining resources, warning about resources that were still referenced.
Definition at line 48 of file resourcemanager.cpp.
bool ResourceManager::setWriteDir | ( | const std::string & | path | ) |
Sets the write directory.
path | The path of the directory to be added. |
true
on success, false
otherwise. Definition at line 141 of file resourcemanager.cpp.
bool ResourceManager::addToSearchPath | ( | const std::string & | path, | |
bool | append | |||
) |
Adds a directory or archive to the search path.
If append is true then the directory is added to the end of the search path, otherwise it is added at the front.
true
on success, false
otherwise. Definition at line 146 of file resourcemanager.cpp.
void ResourceManager::searchAndAddArchives | ( | const std::string & | path, | |
const std::string & | ext, | |||
bool | append | |||
) |
Searches for zip files and adds them to the search path.
Definition at line 156 of file resourcemanager.cpp.
bool ResourceManager::mkdir | ( | const std::string & | path | ) |
bool ResourceManager::exists | ( | const std::string & | path | ) |
Checks whether the given file or directory exists in the search path.
Definition at line 187 of file resourcemanager.cpp.
bool ResourceManager::isDirectory | ( | const std::string & | path | ) |
std::string ResourceManager::getPath | ( | const std::string & | file | ) |
Returns the real path to a file.
Note that this method will always return a path, it does not check whether the file exists.
file | The file to get the real path to. |
Definition at line 197 of file resourcemanager.cpp.
Creates a resource and adds it to the resource map.
idPath | The resource identifier path. | |
fun | A function for generating the resource. | |
data | Extra parameters for the generator. |
NULL
if the resource could not be generated. Definition at line 217 of file resourcemanager.cpp.
Loads a resource from a file and adds it to the resource map.
path | The file name. | |
fun | A function for parsing the file. |
NULL
if the resource could not be loaded. Definition at line 269 of file resourcemanager.cpp.
bool ResourceManager::copyFile | ( | const std::string & | src, | |
const std::string & | dst | |||
) |
Copies a file from one place to another (useful for extracting raw files from a zip archive, for example).
src | Source file name | |
dst | Destination file name |
Definition at line 422 of file resourcemanager.cpp.
Image * ResourceManager::getImage | ( | const std::string & | idPath | ) |
Convenience wrapper around ResourceManager::get for loading images.
Definition at line 311 of file resourcemanager.cpp.
Music * ResourceManager::getMusic | ( | const std::string & | idPath | ) |
Convenience wrapper around ResourceManager::get for loading songs.
Definition at line 275 of file resourcemanager.cpp.
SoundEffect * ResourceManager::getSoundEffect | ( | const std::string & | idPath | ) |
Convenience wrapper around ResourceManager::get for loading samples.
Definition at line 280 of file resourcemanager.cpp.
ImageSet * ResourceManager::getImageSet | ( | const std::string & | imagePath, | |
int | w, | |||
int | h | |||
) |
Creates a image set based on the image referenced by the given path and the supplied sprite sizes.
Definition at line 333 of file resourcemanager.cpp.
SpriteDef * ResourceManager::getSprite | ( | const std::string & | path, | |
int | variant = 0 | |||
) |
Creates a sprite definition based on a given path and the supplied variant.
Definition at line 353 of file resourcemanager.cpp.
void ResourceManager::release | ( | Resource * | res | ) |
Releases a resource, placing it in the set of orphaned resources.
Definition at line 361 of file resourcemanager.cpp.
void * ResourceManager::loadFile | ( | const std::string & | fileName, | |
int & | fileSize | |||
) |
Allocates data into a buffer pointer for raw data loading.
The returned data is expected to be freed using free()
.
fileName | The name of the file to be loaded. | |
fileSize | The size of the file that was loaded. |
NULL
on fail. Definition at line 393 of file resourcemanager.cpp.
std::vector< std::string > ResourceManager::loadTextFile | ( | const std::string & | fileName | ) |
SDL_Surface * ResourceManager::loadSDLSurface | ( | const std::string & | filename | ) |
Loads the given filename as an SDL surface.
The returned surface is expected to be freed by the caller using SDL_FreeSurface.
Definition at line 472 of file resourcemanager.cpp.
ResourceManager * ResourceManager::getInstance | ( | ) | [static] |
Returns an instance of the class, creating one if it does not already exist.
Definition at line 379 of file resourcemanager.cpp.
void ResourceManager::deleteInstance | ( | ) | [static] |
void ResourceManager::cleanUp | ( | Resource * | resource | ) | [static, private] |
Deletes the resource after logging a cleanup message.
Definition at line 95 of file resourcemanager.cpp.
void ResourceManager::cleanOrphans | ( | ) | [private] |
Definition at line 109 of file resourcemanager.cpp.
friend class Resource [friend] |
Definition at line 43 of file resourcemanager.h.
ResourceManager * ResourceManager::instance = NULL [static, private] |
Definition at line 215 of file resourcemanager.h.
Resources ResourceManager::mResources [private] |
Definition at line 218 of file resourcemanager.h.
Resources ResourceManager::mOrphanedResources [private] |
Definition at line 219 of file resourcemanager.h.
time_t ResourceManager::mOldestOrphan [private] |
Definition at line 220 of file resourcemanager.h.