I'm trying to make a sprite cache for SFML sprites. The basic idea I got was to use a map of an enum and a sprite pointer. Then when I would ask for a certain sprite I'd have a manager that would check if the pointer for that sprite is null. If it is, then it would create a new sprite object and clip it to fit what it's supposed to be. If it isn't null, then it means that the sprite object for this particular enum already exists, and I get that.So far I've made this and it works, but I'm not sure I did it quite right.This is my example code. It contains a small class called SpriteManager which holds the sprite map. It does the before mentioned pointer checking and creating, as well as assigning a texture to, and clipping the sprite.Here's the example code:#include <SFML/Graphics.hpp>#include <algorithm>#include <map>enum Sprites{    ITEM1,    ITEM2,    ITEM3,    ITEM4,    ITEM5};const int WIDTH = 100;const int HEIGHT = 100;typedef std::map< Sprites, sf::Sprite* > SpriteMap;typedef std::map< Sprites, sf::Sprite* >::iterator iter;class SpriteManager{private:    SpriteMap spriteMap;    sf::Texture& m_texture;    void clipSprite(Sprites name)    {        spriteMap.at(name)->setTexture(m_texture);        switch(name)        {        case ITEM1: spriteMap.at(name)->setTextureRect(sf::IntRect(0,0,WIDTH,HEIGHT));break;        case ITEM2: spriteMap.at(name)->setTextureRect(sf::IntRect((1*WIDTH),0,WIDTH,HEIGHT));break;        case ITEM3: spriteMap.at(name)->setTextureRect(sf::IntRect((2*WIDTH),0,WIDTH,HEIGHT));break;        case ITEM4: spriteMap.at(name)->setTextureRect(sf::IntRect((3*WIDTH),0,WIDTH,HEIGHT));break;        case ITEM5: spriteMap.at(name)->setTextureRect(sf::IntRect((4*WIDTH),0,WIDTH,HEIGHT));break;        }    }public:    SpriteManager(sf::Texture& texture) : m_texture(texture)    {}    sf::Sprite* getSprite(Sprites name)    {        if(!spriteMap[name])        {            spriteMap[name] = new sf::Sprite();            clipSprite(name);            return spriteMap[name];        }        else            return spriteMap[name];    }    ~SpriteManager()    {        for(iter i = spriteMap.begin(); i != spriteMap.end(); ++i )        {            delete i->second;        }    }};int main(){    sf::RenderWindow window(sf::VideoMode(800,600), Test, sf::Style::Titlebar | sf::Style::Close);    sf::RectangleShape background(sf::Vector2f(800.0f,600.0f));    window.setFramerateLimit(30);    sf::Texture spriteSheet;    if(!spriteSheet.loadFromFile(TestSprites.png))    {        return 1;    }    SpriteManager sprites(spriteSheet);    sf::Sprite *sprite = sprites.getSprite(ITEM1);    sf::Sprite *sprite2 = sprites.getSprite(ITEM3);    sprite->setPosition(100,100);    sprite2->setPosition(200,100);    while(window.isOpen())    {        sf::Event event;        while( window.pollEvent(event))        {            if(event.type == sf::Event::Closed)            {                window.close();            }        }        window.clear();        window.draw(background);        window.draw(*sprite);        window.draw(*sprite2);        window.display();    }    return 0;}So far my biggest concerns are:Is the destructor ok? I figured that since I'm allocating sprites that I should delete them all upon the manager going out of scope, though the more I think about it the more uneasy I feel. What would I do if I have to have two of the same sprites on the screen at the same time? Basically what happens now is, if I have two calls for a sprite with the same name:sf::Sprite *sprite1 = sprites.getSprite(ITEM1);sprite1->setPosition(100,100);sf::Sprite *sprite2 = sprites.getSprite(ITEM1);sprite2->setPosition(100,200);I won't actually have two sprites on the screen but only one at the position 100x, 200y, since when I called the first getSprite I created a sprite under ITEM1 key and then when I call the getSprite method the second time I get that first sprite again. Now this is great for memory conservation, however a lot of the times I'll need to have multiple instances of the same sprite on screen. For that I have an idea to add some sort of a counter or something, but nothing concrete yet. Any suggestions?
Sprite cache for SFML sprites
c++;cache;sfml