timer.hpp 1.7 KB

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