【问题标题】:Using multiple timers in Allegro 5在 Allegro 5 中使用多个计时器
【发布时间】:2018-06-08 03:56:22
【问题描述】:

我观看了多个关于在 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

"TimerEvent: " [实际事件地址] [VSyncTimer 地址] [LoopTimer 地址] [InGameTimer 地址]

这些地址的问题在哪里?

【问题讨论】:

    标签: c++ timer allegro


    【解决方案1】:

    我不小心初始化了定时器实例两次,这创建了(当然)不同地址的新定时器。但是由于我的愚蠢组织,在事件队列中注册了旧的。我通过将队列注册放入 InitTimer 函数来解决此问题,并通过双定时器初始化修复了该错误。 现在一切正常!

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多