www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/11/12:05:59

Date: Sun, 11 Oct 1998 17:04:19 +0100 (BST)
From: George Foot <george DOT foot AT merton DOT oxford DOT ac DOT uk>
To: djgpp AT delorie DOT com
Subject: Re: vsync
In-Reply-To: <36209c73.0@d2o63.telia.com>
Message-ID: <Pine.OSF.4.05.9810111651290.22222-100000@sable.ox.ac.uk>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Sun, 11 Oct 1998, Henrik Rydg rd wrote:

> Mark Tsui skrev i meddelandet <6vp6lh$g71$2 AT otis DOT netspace DOT net DOT au>...
> >when I use vsync in my program, the maximum frame rate is only about 60
> >frames per second. when I remove the vsync, the program runs at 3000 and
> >something frames per second, however, the graphics looked all flickered,
> how
> >do I solve this problem?
> >
> >Any help will be appreciated.
> >
> >
> 60 FPS is about as fast as your eyes can percieve, so that shoulnd't be a

I think most films go at 25fps... but I could be wrong.

> problem. If your program goes in 3000 FPS it has to be a quite simple
> program, right? Which means that when you start adding more stuff the frame
> rate will plunge, and so the vsync() won't gobble up so much time. But when
> the framerate goes below 60 FPS, then you're in trouble. vsync waits for the
> next refresh, and if your program hasn't finished drawing early enough the
> framerate goes straight down to 30 FPS. And if your program can't cope with
> that, it will go down to 15. And the program will go four times as slow.
> Does anyone know how to maintain a constant speed?

Yes, don't make your game speed depend upon the graphics speed.
In most cases it doesn't matter too much how quickly frames are
displayed on the monitor; the important thing is how quickly
game cycles are performed.  If you set up a timer at the rate
you want game cycles to be performed, making it increment a
*volatile* variable, you can test this variable in your game
loop.  If it's greater than 0, you need to do some game cycles.
Each time you do a game cycle, you decrement it.

    game_cycles_to_do = 0;
    do {
        draw_graphics();
        while (game_cycles_to_do == 0);
        do {
            do_one_game_cycle();
        } while (--game_cycles_to_do);
    } while (!end_of_game);

If you arrange for `game_cycles_to_do' to be incremented at a
constant rate (independent of the frame rate -- don't choose
60Hz!) then your game action will proceed at that speed, on
average.  Effectively you're drawing a frame of graphics, during
which time the counter will have increased, and then performing
a few game cycles to catch up with time lost in the graphics
drawing routine.  Now your graphics routine can include a vsync,
with no penalty.

How do you decide how quickly to increment the counter?  Tricky
question.  Don't do it at 60Hz or 70Hz.  You don't want to try
to make it run at the same speed as the monitor because you get
all sorts of nasty effects.  You can set it slowly if you like,
but then your computer is just sitting around doing nothing for
a lot of the time.  I recommend setting it fairly high, if your
game logic is fast enough to cope with being run that often.  If
your game logic starts taking too long, the game will appear to
freeze from the user's point of view, because the second inner
`while' loop will never exit.  You need to take this into
account.

Please read Allegro Vivace (chapter 8) and the Allegro FAQ and
Programming Techniques page for more information:

    http://www.canvaslink.com/gfoot/vivace/
    http://www.canvaslink.com/allegro/faq/

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

xu do tavla fo la lojban  --  http://xiron.pc.helsinki.fi/lojban/lojban.html

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019