timer.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef TIMER_HPP
  2. #define TIMER_HPP
  3. #include <cstddef>
  4. #include <functional>
  5. #include <memory>
  6. #include <ncurses.h>
  7. #include <string>
  8. #include <vector>
  9. class timer;
  10. class timeraction {
  11. public:
  12. virtual ~timeraction() = default;
  13. virtual void update(const timer &t) = 0;
  14. };
  15. class timer {
  16. private:
  17. double accumulator = 0;
  18. int triggeramount = 0;
  19. double time_interval = 1; // only display every 1 seconds
  20. public:
  21. std::string name;
  22. int triggercounter = 0;
  23. bool is_infinite = false;
  24. std::unique_ptr<timeraction> action;
  25. bool is_expired = false;
  26. //infinite timer
  27. timer(std::string name_input, size_t interval_input,
  28. std::unique_ptr<timeraction> action_input
  29. );
  30. //limited timer
  31. timer(std::string name, size_t interval_input, int triggeramount,
  32. std::unique_ptr<timeraction> action
  33. );
  34. // callbackfunc once enough time has accumulated
  35. void updatetime(double DeltaTime);
  36. };
  37. // manage collection of timers
  38. class timermanager {
  39. std::vector<std::unique_ptr<timer>> timers;
  40. public:
  41. void addtimer(std::unique_ptr<timer> input_timer);
  42. void updatetimers(double dtime);
  43. };
  44. class swapaction : public timeraction {
  45. public:
  46. size_t it_i = 0;
  47. bool iscomplete = false;
  48. std::vector<int> &input_vector;
  49. swapaction(std::vector<int> &input_vector);
  50. void update(const timer &t);
  51. };
  52. #endif
  53. /*
  54. externalize state
  55. */