timer ic
This commit is contained in:
@@ -1,12 +1,132 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
#include <ncurses.h>
|
#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
|
||||||
|
*/
|
||||||
|
// void printhellos(int inputnumber) {
|
||||||
|
// for (int i = 0; i < inputnumber; i++) {
|
||||||
|
// mvprintw(i, 10, "hello %d", i);
|
||||||
|
// refresh();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 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 {
|
||||||
|
int currenttime = 10;
|
||||||
|
void myupdatedisplay() {
|
||||||
|
currenttime -= 1;
|
||||||
|
printf("time: %d\n", currenttime);
|
||||||
|
if (currenttime < 1) {
|
||||||
|
currenttime = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct timer {
|
||||||
|
std::function<void()> callbackfunc;
|
||||||
|
double accumulator = 0;
|
||||||
|
size_t interval = 2; // only display every 2 seconds
|
||||||
|
|
||||||
|
void updatetime(double DeltaTime) {
|
||||||
|
accumulator += DeltaTime;
|
||||||
|
printf("accumulated: %f\n", accumulator);
|
||||||
|
if (accumulator >= interval) {
|
||||||
|
printf("interval\n");
|
||||||
|
accumulator = 0;
|
||||||
|
callbackfunc();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
initscr();
|
// initscr();
|
||||||
move(11, 26);
|
// nodelay(stdscr,FALSE);
|
||||||
printw("Hello curses");
|
// move(11, 26);
|
||||||
refresh();
|
// printw("Hello curses");
|
||||||
getch();
|
// refresh();
|
||||||
endwin();
|
printf("verification\n");
|
||||||
|
std::cout << "couttest\n";
|
||||||
|
//secondsdisplay mymindisp = {};
|
||||||
|
printhelloswithupdate myhelloprinter = {};
|
||||||
|
auto starttime =
|
||||||
|
std::chrono::system_clock::now();
|
||||||
|
timer clocktimer = {};
|
||||||
|
mysecondsdisplay secdisp;
|
||||||
|
clocktimer.callbackfunc = [&]() { secdisp.myupdatedisplay(); };
|
||||||
|
while (true) {
|
||||||
|
auto currenttime =
|
||||||
|
std::chrono::system_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
||||||
|
double DoubleDeltaTime = myDeltaTime.count();
|
||||||
|
clocktimer.updatetime(DoubleDeltaTime);
|
||||||
|
//mymindisp.updatetime(DoubleDeltaTime);
|
||||||
|
myhelloprinter.updatehello();
|
||||||
|
printf("deltatime:%f\n", DoubleDeltaTime);
|
||||||
|
starttime = currenttime;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
|
}
|
||||||
|
|
||||||
|
// getch();
|
||||||
|
// endwin();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user