Timer based issues

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.

-Jamie

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


±-------------------------------------+

I recently added a better example to the SDL_AddTimer() page in the Doc Wiki:

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fAddTimer

The general idea is to push an event when the timer callback is called.
You could also simply set a flag, sending the flag variable as (part of)
the ‘param’ that gets passed to the callback function, and then test for
that in your running loop (assuming you’re NOT using SDL_WaitEvent(),
in which case the flag might not get noticed until the user hits a key
or wiggles the mouse or something).

Make sense?

-bill!On Thu, Apr 14, 2005 at 01:40:45PM -0400, Jamie Bernier wrote:

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 general idea is to push an event when the timer callback is called.
You could also simply set a flag, sending the flag variable as (part of)
the ‘param’ that gets passed to the callback function, and then test for
that in your running loop (assuming you’re NOT using SDL_WaitEvent(),

The flag idea has worked better for me (YMMV). The event queue is a fifo, so
if responding to the timer quickly is critical, you might get some surprises.
Lots of mouse motion events, for example, can delay your main program’s
response to the timer.

JeffOn Thursday 14 April 2005 12:22 pm, Bill Kendrick wrote: