2009-07-31 36 views

Trả lời

3

Trích từ wiki allegro:

al_install_timer(1.0/FPS); 

... 

while (1) { 
     al_wait_for_event(queue, &event); 

     /* handle input events */ 

     if (event.type == ALLEGRO_EVENT_TIMER) { 
       handle_game_tick(); 
       need_redraw = true; 
     } 

     if (need_redraw && al_event_queue_is_empty(queue)) { 
       render_last_frame(); 
       need_redraw = false; 
     } 
} 

Nếu bạn muốn khung bỏ qua, bỏ qua render_last_frame() lệnh bất cứ khi nào bạn phát hiện rằng bạn đang tụt hậu so với trong khung (ví dụ bằng cách sử dụng các al_current_time() chức năng).

+0

Ah, vì vậy bạn nên tận dụng sự kiện. Threading có thể cải thiện khả năng ổn định hình ảnh không? Hàng đợi sự kiện có an toàn không? – amarillion

+0

Threading sẽ không giúp gì cả. Thông thường, nó có thể giúp ích nếu bạn sử dụng thời gian chính xác khi bạn kết xuất (một lần nữa, al_current_time() là bạn của bạn). Điều này sẽ cho phép những thứ như nội suy chính xác vị trí để hiển thị một cái gì đó liên quan đến logic trò chơi. Bạn cũng nên nhớ rằng al_flip_display() sẽ chờ đợi cho vsync (nếu nó được kích hoạt, nhưng có vsync trên là phải cho hoạt hình hoàn hảo trơn tru anyway). Và A5 là 100% chủ đề lưu - hàng đợi sự kiện đặc biệt vì chúng là một trong những phương tiện để giao tiếp trên các chủ đề. –

+0

Liên kết đến trang wiki được đề cập: http://wiki.allegro.cc/index.php?title=Porting_from_A4_to_A5 – Leftium

3

Here là một phiên bản hoàn chỉnh hơn về câu trả lời của Allefant (làm theo các liên kết cho chi tiết line-by-line giải thích):

#include <stdio.h> 
#include <allegro5/allegro.h> 

const float FPS = 60; 

int main(int argc, char **argv) 
{ 
    ALLEGRO_DISPLAY *display = NULL; 
    ALLEGRO_EVENT_QUEUE *event_queue = NULL; 
    ALLEGRO_TIMER *timer = NULL; 
    bool redraw = true; 

    if(!al_init()) { 
     fprintf(stderr, "failed to initialize allegro!\n"); 
     return -1; 
    } 

    timer = al_create_timer(1.0/FPS); 
    if(!timer) { 
     fprintf(stderr, "failed to create timer!\n"); 
     return -1; 
    } 

    display = al_create_display(640, 480); 
    if(!display) { 
     fprintf(stderr, "failed to create display!\n"); 
     al_destroy_timer(timer); 
     return -1; 
    } 

    event_queue = al_create_event_queue(); 
    if(!event_queue) { 
     fprintf(stderr, "failed to create event_queue!\n"); 
     al_destroy_display(display); 
     al_destroy_timer(timer); 
     return -1; 
    } 

    al_register_event_source(event_queue, al_get_display_event_source(display)); 

    al_register_event_source(event_queue, al_get_timer_event_source(timer)); 

    al_clear_to_color(al_map_rgb(0,0,0)); 

    al_flip_display(); 

    al_start_timer(timer); 

    while(1) 
    { 
     ALLEGRO_EVENT ev; 
     al_wait_for_event(event_queue, &ev); 

     if(ev.type == ALLEGRO_EVENT_TIMER) { 
     redraw = true; 
     } 
     else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 
     break; 
     } 

     if(redraw && al_event_queue_is_empty(event_queue)) { 
     redraw = false; 
     al_clear_to_color(al_map_rgb(0,0,0)); 
     al_flip_display(); 
     } 
    } 

    al_destroy_timer(timer); 
    al_destroy_display(display); 
    al_destroy_event_queue(event_queue); 

    return 0; 
} 
Các vấn đề liên quan