62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
|
|
namespace td {
|
|
namespace utils {
|
|
|
|
std::uint64_t getTime();
|
|
|
|
|
|
typedef std::function<void()> TimerExecFunction;
|
|
|
|
// utililty class to call 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 call function at regular period of time
|
|
class Timer {
|
|
private:
|
|
std::uint64_t m_Interval;
|
|
std::uint64_t m_InternalTime = 0;
|
|
|
|
bool m_Waiting = true;
|
|
public:
|
|
Timer() : m_Interval(0) {}
|
|
Timer(std::uint64_t interval) : m_Interval(interval) {}
|
|
|
|
bool update(std::uint64_t delta);
|
|
|
|
void wait(); // let update return true on the next tick (if it was not used)
|
|
|
|
void reset();
|
|
|
|
void setInterval(std::uint64_t newInterval) { m_Interval = newInterval; }
|
|
std::uint64_t getInterval() const { return m_Interval; }
|
|
};
|
|
|
|
} // namespace utils
|
|
} // namespace td
|