SDL_PollEvent question

Hi all,
I have a quick question regarding using SDL_PollEvent.
I have a program that continually checks a number of
different if()statements within a while(1) loop.
In that loop, I am using SDL_PollEvent to check
for key presses with the statement

while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
// record key press
}

The key presses are being recorded - my question is this:
each time the program runs through the while(1) loop,
does it check to see whether an SDL_Event has occurred,
and if one has, does it then enter the while(SDL_PollEvent) loop?
I realize that this is a very simplistic question,
but I want to be certain that this code is executing
the way that I think it is executing.
Thank you in advance.

Chris Dickinson

Each time the main loop runs, it runs a loop that keeps calling SDL_PollEvent until it returns false.

Since events are not produced as fast as this consumes them, this is a fairly logical approach.

You could change it to use if instead of while, but then if multiple events come in during one iteration of the main loop (for example if the main loop involves rendering and is running at a low
framerate), then you would have queued events still waiting for a few frames, and that would be bad.

So it is best to make sure you poll and process all events that are waiting each time, the while(SDL_PollEvent(&event)) loop does this.On 05/27/2010 07:32 AM, Chris Dickinson wrote:

Hi all,
I have a quick question regarding using SDL_PollEvent.
I have a program that continually checks a number of
different if()statements within a while(1) loop.
In that loop, I am using SDL_PollEvent to check
for key presses with the statement

while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
// record key press
}

The key presses are being recorded - my question is this:
each time the program runs through the while(1) loop,
does it check to see whether an SDL_Event has occurred,
and if one has, does it then enter the while(SDL_PollEvent) loop?
I realize that this is a very simplistic question,
but I want to be certain that this code is executing
the way that I think it is executing.
Thank you in advance.

Chris Dickinson


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier

In short, yes.

Jonny DOn Thu, May 27, 2010 at 7:36 AM, Forest Hale wrote:

Each time the main loop runs, it runs a loop that keeps calling
SDL_PollEvent until it returns false.

Since events are not produced as fast as this consumes them, this is a
fairly logical approach.

You could change it to use if instead of while, but then if multiple events
come in during one iteration of the main loop (for example if the main loop
involves rendering and is running at a low
framerate), then you would have queued events still waiting for a few
frames, and that would be bad.

So it is best to make sure you poll and process all events that are waiting
each time, the while(SDL_PollEvent(&event)) loop does this.

On 05/27/2010 07:32 AM, Chris Dickinson wrote:

Hi all,
I have a quick question regarding using SDL_PollEvent.
I have a program that continually checks a number of
different if()statements within a while(1) loop.
In that loop, I am using SDL_PollEvent to check
for key presses with the statement

while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
// record key press
}

The key presses are being recorded - my question is this:
each time the program runs through the while(1) loop,
does it check to see whether an SDL_Event has occurred,
and if one has, does it then enter the while(SDL_PollEvent) loop?
I realize that this is a very simplistic question,
but I want to be certain that this code is executing
the way that I think it is executing.
Thank you in advance.

Chris Dickinson


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


LordHavoc
Author of DarkPlaces Quake1 engine -
http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged
demo." - James Klass
"A game is a series of interesting choices." - Sid Meier


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org