R: [SDL] animations etc

Marco Pacifico marco.pacifico at prograph.it
Tue Sep 17 01:57:01 PDT 2002

This is a simple routine to call every frame update. It also handles a
pause time: 

void UpdateTime()
	if(!g_pause)	// Pause status
		// Store old time
		Uint32 oldtime = g_currtime;

		// Get current timer
		// pauseTotal is a Uint32 counter of the total pause
time which is incremented
		// every frame during pause
		g_currtime = SDL_GetTicks() - pauseTotal;

		// Calculate time delta
		g_deltatime = (float)(g_currtime - oldtime);
		g_deltatime /= 1000;

		// Clamp if too much time has passed
		if(g_deltatime >= 0.3f)
			g_deltatime = 0.3f;
		g_deltatime = 0;

The obtained deltatime must be used to calculate the animation frame
index like this:
	obj->frame += anm.speed * g_deltatime;

So that, you have also a frame skipping method :) Hope this helps,

Marco Pacifico
Lead Programmer
Product Manager

Prograph Research S.r.l.
Longarone (BL) - Italy

-----Messaggio originale-----
Da: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] Per conto di Alan
Inviato: martedì 17 settembre 2002 8.37
A: sdl at libsdl.org
Oggetto: [SDL] animations etc

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...

thanx (>'.')>

More information about the SDL mailing list