added constructor to timer and implemented basic ncurses again
This commit is contained in:
@@ -11,12 +11,6 @@
|
|||||||
/* let's add a timer
|
/* let's add a timer
|
||||||
function "subscribes" to a function that omits a signal whenever a second has pased
|
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
|
todo
|
||||||
@@ -28,62 +22,61 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
struct mysecondsdisplay {
|
struct mysecondsdisplay {
|
||||||
int currenttime = 10;
|
int intervalcount = 10;
|
||||||
void myupdatedisplay() {
|
void myupdatedisplay() {
|
||||||
currenttime -= 1;
|
intervalcount -= 1;
|
||||||
printf("time: %d\n", currenttime);
|
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
||||||
if (currenttime < 1) {
|
refresh();
|
||||||
currenttime = 10;
|
if (intervalcount < 1) {
|
||||||
|
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::function<void()> callbackfunc;
|
std::function<void()> callbackfunc;
|
||||||
|
|
||||||
timer(size_t interval_input): interval(interval_input){}
|
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) {
|
void updatetime(double DeltaTime) {
|
||||||
accumulator += DeltaTime;
|
accumulator += DeltaTime;
|
||||||
printf("accumulated: %f\n", accumulator);
|
if (accumulator >= time_interval) {
|
||||||
if (accumulator >= interval) {
|
accumulator -= time_interval;
|
||||||
printf("interval\n");
|
|
||||||
accumulator = 0;
|
|
||||||
callbackfunc();
|
callbackfunc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double accumulator = 0;
|
double accumulator = 0;
|
||||||
size_t interval = 1; // only display every 1 seconds
|
size_t time_interval = 1; // only display every 1 seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// initscr();
|
initscr();
|
||||||
// nodelay(stdscr,FALSE);
|
nodelay(stdscr,FALSE);
|
||||||
// move(11, 26);
|
refresh();
|
||||||
// printw("Hello curses");
|
// printf("verification\n");
|
||||||
// refresh();
|
|
||||||
printf("verification\n");
|
auto starttime = std::chrono::system_clock::now(); // initial starttime
|
||||||
auto starttime = std::chrono::system_clock::now();
|
|
||||||
timer clocktimer(1);
|
|
||||||
mysecondsdisplay secdisp;
|
mysecondsdisplay secdisp;
|
||||||
clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
|
timer clocktimer(1, [&]() { secdisp.myupdatedisplay();} );
|
||||||
|
|
||||||
|
//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 DoubleDeltaTime = myDeltaTime.count();
|
double dtime = myDeltaTime.count();
|
||||||
clocktimer.updatetime(DoubleDeltaTime);
|
|
||||||
printf("deltatime:%f\n", DoubleDeltaTime);
|
clocktimer.updatetime(dtime);
|
||||||
|
|
||||||
starttime = currenttime;
|
starttime = currenttime;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
}
|
}
|
||||||
|
|
||||||
// getch();
|
getch();
|
||||||
// endwin();
|
endwin();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user