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

Author Topic: Should I put drawing and processing in separate threads (and other questions)?  (Read 2512 times)

0 Members and 1 Guest are viewing this topic.

code4240

  • Newbie
  • *
  • Posts: 5
    • View Profile
I currently have a standard game loop that calls process() and then draw() on the contents of a vector<Entity> of dynamically allocated Entities. (I have 2 vectors of pointers, one for processing and one for drawing, and the drawing vector is sorted by y-value of each entity before drawing because this is a top-down game that layers sprites based on that. They currently use raw-pointers because I didn't know shared_ptr existed when I started this project, but I'm going to refactor them into shared_ptr so the drawing vector won't lose stuff in the middle of a draw run)

I considered putting drawing in a separate thread so that the game logic won't be blocked if the draw calls take too long for some reason (although I don't really anticipate this happening).

Really I'm wondering how I should handle this situation, as well as whether the way I'm managing the sprite layering correctly and how I should determine the tick rate of the game logic.

(Here's a screenshot of my game right now if you're curious
it's my first game and it's really primitive right now so go easy on me pls)
(click to show/hide)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Short answer: No.

Changing an object while you're drawing it at the same time leads to undefined behavior.
And if you don't keep the rendering and updating in-sync the drawn frame might not represent the current state.

As such you'll have to sync rendering and processing and thus lose any parallelism you might have gotten out of it.

This all without considering the overhead of multi-threading and all the inherited problems which require advanced knowledge in parallel programming.

Long answer: No.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
It's possible, with significant performance gains - if done right. There'd be a heap of programming overhead required for the design, regarding resource locking. And quite likely will result in hard to debug - undefined behaviour.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
It's possible, with significant performance gains - if done right.
No, for a simply game there will never be a significant performance gain.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything