Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL_AddTimer() Callback

Started by QBomb Oct 17, 2003 at 10:05 AM 3 replies 3.1k views
Original Post
QBomb
QBomb
Is there someone here who can help me out with setting up a callback function for SDL_AddTimer() ? I''m wanting to create a callback function that will simply call SDL_Flip().I guess what I''m really asking is what the function prototype should look like. I''m coding in C++, and I read somewhere that you must declare the callback member function as static? (This is to negate the fact that non-static member functions contain a "this" pointer which doesn''t jive with the SDL_AddTimer function) Anyhow, here is what the SDL docs have on SDL_AddTimer()... 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); Any help or suggestions are appreciated and TIA.
citizen3019
citizen3019
quote:
Original post by QBomb
/* type definition for the "new" timer callback function */
typedef Uint32 (*SDL_NewTimerCallback)(Uint32 interval, void *param);



If the function is a class member...
static Uint32 fnSDLTimerCallback(Uint32 interval, void *pvParam)
{
return 0L;
}

...else if it is not a class member function the static is not needed:
Uint32 fnSDLTimerCallback(Uint32 interval, void *pvParam)
{
return 0L;
}

* This has to do with the calling conventions of:
__stdcall = args passed right to left
and
__thiscall = args also passed from right to left, but also the invisible invoking object *this is passed for accessing the virtual function table

Hope that helps. If you are using MS VC6 then look up C/C++ calling conventions for further documentation.
QBomb
QBomb
Ok, that clears up the need for it to be static.

I''m getting the following link error:

[Linker error] undefined reference to `GWindow::flip(unsigned, void*)''

Here''s the member function prototype:
static Uint32 flip( Uint32 interval, void* param);

Here''s the function definition:
static Uint32 flip(Uint32 interval, void* param)
{
SDL_Flip( (SDL_Surface*)param );
return 0L;
}

And here is where I register the function:
flipTimer = SDL_AddTimer( (33/10)*10, flip, (void*)screen );

Any ideas on what I''m doing wrong? flip() is not being called anywhere else. Does that typedef in my previous post need to be included in the source? (I get the error regardless if it is or not, but I want to include it if I should)
citizen3019
citizen3019
quote:
Original post by QBomb
Does that typedef in my previous post need to be included in the source?


No.
quote:
Original post by QBomb
[Linker error] undefined reference to `GWindow::flip(unsigned, void*)'
definition:
static Uint32 flip(Uint32 interval, void* param)
{
SDL_Flip( (SDL_Surface*)param );
return 0L;
}


define the function as:
Uint32 GWindow::flip(Uint32 interval, void *param)
{
SDL_Flip( (SDL_Surface*)param );
return 0L;
}
remove static from the definition. Leave it on the class declaration and be sure too include the class your function belongs to in the definition.

** ALSO, it can be tricky accessing class variables in static functions as static functions do not pass invoking object *this. Keep that in mind=)





[edited by - citizen3019 on October 17, 2003 11:42:52 AM]

[edited by - citizen3019 on October 17, 2003 11:46:21 AM]
TravisWells
TravisWells
You can call non-static member functions using a proxy function (not sure about name, seems right).

Basicly, right a non-member or static function that cast''s the void pointer to the correct class, and call the method on that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.