Files
Tower-Defense/src/misc/Time.cpp

61 lines
1.3 KiB
C++

#include "misc/Time.h"
#include <chrono>
namespace td {
namespace utils {
void AutoTimer::update() {
m_InternalTime += getTime() - m_LastTime;
if (m_InternalTime >= m_Interval) {
if (m_Function != nullptr)
m_Function();
m_InternalTime %= m_Interval;
}
m_LastTime = getTime();
}
void AutoTimer::update(std::uint64_t delta) {
m_InternalTime += delta;
if (m_InternalTime >= m_Interval) {
if (m_Function != nullptr)
m_Function();
m_InternalTime %= m_Interval;
}
m_LastTime = getTime();
}
void AutoTimer::reset() {
m_InternalTime = 0;
m_LastTime = getTime();
}
bool Timer::update(std::uint64_t delta) {
if (m_Waiting) {
m_InternalTime = 0;
m_Waiting = false;
return true;
}
m_InternalTime += delta;
if (m_InternalTime >= m_Interval) {
m_InternalTime %= m_Interval;
return true;
}
return false;
}
void Timer::wait() {
m_Waiting = true;
}
void Timer::reset() {
m_InternalTime = 0; // let the timer active once at the beginning
m_Waiting = true;
}
std::uint64_t getTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count();
}
} // namespace utils
} // namespace td