Mail Archives: djgpp/1998/07/31/06:15:30
Dan Goodman <dog AT fcbob DOT demon DOT co DOT uk> wrote in article
<901344523 DOT 17805 DOT 0 DOT nnrp-03 DOT 9e989aee AT news DOT demon DOT co DOT uk>...
> [...]
> Does anyone know of a neat way to make sure the game runs at the right
speed
> regardless of the speed of the computer. Currently, my game runs at
1000fps
> if I use dirty rectangles, but 30fps with double buffering, [...]
I use this template (extracted out of Allegro\demo\demo.c):
int updated=0, game_time, tmp_timer;
void game_timer() {game_time++} // count game "ticks"
...
MyLoop()
{
int game_over=0;
allegro_init(); install_timer();
install_int(game_timer, my_game_ticks_millisec);
....
do
{
if ((tmp_timer=game_time))
{
game_timer=0;
update=MovePlayers(tmp_timer, &game_over);
// elapsed time=tmp_timer*my_game_ticks_millisec
if (update)
RedrawScreen(); // with no vsync!
}
} while(game_over)
}
The MovePlayer routine should be optimized to calculate the new player
positions and updated the temporary buffer only once. The sceen will be
redraw (dirty rects) only and only if something change. You can also use
double buffer, but You cannot use the vsync or Flip Page (implicit wait for
vsync) since You will busy wait for a vsync. The time taken by
MovePlayer(x)+RadrawScrenn() should be less then my_game_ticks_millisec.
Anyway, if it is higher, the game_time counter will allow you to move
player at the right velocity. On slow computer, You'll see object moving at
the right velocity, but with low frame rate (the objects will "jump"
instead of moving with continuity).
Note that You use vsynch the frame rate cannot be higher than your vertical
synch rate (61Hz = 61 frame per seconds on my SVGA 800x600x256)
- Raw text -