Compare commits

...

4 Commits

Author SHA1 Message Date
lyrgb 523dac1f4d 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
2026-07-10 21:54:58 +02:00
lyrgb f3b7de0489 put classes in own file implemented basic wchar printing using timer 2026-07-10 19:49:09 +02:00
lyrgb 3834d764c1 utf drawing test 2026-07-10 10:06:04 +02:00
lyrgb 0cc5d69c0a expanded timer to infinite and limited timers.
simplified display.
2026-07-09 18:37:35 +02:00
3 changed files with 184 additions and 61 deletions
+46 -59
View File
@@ -1,5 +1,7 @@
#include "timer.hpp"
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <clocale>
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
#include <functional> #include <functional>
@@ -9,6 +11,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
#include <utility>
#include <vector> #include <vector>
/* /*
@@ -20,84 +23,68 @@
centralize into event sender and subscriber centralize into event sender and subscriber
*/ */
/* void myupdatedisplay(const timer &timer_input) {
find a way to eject timer int x_offset = COLS / 4;
find a way to group timers into an object mvprintw(timer_input.triggercounter, timer_input.triggercounter + x_offset,
*/ "test: █");
}
struct mysecondsdisplay { std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int intervalcount = 10;
void myupdatedisplay() { void swapnumbers(std::vector<int> &input_numbers) {
intervalcount -= 1; for (size_t i = 0; i < input_numbers.size() / 2; ++i) {
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount); int temp = input_numbers[i];
if (intervalcount < 1) { input_numbers[i] = input_numbers[input_numbers.size() -1 - i];
intervalcount = 10; input_numbers[input_numbers.size() -1 - i] = temp;
} }
} }
};
// calls callbackfunc on specified interval based on accumulated deltatime void displaynumbers(int myy, std::vector<int> &input_numbers){
class timer { for (size_t i = 0; i < input_numbers.size(); i++) {
public: mvprintw(myy, 10+i, "%d,", input_numbers[i]);
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();
} }
} }
private: void createbalk(const timer &timer_input, cchar_t &input_char) {
double accumulator = 0; int ticnt = timer_input.triggercounter;
double time_interval = 1; // only display every 1 seconds mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
}; }
// manage collection of timers void myinitialize(){
class timermanager { setlocale(LC_ALL, "");
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() {
initscr(); initscr();
nodelay(stdscr, FALSE); nodelay(stdscr, FALSE);
refresh(); 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 int main() {
mysecondsdisplay secdisp; myinitialize();
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, [&]() { secdisp.myupdatedisplay(); }); 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; 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) {
auto currenttime = std::chrono::system_clock::now(); auto currenttime = std::chrono::steady_clock::now();
std::chrono::duration<double> myDeltaTime = currenttime - starttime; std::chrono::duration<double> myDeltaTime = currenttime - starttime;
double dtime = myDeltaTime.count(); double dtime = myDeltaTime.count();
// mvprintw(11, 11, "dtime: %f", dtime);
// clocktimer.updatetime(dtime);
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));
} }
+64
View File
@@ -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;}
}
}
+72
View File
@@ -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
*/