62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
#ifndef TIMER_HPP
|
|
#define TIMER_HPP
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <ncurses.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
class Timer;
|
|
|
|
class TimerAction {
|
|
public:
|
|
virtual ~TimerAction() = default;
|
|
virtual void update(const Timer &t) = 0;
|
|
};
|
|
|
|
class Timer {
|
|
private:
|
|
double accumulator = 0;
|
|
int trigger_amount = 0;
|
|
double time_interval = 1; // only display every 1 seconds
|
|
|
|
public:
|
|
std::string name;
|
|
int trigger_counter = 0;
|
|
bool is_infinite = false;
|
|
std::unique_ptr<TimerAction> action;
|
|
bool is_expired = false;
|
|
|
|
|
|
//infinite timer
|
|
Timer(std::string name_input, double interval_input,
|
|
std::unique_ptr<TimerAction> action_input
|
|
);
|
|
|
|
//limited timer
|
|
Timer(std::string name, double interval_input, int trigger_amount,
|
|
std::unique_ptr<TimerAction> action
|
|
);
|
|
// callbackfunc once enough time has accumulated
|
|
void updatetime(double delta_time);
|
|
};
|
|
|
|
// manage collection of timers
|
|
class TimerManager {
|
|
std::vector<std::unique_ptr<Timer>> timers;
|
|
std::chrono::steady_clock::time_point prev_time; // initial starttime
|
|
|
|
public:
|
|
TimerManager();
|
|
void addtimer(std::unique_ptr<Timer> input_timer);
|
|
void updatetimers();
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
/*
|
|
externalize state
|
|
*/
|