SFML community forums

Help => Graphics => Topic started by: RXaG on May 11, 2022, 08:38:31 pm

Title: Trouble moving two sprites in the same window
Post by: RXaG on May 11, 2022, 08:38:31 pm
Hello, I am trying to create a Space Invaders type program, and in that game the "aliens" move side to side at the same time. I tried achieving this using std::thread as well as sf::Thread, however I came to realize that in SFML  the window.setActive must be set to false before using it in another thread, thus not allowing me to move two "sprites" or aliens at the same time to the best of my knowledge. Is there something I'm missing or another way to achieve this?
Title: Re: Trouble moving two sprites in the same window
Post by: Arcade on May 11, 2022, 10:28:02 pm
I would avoid trying to use threads for this. Most games you create, especially simpler ones like Space Invaders, don't need multiple threads for things to visually appear to be happening at the same time. In fact you should almost never need to use multiple threads at all. If anything, having multiple threads will greatly complicate your code and they are prone to causing issues that are really hard to debug.

At a high level, most games work by having an infinite loop, where each time through you clear the screen, draw everything, and then display it to the player. (See the tutorials (https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php) if you aren't familiar with the basics). Remember that the player won't see objects move at the very moment the code tells the object to move. The player only sees things move once the window has been displayed. So for 2 objects to move "at the same time", all you need to do is move both of them between your calls to the display function.