2

标题说明了一切。当窗口处于焦点时,一切正常(我可以使用 esc 按钮或按 X 退出窗口),但是一旦我最小化它,或者打开另一个窗口,当我再次将 Allegro 应用程序置于焦点时 - 事件别再工作了。它不再对 Escape 或 X 做出反应。有什么解决办法吗?

代码:

#include <allegro/allegro5/allegro.h>
#include <allegro/allegro5/allegro_native_dialog.h>
#include <allegro/allegro5/allegro_font.h>
#include <allegro/allegro5/allegro_ttf.h>
#include <allegro/allegro5/allegro_image.h>
#include <allegro/allegro5/allegro_primitives.h>
#include <iostream>

#define SCREENWIDTH 800
#define SCREENHEIGHT 600

using namespace std;

bool running = true;

int main(int argc, char** argv)
{
    al_init();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init_primitives_addon();
    al_install_keyboard();

    ALLEGRO_DISPLAY* display = al_create_display(SCREENWIDTH, SCREENHEIGHT);
    ALLEGRO_FONT* font = al_load_font("soviet.ttf", 36, 0);
    ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_display_event_source(display));


    if(!al_init())
    {
        al_show_native_message_box(NULL, NULL, NULL, "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_YES_NO);
        return -1;
    }

    if(!display)
    {
        al_show_native_message_box(display, "ERROR", "Display settings", "Display window was not created sucessfully", NULL, ALLEGRO_MESSAGEBOX_YES_NO);
        return -1;
    }

    int x1 = 10;
    int y1 = 10;
    int x2 = 30;
    int y2 = 30;

    while(running)
    {
        ///OnUpdate
        ALLEGRO_EVENT events;
        al_peek_next_event(event_queue, &events);

        if(events.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            if(events.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
            {
                running = false;
                break;
            }
        }
        if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            running = false;
            break;
        }

        x1++;
        y1++;
        x2++;
        y2++;

        ///OnRender
        al_draw_text(font, al_map_rgb(255, 0, 255), 200, 200, 0, "Test");
        al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 0));

        al_flip_display();
        al_clear_to_color(al_map_rgb(0, 0, 0));
        al_rest(0.05);
    }

    ///DEALLOCATE MEMORY
    al_destroy_font(font);
    al_destroy_display(display);

    return 0;
}
4

1 回答 1

0

al_peek_next_event()不会删除事件,也不会阻止。因此,由于这两个原因,您的代码将无法正常工作。

如果您将其替换为al_wait_for_event()(which blocks and remove the event),那么我认为您的代码将执行您期望它执行的操作。

于 2013-03-19T22:05:44.257 回答