From 76c7281b84ff4efa837b797f6cd1c07c87e53b52 Mon Sep 17 00:00:00 2001 From: my name Date: Tue, 7 Jul 2026 02:12:41 +0200 Subject: [PATCH] refactored into class, some cleanup --- cpp/ui/ncurses/hw/hwcurses.cpp | 79 ++++++++-------------------------- 1 file changed, 18 insertions(+), 61 deletions(-) diff --git a/cpp/ui/ncurses/hw/hwcurses.cpp b/cpp/ui/ncurses/hw/hwcurses.cpp index 6c1195e..0785400 100644 --- a/cpp/ui/ncurses/hw/hwcurses.cpp +++ b/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 { - std::function callbackfunc; - double accumulator = 0; - size_t interval = 2; // only display every 2 seconds +//calls callbackfunc on specified interval based on accumulated deltatime +class timer { +public: + std::function callbackfunc; + + 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 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();