To expand a bit on the previous answers, a very simple resource manager would always work a bit like this:
std::map<std::string, sf::Texture> textures;
sf::Texture& getTexture(std::string file) {
std::map<std::string, sf::Texture>::iterator a = textures.find(file); // Try to find an existing texture entry.
if (a != textures.end()) // We've got one
return a->second; // Return it
// Load a texture and return it:
if (!textures[file].loadFromFile(file))
;// Here you'd create some warning or similar
return textures[file];
}
The call to
textures[file].loadFromFile(file) has some "magic" in it. When using
operator[] to access a member of the map, it is created, if it doesn't exist. Therefore we can just use the returned (new) texture object and try to load the file.
Note that this implementation always returns a valid texture object, even if loading fails (you'd just get pure white rather than a texture).
To free all textures when the program is shutting down (or you'd like to clear your loaded textures in some way), all you have to do is calling
textures.clear();.