| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include <chrono>
- #include <cstdio>
- #include <ctime>
- #include <functional>
- #include <iostream>
- #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 mysecondsdisplay {
- int currenttime = 10;
- void myupdatedisplay() {
- currenttime -= 1;
- printf("time: %d\n", currenttime);
- if (currenttime < 1) {
- currenttime = 10;
- }
- }
- };
- //calls callbackfunc on specified interval based on accumulated deltatime
- class timer {
- public:
- std::function<void()> callbackfunc;
- timer(size_t interval_input): interval(interval_input){}
-
- void updatetime(double DeltaTime) {
- accumulator += DeltaTime;
- printf("accumulated: %f\n", accumulator);
- if (accumulator >= interval) {
- printf("interval\n");
- accumulator = 0;
- callbackfunc();
- }
- }
- private:
- double accumulator = 0;
- size_t interval = 1; // only display every 1 seconds
- };
- int main() {
- // initscr();
- // nodelay(stdscr,FALSE);
- // move(11, 26);
- // printw("Hello curses");
- // refresh();
- printf("verification\n");
- auto starttime = std::chrono::system_clock::now();
- timer clocktimer(1);
- 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);
- printf("deltatime:%f\n", DoubleDeltaTime);
- starttime = currenttime;
- std::this_thread::sleep_for(std::chrono::milliseconds(250));
- }
- // getch();
- // endwin();
- return 0;
- }
|