Compare commits

...

3 Commits

Author SHA1 Message Date
lyrgb e337ee3841 added constructor to timer and implemented basic ncurses again 2026-07-07 02:54:27 +02:00
lyrgb 76c7281b84 refactored into class, some cleanup 2026-07-07 02:12:41 +02:00
lyrgb 69a943371c timer ic 2026-07-06 03:50:11 +02:00
+72 -2
View File
@@ -1,10 +1,80 @@
#include <chrono>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <ncurses.h>
#include <unistd.h>
#include <thread>
// dynamic, let's for loop hello
/* let's add a timer
function "subscribes" to a function that omits a signal whenever a second has pased
*/
/*
todo
figure out namespaces, using, and auto
formalize timer struct
read about std function and std bind
try to do with objects / classes instead
centralize into event sender and subscriber
*/
struct mysecondsdisplay {
int intervalcount = 10;
void myupdatedisplay() {
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<void()> callbackfunc;
timer(size_t interval_input, std::function<void()> callback): callbackfunc(callback), time_interval(interval_input){}
//callbackfunc once enough time has accumulated
void updatetime(double DeltaTime) {
accumulator += DeltaTime;
if (accumulator >= time_interval) {
accumulator -= time_interval;
callbackfunc();
}
}
private:
double accumulator = 0;
size_t time_interval = 1; // only display every 1 seconds
};
int main() {
initscr();
move(11, 26);
printw("Hello curses");
nodelay(stdscr,FALSE);
refresh();
// printf("verification\n");
auto starttime = std::chrono::system_clock::now(); // initial starttime
mysecondsdisplay secdisp;
timer clocktimer(1, [&]() { secdisp.myupdatedisplay();} );
//calculate delta between current and previous time
while (true) {
auto currenttime = std::chrono::system_clock::now();
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
double dtime = myDeltaTime.count();
clocktimer.updatetime(dtime);
starttime = currenttime;
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
getch();
endwin();