SFML community forums

Help => Graphics => Topic started by: Me-Myself-And-I on December 23, 2022, 12:44:58 am

Title: Draw sprite to renderwindow automatically
Post by: Me-Myself-And-I on December 23, 2022, 12:44:58 am
Hi everyone.
I was wondering if theres a more automated way to draw sprites to the window.
Everthing sfml related i've seen has drawn sprites to the window by typing out every sprite that the window should draw.
window.clear();
window.draw(sprite1);
window.draw(sprite2);
window.draw(sprite3);
window.draw(sprite4);
window.draw(sprite5);
window.draw(sprite6);
window.draw(sprite7);
and so on...

Is there a more efficient way thats more automated?Maybe by a class?   
Title: Re: Draw sprite to renderwindow automatically
Post by: fallahn on December 23, 2022, 05:39:34 pm
A simple approach would be to place your sprites in a vector, and loop over it when you draw:

std::vector<sf::Sprite> sprites;

//insert sprites here

for (const auto& sprite : sprites)
{
    window.draw(sprite);
}

However if you're getting more serious about your code design I highly recommend reading SFML Game Development  (https://www.packtpub.com/product/sfml-game-development/9781849696845)and Game Programming Patterns (https://gameprogrammingpatterns.com/contents.html) - both of which I found invaluable when I was learning SFML.