Event Handling, Timers, Linux and Windows

Hi All,

I have been investigating an issue I have with an application I am
developing using SDL and OpenGL. I require mouse and keyboard
interaction for it.

I set up video and timers subsystems and create a couple of timers for
various different tasks, one timer running every 10ms posts a new user
event which I then catch in a main while loop to render a new frame.

This main while loop (using SDL_PollEvent) also contains keyboard and
mouse event processing. At the end of this loop I have a call to
SDL_Delay(10) to allow other apps to still run in background.

When running the code on Windows XP Pro I get the following behaviour -
otherwise on Linux (Debian) it works fine. Using SDL 1.2.8.

When the application first starts up mouse events seem to work correctly
if a little slow. (I draw the mouse coords on the screen to see how
quickly they are updating). After a few seconds however mouse and
keyboard events stop being picked up by the while loop.

This behaviour is slightly strange. If I move the Push Event code to
the end of the while loop (rather than in the 10ms timer called
function) events seem to work no problem. I would prefer to keep the
Push Event code in the 10ms timer however.

I was wondering if anyone has come across something similar? I assume
this difference in behaviour is because of threading differences between
Linux and Windows. Anyone any suggestions or comments? Is this method
a recipe for disaster?

Cheers,
dhm

I’m curious, why push events into the main loop (which is running, anyway?)
(Maybe I just didn’t understand what you’re trying to do… :^/ )

What I normally do is something fairly simple:

while (!done)
{
old_time = SDL_GetTicks();

while (SDL_PendingEvent(&event))
{
  ... handle events (mouse, keyboard, etc.) ...
}

... draw screen ...

cur_time = SDL_GetTicks();
remaining = (old_time + (1000 / FPS)) - cur_time;

SDL_Delay(remaining);

}

Would something similar not work for your needs?

-bill!On Sat, Mar 26, 2005 at 05:45:10PM +0000, David Muir wrote:

This behaviour is slightly strange. If I move the Push Event code to
the end of the while loop (rather than in the 10ms timer called
function) events seem to work no problem. I would prefer to keep the
Push Event code in the 10ms timer however.

Hi Bill,

I was pushing events to the main loop because they were potentially from
a different thread (created using Add Timer function in SDL).

I got a very useful response from Donny Viszneki who sent me a link:


which turned out to be very helpful.

This follows your example code snippet too :slight_smile: I am moving into a
similar framework myself - looks like I was trying to make it far more
complicated than I thought :slight_smile:

Thanks for the response Bill,
Cheers,
DaveOn Sun, 2005-03-27 at 21:39 -0800, Bill Kendrick wrote:

On Sat, Mar 26, 2005 at 05:45:10PM +0000, David Muir wrote:

This behaviour is slightly strange. If I move the Push Event code to
the end of the while loop (rather than in the 10ms timer called
function) events seem to work no problem. I would prefer to keep the
Push Event code in the 10ms timer however.

I’m curious, why push events into the main loop (which is running, anyway?)
(Maybe I just didn’t understand what you’re trying to do… :^/ )

What I normally do is something fairly simple:

while (!done)
{
old_time = SDL_GetTicks();

while (SDL_PendingEvent(&event))
{
  ... handle events (mouse, keyboard, etc.) ...
}

... draw screen ...

cur_time = SDL_GetTicks();
remaining = (old_time + (1000 / FPS)) - cur_time;

SDL_Delay(remaining);

}

Would something similar not work for your needs?

-bill!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Heh, sometimes I feel dumb for having such a simplex method of coding,
but then sometimes (like this) I feel smart. :^)

-bill!
(damn, I sound like the Pakleds from Star Trek…
“We look for things… things that make us go!” ;^) )On Tue, Mar 29, 2005 at 12:36:49AM +0100, David Muir wrote:

This follows your example code snippet too :slight_smile: I am moving into a
similar framework myself - looks like I was trying to make it far more
complicated than I thought :slight_smile: