SDL_SetTimer

How can I use this? I do it like this:
SDL_TimerCallback timertest(Uint32 interval);

SDL_TimerCallback timertest(Uint32 interval){
return 0; /* only to test */
};

int main {

SDL_setTimer(100,timertest(100));

}

No compile errors, but runtime error! What’s wrong??

How can I use this? I do it like this:
SDL_TimerCallback timertest(Uint32 interval);

SDL_TimerCallback timertest(Uint32 interval){
return 0; /* only to test */
};

int main {

SDL_setTimer(100,timertest(100));

}

No compile errors, but runtime error! What’s wrong??

What version of SDL are you using? What error are you getting?

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

“Stefan Renz” wrote in message
news:8t23n8$rcv$1 at ftp.lokigames.com

How can I use this? I do it like this:
SDL_TimerCallback timertest(Uint32 interval);

SDL_TimerCallback timertest(Uint32 interval){

This declares a function returning a SDL_TimerCallback. A SDL_TimerCallback
is a pointer to a function that takes an Uint32 as argument and returns an
Uint32.

return 0; /* only to test */
};

This return the value ‘0’ as an SDL_TimerCallback, i.e. a null pointer.

int main {

SDL_setTimer(100,timertest(100));

This calls timertest with an argument of 100, and passes the return value
(which is 0) to SDL_setTimer.


}

No compile errors, but runtime error! What’s wrong??

SDL_SetTimer takes a pointer to a function as argument, and then tries to
call that function at regular intervals. You passed pointer that does not
point to a function (i.e. a null pointer). Therefore, a runtime error
occurs when the pointer is dereferenced.–
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

I think I use the newest version of SDL, 3.X or something like this. (Sorry
but I don’t want to reboot in Linux to look)
I get an Segmentation fault. Stupid Question: How can I make an pointer to a
function?
Should I do it like this:
Uint32 timertest(Uint32 interval);

Uint32 timertest(Uint32 interval){

return 0; /* or the next interval */
};

main {

SDL_setTimer(100,timertest(100));

};

Please don’t laugh.

sdl-news at lokigames.com wrote:

Should I do it like this:
Uint32 timertest(Uint32 interval);

Uint32 timertest(Uint32 interval){

return 0; /* or the next interval */
};

main {

SDL_setTimer(100,timertest(100));

};

Absolutely not. You have to use the ‘&’ operator, I don’t remember what it is
called but it takes the address of what you apply it to. In your example you
would do:

int main() {

SDL_SetTimer(100, &timertest);

};

I think I use the newest version of SDL, 3.X or something like this. (Sorry
but I don’t want to reboot in Linux to look)
I get an Segmentation fault. Stupid Question: How can I make an pointer to a
function?
Should I do it like this:
Uint32 timertest(Uint32 interval);

Uint32 timertest(Uint32 interval){

return 0; /* or the next interval */
};

main {

SDL_setTimer(100,timertest(100));

};

Do it like this:
SDL_SetTimer(100, timertest);

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software