|
@@ -1,16 +1,15 @@
|
|
|
|
|
+#include <algorithm>
|
|
|
#include <chrono>
|
|
#include <chrono>
|
|
|
#include <cstdio>
|
|
#include <cstdio>
|
|
|
#include <ctime>
|
|
#include <ctime>
|
|
|
#include <functional>
|
|
#include <functional>
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
|
|
+#include <memory>
|
|
|
#include <ncurses.h>
|
|
#include <ncurses.h>
|
|
|
-#include <unistd.h>
|
|
|
|
|
|
|
+#include <string>
|
|
|
#include <thread>
|
|
#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
|
|
|
|
|
-*/
|
|
|
|
|
|
|
+#include <unistd.h>
|
|
|
|
|
+#include <vector>
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
todo
|
|
todo
|
|
@@ -21,26 +20,32 @@
|
|
|
centralize into event sender and subscriber
|
|
centralize into event sender and subscriber
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ find a way to eject timer
|
|
|
|
|
+ find a way to group timers into an object
|
|
|
|
|
+*/
|
|
|
|
|
+
|
|
|
struct mysecondsdisplay {
|
|
struct mysecondsdisplay {
|
|
|
int intervalcount = 10;
|
|
int intervalcount = 10;
|
|
|
void myupdatedisplay() {
|
|
void myupdatedisplay() {
|
|
|
intervalcount -= 1;
|
|
intervalcount -= 1;
|
|
|
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
|
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
|
|
- refresh();
|
|
|
|
|
if (intervalcount < 1) {
|
|
if (intervalcount < 1) {
|
|
|
intervalcount = 10;
|
|
intervalcount = 10;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-//calls callbackfunc on specified interval based on accumulated deltatime
|
|
|
|
|
|
|
+// calls callbackfunc on specified interval based on accumulated deltatime
|
|
|
class timer {
|
|
class timer {
|
|
|
public:
|
|
public:
|
|
|
|
|
+ std::string name;
|
|
|
std::function<void()> callbackfunc;
|
|
std::function<void()> callbackfunc;
|
|
|
|
|
|
|
|
- timer(size_t interval_input, std::function<void()> callback): callbackfunc(callback), time_interval(interval_input){}
|
|
|
|
|
|
|
+ timer(std::string name, size_t interval_input, std::function<void()> callback)
|
|
|
|
|
+ : name(name), callbackfunc(callback), time_interval(interval_input) {}
|
|
|
|
|
|
|
|
- //callbackfunc once enough time has accumulated
|
|
|
|
|
|
|
+ // callbackfunc once enough time has accumulated
|
|
|
void updatetime(double DeltaTime) {
|
|
void updatetime(double DeltaTime) {
|
|
|
accumulator += DeltaTime;
|
|
accumulator += DeltaTime;
|
|
|
if (accumulator >= time_interval) {
|
|
if (accumulator >= time_interval) {
|
|
@@ -48,31 +53,53 @@ public:
|
|
|
callbackfunc();
|
|
callbackfunc();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
private:
|
|
private:
|
|
|
double accumulator = 0;
|
|
double accumulator = 0;
|
|
|
- size_t time_interval = 1; // only display every 1 seconds
|
|
|
|
|
|
|
+ double time_interval = 1; // only display every 1 seconds
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+// manage collection of timers
|
|
|
|
|
+class timermanager {
|
|
|
|
|
+ std::vector<std::unique_ptr<timer>> timers;
|
|
|
|
|
+
|
|
|
|
|
+public:
|
|
|
|
|
+ void addtimer(std::unique_ptr<timer> input_timer) {
|
|
|
|
|
+ timers.push_back(std::move(input_timer)); }
|
|
|
|
|
+
|
|
|
|
|
+ void updatetimers(double dtime) {
|
|
|
|
|
+ for (auto &mytimer : timers) {
|
|
|
|
|
+ mytimer->updatetime(dtime);
|
|
|
|
|
+ mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
|
|
|
|
+ };
|
|
|
|
|
+ refresh();
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
int main() {
|
|
|
initscr();
|
|
initscr();
|
|
|
- nodelay(stdscr,FALSE);
|
|
|
|
|
|
|
+ nodelay(stdscr, FALSE);
|
|
|
refresh();
|
|
refresh();
|
|
|
// printf("verification\n");
|
|
// printf("verification\n");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
auto starttime = std::chrono::system_clock::now(); // initial starttime
|
|
auto starttime = std::chrono::system_clock::now(); // initial starttime
|
|
|
mysecondsdisplay secdisp;
|
|
mysecondsdisplay secdisp;
|
|
|
- timer clocktimer(1, [&]() { secdisp.myupdatedisplay();} );
|
|
|
|
|
-
|
|
|
|
|
- //calculate delta between current and previous time
|
|
|
|
|
|
|
+ auto clocktimer = std::make_unique<timer>("secondsprinter", 1, [&]() { secdisp.myupdatedisplay(); });
|
|
|
|
|
+ timermanager mytimermanager;
|
|
|
|
|
+ mytimermanager.addtimer(std::move(clocktimer));
|
|
|
|
|
+
|
|
|
|
|
+ mvprintw(5, 5, "timer added");
|
|
|
|
|
+ // calculate delta between current and previous time
|
|
|
while (true) {
|
|
while (true) {
|
|
|
auto currenttime = std::chrono::system_clock::now();
|
|
auto currenttime = std::chrono::system_clock::now();
|
|
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
|
double dtime = myDeltaTime.count();
|
|
double dtime = myDeltaTime.count();
|
|
|
|
|
+ // mvprintw(11, 11, "dtime: %f", dtime);
|
|
|
|
|
+ // clocktimer.updatetime(dtime);
|
|
|
|
|
+ mytimermanager.updatetimers(dtime);
|
|
|
|
|
|
|
|
- clocktimer.updatetime(dtime);
|
|
|
|
|
-
|
|
|
|
|
starttime = currenttime;
|
|
starttime = currenttime;
|
|
|
- std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
|
|
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
getch();
|
|
getch();
|