| | |
Render Loop & Game Loop?
![]() |
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 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!
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.
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:
Other stuff is every 3rd iteration, since there are 3 'ticks' at 60Hz for every one 'tick' at 20Hz.
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
} Plato forgot the nullahedron..
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.
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.
![]() |
Similar Threads
- Cricket game :help needed (C)
- Help with a date loop & error '80020009' (ASP)
- Multiple keypresses at once in pygame (Python)
- do-while loop & other stuff (Java)
- C++ Loop Question Here (C++)
- For loop in guessing game, utter confusion! (Java)
- What's going on with character values in this loop? (C++)
Other Threads in the Game Development Forum
- Previous Thread: Want an advise with u guyz help me..
- Next Thread: C++ Help Needed Please!
| Thread Tools | Search this Thread |
3d advertising ai algorithm ban c++ cambridge camera censorship china competition console development engine fov fpx game gamer games gaming gauntanamo government idaho in-gameadvertisement intellectualproperty laracroft live manhunt math mathematics matrix mercenaries microsoft mmorpg modded msn naked news nintendo obama palin physics pirate playstation politics projection ps3 rpg search software sony stephenhawking stocks studio technology terrorism tombraider uk videogame web wii world-of-warcraft xbox xbox-live xbox360






