Render Loop & Game Loop?

Reply

Join Date: Jun 2009
Posts: 10
Reputation: eXsolved is an unknown quantity at this point 
Solved Threads: 0
eXsolved eXsolved is offline Offline
Newbie Poster

Render Loop & Game Loop?

 
0
  #1
Jul 6th, 2009
Hey All

I'm building a simple game engine (yes I know...) but it's more for personal experience. I'm up to the stage where I have built a simple wrapper for directx and win32. Anyway, all is going great so far. Now, here's something I am a little bit confused on... FPS. Currently my game loop rotates (funny way of saying it) at 30 FPS. Every frame I call the RenderFrame function (directx) as well as do AI calculations & other bits. Now here is where I am confused:

If I increase the FPS limit, say from 30 to 60, off course all the AI code etc executes twice as fast. Is there some way to execute game code at the same speed no matter how fast the FPS (for rendering the image in directx) is? I'm assuming I may need 2 loops, one that controls the speed of the rendering and another for the gamecode, but that means threading... and I'm not sure if that's they way to do it.

Thanks!
Last edited by eXsolved; Jul 6th, 2009 at 7:05 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Render Loop & Game Loop?

 
0
  #2
Jul 6th, 2009
You can use threads, I do: One thread for render calls, one thread for ai+physics+scripting. Both mutexed so only one entire update body runs at the same time, which isn't the most efficient way to do things, but the only point of that use of threads was only to keep code simple.

Alternatively, you can just 'skip' loop iterations, i.e. render every iteration and only do 'other things' every 4th/5th/etc iteration. E.g. say your render rate is 60Hz, and you want to update physics+ai+whatever at 20Hz:
unsigned iteration = 0;
while ( true ) {
  if ( ( iteration % 3 ) == 0 ) {
    do_other_stuff ( );
  }
  do_render_stuff ( );
  ++iteration;
  // some sleep code
}
Other stuff is every 3rd iteration, since there are 3 'ticks' at 60Hz for every one 'tick' at 20Hz.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: eXsolved is an unknown quantity at this point 
Solved Threads: 0
eXsolved eXsolved is offline Offline
Newbie Poster

Re: Render Loop & Game Loop?

 
0
  #3
Jul 6th, 2009
Ah yes, that makes sense. Thank you.

Are there any good tutorials/articles on threading & or mutex, as I have very little experience in that area.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Render Loop & Game Loop?

 
0
  #4
Jul 6th, 2009
You should definitely split the work load between threads, Render thread should ONLY handle rendering, but the output of the physics and other threads that alter the data the rendering thread uses should be in a request form. Data is kept separate but a shadow copy of the data the render thread requires is set on a gate. (A two element array!) Render thread uses one until the gate is thrown and then uses the other. Physics and other threads write into the slot being ignored by the rendering thread.

Do not put a semaphore/mutex/critical-section (any form of blocking) on the rendering thread as it will affect your frame rate!
Just have it read the gate index and read the values that gate references.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC