Example #1
0
void CallbackTimer::Update()
{
	if (!m_timeouts.empty())
	{
		if (m_resort)
		{
			std::sort(m_timeouts.begin(), m_timeouts.end());
			m_resort = false;
		}

		CTimeValue now = gEnv->pTimer->GetFrameStartTime();

		while (!m_timeouts.empty() && (m_timeouts.front().timeout <= now))
		{
			TimeoutInfo timeout = m_timeouts.front();
			m_timeouts.pop_front();

			TimerInfo timer = m_timers[timeout.timerID];

			timer.callback(timer.userdata, timeout.timerID);

			if (m_timers.validate(timeout.timerID))
			{
				if (!timer.repeating)
					m_timers.erase(timeout.timerID);
				else
				{
					CTimeValue nextTimeout = timeout.timeout + timer.interval;
					if (nextTimeout.GetValue() <= now.GetValue())
						nextTimeout.SetValue(now.GetValue() + 1);

					timeout.timeout = nextTimeout;
					m_timeouts.push_back(timeout);
					m_resort = true;
				}
			}
		}
	}
}