Compare commits
4 Commits
02a12622c2
...
523dac1f4d
| Author | SHA1 | Date | |
|---|---|---|---|
| 523dac1f4d | |||
| f3b7de0489 | |||
| 3834d764c1 | |||
| 0cc5d69c0a |
@@ -1,5 +1,7 @@
|
||||
#include "timer.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <clocale>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
@@ -9,6 +11,7 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
@@ -20,84 +23,68 @@
|
||||
centralize into event sender and subscriber
|
||||
*/
|
||||
|
||||
/*
|
||||
find a way to eject timer
|
||||
find a way to group timers into an object
|
||||
*/
|
||||
void myupdatedisplay(const timer &timer_input) {
|
||||
int x_offset = COLS / 4;
|
||||
mvprintw(timer_input.triggercounter, timer_input.triggercounter + x_offset,
|
||||
"test: █");
|
||||
}
|
||||
|
||||
struct mysecondsdisplay {
|
||||
int intervalcount = 10;
|
||||
void myupdatedisplay() {
|
||||
intervalcount -= 1;
|
||||
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
||||
if (intervalcount < 1) {
|
||||
intervalcount = 10;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||
class timer {
|
||||
public:
|
||||
std::string name;
|
||||
std::function<void()> callbackfunc;
|
||||
|
||||
timer(std::string name, size_t interval_input, std::function<void()> callback)
|
||||
: name(name), callbackfunc(callback), time_interval(interval_input) {}
|
||||
|
||||
// callbackfunc once enough time has accumulated
|
||||
void updatetime(double DeltaTime) {
|
||||
accumulator += DeltaTime;
|
||||
if (accumulator >= time_interval) {
|
||||
accumulator -= time_interval;
|
||||
callbackfunc();
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
double accumulator = 0;
|
||||
double time_interval = 1; // only display every 1 seconds
|
||||
};
|
||||
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
||||
int ticnt = timer_input.triggercounter;
|
||||
mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
|
||||
}
|
||||
|
||||
// manage collection of timers
|
||||
class timermanager {
|
||||
std::vector<std::unique_ptr<timer>> timers;
|
||||
|
||||
public:
|
||||
void addtimer(std::unique_ptr<timer> input_timer) {
|
||||
timers.push_back(std::move(input_timer)); }
|
||||
|
||||
void updatetimers(double dtime) {
|
||||
for (auto &mytimer : timers) {
|
||||
mytimer->updatetime(dtime);
|
||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||
};
|
||||
refresh();
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
void myinitialize(){
|
||||
setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
nodelay(stdscr, FALSE);
|
||||
refresh();
|
||||
// printf("verification\n");
|
||||
start_color();
|
||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
||||
attron(COLOR_PAIR(1));
|
||||
printw("Hello World! 🌍 テスト 测试");
|
||||
}
|
||||
|
||||
auto starttime = std::chrono::system_clock::now(); // initial starttime
|
||||
mysecondsdisplay secdisp;
|
||||
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, [&]() { secdisp.myupdatedisplay(); });
|
||||
timermanager mytimermanager;
|
||||
mytimermanager.addtimer(std::move(clocktimer));
|
||||
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
|
||||
|
||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
||||
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");
|
||||
// calculate delta between current and previous time
|
||||
while (true) {
|
||||
auto currenttime = std::chrono::system_clock::now();
|
||||
auto currenttime = std::chrono::steady_clock::now();
|
||||
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
||||
double dtime = myDeltaTime.count();
|
||||
// mvprintw(11, 11, "dtime: %f", dtime);
|
||||
// clocktimer.updatetime(dtime);
|
||||
mytimermanager.updatetimers(dtime);
|
||||
|
||||
displaynumbers(3,mynumbers);
|
||||
starttime = currenttime;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||
#include "timer.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// infinite timer
|
||||
timer::timer(std::string name_input, size_t interval_input,
|
||||
std::unique_ptr<timeraction> action_input)
|
||||
: time_interval(interval_input), name(name_input), is_infinite(true),
|
||||
action(std::move(action_input)) {}
|
||||
|
||||
// limited timer
|
||||
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
||||
std::unique_ptr<timeraction> action_input)
|
||||
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
||||
action(std::move(action_input)) {}
|
||||
|
||||
// callbackfunc once enough time has accumulated
|
||||
void timer::updatetime(double DeltaTime) {
|
||||
accumulator += DeltaTime;
|
||||
if (!is_expired) {
|
||||
if (accumulator >= time_interval) {
|
||||
action->update(*this);
|
||||
accumulator -= time_interval;
|
||||
triggercounter++;
|
||||
if (!is_infinite && triggercounter >= triggeramount) {
|
||||
is_expired = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
|
||||
timers.push_back(std::move(input_timer));
|
||||
}
|
||||
|
||||
void timermanager::updatetimers(double dtime) {
|
||||
for (auto &mytimer : timers) {
|
||||
mytimer->updatetime(dtime);
|
||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||
}
|
||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||
[](auto &timer) { return timer->is_expired; }),
|
||||
timers.end());
|
||||
// implement delete timers
|
||||
refresh();
|
||||
}
|
||||
|
||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||
|
||||
void swapaction::update(const timer &input_timer){
|
||||
if (!iscomplete) {
|
||||
int temp = input_vector[it_i];
|
||||
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
||||
input_vector[input_vector.size() - 1 - it_i] = temp;
|
||||
it_i++;
|
||||
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#ifndef TIMER_HPP
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class timer;
|
||||
|
||||
class timeraction {
|
||||
public:
|
||||
virtual ~timeraction() = default;
|
||||
virtual void update(const timer &t) = 0;
|
||||
};
|
||||
|
||||
class timer {
|
||||
private:
|
||||
double accumulator = 0;
|
||||
int triggeramount = 0;
|
||||
double time_interval = 1; // only display every 1 seconds
|
||||
|
||||
|
||||
public:
|
||||
std::string name;
|
||||
int triggercounter = 0;
|
||||
bool is_infinite = false;
|
||||
std::unique_ptr<timeraction> action;
|
||||
bool is_expired = false;
|
||||
|
||||
|
||||
//infinite timer
|
||||
timer(std::string name_input, size_t interval_input,
|
||||
std::unique_ptr<timeraction> action_input
|
||||
);
|
||||
|
||||
//limited timer
|
||||
timer(std::string name, size_t interval_input, int triggeramount,
|
||||
std::unique_ptr<timeraction> action
|
||||
);
|
||||
// callbackfunc once enough time has accumulated
|
||||
void updatetime(double DeltaTime);
|
||||
};
|
||||
|
||||
// manage collection of timers
|
||||
class timermanager {
|
||||
std::vector<std::unique_ptr<timer>> timers;
|
||||
|
||||
public:
|
||||
void addtimer(std::unique_ptr<timer> input_timer);
|
||||
|
||||
void updatetimers(double dtime);
|
||||
};
|
||||
|
||||
class swapaction : public timeraction {
|
||||
public:
|
||||
size_t it_i = 0;
|
||||
bool iscomplete = false;
|
||||
std::vector<int> &input_vector;
|
||||
swapaction(std::vector<int> &input_vector);
|
||||
void update(const timer &t);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
externalize state
|
||||
*/
|
||||
Reference in New Issue
Block a user