After looking around for a Resource Manager I found the imagemanager class on the French side of the Wiki. It is a fairly nice class to be sure, and well commented.
But I thought it would be nice to make a more generalized version. So here for the English side of the Wiki, my Generalized Resource Manager class and it's implementations:
First we have our templated base class, this allows us to consolidate nearly all of our common Resource Manager functionality in one generalized base class:
/** MIT License: Copyright (c) 2008 Adam W. Brown (www.darkrockstudios.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef RESOURCEMANAGER_H_INCLUDED #define RESOURCEMANAGER_H_INCLUDED #include <string> #include <map> template< class T > class ResourceManager { public: typedef std::pair< std::string, T* > Resource; typedef std::map< std::string, T* > ResourceMap; private: ResourceMap m_resource; T* find( const std::string& strId ) { T* resource = NULL; typename ResourceMap::iterator it = m_resource.find( strId ); if( it != m_resource.end() ) { resource = it->second; } return resource; } protected: virtual T* load( const std::string& strId ) = 0; public: ResourceManager() { } virtual ~ResourceManager() { releaseAllResources(); } T* getResource( const std::string& strId ) { T* resource = find( strId ); if( resource == NULL ) { resource = load( strId ); // If the resource loaded successfully, add it do the resource map if( resource != NULL ) m_resource.insert( Resource( strId, resource ) ); } return resource; } void releaseResource( const std::string& strId ) { T* resource = find( strId ); if( resource != NULL ) { delete resource; m_resource.erase( m_resource.find( strId ) ); } } void releaseAllResources() { while( m_resource.begin() != m_resource.end() ) { delete m_resource.begin()->second; m_resource.erase( m_resource.begin() ); } } }; #endif // RESOURCEMANAGER_H_INCLUDED
Next we want to specialize our base class to be a Resource Manager for a specific type of Resource, lets make one for sf::Image first:
#ifndef IMAGEMANAGER_H_INCLUDED #define IMAGEMANAGER_H_INCLUDED #include <SFML/Graphics.hpp> #include "ResourceManager.h" class ImageManager : public ResourceManager< sf::Image > { private: protected: virtual sf::Image* load( const std::string& strId ); public: }; extern ImageManager gImageManager; #endif // IMAGEMANAGER_H_INCLUDED
#include <iostream> #include "ImageManager.h" using namespace std; ImageManager gImageManager; sf::Image* ImageManager::load( const std::string& strId ) { sf::Image* image = new sf::Image(); if( !image->LoadFromFile( strId ) ) { cout << "[WARN] ImageManager failed to load: " << strId << endl; delete image; image = NULL; } return image; }
Ok, now we have our specialized ImageManager class, and a global instance of it called gImageManager where ever we include “ImageManager.h”
Now lets use it!
#include "Graphics.hpp" #include "ImageManager.h" int main() { sf::Sprite sprite; sf::Image* image = gImageManager.getResource( "test.png" ); if( image != NULL ) { sprite.SetImage( *image ); } return EXIT_SUCCESS; }
And that's it!
Here, just for completeness, is an implementation for Audio Buffers:
NOTE: Make SURE you stop any sounds that are playing before you release the resource from this manager or your program will crash! This goes especially for when you are using a global instance, and the program is shutting down, make sure all sounds are stopped before the program gets to the SoundBufferManager's destructor!
#ifndef SOUNDBUFFERMANAGER_H_INCLUDED #define SOUNDBUFFERMANAGER_H_INCLUDED #include <SFML/Audio.hpp> #include "ResourceManager.h" class SoundBufferManager : public ResourceManager< sf::SoundBuffer > { private: protected: virtual sf::SoundBuffer* load( const std::string& strId ); public: }; extern SoundBufferManager gSoundBufferManager; #endif // SOUNDBUFFERMANAGER_H_INCLUDED
#include <iostream> #include "SoundBufferManager.h" using namespace std; SoundBufferManager gSoundBufferManager; sf::SoundBuffer* SoundBufferManager::load( const std::string& strId ) { sf::SoundBuffer* buffer = new sf::SoundBuffer(); if( !buffer->LoadFromFile( strId ) ) { cout << "[WARN] SoundBufferManager failed to load: " << strId << endl; delete buffer; buffer = NULL; } return buffer; }
One last thing, the destructor caused some problems for me with deleting things that other threads were still using. If you are just using the global instances of the managers, it really isn't necessary since the program is ending when the destructor is called anyway, so technically your leaking memory, but only for a split second before the program finishes and exits. There fore, if it gives you any problems at all (a crash when exiting the program) just comment out the body of the destructor and you should be good to go.
If anyone uses this I'd love to hear about your project, just for sh*ts and giggles.
Wavesonics - http://www.darkrockstudios.com/