hwcurses.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <functional>
  6. #include <iostream>
  7. #include <memory>
  8. #include <ncurses.h>
  9. #include <string>
  10. #include <thread>
  11. #include <unistd.h>
  12. #include <vector>
  13. /*
  14. todo
  15. figure out namespaces, using, and auto
  16. formalize timer struct
  17. read about std function and std bind
  18. try to do with objects / classes instead
  19. centralize into event sender and subscriber
  20. */
  21. // calls callbackfunc on specified interval based on accumulated deltatime
  22. class timer {
  23. private:
  24. double accumulator = 0;
  25. int triggeramount = 0;
  26. double time_interval = 1; // only display every 1 seconds
  27. public:
  28. std::string name;
  29. int triggercounter = 0;
  30. bool is_infinite = false;
  31. std::function<void(const timer&)> callbackfunc;
  32. bool is_expired = false;
  33. //infinite timer
  34. timer(std::string name_input, size_t interval_input,
  35. std::function<void(const timer &)> callback_input)
  36. : time_interval(interval_input), name(name_input), is_infinite(true),
  37. callbackfunc(callback_input) {}
  38. //limited timer
  39. timer(std::string name, size_t interval_input, int triggeramount,
  40. std::function<void(const timer &)> callback)
  41. : triggeramount(triggeramount), time_interval(interval_input), name(name),
  42. callbackfunc(callback) {}
  43. // callbackfunc once enough time has accumulated
  44. void updatetime(double DeltaTime) {
  45. accumulator += DeltaTime;
  46. if (!is_expired) {
  47. if (accumulator >= time_interval) {
  48. callbackfunc(*this);
  49. accumulator -= time_interval;
  50. triggercounter++;
  51. if (!is_infinite && triggercounter >= triggeramount) {
  52. is_expired = true;
  53. }
  54. }
  55. }
  56. }
  57. };
  58. void myupdatedisplay(const timer &timer_input) {
  59. mvprintw(timer_input.triggercounter, timer_input.triggercounter, "hello %d",
  60. timer_input.triggercounter);
  61. }
  62. // manage collection of timers
  63. class timermanager {
  64. std::vector<std::unique_ptr<timer>> timers;
  65. public:
  66. void addtimer(std::unique_ptr<timer> input_timer) {
  67. timers.push_back(std::move(input_timer)); }
  68. void updatetimers(double dtime) {
  69. for (auto &mytimer : timers) {
  70. mytimer->updatetime(dtime);
  71. mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
  72. }
  73. timers.erase(std::remove_if(timers.begin(), timers.end(),
  74. [](auto &timer) { return timer->is_expired; }),
  75. timers.end());
  76. // implement delete timers
  77. ;
  78. refresh();
  79. }
  80. };
  81. int main() {
  82. initscr();
  83. nodelay(stdscr, FALSE);
  84. refresh();
  85. // printf("verification\n");
  86. auto starttime = std::chrono::steady_clock::now(); // initial starttime
  87. auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, myupdatedisplay);
  88. timermanager mytimermanager;
  89. mytimermanager.addtimer(std::move(clocktimer));
  90. mvprintw(5, 5, "timer added");
  91. // calculate delta between current and previous time
  92. while (true) {
  93. auto currenttime = std::chrono::steady_clock::now();
  94. std::chrono::duration<double> myDeltaTime = currenttime - starttime;
  95. double dtime = myDeltaTime.count();
  96. mytimermanager.updatetimers(dtime);
  97. starttime = currenttime;
  98. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  99. }
  100. getch();
  101. endwin();
  102. return 0;
  103. }