2
标题说明了一切。一切正常(我可以使用esc按钮或按X退出窗口),当窗口焦点时,但只要我最小化它,或打开另一个窗口,当我再次将Allegro应用程序放在焦点上时,事件不再工作了。它不再对Escape或X做出反应。任何解决方案Allegro 5没有响应键盘事件或关闭后关闭窗口
代码:
#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;
}
我的代码确实有效。这是应用程序窗口退出焦点后停止工作的事件。 如果我用al_wait_for_event()设置它,我的游戏循环只会在按住事件捕捉器中的按键时运行。 所以,我真的没有找到你的答案有帮助,除非我不明白是因为我的noobness。 xD – 2013-03-20 17:51:36
@MatejŠtajduhar。这是因为您应该为事件队列添加一个计时器。否则,您也可以使用'al_get_next_event()',但只处理事件返回true。 'peek'在这里使用绝对是错误的功能。 – Matthew 2013-03-20 17:55:32
你说得对,对不起!感谢帮助的人! =) – 2013-03-20 18:37:42