0cc5d69c0a
simplified display.
123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <ctime>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <ncurses.h>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
/*
|
|
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
|
|
*/
|
|
|
|
|
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
|
class timer {
|
|
private:
|
|
double accumulator = 0;
|
|
int triggeramount = 0;
|
|
double time_interval = 1; // only display every 1 seconds
|
|
|
|
public:
|
|
std::string name;
|
|
int triggercounter = 0;
|
|
bool is_infinite = false;
|
|
std::function<void(const timer&)> callbackfunc;
|
|
bool is_expired = false;
|
|
|
|
//infinite timer
|
|
timer(std::string name_input, size_t interval_input,
|
|
std::function<void(const timer &)> callback_input)
|
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
|
callbackfunc(callback_input) {}
|
|
|
|
//limited timer
|
|
timer(std::string name, size_t interval_input, int triggeramount,
|
|
std::function<void(const timer &)> callback)
|
|
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
|
callbackfunc(callback) {}
|
|
|
|
// callbackfunc once enough time has accumulated
|
|
void updatetime(double DeltaTime) {
|
|
accumulator += DeltaTime;
|
|
if (!is_expired) {
|
|
if (accumulator >= time_interval) {
|
|
callbackfunc(*this);
|
|
accumulator -= time_interval;
|
|
triggercounter++;
|
|
if (!is_infinite && triggercounter >= triggeramount) {
|
|
is_expired = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
};
|
|
|
|
void myupdatedisplay(const timer &timer_input) {
|
|
mvprintw(timer_input.triggercounter, timer_input.triggercounter, "hello %d",
|
|
timer_input.triggercounter);
|
|
}
|
|
|
|
// 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());
|
|
}
|
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
|
[](auto &timer) { return timer->is_expired; }),
|
|
timers.end());
|
|
// implement delete timers
|
|
;
|
|
refresh();
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
initscr();
|
|
nodelay(stdscr, FALSE);
|
|
refresh();
|
|
// printf("verification\n");
|
|
|
|
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
|
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, myupdatedisplay);
|
|
timermanager mytimermanager;
|
|
mytimermanager.addtimer(std::move(clocktimer));
|
|
|
|
mvprintw(5, 5, "timer added");
|
|
// calculate delta between current and previous time
|
|
while (true) {
|
|
auto currenttime = std::chrono::steady_clock::now();
|
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
double dtime = myDeltaTime.count();
|
|
mytimermanager.updatetimers(dtime);
|
|
|
|
starttime = currenttime;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
}
|
|
|
|
getch();
|
|
endwin();
|
|
|
|
return 0;
|
|
}
|