在Allegro 5中使用多个定时器
我观察了多个关于在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]
哪里是这些不会忽略这个问题?
回答:
我意外地初始化了定时器实例两次,它创建了新的定时器(当然)有不同的地址。但在事件队列中,老年人因为我的愚蠢组织而被登记。我通过将队列注册放到InitTimer函数来解决这个问题,并用双定时器初始化修复了这个bug。 现在一切正常!
以上是 在Allegro 5中使用多个定时器 的全部内容, 来源链接: utcz.com/qa/265449.html