Hi!
I've run into yet another problem with my Pong game.
I'm trying to store my Widgets in a STL vector, but the image always changes. Here's an example:
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
using namespace std;
class Widget
{
public:
void setImage (string file)
{
image.LoadFromFile (file);
sprite.SetImage (image);
}
void draw (sf::RenderWindow &app)
{
app.Draw (sprite);
}
private:
sf::Sprite sprite;
sf::Image image;
};
int main()
{
sf::RenderWindow app;
app.Create (sf::VideoMode (800,600,32), "ex");
vector <Widget> widgets;
Widget wid;
wid.setImage ("play.png");
widgets.push_back (wid); //now there should be a copy of wid in the vector
wid.setImage ("exit.png"); // why does this interfere with the content of the vector?
while (true)
{widgets[0].draw(app); app.Display(); } //this displays exit.png
return 0;
}
The push_back function doesn't seem to be copying the Image correctly. I tried the same example using an integer instead of a Widget objects and the variables are copied and stored correctly.
Thanks.