Просмотр исходного кода

refactored into class, some cleanup

my name 4 месяцев назад
Родитель
Сommit
76c7281b84
1 измененных файлов с 17 добавлено и 60 удалено
  1. 17 60
      cpp/ui/ncurses/hw/hwcurses.cpp

+ 17 - 60
cpp/ui/ncurses/hw/hwcurses.cpp

@@ -27,48 +27,6 @@
   centralize into event sender and subscriber
 */
 
-struct printhelloswithupdate {
-  int numofhellos = 0;
-  int maxhellos = 10;
-  int xloc = 8;
-  int yloc = 8;
-  void updatehello() {
-    if (numofhellos < maxhellos) {
-	  mvprintw(xloc, yloc, "hello: %d", numofhellos);
-      numofhellos++, xloc++, yloc++;	  
-	  }
-  }
-};
-
-struct updater {
-
-};
-
-struct secondsdisplay {
-  double accumulator = 0;
-  size_t interval = 2;//only display every 2 seconds
-  int currenttime = 10;
-  int minutex = 5;
-  int minutey = 5;
-  void updatedisplay() {
-    printf("time: %d\n", currenttime);
-    // mvprintw(minutex, minutey, "time: %d", currenttime);
-    // refresh();
-  }
-  void updatetime(double DeltaTime) {
-    accumulator += DeltaTime;
-    if (accumulator >= interval) {
-      currenttime -= 1;
-      accumulator = 0;
-      updatedisplay();
-    }
-    if (currenttime < 1) {
-      currenttime = 10;
-    }
-  }
-};
-
-
 struct mysecondsdisplay {
   int currenttime = 10;
   void myupdatedisplay() {
@@ -80,21 +38,27 @@ struct mysecondsdisplay {
   }
 };
 
-struct timer {
+//calls callbackfunc on specified interval based on accumulated deltatime
+class timer {
+
+public:
   std::function<void()> callbackfunc;
-  double accumulator = 0;
-  size_t interval = 2; // only display every 2 seconds
 
+  timer(size_t interval_input): interval(interval_input){}
+  
   void updatetime(double DeltaTime) {
     accumulator += DeltaTime;
-	printf("accumulated: %f\n", accumulator);    
+    printf("accumulated: %f\n", accumulator);
     if (accumulator >= interval) {
-	  printf("interval\n");
+      printf("interval\n");
       accumulator = 0;
       callbackfunc();
     }
-
   }
+
+private:
+  double accumulator = 0;
+  size_t interval = 1; // only display every 1 seconds
 };
 
 int main() {
@@ -104,27 +68,20 @@ int main() {
   // printw("Hello curses");
   // refresh();
   printf("verification\n");
-  std::cout << "couttest\n";
-  //secondsdisplay mymindisp = {};
-  printhelloswithupdate myhelloprinter = {};
-  auto starttime =
-      std::chrono::system_clock::now();
-  timer clocktimer = {};
+  auto starttime = std::chrono::system_clock::now();
+  timer clocktimer(1);
   mysecondsdisplay secdisp;
-    clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };  
+  clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
   while (true) {
-    auto currenttime =
-        std::chrono::system_clock::now();
+    auto currenttime = std::chrono::system_clock::now();
     std::chrono::duration<double> myDeltaTime = currenttime - starttime;
     double DoubleDeltaTime = myDeltaTime.count();
     clocktimer.updatetime(DoubleDeltaTime);
-    //mymindisp.updatetime(DoubleDeltaTime);
-    myhelloprinter.updatehello();
     printf("deltatime:%f\n", DoubleDeltaTime);
     starttime = currenttime;
     std::this_thread::sleep_for(std::chrono::milliseconds(250));
   }
-  
+
   // getch();
   // endwin();