Here's my little resource Manager:
#include <string>
#include <map>
template <typename Resource>
class ResourceManager
{
public:
bool load(std::string address)
{
Resource temporaryResource;
bool success = temporaryResource.loadFromFile(address); // does not handle errors
resourceHolder.insert( {address, temporaryResource} );
return success;
}
Resource* get(std::string ID)
{
return &resourceHolder[ID];
}
private:
std::map <std::string, Resource> resourceHolder;
};
Now I have to use it like this:
ResourceManager <sf::Font> fontmanager;
ResourceManager <sf::Texture> texturemanager;
if (!fontmanager.load("consolas.ttf")) return -1;
if (!texturemanager.load("spritesheet.png")) return -2;
Sf::Sprite sprite(*texturemanager.get("spritesheet.png"));
sf::Text text("string", *fontmanager.get("consolas.ttf"), 24);
window.draw(sprite);
window.draw(text);
Instead I would like to have a single super resource manager that could handle all types of stuff that have
loadFromFile function, so I could use it like this:
ResourceManager UltimateResourceManager;
if (!UltimateResourceManager.load("consolas.ttf")) return -1;
if (!UltimateResourceManager.load("spritesheet.png")) return -2;
Sf::Sprite sprite(*UltimateResourceManager.get("spritesheet.png"));
sf::Text text("string", *UltimateResourceManager.get("consolas.ttf"), 24);
window.draw(sprite);
window.draw(text);
I've tried to find a base class in SFML that provides pure virtual
loadFromFile function but my search was unsuccessful.
So, is my idea possible, and more importantly, can it be made in easy way?
Oh, and yeah, I know that my resource manager can't delete any resources, but I think that's OK as long as my game doesn't eat more than 100MB RAM, and that's a problem not to worry about for a long time for me