Hi! I'm newbie in STL use and I'm doing some tests with a vector of my own class. The code:
class Animation{
sf::Texture m_texture;
sf::Sprite m_image;
// Other stuff
public:
// Constructors
Animation(){}
Animation (std::string file){
m_texture.loadFromFile(file);
m_image = sf::Sprite(m_texture);
// ...
}
Animation (const Animation& anim){ // Copy constructor
m_texture = sf::Texture(anim.m_texture);
m_image = sf::Sprite(anim.m_image);
// ...
}
// get the sprite to draw
sf::Sprite& getSprite(){
return m_image;
}
};
class Game{
std::vector <Animation> m_player;
// ...
public:
Game(){}
void run(sf::RenderWindow *m_window){
loadLevel(); // Load players and so on...
// Load players, method 2 (with this game works fine)
/*Animation a("001.png"), b("002.png");
m_player.push_back(a);
m_player.push_back(b);*/
while (m_window->isOpen()){
sf::Event event;
while(m_window->pollEvent(event)){
if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape)){
m_window->close();
}
}
m_window->clear();
for (std::vector<Animation>::iterator i = m_player.begin(); i < m_player.end(); i++){ // Draw the animations
m_window->draw((*i).getSprite()); // Here I get the error
}
m_window->display();
}
unloadLevel();
}
void loadLevel(){
Animation a("001.png");
Animation b("002.png");
m_player.push_back(a);
m_player.push_back(b);
}
void unloadLevel(){
m_player.clear();
}
};
int main(int argc, char **argv){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
Game game;
game.run(&window);
return 0;
}
The problem is that, when I use "loadLevel()" to load my animations, I get an error when I try to show them, or get an empty texture (the shape of the image filled in blank), but when I use the "method 2" instead of "loadLevel()" it works fine...
Any idea?
I'm using SFML 2.0 with CodeLite 5570 (Win7 x64).
Thank you.
PS: You can download the project from
HERE