Problems with Timers

Hey all.

I have some code that looks like this

updateGame()
{
SDL_Event event;

while ( SDL_PollEvent( &event ) )
{

}
}

drawGame()
{

}

while(!done)
{
updateGame();
drawGame();
}

Now, I tried to change my code to use an SDL Timer. I added the
Uint32 and void *params parameters to updateGame and put this line
before my while loop:

SDL_AddTimer(30,updateGame,NULL);

Now, sometimes it works for a little, but it always ends up locking up
and Not Responding. It stops drawing, the screen turns white, and I
get an hourglass. What’s weird, I can still quit the game by hitting
escape, which is an event which calls Quit(0);

Can anyone help me? I posted the code here for reference:
http://ucfpawn.homelinux.com/main.cpp--
Jason G

Check out this thread from the mailing list. I had something simular…

http://twomix.devolution.com/pipermail/sdl/2004-November/065613.html

Walt

Jason G wrote:>Hey all.

I have some code that looks like this

updateGame()
{
SDL_Event event;

while ( SDL_PollEvent( &event ) )
{

}
}

drawGame()
{

}

while(!done)
{
updateGame();
drawGame();
}

Now, I tried to change my code to use an SDL Timer. I added the
Uint32 and void *params parameters to updateGame and put this line
before my while loop:

SDL_AddTimer(30,updateGame,NULL);

Now, sometimes it works for a little, but it always ends up locking up
and Not Responding. It stops drawing, the screen turns white, and I
get an hourglass. What’s weird, I can still quit the game by hitting
escape, which is an event which calls Quit(0);

Can anyone help me? I posted the code here for reference:
http://ucfpawn.homelinux.com/main.cpp

I’m not doing graphics in the timer though, the graphics is handled in
the main loop

Do you think that the timer is getting a lower priority or something?On Mon, 29 Nov 2004 14:40:10 -0700, walt wrote:

Check out this thread from the mailing list. I had something simular…

http://twomix.devolution.com/pipermail/sdl/2004-November/065613.html

Walt

Jason G wrote:

Hey all.

I have some code that looks like this

updateGame()
{
SDL_Event event;

while ( SDL_PollEvent( &event ) )
{

}
}

drawGame()
{

}

while(!done)
{
updateGame();
drawGame();
}

Now, I tried to change my code to use an SDL Timer. I added the
Uint32 and void *params parameters to updateGame and put this line
before my while loop:

SDL_AddTimer(30,updateGame,NULL);

Now, sometimes it works for a little, but it always ends up locking up
and Not Responding. It stops drawing, the screen turns white, and I
get an hourglass. What’s weird, I can still quit the game by hitting
escape, which is an event which calls Quit(0);

Can anyone help me? I posted the code here for reference:
http://ucfpawn.homelinux.com/main.cpp


Jason G

I’m not doing graphics in the timer though, the graphics is handled in
the main loop

You can’t call SDL_PollEvent() or SDL_PumpEvents() in the timer either. :slight_smile:
Basically think that all input and graphics can only be done in the main
thread, and that the timer is in another thread and you’ll be fine. Note
that the timer may not actually be another thread on some systems, but that
should help you think about it. :slight_smile:

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Ah, I see.

Well, can I use SDL to make a time-based input while also having a
processor-based graphics drawing loop? Is there any way to pull that
off without using separate threads? I guess I can have the timer flip
a boolean switch that the main loop checks for, like this:

bool doevents=false;

Uint32 myTimer(Uint32 interval, void *params)
{
doevents=true;
return interval;
}

int main()
{

while(!done)
{
draw_graphics();
if(doevents) { do_SDL_events(); doevents=false; }
}

Would this be an easy efficient way to handle this problem, or is
there a better way?

Thanks for responding BTW, you rock!

Jason GOn Tue, 30 Nov 2004 07:30:28 -0800, Sam Lantinga wrote:

I’m not doing graphics in the timer though, the graphics is handled in
the main loop

You can’t call SDL_PollEvent() or SDL_PumpEvents() in the timer either. :slight_smile:
Basically think that all input and graphics can only be done in the main
thread, and that the timer is in another thread and you’ll be fine. Note
that the timer may not actually be another thread on some systems, but that
should help you think about it. :slight_smile:

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment


Jason G