Say I have a very large number of game elements that share the same sprite properties (size, textureRect, linear transforms, etc), except that they differ only in texture in position.
I want to render all of them. So say I have a vector where all of them are stored, like
std::vector<sf::Sprite>;
. So I would just iterate throw this vector and call draw on all of them, like this.
for (int i = 0; i < sprites.size(); ++i)
window.draw(sprites[i]);
Now, I've been thinking, what if I created only one static sprite instance, and stored only the position and the texture for each given game element? So, maybe I would have something like
std::vector<std::pair<sf::Vector2f, sf::Texture>> spriteInfos;
static sf::Sprite staticSprite;
. Then it would go like this:
for (int i = 0; i < spriteInfos.size(); ++i)
{
staticSprite.setPosition(spriteInfos[i].first);
staticSprite.setTexture(spriteInfos[i].second);
window.draw(staticSprite);
}
It seems to me that the above method saves up a lot of memory from underlying properties inside each sprite that would have been duplicated on the first method, especially if I have a very large number of sprites active at the same time.
My question is, what do you make of this approach? Is it worth it, and if not, why, and do you think it may affect my performance?
Thanks!!