0

在我的 SDL 1.2 程序中,我有一个KeyPressed函数,它需要检查是否按下了某个键。如果鼠标指针没有移动到窗口前面,则程序会在按下键时按预期停止。另一方面,如果将鼠标指针移到窗口前面,事件队列似乎已填满,之后程序不再响应按键。当没有键盘事件时,有没有办法清空事件队列?

#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;
}
4

2 回答 2

2

您应该使用轮询在 while 循环中检查事件,这将在处理事件时从队列中弹出事件。使用多个if/then语句或一个swicth块来处理事件。我推荐这种方法,因为您很容易错过重要事件,例如SDL_QUIT使用时SDL_PeepEvent

基本示例:

SDL_Event e;
while (SDL_PollEvent(&e))
{
    if (e.type == SDL_QUIT)
    {
        // do something here
    }
}
于 2014-11-25T16:51:54.143 回答
0

受 Zammalad 评论启发的以下解决方案似乎有效。当没有找到按键事件时,我们从队列中删除一个事件(通常是鼠标事件),以便为新事件腾出空间。为了使代码更健壮,我还将标量事件替换为数组事件

#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;
}
于 2014-11-27T09:19:23.653 回答