Consider the following two chunks of code:
sprite.hpp:
/* sprite.h */
#ifndef SPRITE_H_
#define SPRITE_H_
#include <string>
#include <SFML/Graphics.hpp>
class CSprite {
friend class CEngine;
private:
sf::Image m_TempImage;
sf::Sprite s_Hero[4];
sf::Sprite s_Grass;
sf::Sprite s_Gravel;
sf::Sprite s_Sky;
public:
CSprite();
~CSprite();
bool LoadSprite(std::string filename, sf::Sprite &sprite);
};
#endif
and sprite.cpp:
/* sprite.cpp */
#include "include/sprite.hpp"
CSprite::CSprite() {
/* Constructor */
}
CSprite::~CSprite() {
/* Destructor */
}
bool CSprite::LoadSprite(std::string filename, sf::Sprite &sprite) {
if(!m_TempImage.LoadFromFile(filename)) {
return true;
} else {
sprite.SetImage(m_TempImage);
return false;
}
}
The files obviously try to handle sprite loading. The problem however is that when I call LoadSprite for example like this:
Sprites.LoadSprite("gravel.png", Sprites.s_Gravel);
Sprites.LoadSprite("grass.png", Sprites.s_Grass);
Sprites.LoadSprite("sky.png", Sprites.s_Sky);
ONLY the last sprite seems to have the correct binded to it. All the previous sprites will have the LAST sprites image in them. Consider the following example:
Sprites.s_Grass.SetPosition(0,0);
m_App.Draw(Sprites.s_Grass);
Sprites.s_Gravel.SetPosition(8,8);
m_App.Draw(Sprites.s_Gravel);
Sprites.s_Sky.SetPosition(16,16);
m_App.Draw(Sprites.s_Sky);
Yields 3 Sprites.s_Sky sprites, instead of 3 different sprites.
If I change the order of LoadSprite functions, to load say s_Gravel as the last sprite, then ALL the loaded sprites will have the image of s_Gravel.
I suspect that I am doing something wrong with the m_TempImage in the LoadSprite function declaration. Though, I am not sure.
Any thoughts?