Help with timers needed!

Could somebody be so nice and help me with SDL’s timers.

I cannot figure out how to create a timer (SDL_AddTimer, SDL_SetTimer)
and a callback function. Well, maybe more exactly my problem is that I
cannot understand how to make a callback function with parameters.

Well I want to be able to call that the timer calls the callback
function and that I manually call it under certain circumstances (like
the user presses a key). Is this possible at all?

I’m trying to do something like (it should switch something on and off
on a regular interval).

// the callback function
SDL_TimerCallback Switch( bool *on )
{
}

// the timer
timer = AddTimer( 1000, Switch(…

This is the description of SDL_AddTimer from the docs SDL_AddTimer –
Add a timer which will call a callback after the specified number of
milliseconds has elapsed.

Synopsis
#include "SDL.h"
SDL_TimerID SDL_AddTimer(Uint32 interval,
SDL_NewTimerCallback callback, void *param);

Callback
/* type definition for the "new"
timer callback function */
typedef Uint32 (*SDL_NewTimerCallback)
(Uint32 interval, void *param);

Thank you very much for any working example and explanation of
callbacks!!!

Could somebody be so nice and help me with SDL’s timers.

I cannot figure out how to create a timer (SDL_AddTimer,
SDL_SetTimer) and a callback function. Well, maybe more exactly my
problem is that I cannot understand how to make a callback function
with parameters.

Well I want to be able to call that the timer calls the callback
function and that I manually call it under certain circumstances
(like the user presses a key). Is this possible at all?

Yes. It may not be the best design, but you can call callback
functions directly if you like.

I’m trying to do something like (it should switch something on and
off on a regular interval).

// the callback function
SDL_TimerCallback Switch( bool *on )
{
}

A callback is a call through a function pointer. It’s exactly like a
normal function call, except that the actual address of the function
isn’t hardcoded into the call. That way, you can “plug” any function
in, at run time. That is, any function with the right prototype. The
call (argument pushing and stuff) is still hardcoded; it’s just the
address that isn’t.

// the timer
timer = AddTimer( 1000, Switch(…

It should look something like:

timer = SDL_AddTimer( 1000, Switch, <param>);

The callback argument takes only the address of your function; nothing
else. The prototype is hardcoded, and whatever arguments are passed
is up to SDL’s timer implementation.

As it is, SDL will pass of the AddTimer() call on to your
callback. (This is a very common thing in callback based APIs.) You
can use that to pass “custom” arguments to your callbacks (pointer to
a struct, for example), but you can’t change the actual argument list
of the callback function. It has to be exactly as specified; ie:

typedef Uint32 (*SDL_NewTimerCallback)
		(Uint32 interval, void *param);

or as you would declare your callback function:

Uint32 my_timer_func(Uint32 interval, void *param);

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Saturday 10 May 2003 10.34, Billi Bot wrote:

Hi David, thank you for your reply!

Yes. It may not be the best design, but you can call callback
functions directly if you like.

What would be a good design? To make a function that is called by the
callback function and to call it manually when needed? Is this a better
solution? Well, this might not be a topic that fits in a message and
that somebody would like to discuss but… well, I ask.

timer = SDL_AddTimer( 1000, Switch, );

or as you would declare your callback function:

Uint32 my_timer_func(Uint32 interval, void *param);

I’m sorry but I still cannot understand how to pass the parameters

Do you mean something like this bellow?
int a = 100;
param = &a;

timer = SDL_AddTimer( 100, my_timer_func, param );

Sorry for bothering anyone but I’m not much experienced in C :wink:

Hi David, thank you for your reply!

Yes. It may not be the best design, but you can call callback
functions directly if you like.

What would be a good design? To make a function that is called by
the callback function and to call it manually when needed? Is this
a better solution? Well, this might not be a topic that fits in a
message and that somebody would like to discuss but… well, I ask.

Well, that depends entirely on your application and how you decide to
design it. It’s just been my experience that using API provided
callback prototypes for other things than they’re really meant for
(ie for some backend to call functions your provide) isn’t a very
good idea. The most obvious reason is that it makes your code harder
to port to other APIs. Another reason is that it gives you more of
that horrible void pointer casting you need to pass custom data to
the callbacks.

timer = SDL_AddTimer( 1000, Switch, );

or as you would declare your callback function:

Uint32 my_timer_func(Uint32 interval, void *param);

I’m sorry but I still cannot understand how to pass the parameters

Do you mean something like this bellow?
int a = 100;
param = &a;

Yeah, that would be a most basic example. ‘a’ could be anything you
can get the address of. Just make sure the data is still there when
the callback function is invoked, and that you get the typecasting
right. In this case you could just

timer = SDL_AddTimer( 1000, Switch, &a);

In your callback, you have to cast the pointer before you can use it,
something like this:

Uint32 my_timer_func(Uint32 interval, void *param)
{
	int *my_param = (int *)param;
	...
}

Of course, whatever you pass can be written as well, as it’s passed by
reference. Kinda’ natural with structs, but it’s not how you pass an
int argument normally. Might be worth keeping the difference in mind.

timer = SDL_AddTimer( 100, my_timer_func, param );

Sorry for bothering anyone but I’m not much experienced in C :wink:

Might be a good idea to get a book on C. It’s a rather picky and
sensitive language, so I’d strongly recommend that you read up on
stuff before trying to make it work through experimentation. Too many
chances of running into something that’s “almost” right…

Unfortunately, the only C book I can think of is also one of the few
I’ve ever seen in swedish - and it’s not a translation. Sorry… :slight_smile:

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Saturday 10 May 2003 17.56, Billi Bot wrote:

Well, thank you your message cleared some of the problems for me!
I have C/C++ books thought quite old and not that great but there are
still many online books too :wink: