Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to handle event with SDL/C++

Tags:

c++

sdl

I am using SDL for the view parts of my game project. And I want to handle key press events without interrupting the main thread. So I decided to run an infinite loop in another view thread to catch any events and inform the main thread. However, I am not sure that this is the best since this may cause a workload and decrease the system performace? Is there any better way to do this kind of things? Thanks.

like image 931
Ertan Avatar asked Nov 13 '10 23:11

Ertan


1 Answers

Don't bother with another thread. What's the point?

What does your main thread do? I imagine something like this:

  1. Update Logic
  2. Render
  3. Goto 1

If you receive input after (or during) the update cycle then you have to wait till the next update cycle before you'll see the effects. The same is true during rendering. You might as well just check for input before the update cycle and do it all singlethreaded.

  1. Input
  2. Update Logic
  3. Render
  4. Goto 1

Multithreading gains nothing here and just increases complexity.

For some added reading, check out Christer Ericson's blog post about input latency (he's the director of technology for the team that makes God of War).

like image 174
Peter Alexander Avatar answered Sep 28 '22 05:09

Peter Alexander