Advertisement

Accurate timing

Started by May 27, 2001 05:26 PM
1 comment, last by Dave2001 23 years, 8 months ago
I am using QueryPerformanceCounter to keep my program at a framerate of 60fps, but it seems to go a little jerky. Here is the code:
  

__int64 pc_frequency, pc_last, pc_now;

...

QueryPerformanceFrequency ((LARGE_INTEGER*)&pc_frequency);
pc_frequency /= 60;  // get frequency per frame

pc_last = 0;

// -=-=-=-=- MAIN LOOP -=-=-=-=-

while (!bQuit)
{
  GameMain ();

  do {
    QueryPerformanceCounter ((LARGE_INTEGER*)&pc_now);
  } while (pc_now < pc_last + pc_frequency);
  pc_last = pc_now;

  Flip ();
  ...
  
Can anyone tell me how to make this code work better? Dave2001, Dave2999@hotmail.com
Aaargh!

Trying to force your code to run at a fixed speed is a BAD idea (assuming this is under Windows or another multi-tasking OS).

What the majority of games do is update all animation (& physics, network positioning etc) based on the elapsed time since the last frame.

character.xpos += speed * time_in_miliseconds_since_last_frame;

where speed is how far the object should have moved in a single millisecond. So if the elapsed time between frames was 2 milliseconds, the pos is moved speed*2 units. speed is usually small floating point value.

Its probably easiest to think about a car:

Its speed is measured in Miles (or Km) Per Hour. From that you can work out how far it would have moved in 10 minutes - its the same principle.

If you average the elapsed time over a few frames and put in a maximum threshold, you can get very solid, non-jerky animation. (Its been a smooth enough method for the last 4 commercial PC products I''ve worked on).

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Advertisement
Assumptions are usually a mistake in performance tuning. How did you arrive at the conclusion that the problem was in the delay instead of in GameMain?
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement