2014-12-07 32 views
0

我有這樣的問題:我想顯示消息「你好!」在比賽開始之前,在Allegro屏幕的中心附近。不知道爲什麼在我編譯程序之後總是隻有全窗口的白色,而不是消息「你好!」。我不知道爲什麼它不顯示消息「你好!」 。但是,如果我刪除註釋行之間的代碼// *****程序顯示消息「你好!」好。有人能告訴我如何解決這個問題嗎?Allegro5顯示文本不起作用

#include<allegro5\allegro5.h> 
#include<allegro5\allegro_native_dialog.h> 
#include<allegro5\allegro_font.h> 
#include<allegro5\allegro_ttf.h> 
#include<allegro5\allegro_primitives.h> 

#include<iostream> 
using namespace std; 
int main(){ 
    int ScreenWidth = 800, ScreenHeight = 600; 

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

    al_set_new_display_flags(ALLEGRO_WINDOWED); 
    ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);//creating window with dimensions: 800:600 px 
    al_set_window_position(display, 200, 100);//place from left top positioning the frame of display 
    al_set_window_title(display, "Try to catch me!"); 

    if (!display)//show this message if sth wrong with display 
    { 
     al_show_native_message_box(display, "Sample Title", "Display Settings", "Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 
     return -1; 
    } 

    al_init_font_addon();//initialization font addon 
    al_init_ttf_addon();//initialization ttf(true type font) addon 

    //INTRO 
    ALLEGRO_FONT *fontOrbionBlack36 = al_load_font("fonts/Orbitron Black.ttf", 36, NULL); 
    al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth/2, ScreenHeight/2, ALLEGRO_ALIGN_CENTRE, "Hello!"); 
    al_rest(5.0); 

    //******************************************************************************** 
    al_init_primitives_addon(); 
    al_install_keyboard(); 

    ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255); 
    ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 

    al_register_event_source(event_queue, al_get_keyboard_event_source()); 

    bool done = false; 
    int x = 10, y = 10; 
    int moveSpeed = 5; 

    while (!done) 
    { 
     ALLEGRO_EVENT events; 
     al_wait_for_event(event_queue, &events); 
     if (events.type = ALLEGRO_EVENT_KEY_DOWN) 
     { 
      switch (events.keyboard.keycode) 
      { 
      case ALLEGRO_KEY_DOWN: 
       y += moveSpeed; 
       break; 
      case ALLEGRO_KEY_UP: 
       y -= moveSpeed; 
       break;   
      case ALLEGRO_KEY_ESCAPE: 
       done = true; 
       break; 
      } 
     } 
     al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0); 
     al_flip_display(); 
     al_clear_to_color(al_map_rgb(0, 0, 0)); 
    } 
    //***************************************************************************************** 

    al_flip_display(); 
    al_rest(10.0);//rest is very simmilar to Sleep() it is waitg function, waiting 3s then close the allegro display 
    //al_destroy_font(fontOrbionBlack18); 
    al_destroy_font(fontOrbionBlack36); 
    al_destroy_display(display);//destroy the display at the end of the programm 


    return 0; 
} 

回答

0

這裏有一些問題阻止您看到文本。

  1. 您已經設置了「等待事件然後繪製」循環,通常與使用計時器相關聯,但您沒有計時器。這意味着,除非您觸發某種事件(例如通過按鍵盤鍵),否則al_wait_for_event將無限期停止,並且您的程序將永遠無法到達al_flip_display的呼叫。你可以閱讀更多關於定時器here,但暫時只知道你將不得不按鍵盤鍵來讓這個程序進展。

  2. 您的清除/繪製/翻轉呼叫的順序有點混亂。

嘗試是這樣的主循環中:

al_clear_to_color(al_map_rgb(0, 0, 0));` 
al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0); 
al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth/2, ScreenHeight/2, ALLEGRO_ALIGN_CENTRE, "Hello!"); 
al_flip_display(); 

換句話說,清晰,再畫,然後翻轉。目前您只能繪製文本一次(在主循環之前)。這意味着清除屏幕將清除文本 - 您需要在每幀之後繪製文本,之後清除屏幕,但之前翻轉顯示。

如果你改變了這兩件事,你應該可以在按下鍵盤按鈕後看到文字。不過,也有一些其他的變化,這可能有助於:

  1. ,除非他們提供有用的功能我想刪除這些調用al_rest。目前他們只是讓你等待5秒鐘,然後開始你的主循環(這意味着即使你按下一個鍵,你也不能立即看到文本)。

  2. 沒有理由在主循環退出後翻轉顯示,儘管我想這是您之前的顯示文本實驗中遺留下來的。

  3. 檢查事件類型時,請使用events.type == ALLEGRO_EVENT_KEY_DOWN而不是events.type = ALLEGRO_EVENT_KEY_DOWN。目前,您實際上將值ALLEGRO_EVENT_KEY_DOWN指定爲events.type,覆蓋原來的值。您的編譯器可能(也可能應該)已經向您發出警告。