hwcurses.cpp 3.5 KB

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