went from callback lambda to class with update function (command pattern?) so it can keep state. (to bypass for loop by counting iterations)
implemented reversing array and "animating" it
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <clocale>
|
#include <clocale>
|
||||||
@@ -10,7 +11,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "timer.hpp"
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -24,15 +25,32 @@
|
|||||||
|
|
||||||
void myupdatedisplay(const timer &timer_input) {
|
void myupdatedisplay(const timer &timer_input) {
|
||||||
int x_offset = COLS / 4;
|
int x_offset = COLS / 4;
|
||||||
mvprintw(timer_input.triggercounter, timer_input.triggercounter+x_offset, "test: █");
|
mvprintw(timer_input.triggercounter, timer_input.triggercounter + x_offset,
|
||||||
|
"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);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
void myinitialize(){
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
initscr();
|
initscr();
|
||||||
nodelay(stdscr, FALSE);
|
nodelay(stdscr, FALSE);
|
||||||
@@ -41,16 +59,24 @@ int main() {
|
|||||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
||||||
attron(COLOR_PAIR(1));
|
attron(COLOR_PAIR(1));
|
||||||
printw("Hello World! 🌍 テスト 测试");
|
printw("Hello World! 🌍 テスト 测试");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
myinitialize();
|
||||||
const wchar_t mywch = L'█';
|
const wchar_t mywch = L'█';
|
||||||
cchar_t myc_char;
|
cchar_t myc_char;
|
||||||
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
||||||
// printf("verification\n");
|
// printf("verification\n");
|
||||||
|
|
||||||
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
||||||
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, [&myc_char](const timer& t){createbalk(t,myc_char);});
|
|
||||||
timermanager mytimermanager;
|
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));
|
mytimermanager.addtimer(std::move(clocktimer));
|
||||||
|
//swapnumbers(mynumbers);
|
||||||
|
displaynumbers(2,mynumbers);
|
||||||
mvprintw(5, 5, "timer added");
|
mvprintw(5, 5, "timer added");
|
||||||
// calculate delta between current and previous time
|
// calculate delta between current and previous time
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -58,7 +84,7 @@ int main() {
|
|||||||
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
||||||
double dtime = myDeltaTime.count();
|
double dtime = myDeltaTime.count();
|
||||||
mytimermanager.updatetimers(dtime);
|
mytimermanager.updatetimers(dtime);
|
||||||
|
displaynumbers(3,mynumbers);
|
||||||
starttime = currenttime;
|
starttime = currenttime;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,25 +6,26 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// infinite timer
|
// infinite timer
|
||||||
timer::timer(std::string name_input, size_t interval_input,
|
timer::timer(std::string name_input, size_t interval_input,
|
||||||
std::function<void(const timer &)> callback_input)
|
std::unique_ptr<timeraction> action_input)
|
||||||
: time_interval(interval_input), name(name_input), is_infinite(true),
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
||||||
callbackfunc(callback_input) {}
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
// limited timer
|
// limited timer
|
||||||
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
||||||
std::function<void(const timer &)> callback)
|
std::unique_ptr<timeraction> action_input)
|
||||||
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
||||||
callbackfunc(callback) {}
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
// callbackfunc once enough time has accumulated
|
// callbackfunc once enough time has accumulated
|
||||||
void timer::updatetime(double DeltaTime) {
|
void timer::updatetime(double DeltaTime) {
|
||||||
accumulator += DeltaTime;
|
accumulator += DeltaTime;
|
||||||
if (!is_expired) {
|
if (!is_expired) {
|
||||||
if (accumulator >= time_interval) {
|
if (accumulator >= time_interval) {
|
||||||
callbackfunc(*this);
|
action->update(*this);
|
||||||
accumulator -= time_interval;
|
accumulator -= time_interval;
|
||||||
triggercounter++;
|
triggercounter++;
|
||||||
if (!is_infinite && triggercounter >= triggeramount) {
|
if (!is_infinite && triggercounter >= triggeramount) {
|
||||||
@@ -49,3 +50,15 @@ void timermanager::updatetimers(double dtime) {
|
|||||||
// implement delete timers
|
// implement delete timers
|
||||||
refresh();
|
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;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,13 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class timer;
|
||||||
|
|
||||||
|
class timeraction {
|
||||||
|
public:
|
||||||
|
virtual ~timeraction() = default;
|
||||||
|
virtual void update(const timer &t) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class timer {
|
class timer {
|
||||||
private:
|
private:
|
||||||
@@ -21,18 +27,19 @@ public:
|
|||||||
std::string name;
|
std::string name;
|
||||||
int triggercounter = 0;
|
int triggercounter = 0;
|
||||||
bool is_infinite = false;
|
bool is_infinite = false;
|
||||||
std::function<void(const timer&)> callbackfunc;
|
std::unique_ptr<timeraction> action;
|
||||||
bool is_expired = false;
|
bool is_expired = false;
|
||||||
|
|
||||||
|
|
||||||
//infinite timer
|
//infinite timer
|
||||||
timer(std::string name_input, size_t interval_input,
|
timer(std::string name_input, size_t interval_input,
|
||||||
std::function<void(const timer &)> callback_input);
|
std::unique_ptr<timeraction> action_input
|
||||||
|
);
|
||||||
|
|
||||||
//limited timer
|
//limited timer
|
||||||
timer(std::string name, size_t interval_input, int triggeramount,
|
timer(std::string name, size_t interval_input, int triggeramount,
|
||||||
std::function<void(const timer &)> callback);
|
std::unique_ptr<timeraction> action
|
||||||
|
);
|
||||||
// callbackfunc once enough time has accumulated
|
// callbackfunc once enough time has accumulated
|
||||||
void updatetime(double DeltaTime);
|
void updatetime(double DeltaTime);
|
||||||
};
|
};
|
||||||
@@ -47,4 +54,19 @@ public:
|
|||||||
void updatetimers(double dtime);
|
void updatetimers(double dtime);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
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