How do I empty the SDL event queue?

3.9k views Asked by At

In my SDL 1.2 program I have a function KeyPressed which needs to check if a key has been pressed. If the mouse pointer has not been moved in front of the window the program halts as intended when a key is pressed. On the other hand, if the mouse pointer is moved in front of the window the event queue seems to fill up and after that the program does not respond to key presses. Is there a way to empty the event queue when there are no keyboard events in it?

#include <SDL/SDL.h>
#include <stdio.h>

static void Init(int *error)
{
    SDL_Surface *display;

    *error = SDL_Init(SDL_INIT_VIDEO);
    if (! *error) {
        display = SDL_SetVideoMode(640, 480, 8, 0);
        if (display != NULL) {
            *error = 0;
        } else {
            fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
            *error = 1;
        }
    } else {
        fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
        *error = 1;
    }   
}


static int KeyPressed(void)
{
    SDL_Event event;
    int count, result;

    result = 0;
    SDL_PumpEvents();
    count = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_EVENTMASK(SDL_KEYDOWN));
    switch (count) {
        case -1:
            fprintf(stderr, "SDL_PeepEvents: %s\n", SDL_GetError());
            break;
        case 0:
            break;
        case 1:
            result = 1;
            break;
    }
    return result;
}


int main(void)
{
    int error;

    Init(&error);
    if (! error) {
        do {
            /*nothing*/
        } while (! KeyPressed());
    }
    return error;
}
2

There are 2 answers

0
August Karlstrom On BEST ANSWER

The following solution, inspired by Zammalad's comment seems to work. When no key down event is found we remove one event (typically a mouse event) from the queue to make room for new events. To make the code more robust I have also replaced the scalar event with the array events.

#define LEN(a) ((int) (sizeof (a) / sizeof (a)[0]))

static int KeyPressed(void)
{
    SDL_Event events[1];
    int count, result;

    result = 0;
    SDL_PumpEvents();
    /*search the event queue for key down events*/
    count = SDL_PeepEvents(events, LEN(events), SDL_PEEKEVENT, SDL_EVENTMASK(SDL_KEYDOWN));
    switch (count) {
        case -1:
            fprintf(stderr, "SDL_PeepEvents: %s\n", SDL_GetError());
            break;
        case 0:
            /*make room for new events by removing (non-key down) event from the queue*/
            count = SDL_PeepEvents(events, LEN(events), SDL_GETEVENT, -1);
            if ((count > 0) && (events[0].type == SDL_QUIT)) {
                exit(EXIT_SUCCESS);
            }
            break;
        case 1:
            result = 1;
            break;
    }
    return result;
}
2
TPS On

You should be checking event's in a while loop using polling which will pop the events from the queue as they are handled. Use either multiple if/then statements or a swicth block for handling the event. I recommend this method as you can easily miss important events such as SDL_QUIT when using SDL_PeepEvent

Basic example:

SDL_Event e;
while (SDL_PollEvent(&e))
{
    if (e.type == SDL_QUIT)
    {
        // do something here
    }
}