2017-12-27 131 views
0

我觀察了多個關於在Allegro中使用多個定時器的教程,但是這種編程方式對我來說並不適用。 問題是,源地址永遠不會匹配我想要觀看的定時器地址。在Allegro 5中使用多個定時器

我使用多個類/實例來封裝我的代碼,因爲它將來會非常複雜。我的遊戲的主循環位於Game類/實例中。定時器和事件封裝在Engine類/實例中,該實例是Game實例的成員。

Game.cpp:

void Game::GameLoop() { 
    al_wait_for_event(GameEngine.LoopTimerEvent_queue, &GameEngine.LoopTimerEvent); 

    if (GameEngine.LoopTimerEvent.type == ALLEGRO_EVENT_TIMER) 
    { 
    // DEBUG: EVENT SOURCE ADRESSES DON'T MATCH TO THE TIMER ADRESSES 
    std::cout << "TimerEvent: " << GameEngine.LoopTimerEvent.timer.source << " " << GameEngine.VSyncTimer << " " << GameEngine.LoopTimer << " " << GameEngine.InGameTimer << "\n"; 

    if (GameEngine.LoopTimerEvent.timer.source == GameEngine.InGameTimer) 
    { 
     std::cout << "InGameTimerEvent"; 
    } 
    if (GameEngine.LoopTimerEvent.timer.source == GameEngine.VSyncTimer) 
    { 
     std::cout << "VSyncTimerEvent"; 
    } 
    if (GameEngine.LoopTimerEvent.timer.source == GameEngine.LoopTimer) 
    { 
     std::cout << "LoopTimerEvent"; 
    } 
    } 
} 

Engine.cpp:

Engine::Engine() { 
    if (al_init()) std::cout << "allegro initialized\n"; 
    else std::cout << "failed to initialize allegro!\n"; 

    if (InitTimer()) std::cout << "Timer initialized\n"; 
    else std::cout << "failed to initialize timer!\n"; 

    LoopTimerEvent_queue = al_create_event_queue(); 

    al_register_event_source(LoopTimerEvent_queue, al_get_timer_event_source(LoopTimer)); 
    al_register_event_source(LoopTimerEvent_queue, al_get_timer_event_source(VSyncTimer)); 
    al_register_event_source(LoopTimerEvent_queue, al_get_timer_event_source(InGameTimer)); 
    std::cout << "Event queues initialized\n"; 
} 


bool Engine::InitTimer() { 
    LoopTimer = al_create_timer(1.0); 
    if (!LoopTimer) 
    { 
    std::cout << "failed to initialize LoopTimer!\n"; 
    return false; 
    } 

    InGameTimer = al_create_timer(1.0/m_iTimeScale); 
    if (!InGameTimer) 
    { 
    std::cout << "failed to initialize InGameTimer!\n"; 
    return false; 
    } 

    VSyncTimer = al_create_timer(1.0/FPS); 
    if (!VSyncTimer) 
    { 
    std::cout << "failed to initialize VSyncTimer!\n"; 
    return false; 
    } 

    al_start_timer(LoopTimer); 
    al_start_timer(VSyncTimer); 
    al_start_timer(InGameTimer); 
    std::cout << "Timers started\n"; 

    return true; 
} 

Engine.h:

class Engine { 
public: 
    ALLEGRO_DISPLAY* pDisplay = NULL; 
    ALLEGRO_TIMER* VSyncTimer = NULL; 
    ALLEGRO_TIMER* LoopTimer = NULL; 
    ALLEGRO_TIMER* InGameTimer = NULL; 
    ALLEGRO_EVENT LoopTimerEvent; 
    ALLEGRO_EVENT_QUEUE* LoopTimerEvent_queue = NULL; 

    Logger EngineLogger; 
    EventHandler GameEvents; 

    private: 
    double m_iTimeScale = 2.0; 

public: 
    Engine(); 
    ~Engine(); 

    bool InitEngine(); 
    bool InitTimer(); 
    bool InitDisplay(); 

    void UpdateDisplay(); 

    float GetTimeScale(); 
    void SetTimeScale(float timescale); 
}; 

輸出
TimerEvent: 031A0D80 0326AF30 0326A380 0326B090

「TimerEvent:」[實際事件ADRESS] [VSyncTimer ADRESS] [LoopTimer ADRESS] [InGameTimer ADRESS]

哪裏是這些不會忽略這個問題?

回答

0

我意外地初始化了定時器實例兩次,它創建了新的定時器(當然)有不同的地址。但在事件隊列中,老年人因爲我的愚蠢組織而被登記。我通過將隊列註冊放到InitTimer函數來解決這個問題,並用雙定時器初始化修復了這個bug。 現在一切正常!