48 lines
1.1 KiB
C++
48 lines
1.1 KiB
C++
#ifndef TIMER_HPP
|
|
#define TIMER_HPP
|
|
|
|
#include <cstddef>
|
|
#include <ctime>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <chrono>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
//check if the timer is still valid, check if enough time has passed and if so, actuate action
|
|
class Timer {
|
|
public:
|
|
string name;
|
|
size_t triggeramount;
|
|
|
|
bool isexpired = false;
|
|
bool isinfinite = false;
|
|
size_t triggercount = 0;
|
|
function<bool()> timeraction;
|
|
|
|
Timer(string name, int triggercount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
|
void add_time(std::chrono::milliseconds deltatime);
|
|
|
|
|
|
|
|
private:
|
|
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
|
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
|
};
|
|
|
|
//CRUD timers and give them dt on update
|
|
class TimerManager {
|
|
public:
|
|
vector<unique_ptr<Timer>> timers;
|
|
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
|
void add_timer(unique_ptr<Timer> newtimer);
|
|
void delete_timer();
|
|
void update_timers();
|
|
|
|
private:
|
|
std::chrono::steady_clock::time_point previoustime;
|
|
};
|
|
|
|
#endif
|