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