我看了很多关于在Allegro中使用多个计时器的教程,但这种编程方式并不适合我。 问题是源地址永远不会与我想要观看的计时器地址匹配。
我使用多个类/实例来封装我的代码,因为将来会非常复杂。我的游戏的主循环位于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
&#34; TimerEvent:&#34; [实际事件地址] [VSyncTimer地址] [LoopTimer地址] [InGameTimer地址]
这些地址的问题在哪里?
答案 0 :(得分:0)
我偶然初始化了两次定时器实例,这创造了新的定时器(当然)不同的地址。但是在事件队列中,由于我的愚蠢组织,OLD被注册了。我通过将队列注册放到InitTimer函数并通过双定时器初始化修复了错误来解决这个问题。 现在一切都很完美!