Okay, First of all hey. I just started using SFML and it's great. In fact I'd just finished reading my OpenGL book, coding a whole load of Vertex Buffer Object stuff for drawing 2D, and then I found this. So yeah thanks for saving me from bug testing all that stuff!
Anyway on to my question.. I've written a quick singleton class which takes a string to an image file and returns the loaded image. It stores the images in a map, and if the string exists, returns the loaded image, and if not loads the image, adds it to the map and then returns it. Kind of what's explained in the tutorials. It works and everything, but my problem is that I can't remember for the life of me whether I've typed this out right and whether it is indeed using the version stored, or a copy. It's been a while since university now and I've been slacking (getting baked) instead of programming, so I was hoping maybe one of you could just quickly confirm whether this is valid or not.
Anyway enough blabbering.
spriteManager.h:
#ifndef _SPRITEMANAGER_H
#define _SPRITEMANAGER_H
#include <map>
#include <string>
#include "sysInc.h"
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
class spriteManager
{
private:
static bool instanceFlag;
static spriteManager *single;
std::map <string, sf::Image*> m_Images;
sf::Image *m_Temp;
spriteManager()
{
}
public:
static spriteManager* getInstance();
void destroy();
void init();
sf::Image*& loadImage(string filename);
~spriteManager()
{
}
};
#endif
spriteManager.cpp
#include "spriteManager.h"
bool spriteManager::instanceFlag = false;
spriteManager* spriteManager::single = NULL;
spriteManager* spriteManager::getInstance()
{
if(! instanceFlag)
{
single = new spriteManager();
instanceFlag = true;
return single;
}
else
{
return single;
}
}
void spriteManager::destroy(){
LOG("Closing sprite manager");
instanceFlag = false;
delete single;
}
void spriteManager::init(){
m_Temp = new sf::Image();
}
sf::Image*& spriteManager::loadImage(string filename){
if(m_Images.find(filename) == m_Images.end()){
if (!m_Temp->LoadFromFile(filename)){
LOG("[ERROR]: Loading sprite failed");
}else{
m_Images[filename] = m_Temp;
m_Temp = NULL;
}
}
return m_Images[filename];
}
When I use the spriteManager I use something like this:
m_MainImage.SetImage(*spriteManager::getInstance()->loadImage(mainImage));
Anyway I hope someone can help me with this. Also if you spot any other dumb errors (they're probably there) that would be a great help!
Thanks for taking the time to read.
Leprosy
*edit* I realise I forgot to remove all the pointers! I have fixed this now.
*edit again* Oops just realised I posted this in the wrong place. Sorry about that.