[SDL] animations etc

screwtape at froup.com screwtape at froup.com
Tue Sep 17 01:19:00 PDT 2002

On Mon, Sep 16, 2002 at 11:36:43PM -0700, Alan Wolfe wrote:
>    hiya, i was wondering if anyone had any links or information on how to do
>    animation that updates based on time intervals instead of numbers of
>    frames to make sure different speed computers each see the same thing.  Do
>    you just use a timer and update things to make them animate when the timer
>    is called or what? Im wonder what the tried and true way of doing this
>    is...

You have an event loop that you'll be iterating through quite
frequently, and you have a function to get a number which increases at
a constant rate in real time (as opposed to CPU time).

All you need is a sorted list of happenings[1] and specified times - for
example, when you want a bomb to explode five seconds from now, add a
new happening to the list with the time 'SDL_GetTicks() + 5000' (because
SDL_GetTicks measures 1000 ticks per second).

Then every time through your event loop, get the current value of
SDL_GetTicks and look through the list-of-happenings. Each time you
see a happening whose target time is less than the current time, then
do whatever it wants and remove it from the list.

hint: if your list-of-happenings is a C array, the library function
qsort is likely to be helpful. If you're using C++, I think the STL
has a sorted queue class.

[1] Traditionally this things are called 'events', but SDL already has
things called 'events' that work in a rather different fashion. To
avoid confusion, I've called them 'happenings'

 ___________ ____________________________
| Screwtape | Reply-To: munged on Usenet |________ ______ ____ __ _  _   _
| "Why fall in love when there's better things to do?" -- DEVO

More information about the SDL mailing list