Alert alert alert.
You have a subtle and fatal flaw with this vector.
sf::Sprites do not own their own texture. Instead, they hang on to a pointer of an existing texture. I assume that texture is 'tCard'.
This means
you should not put this struct as an object in a vector because when the vector resizes, it might move the object to a different block in memory. If that happens, your sprite will have a pointer to a texture that no longer exists (it has been moved).
So every time you push_back or resize() this vector... all of your sprites have potential to break.
You can solve this one of several ways.
option 1) Do not put the sf::Texture in the Card struct... and instead put it in some other kind of resource manager (you should not have a 1:1 ratio of sprites to textures anyway, as this can often lead to duplicated textures being loaded).
option 2) Instead of a vector, use a container that guarantees it won't move elements around on resize (like std::list). Though depending on how you're using this vector, that may not be possible/practical.
option 3) Instead of having the sf::Texture as an object in the Card struct, make it a [smart] pointer in the card struct:
|
std::unique_ptr<sf::Texture> tCard;
|
Now even if the card moves... the actual texture will not because it is allocated elsewhere on the heap.
option 4) Same idea as option 3.... but instead of pointer-izing the texture, pointerize the entire Card within the vector:
1 2 3 4 5 6 7
|
// bad
// std::vector<Card> v;
// v.push_back( Card(1,2,3) );
// good
std::vector< std::unique_ptr<Card> > v;
v.emplace_back( new Card(1,2,3) );
|
EDIT:
Also, I highly recommend you use smart pointers whenever you do anything with new. If you are deleting things manually, you're probably doing it wrong.
1 2 3
|
std::unique_ptr<Card> p( new Card );
// no need to delete the card... it will be automatically deleted
|
RAII and smart pointers are your friend. They make code easier and safer. Get familiar with them asap.