Hi there,
I'm fairly new to SFML so please bear over with me if this has been answered a million times before. I just haven't been able to find a good example of this and it feels like there's something basic I'm not getting.
Let's say I have a class representing some sprite with a texture and I want to have more than one instance of that class and put that in an STL container. Something like:
class Ball
{
public:
Ball()
{
texture_.loadFromFile("ball.png");
sprite_.setTexture(texture_);
}
private:
sf::Texture texture_;
sf::Sprite sprite_;
}
std::vector<Ball> balls = { Ball(), Ball() };
This is just example code that might not even compile to give an idea of what I'm trying to accomplish, but assuming it does this will still not work because you cannot (I think) copy an SFML texture which results in the "white square problem" I've seen mentioned often.
I have some different ideas on how to solve this, but they all feel sort of "wrong" which is why I feel like I am misunderstanding something.
You could:
- Use a static shared_ptr, and then construct that the first time the class is initialized. This is sort of dirty and the fact that a sprite takes a reference to a texture hints that this is not the way to do it.
- Using a global texture manager or similar which each instance can use to retrieve the texture. This has all the usual issues with global variables.
- Creating the texture outside of the class using it and then giving every instance a reference to that. This is fairly cumbersome and I think it would be nicer to have the class using the texture owning it.
I've looked also looked at the ResourceManager from the Thor library, but that doesn't really seem to be useful for this case.
I know this is not that much a question specific to SFML, but I'm just really interested to hear how others are doing something like this since I cannot imagine I'm the only one facing this.
Thanks a lot.