2017-12-27 15 views
0

Allegro에서 다중 타이머를 사용하는 것에 관한 여러 튜토리얼을 보았지만 그 프로그래밍 방식은 나에게 적합하지 않습니다. 문제는 소스 주소가보고 싶은 타이머 주소와 일치하지 않는다는 것입니다.Allegro 5에서 다중 타이머 사용하기

내 코드를 캡슐화하기 위해 여러 클래스/인스턴스를 사용합니다. 앞으로는 매우 복잡 할 것이기 때문입니다. 내 게임의 메인 루프는 Game 클래스/인스턴스에 있습니다. 타이머와 이벤트는 Game 클래스/인스턴스에 캡슐화됩니다.이 인스턴스는 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]

이 adresses에 문제가

?

답변

0

우연히 타이머 인스턴스를 두 번 초기화하여 (물론) 다른 주소로 새 타이머를 만들었습니다. 그러나 이벤트 대기열에서 나의 바보 같은 조직 때문에 오래된 것들이 등록되었습니다. 큐 등록을 InitTimer 함수에 넣고 더블 타이머 초기화로 버그를 수정했습니다. 이제 모든 것이 완벽하게 작동합니다!