31
SFML projects / Re: OO Sprite Batch
« on: January 22, 2015, 10:08:03 am »
I think you missunderstood me.
SpriteBatch has a std::vector<sf::Vertex>
Sprite has Pointers to the vertices in SpriteBatch with std::vector<sf::Vertex *>.
So if SpriteBatch should reallocate its vertices, the Pointer in Sprite won't be valid anymore.
Please correct me if I got it wrong.
But I didn't see, that he won't add elements to his SpriteBatch-Vector if a certain size is reached (in which case he will just return an nullptr). So the Pointers in the return will stay valid.
SpriteBatch has a std::vector<sf::Vertex>
Sprite has Pointers to the vertices in SpriteBatch with std::vector<sf::Vertex *>.
So if SpriteBatch should reallocate its vertices, the Pointer in Sprite won't be valid anymore.
Please correct me if I got it wrong.
But I didn't see, that he won't add elements to his SpriteBatch-Vector if a certain size is reached (in which case he will just return an nullptr). So the Pointers in the return will stay valid.
std::array<sf::Vertex*, 4> SpriteBatch::addSprite()
{
if((spriteNum + 1) * 4 <= vertices.size())
{
unsigned int s = spriteNum * 4;
spriteNum++;
return {&vertices[s], &vertices[s + 1], &vertices[s + 2], &vertices[s + 3]};
}
else
return {nullptr};
}
{
if((spriteNum + 1) * 4 <= vertices.size())
{
unsigned int s = spriteNum * 4;
spriteNum++;
return {&vertices[s], &vertices[s + 1], &vertices[s + 2], &vertices[s + 3]};
}
else
return {nullptr};
}