1
Graphics / Transparency mask from colour vs PNG Alpha
« on: December 10, 2010, 03:21:42 pm »
Cheers thanks.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#ifndef _SPRITEMANAGER_H
#define _SPRITEMANAGER_H
#include <map>
#include <string>
#include "sysInc.h"
#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
#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(){
for(std::map<string, sf::Image*>::iterator it = m_Images.begin(); it != m_Images.end(); ++it)
{
sf::Image* temp = (*it).second;
delete temp;
}
m_Images.clear();
LOG("Closing sprite manager");
instanceFlag = false;
delete single;
}
void spriteManager::init(){
}
sf::Image* spriteManager::loadImage(string filename){
if(m_Images.find(filename) == m_Images.end()){
m_Temp = new sf::Image();
if (!m_Temp->LoadFromFile(filename)){
LOG("[ERROR]: Loading sprite failed");
}else{
m_Images[filename] = m_Temp;
m_Temp = NULL;
}
}
return m_Images[filename];
}
Looks a bit weird. Should it really return a reference to a pointer?
I don't see you calling init() to initialize the m_Temp pointer. Every time you load a new image from disc you need to allocate a new sf::Image to store it in.
Your destructor doesn't free the allocated memory.
And I'm not sure that an object can delete itself.
#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
#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];
}
m_MainImage.SetImage(*spriteManager::getInstance()->loadImage(mainImage));