Skip to main content
gamedev.net
🔒 Locked

game loop structure?

Started by nino Nov 21, 2000 at 10:35 PM 3 replies 700+ views
Original Post
nino
nino
Hi, I was wondering what the best way to set up the main game loop is right now i''m using QueryPerformanceCounter to make the game run at a constant 60FPS. my questions are: do you check for user input every frame or in between frames? also if it''s not time to update the frame do you do something else, like process game logic and sound etc, or do you just do everything when it''s time to change frames like this? if(time to draw frame){ GetInput(); DoSound(); DoAI(); DrawFrame(); } can you do things between frames or no? thanks for the help people NiNo
errand
errand
Usually (all the time actually) you should do this.

1. Check for windows messages and process them if any
2. Check input
3. Do sound
4. Do ai
5. Draw frame
6. goto 1 immediatly

You want to make the game as fast as possible and not lock it at 50 or 60 fps. Cause if you try to lock it will be jerky and wrongly updated if the system running the game can''t handle your predetermined fps rate.

What you do is that you base your ai-decisions, movement on time instead of frames. Ie move players with 120 pix / sec and not 2 pix/frame. This way you will always move the same amount of distance no matter how fast your computer can run the game. On a highend compu you will move less pixels per frame put instead your will have a higher framerate and vice versa.
Death is lifes way saying your fired.
Cygon
Cygon
In addition to errand, I''d suggest you to also check wether your application is active (not alt-tabbed away, not minimized, not hidden). Window''s WaitMessage() function gives away all timeslices (cpu power) and waits for a message. If you application is restored again, WaitMessage() will return and you will receive a WM_ACTIVATEAPP message!

while(no_quit_message) {
if(new_message) {
1. Handle message
} else if(application_in_foregroud) {
2. Do movements x time_passed // includes ai & sound
3. Render a frame
} else {
4. WaitMessage() // important! else the app will take up a lot of CPU power even when the user Alt-Tabbed out
}
}

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Cygon
Cygon
I think this would be a bit large to post, but you can find all the code in my TrueFX library at
www.LunaticSystems.de/truefx

(Look for TrueFX3beta4.zip, the older TrueFX2 doesn''t yet contain the application base)

It''s a free game programming utility library with sourcecode. You''ll find everything in the folders named TFXApp and TFXTimer.

You can either take my code as example, or you could derive your applications main class from CTFXApp. You can then declare some functions, Init(), Shutdown(), Pause(), Resume(), Timer(), Render().

Short explanation:
Init() and Shutdown() are called when the application starts or quits, Pause() and Resume() when the application looses or regains focus (alt-tab), Timer() is called each 10 ms (100 times a second) while Render() is either called as often as possible (when the app is in foreground) or when the window contents need to be redrawn (when the app is in background and anothet window is dragged around over it).

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.