refactored into class, some cleanup

This commit is contained in:
2026-07-07 02:12:41 +02:00
parent 69a943371c
commit 76c7281b84
+16 -59
View File
@@ -27,48 +27,6 @@
centralize into event sender and subscriber 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 { struct mysecondsdisplay {
int currenttime = 10; int currenttime = 10;
void myupdatedisplay() { 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; 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) { void updatetime(double DeltaTime) {
accumulator += DeltaTime; accumulator += DeltaTime;
printf("accumulated: %f\n", accumulator); printf("accumulated: %f\n", accumulator);
if (accumulator >= interval) { if (accumulator >= interval) {
printf("interval\n"); printf("interval\n");
accumulator = 0; accumulator = 0;
callbackfunc(); callbackfunc();
} }
} }
private:
double accumulator = 0;
size_t interval = 1; // only display every 1 seconds
}; };
int main() { int main() {
@@ -104,22 +68,15 @@ int main() {
// printw("Hello curses"); // printw("Hello curses");
// refresh(); // refresh();
printf("verification\n"); printf("verification\n");
std::cout << "couttest\n"; auto starttime = std::chrono::system_clock::now();
//secondsdisplay mymindisp = {}; timer clocktimer(1);
printhelloswithupdate myhelloprinter = {};
auto starttime =
std::chrono::system_clock::now();
timer clocktimer = {};
mysecondsdisplay secdisp; mysecondsdisplay secdisp;
clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); }; clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
while (true) { while (true) {
auto currenttime = auto currenttime = std::chrono::system_clock::now();
std::chrono::system_clock::now();
std::chrono::duration<double> myDeltaTime = currenttime - starttime; std::chrono::duration<double> myDeltaTime = currenttime - starttime;
double DoubleDeltaTime = myDeltaTime.count(); double DoubleDeltaTime = myDeltaTime.count();
clocktimer.updatetime(DoubleDeltaTime); clocktimer.updatetime(DoubleDeltaTime);
//mymindisp.updatetime(DoubleDeltaTime);
myhelloprinter.updatehello();
printf("deltatime:%f\n", DoubleDeltaTime); printf("deltatime:%f\n", DoubleDeltaTime);
starttime = currenttime; starttime = currenttime;
std::this_thread::sleep_for(std::chrono::milliseconds(250)); std::this_thread::sleep_for(std::chrono::milliseconds(250));