0

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!

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by wildgoose
0

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.

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.