Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trouble moving two sprites in the same window  (Read 1366 times)

0 Members and 1 Guest are viewing this topic.

RXaG

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Trouble moving two sprites in the same window
« 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?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Trouble moving two sprites in the same window
« Reply #1 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 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.