154 lines
3.1 KiB
C++
154 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
|
|
namespace blitz {
|
|
namespace utils {
|
|
|
|
std::uint64_t GetTime();
|
|
|
|
typedef std::function<void()> TimerExecFunction;
|
|
|
|
// utililty class to call a function at regular period of time
|
|
class AutoTimer {
|
|
private:
|
|
std::uint64_t m_Interval;
|
|
TimerExecFunction m_Function;
|
|
|
|
std::uint64_t m_LastTime = GetTime();
|
|
std::uint64_t m_InternalTime = 0;
|
|
|
|
public:
|
|
AutoTimer() : m_Interval(0), m_Function(nullptr) {}
|
|
AutoTimer(std::uint64_t interval) : m_Interval(interval), m_Function(nullptr) {}
|
|
AutoTimer(std::uint64_t interval, TimerExecFunction callback) : m_Interval(interval), m_Function(callback) {}
|
|
|
|
void Update();
|
|
void Update(std::uint64_t delta);
|
|
|
|
void Reset();
|
|
|
|
void SetInterval(std::uint64_t newInterval) {
|
|
m_Interval = newInterval;
|
|
}
|
|
|
|
std::uint64_t GetInterval() const {
|
|
return m_Interval;
|
|
}
|
|
|
|
void SetCallbackFunction(TimerExecFunction newCallback) {
|
|
m_Function = newCallback;
|
|
}
|
|
|
|
TimerExecFunction GetCallbackFunction() const {
|
|
return m_Function;
|
|
}
|
|
};
|
|
|
|
// utililty class to trigger update at regular period of time
|
|
class Timer {
|
|
private:
|
|
std::uint64_t m_Interval; // in millis
|
|
std::uint64_t m_InternalTime = 0;
|
|
|
|
public:
|
|
Timer() : m_Interval(0) {}
|
|
Timer(std::uint64_t interval) : m_Interval(interval) {}
|
|
|
|
bool Update(std::uint64_t delta);
|
|
|
|
void Reset();
|
|
// void ResetSoft(); // don't trigger the timer
|
|
|
|
std::uint64_t GetTimeRemaining() const {
|
|
return m_Interval - m_InternalTime;
|
|
}
|
|
|
|
std::uint64_t GetTimeElapsed() const {
|
|
return m_InternalTime;
|
|
}
|
|
|
|
void SetInterval(std::uint64_t newInterval) {
|
|
m_Interval = newInterval;
|
|
}
|
|
|
|
std::uint64_t GetInterval() const {
|
|
return m_Interval;
|
|
}
|
|
};
|
|
|
|
// utililty class to trigger update at regular period of time with a cooldown
|
|
template <typename T>
|
|
class CooldownTimer {
|
|
private:
|
|
T m_Cooldown;
|
|
T m_CooldownTime;
|
|
|
|
public:
|
|
CooldownTimer() : m_Cooldown(0), m_CooldownTime(0) {}
|
|
CooldownTimer(T cooldown) : m_Cooldown(0), m_CooldownTime(cooldown) {}
|
|
|
|
bool Update(T delta) {
|
|
if (m_Cooldown > 0) {
|
|
m_Cooldown = std::max<T>(static_cast<T>(0), static_cast<T>(m_Cooldown - delta));
|
|
}
|
|
return m_Cooldown == 0;
|
|
}
|
|
|
|
void ApplyCooldown() {
|
|
m_Cooldown = m_CooldownTime;
|
|
}
|
|
|
|
void Reset() {
|
|
m_Cooldown = 0;
|
|
}
|
|
|
|
void SetCooldown(T newCooldown) {
|
|
m_CooldownTime = newCooldown;
|
|
}
|
|
|
|
T GetCooldown() const {
|
|
return m_CooldownTime;
|
|
}
|
|
};
|
|
|
|
// utililty class to trigger update at regular period of time with a cooldown
|
|
template <typename T>
|
|
class DelayTimer {
|
|
private:
|
|
T m_DelayTime;
|
|
T m_InternalTime;
|
|
|
|
public:
|
|
DelayTimer() : m_DelayTime(0), m_InternalTime(0) {}
|
|
DelayTimer(T delay) : m_DelayTime(delay), m_InternalTime(delay) {}
|
|
|
|
/**
|
|
* \brief Returns true whether the delay has been passed.
|
|
*/
|
|
bool Update(T delta) {
|
|
m_InternalTime = std::max<T>(static_cast<T>(0), static_cast<T>(m_InternalTime - delta));
|
|
return m_InternalTime == 0;
|
|
}
|
|
|
|
/**
|
|
* \brief Resets the timer
|
|
*/
|
|
void Reset() {
|
|
m_InternalTime = m_DelayTime;
|
|
}
|
|
|
|
void SetDelay(T newDelay) {
|
|
m_DelayTime = newDelay;
|
|
}
|
|
|
|
T GetDelay() const {
|
|
return m_DelayTime;
|
|
}
|
|
};
|
|
|
|
} // namespace utils
|
|
} // namespace blitz
|