It is best not to call a function like what I imagine "GameEngineLoop"
would do (modify mutable state) from a Timer callback. One way to do
it is with custom userevents( error checking not present: read the
documentation for the functions):
// other custom events
Uint32 callback(Uint32 interval, void *unused)
event.type = SDL_USEREVENT;
event.user.code = GAME_ENGINE_LOOP;
SDL_Surface *screen = SDL_GetVideoSurface();
int main(int, char **)
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
SDL_TimerID timer = SDL_AddTimer(static_cast<unsigned>(1000.0f /
30.0f), &callback, 0);
bool running = true;
// test for keys, mouse etc
if(event.type == SDL_USEREVENT)
if(event.user.code == GAME_ENGINE_LOOP)
// else ( handle other events )
else if(event.type == SDL_QUIT)
running = false;
// other event types
Using SDL_WaitEvent prevents “background” processing, which may or may
not be required (you may want animations to play more often - to sync
with the screen frame rate rather than the more artificial game logic
rate). You could choose additional timers to handle this.
Another option would be to try handle this in the main loop itself,
with no timers.On Mon, Jul 7, 2008 at 10:12 PM, Jesse P. wrote:
Can someone help me with above?
I wish to call “GameEngineLoop” function 30 times per second.