【问题标题】:Allegro 5 does not respond to keyboard events or window closing after going out of focusAllegro 5 在失去焦点后不响应键盘事件或窗口关闭
【发布时间】:2013-03-19 17:33:12
【问题描述】:

标题说明了一切。当窗口处于焦点时,一切正常(我可以使用 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;
}

【问题讨论】:

    标签: allegro5


    【解决方案1】:

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

    如果您将其替换为al_wait_for_event()(它会阻止并删除事件),那么我认为您的代码会按照您的预期进行。

    【讨论】:

    • 我的代码确实有效。这是在应用程序窗口失焦并返回后停止工作的事件。如果我使用 al_wait_for_event() 设置它,我的游戏循环只会在我按住事件捕获器中的键时运行。所以,我真的不觉得你的回答有帮助,除非我因为我的菜鸟不明白。 xD
    • @MatejŠtajduhar。那是因为您也应该在事件队列中添加一个计时器。否则,您也可以使用al_get_next_event(),但仅在返回true 时处理该事件。 peek 绝对是在这里使用的错误函数。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多