hwcurses.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 mysecondsdisplay {
  28. int currenttime = 10;
  29. void myupdatedisplay() {
  30. currenttime -= 1;
  31. printf("time: %d\n", currenttime);
  32. if (currenttime < 1) {
  33. currenttime = 10;
  34. }
  35. }
  36. };
  37. //calls callbackfunc on specified interval based on accumulated deltatime
  38. class timer {
  39. public:
  40. std::function<void()> callbackfunc;
  41. timer(size_t interval_input): interval(interval_input){}
  42. void updatetime(double DeltaTime) {
  43. accumulator += DeltaTime;
  44. printf("accumulated: %f\n", accumulator);
  45. if (accumulator >= interval) {
  46. printf("interval\n");
  47. accumulator = 0;
  48. callbackfunc();
  49. }
  50. }
  51. private:
  52. double accumulator = 0;
  53. size_t interval = 1; // only display every 1 seconds
  54. };
  55. int main() {
  56. // initscr();
  57. // nodelay(stdscr,FALSE);
  58. // move(11, 26);
  59. // printw("Hello curses");
  60. // refresh();
  61. printf("verification\n");
  62. auto starttime = std::chrono::system_clock::now();
  63. timer clocktimer(1);
  64. mysecondsdisplay secdisp;
  65. clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
  66. while (true) {
  67. auto currenttime = std::chrono::system_clock::now();
  68. std::chrono::duration<double> myDeltaTime = currenttime - starttime;
  69. double DoubleDeltaTime = myDeltaTime.count();
  70. clocktimer.updatetime(DoubleDeltaTime);
  71. printf("deltatime:%f\n", DoubleDeltaTime);
  72. starttime = currenttime;
  73. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  74. }
  75. // getch();
  76. // endwin();
  77. return 0;
  78. }