hwcurses.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <chrono>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <functional>
  5. #include <iostream>
  6. #include <ncurses.h>
  7. #include <unistd.h>
  8. #include <thread>
  9. // dynamic, let's for loop hello
  10. /* let's add a timer
  11. function "subscribes" to a function that omits a signal whenever a second has pased
  12. */
  13. // void printhellos(int inputnumber) {
  14. // for (int i = 0; i < inputnumber; i++) {
  15. // mvprintw(i, 10, "hello %d", i);
  16. // refresh();
  17. // }
  18. // }
  19. /*
  20. todo
  21. figure out namespaces, using, and auto
  22. formalize timer struct
  23. read about std function and std bind
  24. try to do with objects / classes instead
  25. centralize into event sender and subscriber
  26. */
  27. struct printhelloswithupdate {
  28. int numofhellos = 0;
  29. int maxhellos = 10;
  30. int xloc = 8;
  31. int yloc = 8;
  32. void updatehello() {
  33. if (numofhellos < maxhellos) {
  34. mvprintw(xloc, yloc, "hello: %d", numofhellos);
  35. numofhellos++, xloc++, yloc++;
  36. }
  37. }
  38. };
  39. struct updater {
  40. };
  41. struct secondsdisplay {
  42. double accumulator = 0;
  43. size_t interval = 2;//only display every 2 seconds
  44. int currenttime = 10;
  45. int minutex = 5;
  46. int minutey = 5;
  47. void updatedisplay() {
  48. printf("time: %d\n", currenttime);
  49. // mvprintw(minutex, minutey, "time: %d", currenttime);
  50. // refresh();
  51. }
  52. void updatetime(double DeltaTime) {
  53. accumulator += DeltaTime;
  54. if (accumulator >= interval) {
  55. currenttime -= 1;
  56. accumulator = 0;
  57. updatedisplay();
  58. }
  59. if (currenttime < 1) {
  60. currenttime = 10;
  61. }
  62. }
  63. };
  64. struct mysecondsdisplay {
  65. int currenttime = 10;
  66. void myupdatedisplay() {
  67. currenttime -= 1;
  68. printf("time: %d\n", currenttime);
  69. if (currenttime < 1) {
  70. currenttime = 10;
  71. }
  72. }
  73. };
  74. struct timer {
  75. std::function<void()> callbackfunc;
  76. double accumulator = 0;
  77. size_t interval = 2; // only display every 2 seconds
  78. void updatetime(double DeltaTime) {
  79. accumulator += DeltaTime;
  80. printf("accumulated: %f\n", accumulator);
  81. if (accumulator >= interval) {
  82. printf("interval\n");
  83. accumulator = 0;
  84. callbackfunc();
  85. }
  86. }
  87. };
  88. int main() {
  89. // initscr();
  90. // nodelay(stdscr,FALSE);
  91. // move(11, 26);
  92. // printw("Hello curses");
  93. // refresh();
  94. printf("verification\n");
  95. std::cout << "couttest\n";
  96. //secondsdisplay mymindisp = {};
  97. printhelloswithupdate myhelloprinter = {};
  98. auto starttime =
  99. std::chrono::system_clock::now();
  100. timer clocktimer = {};
  101. mysecondsdisplay secdisp;
  102. clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
  103. while (true) {
  104. auto currenttime =
  105. std::chrono::system_clock::now();
  106. std::chrono::duration<double> myDeltaTime = currenttime - starttime;
  107. double DoubleDeltaTime = myDeltaTime.count();
  108. clocktimer.updatetime(DoubleDeltaTime);
  109. //mymindisp.updatetime(DoubleDeltaTime);
  110. myhelloprinter.updatehello();
  111. printf("deltatime:%f\n", DoubleDeltaTime);
  112. starttime = currenttime;
  113. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  114. }
  115. // getch();
  116. // endwin();
  117. return 0;
  118. }