time keeping / deltatime now handled by timermanager instead of main
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
#include "timer.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <clocale>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
@@ -17,7 +13,6 @@
|
||||
/*
|
||||
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
|
||||
@@ -29,63 +24,51 @@ void myupdatedisplay(const timer &timer_input) {
|
||||
"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) {
|
||||
int ticnt = timer_input.triggercounter;
|
||||
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, "");
|
||||
initscr();
|
||||
nodelay(stdscr, FALSE);
|
||||
refresh();
|
||||
start_color();
|
||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
||||
init_pair(1, COLOR_RED, COLOR_GREEN);
|
||||
attron(COLOR_PAIR(1));
|
||||
printw("Hello World! 🌍 テスト 测试");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
myinitialize();
|
||||
const wchar_t mywch = L'█';
|
||||
cchar_t myc_char;
|
||||
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
||||
// printf("verification\n");
|
||||
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
||||
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||
|
||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
||||
auto clocktimer = std::make_unique<timer>(
|
||||
"secondsprinter", 1, 10,
|
||||
std::move(myaction));
|
||||
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
||||
timermanager mytimermanager;
|
||||
mytimermanager.addtimer(std::move(clocktimer));
|
||||
//swapnumbers(mynumbers);
|
||||
displaynumbers(2,mynumbers);
|
||||
mvprintw(5, 5, "timer added");
|
||||
displaynumbers(2, mynumbers);
|
||||
|
||||
// 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);
|
||||
displaynumbers(3,mynumbers);
|
||||
starttime = currenttime;
|
||||
erase();
|
||||
|
||||
mytimermanager.updatetimers();
|
||||
|
||||
displaynumbers(3, mynumbers);
|
||||
|
||||
refresh();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||
#include "timer.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <string>
|
||||
@@ -39,9 +39,13 @@ void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
|
||||
timers.push_back(std::move(input_timer));
|
||||
}
|
||||
|
||||
void timermanager::updatetimers(double dtime) {
|
||||
void timermanager::updatetimers() {
|
||||
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);
|
||||
prevtime = currenttime;
|
||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||
}
|
||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||
@@ -51,6 +55,8 @@ void timermanager::updatetimers(double dtime) {
|
||||
refresh();
|
||||
}
|
||||
|
||||
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
||||
|
||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||
|
||||
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;}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -10,6 +11,12 @@
|
||||
|
||||
class timer;
|
||||
|
||||
struct balkjes {
|
||||
int startx;
|
||||
int starty;
|
||||
int yhight;
|
||||
};
|
||||
|
||||
class timeraction {
|
||||
public:
|
||||
virtual ~timeraction() = default;
|
||||
@@ -47,11 +54,12 @@ public:
|
||||
// manage collection of timers
|
||||
class timermanager {
|
||||
std::vector<std::unique_ptr<timer>> timers;
|
||||
std::chrono::steady_clock::time_point prevtime; // initial starttime
|
||||
|
||||
public:
|
||||
timermanager();
|
||||
void addtimer(std::unique_ptr<timer> input_timer);
|
||||
|
||||
void updatetimers(double dtime);
|
||||
void updatetimers();
|
||||
};
|
||||
|
||||
class swapaction : public timeraction {
|
||||
@@ -63,6 +71,15 @@ public:
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user