I'm guessing you have code vaguely resembling this:
int main() {
...
sf::Sprite animatedSprite1;
...
sf::Clock clock;
while(window.isOpen) {
// handle events
sf::Time deltaTime = clock.restart();
animatedSprite1.setTextureRect((deltaTime.asSeconds() / 10) * 32, 0); // a 1 x 10 spritesheet
animatedSprite2.setTextureRect((deltaTime.asSeconds() / 5) * 32,
(deltaTime.asSeconds() > (5 * 32)) ? 32 : 0); // a 2 x 5 spritesheet
animatedSprite3.setTextureRect(...); // you get the idea
...
// clear/draw/display
}
}
If so, the answer is a class with an update() method:
class AnimatedSprite {
public:
AnimatedSprite(const sf::Texture&, /* tile size/count/etc */) { ... }
update(sf::Time deltaTime) {
d_sprite.setTextureRect(...);
}
draw(sf::RenderTarget target) { target.draw(d_sprite); }
private:
sf::Sprite d_sprite;
/* tile size/count/etc */
}
int main() {
...
AnimatedSprite animatedSprite1(...);
...
sf::Clock clock;
while(window.isOpen) {
// handle events
sf::Time deltaTime = clock.restart();
animatedSprite1.update(deltaTime);
...
// clear/draw/display
}
}
Note that Thor has some animation classes to help do stuff like this:
http://www.bromeon.ch/libraries/thor/v2.0/tutorial-animations.htmlHapax is right that you do have to loop over all your animating entities every frame no matter what, but when you reduce main()'s job to simply calling update() on them, it's not much of a problem.