Mana
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
ResourceManager Class Reference

A class for loading and managing resources. More...

#include <resourcemanager.h>

Public Member Functions

 ResourceManager ()
 
 ~ResourceManager ()
 Destructor.
 
ResourceRef< ImagegetImage (const std::string &idPath)
 Loads the Image resource found at the given identifier path.
 
ResourceRef< MusicgetMusic (const std::string &path)
 Loads the Music resource found at the given path.
 
ResourceRef< SoundEffectgetSoundEffect (const std::string &path)
 Loads the SoundEffect resource found at the given path.
 
ResourceRef< ImageSetgetImageSet (const std::string &imagePath, int w, int h)
 Loads a image set based on the image referenced by the given path and the supplied sprite sizes.
 
ResourceRef< SpriteDefgetSprite (const std::string &path, int variant=0)
 Loads a SpriteDef based on a given path and the supplied variant.
 

Static Public Member Functions

static bool addToSearchPath (const std::string &path, bool append)
 Adds a directory or archive to the search path.
 
static void searchAndAddArchives (const std::string &path, const std::string &ext, bool append)
 Searches for zip files and adds them to the search path.
 
static std::string getPath (const std::string &file)
 Returns the real path to a file.
 
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 Member Functions

Resourceget (const std::string &idPath, const std::function< Resource *()> &generator)
 Looks up a resource, creating it with the generator function if it does not exist.
 
void release (Resource *)
 Releases a resource, placing it in the set of orphaned resources.
 
void remove (Resource *)
 Removes a resource from the list of resources managed by the resource manager.
 
void cleanOrphans ()
 

Static Private Member Functions

static void cleanUp (Resource *resource)
 Deletes the resource after logging a cleanup message.
 

Private Attributes

std::map< std::string, Resource * > mResources
 
std::map< std::string, Resource * > mOrphanedResources
 
time_t mOldestOrphan = 0
 

Static Private Attributes

static ResourceManagerinstance = nullptr
 

Friends

class Resource
 

Detailed Description

A class for loading and managing resources.

Definition at line 40 of file resourcemanager.h.

Constructor & Destructor Documentation

◆ ResourceManager()

ResourceManager::ResourceManager ( )

Definition at line 46 of file resourcemanager.cpp.

◆ ~ResourceManager()

ResourceManager::~ResourceManager ( )

Destructor.

Cleans up remaining resources, warning about resources that were still referenced.

Definition at line 51 of file resourcemanager.cpp.

Member Function Documentation

◆ addToSearchPath()

bool ResourceManager::addToSearchPath ( const std::string &  path,
bool  append 
)
static

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 130 of file resourcemanager.cpp.

◆ cleanOrphans()

void ResourceManager::cleanOrphans ( )
private

Definition at line 99 of file resourcemanager.cpp.

◆ cleanUp()

void ResourceManager::cleanUp ( Resource resource)
staticprivate

Deletes the resource after logging a cleanup message.

Definition at line 85 of file resourcemanager.cpp.

◆ deleteInstance()

void ResourceManager::deleteInstance ( )
static

Deletes the class instance if it exists.

Definition at line 308 of file resourcemanager.cpp.

◆ get()

Resource * ResourceManager::get ( const std::string &  idPath,
const std::function< Resource *()> &  generator 
)
private

Looks up a resource, creating it with the generator function if it does not exist.

Does not increment the reference count of the resource.

Parameters
idPathThe resource identifier path.
generatorA function for generating the resource.
Returns
A valid resource or nullptr if the resource could not be generated.

Definition at line 182 of file resourcemanager.cpp.

◆ getImage()

ResourceRef< Image > ResourceManager::getImage ( const std::string &  idPath)

Loads the Image resource found at the given identifier path.

The path can include a dye specification after a '|' character.

Definition at line 232 of file resourcemanager.cpp.

◆ getImageSet()

ResourceRef< ImageSet > ResourceManager::getImageSet ( const std::string &  imagePath,
int  w,
int  h 
)

Loads a image set based on the image referenced by the given path and the supplied sprite sizes.

Definition at line 253 of file resourcemanager.cpp.

◆ getInstance()

ResourceManager * ResourceManager::getInstance ( )
static

Returns an instance of the class, creating one if it does not already exist.

Definition at line 300 of file resourcemanager.cpp.

◆ getMusic()

ResourceRef< Music > ResourceManager::getMusic ( const std::string &  path)

Loads the Music resource found at the given path.

Definition at line 212 of file resourcemanager.cpp.

◆ getPath()

std::string ResourceManager::getPath ( const std::string &  file)
static

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
fileThe file to get the real path to.
Returns
The real path.

Definition at line 163 of file resourcemanager.cpp.

◆ getSoundEffect()

ResourceRef< SoundEffect > ResourceManager::getSoundEffect ( const std::string &  path)

Loads the SoundEffect resource found at the given path.

Definition at line 222 of file resourcemanager.cpp.

◆ getSprite()

ResourceRef< SpriteDef > ResourceManager::getSprite ( const std::string &  path,
int  variant = 0 
)

Loads a SpriteDef based on a given path and the supplied variant.

Definition at line 268 of file resourcemanager.cpp.

◆ release()

void ResourceManager::release ( Resource res)
private

Releases a resource, placing it in the set of orphaned resources.

Only called from Resource::decRef,

Definition at line 278 of file resourcemanager.cpp.

◆ remove()

void ResourceManager::remove ( Resource res)
private

Removes a resource from the list of resources managed by the resource manager.

Only called from Resource::decRef,

Definition at line 295 of file resourcemanager.cpp.

◆ searchAndAddArchives()

void ResourceManager::searchAndAddArchives ( const std::string &  path,
const std::string &  ext,
bool  append 
)
static

Searches for zip files and adds them to the search path.

Definition at line 141 of file resourcemanager.cpp.

Friends And Related Symbol Documentation

◆ Resource

friend class Resource
friend

Definition at line 42 of file resourcemanager.h.

Member Data Documentation

◆ instance

ResourceManager * ResourceManager::instance = nullptr
staticprivate

Definition at line 149 of file resourcemanager.h.

◆ mOldestOrphan

time_t ResourceManager::mOldestOrphan = 0
private

Definition at line 152 of file resourcemanager.h.

◆ mOrphanedResources

std::map<std::string, Resource *> ResourceManager::mOrphanedResources
private

Definition at line 151 of file resourcemanager.h.

◆ mResources

std::map<std::string, Resource *> ResourceManager::mResources
private

Definition at line 150 of file resourcemanager.h.


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