timer.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef TIMER_HPP
  2. #define TIMER_HPP
  3. #include <chrono>
  4. #include <memory>
  5. #include <ncurses.h>
  6. #include <string>
  7. #include <vector>
  8. class Timer;
  9. class TimerAction {
  10. public:
  11. virtual ~TimerAction() = default;
  12. virtual void update(const Timer &t) = 0;
  13. };
  14. class Timer {
  15. private:
  16. double accumulator = 0;
  17. int trigger_amount = 0;
  18. double time_interval = 1; // only display every 1 seconds
  19. public:
  20. std::string name;
  21. int trigger_counter = 0;
  22. bool is_infinite = false;
  23. std::unique_ptr<TimerAction> action;
  24. bool is_expired = false;
  25. //infinite timer
  26. Timer(std::string name_input, double interval_input,
  27. std::unique_ptr<TimerAction> action_input
  28. );
  29. //limited timer
  30. Timer(std::string name, double interval_input, int trigger_amount,
  31. std::unique_ptr<TimerAction> action
  32. );
  33. // callbackfunc once enough time has accumulated
  34. void updatetime(double delta_time);
  35. };
  36. // manage collection of timers
  37. class TimerManager {
  38. std::vector<std::unique_ptr<Timer>> timers;
  39. std::chrono::steady_clock::time_point prev_time; // initial starttime
  40. public:
  41. TimerManager();
  42. void addtimer(std::unique_ptr<Timer> input_timer);
  43. void updatetimers();
  44. };
  45. #endif
  46. /*
  47. externalize state
  48. */