Ok, so I seem to be a bit confused with the timer functions. Really, what
I planned on doing is setting up a timer, and when that timer has passed
an interval of 5 seconds, I wanted to be able to draw something. Ect. If
someone could quickly explain the timer functions that would be great or
direct me where I can read more in-depth about the timer functions related
to my issue.
The key to using the timer functions is to remember that pretty much the
only things you can do inside a timer function is a) restart the timer,
and b) send an event.
The normal way to use a timer function is to tell it to fire in, say
five seconds, and when it wakes up have it send an event that tells the
main loop of your program to draw something. Here is an example timer
function that sends an event and reschedules itself:
Uint32 timerCallback(Uint32 interval, void *param)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = MY_TIMEREVENT;
event.user.data1 = (void *)SDL_GetTicks();
event.user.data2 = (void *)0;
SDL_PushEvent(&event);
return interval;
}
Here is how you would declare a timer variable and start the timer
SDL_TimerID timer = 0;
timer = SDL_AddTimer(5000, timerCallback, NULL);
and here is a fragment that shows how you might respond to the event
when you get it in the main loop:
switch (e->type)
{
case SDL_USEREVENT:
switch (e->user.code)
{
case MY_TIMEREVENT:
now = SDL_GetTicks();
dt = now - lastTime;
if (dt >= minFrameTime)
{
drawBounce(dt);
testHits(dt);
checkScore();
lastTime = now;
}
break;
}
break;
This code is all extracted from a little game I used in an article for
Linux Journal a while back. The article was on SDL so I used SDL for
everything. In a real program I would probably keep track of when things
are supposed to happen using a priority queue ordered by time. I would
process events stored in that queue based on the current game time. And,
I would process them in a loop just before the loop that processes SDL
events.
Bob PendletonOn Thu, 2005-04-14 at 13:40 -0400, Jamie Bernier wrote:
-Jamie
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
–
±-------------------------------------+