Polling the event queue

Hi all,
I have a question about polling the event queue. I have a program that is
displaying images (individual letters at various locations on the screen) and
recording button presses when a user finds a particular letter. Once an image
is displayed, the program enters a while(1) loop, checking various conditions
(e.g., checking for a mouse button press, checking to see if feedback was
displayed), and the loop is exited once all of the conditions are false. Each
time that the loop executes, I want to poll the event queue to see if a mouse
button has been pressed. If I call SDL_PollEvent() each time the loop executes,
will the event queue be polled? If the answer is yes, you don’t need to read any
further - when I use that function, my program works fine. My main concern is
getting the time of the mouse button presss - that the time I record is
reasonably close to when the button press occurred - if the event loop is not
being polled each time the loop executes, this will not be the case.

I have tried using SDL_PumpEvents() followed by SDL_PeepEvents() as follows, but
the program crashes after about 50 trials (each display-response sequence
constitutes a trial). Here is the code I was using that includes
SDL_PumpEvents() followed by SDL_PeepEvents():

if (!button) SDL_PumpEvents(); // only check if a button has not been pressed
while (SDL_PeepEvents(&input_event, 1, SDL_GETEVENT, SDL_ALLEVENTS))
{
switch (input_event.type)
{
case SDL_MOUSEBUTTONDOWN:
switch (input_event.button.button)
{
case SDL_BUTTON_LEFT:
button = 6;
break;
case SDL_BUTTON_RIGHT:
button = 7;
break;
}
break;
case SDL_MOUSEBUTTONUP: {};
}

I have the same problem (the program crashing after a certain number of trials)
if I get rid of the while() statement and just enter the above statement if
there is something on the event queue (by checking the return value of
SDL_PeepEvents). The program works fine when I use SDL_PollEvent, but I don’t
know if the event queue is being polled every time the loop executes. Is it
possible that the event queue is filling up when I use SDL_PeepEvents(), and
that is why my program is crashing? Any help would be greatly appreciated -
thank you in advance.

Best,

Chris Dickinson

You actually have to remove the events from the queue. The queue has a
finite length (about 128) and will eventually fill up which could be
why your code fails.

Bob PendletonOn Mon, Oct 6, 2008 at 5:37 PM, Chris Dickinson wrote:

Hi all,
I have a question about polling the event queue. I have a program that is
displaying images (individual letters at various locations on the screen) and
recording button presses when a user finds a particular letter. Once an image
is displayed, the program enters a while(1) loop, checking various conditions
(e.g., checking for a mouse button press, checking to see if feedback was
displayed), and the loop is exited once all of the conditions are false. Each
time that the loop executes, I want to poll the event queue to see if a mouse
button has been pressed. If I call SDL_PollEvent() each time the loop executes,
will the event queue be polled? If the answer is yes, you don’t need to read any
further - when I use that function, my program works fine. My main concern is
getting the time of the mouse button presss - that the time I record is
reasonably close to when the button press occurred - if the event loop is not
being polled each time the loop executes, this will not be the case.

I have tried using SDL_PumpEvents() followed by SDL_PeepEvents() as follows, but
the program crashes after about 50 trials (each display-response sequence
constitutes a trial). Here is the code I was using that includes
SDL_PumpEvents() followed by SDL_PeepEvents():

if (!button) SDL_PumpEvents(); // only check if a button has not been pressed
while (SDL_PeepEvents(&input_event, 1, SDL_GETEVENT, SDL_ALLEVENTS))
{
switch (input_event.type)
{
case SDL_MOUSEBUTTONDOWN:
switch (input_event.button.button)
{
case SDL_BUTTON_LEFT:
button = 6;
break;
case SDL_BUTTON_RIGHT:
button = 7;
break;
}
break;
case SDL_MOUSEBUTTONUP: {};
}

I have the same problem (the program crashing after a certain number of trials)
if I get rid of the while() statement and just enter the above statement if
there is something on the event queue (by checking the return value of
SDL_PeepEvents). The program works fine when I use SDL_PollEvent, but I don’t
know if the event queue is being polled every time the loop executes. Is it
possible that the event queue is filling up when I use SDL_PeepEvents(), and
that is why my program is crashing? Any help would be greatly appreciated -
thank you in advance.

Best,

Chris Dickinson


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

±-------------------------------------+

Bob Pendleton <bob pendleton.com> writes:

You actually have to remove the events from the queue. The queue has a
finite length (about 128) and will eventually fill up which could be
why your code fails.

Bob Pendleton

Thank you , Bob. I had seen a few threads
related to this issue, but none that I found
suggested that if the queue filled,
it could result in a program crash
(only in messages being dropped).

Chris Dickinson