time keeping / deltatime now handled by timermanager instead of main
This commit is contained in:
@@ -1,14 +1,10 @@
|
|||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <clocale>
|
#include <clocale>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <functional>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -17,7 +13,6 @@
|
|||||||
/*
|
/*
|
||||||
todo
|
todo
|
||||||
figure out namespaces, using, and auto
|
figure out namespaces, using, and auto
|
||||||
formalize timer struct
|
|
||||||
read about std function and std bind
|
read about std function and std bind
|
||||||
try to do with objects / classes instead
|
try to do with objects / classes instead
|
||||||
centralize into event sender and subscriber
|
centralize into event sender and subscriber
|
||||||
@@ -29,63 +24,51 @@ void myupdatedisplay(const timer &timer_input) {
|
|||||||
"test: █");
|
"test: █");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|
||||||
|
|
||||||
void swapnumbers(std::vector<int> &input_numbers) {
|
|
||||||
for (size_t i = 0; i < input_numbers.size() / 2; ++i) {
|
|
||||||
int temp = input_numbers[i];
|
|
||||||
input_numbers[i] = input_numbers[input_numbers.size() -1 - i];
|
|
||||||
input_numbers[input_numbers.size() -1 - i] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void displaynumbers(int myy, std::vector<int> &input_numbers){
|
|
||||||
for (size_t i = 0; i < input_numbers.size(); i++) {
|
|
||||||
mvprintw(myy, 10+i, "%d,", input_numbers[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
||||||
int ticnt = timer_input.triggercounter;
|
int ticnt = timer_input.triggercounter;
|
||||||
mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
|
mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void myinitialize(){
|
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
|
||||||
|
void displaynumbers(int myy, std::vector<int> &input_numbers) {
|
||||||
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
|
mvprintw(myy, 10 + i, "%d,", input_numbers[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void myinitialize() {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
initscr();
|
initscr();
|
||||||
nodelay(stdscr, FALSE);
|
nodelay(stdscr, FALSE);
|
||||||
refresh();
|
refresh();
|
||||||
start_color();
|
start_color();
|
||||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
init_pair(1, COLOR_RED, COLOR_GREEN);
|
||||||
attron(COLOR_PAIR(1));
|
attron(COLOR_PAIR(1));
|
||||||
printw("Hello World! 🌍 テスト 测试");
|
printw("Hello World! 🌍 テスト 测试");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
myinitialize();
|
myinitialize();
|
||||||
const wchar_t mywch = L'█';
|
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||||
cchar_t myc_char;
|
|
||||||
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
|
||||||
// printf("verification\n");
|
|
||||||
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
|
||||||
|
|
||||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
auto myaction = std::make_unique<swapaction>(mynumbers);
|
||||||
auto clocktimer = std::make_unique<timer>(
|
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
||||||
"secondsprinter", 1, 10,
|
|
||||||
std::move(myaction));
|
|
||||||
timermanager mytimermanager;
|
timermanager mytimermanager;
|
||||||
mytimermanager.addtimer(std::move(clocktimer));
|
mytimermanager.addtimer(std::move(clocktimer));
|
||||||
//swapnumbers(mynumbers);
|
displaynumbers(2, mynumbers);
|
||||||
displaynumbers(2,mynumbers);
|
|
||||||
mvprintw(5, 5, "timer added");
|
|
||||||
// calculate delta between current and previous time
|
// calculate delta between current and previous time
|
||||||
while (true) {
|
while (true) {
|
||||||
auto currenttime = std::chrono::steady_clock::now();
|
erase();
|
||||||
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
||||||
double dtime = myDeltaTime.count();
|
mytimermanager.updatetimers();
|
||||||
mytimermanager.updatetimers(dtime);
|
|
||||||
displaynumbers(3,mynumbers);
|
displaynumbers(3, mynumbers);
|
||||||
starttime = currenttime;
|
|
||||||
|
refresh();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -39,9 +39,13 @@ void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
|
|||||||
timers.push_back(std::move(input_timer));
|
timers.push_back(std::move(input_timer));
|
||||||
}
|
}
|
||||||
|
|
||||||
void timermanager::updatetimers(double dtime) {
|
void timermanager::updatetimers() {
|
||||||
for (auto &mytimer : timers) {
|
for (auto &mytimer : timers) {
|
||||||
|
auto currenttime = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = currenttime - prevtime;
|
||||||
|
double dtime = myDeltaTime.count();
|
||||||
mytimer->updatetime(dtime);
|
mytimer->updatetime(dtime);
|
||||||
|
prevtime = currenttime;
|
||||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
}
|
}
|
||||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||||
@@ -51,6 +55,8 @@ void timermanager::updatetimers(double dtime) {
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||||
|
|
||||||
void swapaction::update(const timer &input_timer){
|
void swapaction::update(const timer &input_timer){
|
||||||
@@ -62,3 +68,16 @@ void swapaction::update(const timer &input_timer){
|
|||||||
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printaction::printaction(std::vector<balkjes> &input_vector): input_vector(input_vector) {}
|
||||||
|
|
||||||
|
void printaction::update(const timer &input_timer) {
|
||||||
|
if(!iscomplete) {
|
||||||
|
// for each it_i add a balkje then eventually print it
|
||||||
|
if (it_i>=10) {
|
||||||
|
iscomplete=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef TIMER_HPP
|
#ifndef TIMER_HPP
|
||||||
#define TIMER_HPP
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -10,6 +11,12 @@
|
|||||||
|
|
||||||
class timer;
|
class timer;
|
||||||
|
|
||||||
|
struct balkjes {
|
||||||
|
int startx;
|
||||||
|
int starty;
|
||||||
|
int yhight;
|
||||||
|
};
|
||||||
|
|
||||||
class timeraction {
|
class timeraction {
|
||||||
public:
|
public:
|
||||||
virtual ~timeraction() = default;
|
virtual ~timeraction() = default;
|
||||||
@@ -47,11 +54,12 @@ public:
|
|||||||
// manage collection of timers
|
// manage collection of timers
|
||||||
class timermanager {
|
class timermanager {
|
||||||
std::vector<std::unique_ptr<timer>> timers;
|
std::vector<std::unique_ptr<timer>> timers;
|
||||||
|
std::chrono::steady_clock::time_point prevtime; // initial starttime
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
timermanager();
|
||||||
void addtimer(std::unique_ptr<timer> input_timer);
|
void addtimer(std::unique_ptr<timer> input_timer);
|
||||||
|
void updatetimers();
|
||||||
void updatetimers(double dtime);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class swapaction : public timeraction {
|
class swapaction : public timeraction {
|
||||||
@@ -63,6 +71,15 @@ public:
|
|||||||
void update(const timer &t);
|
void update(const timer &t);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class printaction : public timeraction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
bool iscomplete = false;
|
||||||
|
std::vector<balkjes> &input_vector;
|
||||||
|
printaction(std::vector<balkjes> &input_vector);
|
||||||
|
void update(const timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user