ResourceManager Class Reference

#include <resourcemanager.h>

List of all members.


Detailed Description

A class for loading and managing resources.

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.
Resourceget (const std::string &idPath, generator fun, void *data)
 Creates a resource and adds it to the resource map.
Resourceload (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).
ImagegetImage (const std::string &idPath)
 Convenience wrapper around ResourceManager::get for loading images.
MusicgetMusic (const std::string &idPath)
 Convenience wrapper around ResourceManager::get for loading songs.
SoundEffectgetSoundEffect (const std::string &idPath)
 Convenience wrapper around ResourceManager::get for loading samples.
ImageSetgetImageSet (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.
SpriteDefgetSprite (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 ResourceManagergetInstance ()
 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 ResourceManagerinstance = NULL

Friends

class Resource

Member Typedef Documentation

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.


Constructor & Destructor Documentation

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.


Member Function Documentation

bool ResourceManager::setWriteDir ( const std::string &  path  ) 

Sets the write directory.

Parameters:
path The path of the directory to be added.
Returns:
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.

Returns:
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  ) 

Creates a directory in the write path.

Definition at line 182 of file resourcemanager.cpp.

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  ) 

Checks whether the given path is a directory.

Definition at line 192 of file resourcemanager.cpp.

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.

Parameters:
file The file to get the real path to.
Returns:
The real path.

Definition at line 197 of file resourcemanager.cpp.

Resource * ResourceManager::get ( const std::string &  idPath,
generator  fun,
void *  data 
)

Creates a resource and adds it to the resource map.

Parameters:
idPath The resource identifier path.
fun A function for generating the resource.
data Extra parameters for the generator.
Returns:
A valid resource or NULL if the resource could not be generated.

Definition at line 217 of file resourcemanager.cpp.

Resource * ResourceManager::load ( const std::string &  path,
loader  fun 
)

Loads a resource from a file and adds it to the resource map.

Parameters:
path The file name.
fun A function for parsing the file.
Returns:
A valid resource or 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).

Parameters:
src Source file name
dst Destination file name
Returns:
true on success, false on failure. An error message should be in the log file.

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().

Parameters:
fileName The name of the file to be loaded.
fileSize The size of the file that was loaded.
Returns:
An allocated byte array containing the data that was loaded, or NULL on fail.

Definition at line 393 of file resourcemanager.cpp.

std::vector< std::string > ResourceManager::loadTextFile ( const std::string &  fileName  ) 

Retrieves the contents of a text file.

Definition at line 449 of file resourcemanager.cpp.

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]

Deletes the class instance if it exists.

Definition at line 387 of file resourcemanager.cpp.

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.


Friends And Related Function Documentation

friend class Resource [friend]

Definition at line 43 of file resourcemanager.h.


Member Data Documentation

ResourceManager * ResourceManager::instance = NULL [static, private]

Definition at line 215 of file resourcemanager.h.

Definition at line 218 of file resourcemanager.h.

Definition at line 219 of file resourcemanager.h.

Definition at line 220 of file resourcemanager.h.


The documentation for this class was generated from the following files:

Generated on Sun Apr 26 17:30:44 2009 for The Mana World by  doxygen 1.5.5