[SDL] very precise control of display timing

Mattias Engdegård f91-men at nada.kth.se
Wed Aug 22 01:54:01 PDT 2001

Pallier Christophe <pallier at lscp.ehess.fr> wrote:
>I want to display at a fast rate a series of pictures on the screen, 
>and collect mouse clicks timestamped with the best possible accuracy.

Do an SDL_GetTick() as soon as you have received a mouse click event.
The problem is how to reduce the latency of the event when you are
doing something else.

Some targets will add the mouse event to SDL's event queue immediately
(through an OS callback or similar); others, such as X11 or DGA2, will
only do so when PumpEvents is called (directly or via PollEvent or
WaitEvent). Thus, if you call GetTick() in an event filter handler,
you may get lower latency.

Beware that WaitEvent may impose a 10 ms latency because of the way it
is implemented, so you may need to busy-wait

>How can I be sure not to miss a vertical retrace signal? (i.e. how can I
>be sure that my process is going to be running at this critical moments?)
>Plus the granularity of the typical timing functions seems to be around 10

No, SDL_GetTick() is usually accurate down to 1 ms (and you can get even
higher precision using platform-dependent methods, but this is rather
pointless and not really recommended). There is no way to detect
the number of vertical refreshes that have passed. What you can do
(on video targets that support it) is to request a buffer flip at
the next vertical retrace, and then wait for it to happen.
If your machine is fast enough, you could count the frames that way.

Please read the list archives - this has been discussed many many times

More information about the SDL mailing list