943,914 Members | Top Members by Rank

Ad:
Jul 6th, 2009
0

Render Loop & Game Loop?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eXsolved is offline Offline
14 posts
since Jun 2009
Jul 6th, 2009
0

Re: Render Loop & Game Loop?

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.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Jul 6th, 2009
0

Re: Render Loop & Game Loop?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eXsolved is offline Offline
14 posts
since Jun 2009
Jul 6th, 2009
0

Re: Render Loop & Game Loop?

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: Want an advise with u guyz help me..
Next Thread in Game Development Forum Timeline: C++ Help Needed Please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC