From e337ee3841b8513876f5a2146c9e8f0a40adb0b2 Mon Sep 17 00:00:00 2001 From: my name Date: Tue, 7 Jul 2026 02:54:27 +0200 Subject: [PATCH] added constructor to timer and implemented basic ncurses again --- cpp/ui/ncurses/hw/hwcurses.cpp | 61 +++++++++++++++------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/cpp/ui/ncurses/hw/hwcurses.cpp b/cpp/ui/ncurses/hw/hwcurses.cpp index 0785400..41cc4f9 100644 --- a/cpp/ui/ncurses/hw/hwcurses.cpp +++ b/cpp/ui/ncurses/hw/hwcurses.cpp @@ -11,12 +11,6 @@ /* let's add a timer function "subscribes" to a function that omits a signal whenever a second has pased */ -// void printhellos(int inputnumber) { -// for (int i = 0; i < inputnumber; i++) { -// mvprintw(i, 10, "hello %d", i); -// refresh(); -// } -// } /* todo @@ -28,62 +22,61 @@ */ struct mysecondsdisplay { - int currenttime = 10; + int intervalcount = 10; void myupdatedisplay() { - currenttime -= 1; - printf("time: %d\n", currenttime); - if (currenttime < 1) { - currenttime = 10; + intervalcount -= 1; + mvprintw(intervalcount, intervalcount, "hello %d", intervalcount); + refresh(); + if (intervalcount < 1) { + intervalcount = 10; } } }; //calls callbackfunc on specified interval based on accumulated deltatime class timer { - public: std::function callbackfunc; - timer(size_t interval_input): interval(interval_input){} - + timer(size_t interval_input, std::function callback): callbackfunc(callback), time_interval(interval_input){} + + //callbackfunc once enough time has accumulated void updatetime(double DeltaTime) { accumulator += DeltaTime; - printf("accumulated: %f\n", accumulator); - if (accumulator >= interval) { - printf("interval\n"); - accumulator = 0; + if (accumulator >= time_interval) { + accumulator -= time_interval; callbackfunc(); } } - private: double accumulator = 0; - size_t interval = 1; // only display every 1 seconds + size_t time_interval = 1; // only display every 1 seconds }; int main() { - // initscr(); - // nodelay(stdscr,FALSE); - // move(11, 26); - // printw("Hello curses"); - // refresh(); - printf("verification\n"); - auto starttime = std::chrono::system_clock::now(); - timer clocktimer(1); + initscr(); + nodelay(stdscr,FALSE); + refresh(); + // printf("verification\n"); + + auto starttime = std::chrono::system_clock::now(); // initial starttime mysecondsdisplay secdisp; - clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); }; + timer clocktimer(1, [&]() { secdisp.myupdatedisplay();} ); + + //calculate delta between current and previous time while (true) { auto currenttime = std::chrono::system_clock::now(); std::chrono::duration myDeltaTime = currenttime - starttime; - double DoubleDeltaTime = myDeltaTime.count(); - clocktimer.updatetime(DoubleDeltaTime); - printf("deltatime:%f\n", DoubleDeltaTime); + double dtime = myDeltaTime.count(); + + clocktimer.updatetime(dtime); + starttime = currenttime; std::this_thread::sleep_for(std::chrono::milliseconds(250)); } - // getch(); - // endwin(); + getch(); + endwin(); return 0; }