Timing game loop

Started by
9 comments, last by Aardvajk 10 years, 2 months ago

Is there a standard value for the amount of time I should wait between each update of the main thread? I'm using SFML on c++, right now I just have a basic loop:

while(game.isRunning()){

game.HandleEvent();

game.Update();

game.Paint();

//time sleep method

}

I'm making a 2d game, so how long should the loop wait before starting over?

Advertisement

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

If you're not using a fixed time step or waiting for vsync, you may want to keep track of the time between frames. Which in your case it's pretty simple, a frame is each run of your game loop.

You should start a clock right before your game loop starts. At the beginning of each loop save the clock time and restart the clock. This would typically be called the game time as it'll be the time between the previous from to the beginning of the next frame. You would then pass this game time around to the various parts of your game to make sure everything is executed at the correct speed. I.E movement of objects, physics, etc.

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way. What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way. What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

Try doing what I suggested above, and when you're manipulating the speed of your sprites, make sure to multiply the speed by the game time.

I.E

objectPosition.x = objectPosition.x + (speed * gameTime).

This would make sure the speed value for your object is only as fast as the time step. The longer it takes for the next frame (or loop) to begin, the greater that speed value will end up being, and vice versa.

Say it takes .161 seconds to go from frame 1 to frame 2, and you have a speed of 50. 50 * .161. Doing the math would give you a 'speed for this frame' of 8.05, and thus would only move the sprite by that much, instead of the full 50.

objectPosition.x = objectPosition.x + (speed * gameTime).

Just change gameTime to frameTime. The amount of time since the last update of your game (time since the loop…looped).

If you have any form of physics, a fixed time step will also be necessary.
Fixed-Time-Step Implementation


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way. What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

You want to update sprites at 30-75 Hz. 75Hz is at the high end of what a LCD will display. A frame rate higher than the monitor refresh rate cannot be seen. 60Hz is also a common limit for monitors. You may be able to poll the OS to find out the current display refresh rate. 30 Hz is the low end of what will look like smooth motion. You can run slower, but then tics start being visible.
If anything, only throttle the drawing of objects. If you throttle input/physics it will make your game seem laggy. So basically just keep a timer and do something like

input();
update_states();
physics();
if(cur_time > max_frame_time) draw(), cur_time = 0;
cur_time += frame_time;
what


If you have any form of physics, a fixed time step will also be necessary.
Fixed-Time-Step Implementation

What a great explanation.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

To play nice you can put in a Sleep(0).

This will yield your process to anything else that needs processing time after you finished your game loop.

If you do not do this then your process will starve anything waiting and eventually Windows will slice you out at some random place in the game-loop.

The nice part of Sleep(0) is if nothing is waiting then it returns to your app right away (unlike Sleep(1) which will probably take 10ms).

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement