Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Game Timing

This thread is locked; no one can reply to it. rss feed Print
Game Timing
Felix-The-Ghost
Member #9,729
April 2008
avatar

I'm not sure when or why I started doing this, but I've timed my games like this:

      while(counter > 0)
      {
            check_keys();
            player.update();

            game.draw(&player);

            counter = 0;
      }

I noticed this is different than what is taught in the Allegro wiki:

      while (counter == 0)
      rest(1);

      while(counter > 0)
      {
            check_keys();
            player.update();

            counter--;
      }

      game.draw(&player);

Is there a drawback to the way I'm doing it? They both at least appear to be working the same, but perhaps one method is more reliable across different speeds? I'd like to know because the method I currently use uses less code and avoids the rest call. Or maybe that's bad? rest()... rests the CPU doesn't it? But does it make timing non-fluid? Hmm.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Trent Gamblin
Member #261
April 2000
avatar

Your method will 1) burn cpu pointlessly until the while condition is true, and 2) it doesn't separate logic and drawing so if things bog down, it'll run like crap. The wiki method is much better if you analyze it.

Also to prudent, your while could be replaced with an if and nothing would change, so you know somethings wrong.

Double edit, in fact an if would be better :P.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Actually I wondered about the if thing myself :P

I remember seeing this a long time ago on the wiki. Missed it here:

      if(old_ticks <= ticks)//logic is taking too long: abort and draw a frame
        break;

I'm gonna read into this, though I don't know when logic could ever take too long.
It seems to check if a tick has gone by since logic started. But I guess with this method that can be true and the ticks be > 0. Edit: oh I get it, because at the end it's ticks--; Without this it would be possible for the logic to loop forever, yes? Different than assigning it to 0 :P

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Trent Gamblin
Member #261
April 2000
avatar

Ya. That's good to have in there too. I do it this way myself:

int count = logic_counter;
while (count--) {
   //...
}

Same thing though pretty much.

Go to: