put classes in own file implemented basic wchar printing using timer
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "timer.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -21,75 +22,15 @@
|
|||||||
centralize into event sender and subscriber
|
centralize into event sender and subscriber
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
|
||||||
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::function<void(const timer&)> callbackfunc;
|
|
||||||
bool is_expired = false;
|
|
||||||
|
|
||||||
//infinite timer
|
|
||||||
timer(std::string name_input, size_t interval_input,
|
|
||||||
std::function<void(const timer &)> callback_input)
|
|
||||||
: time_interval(interval_input), name(name_input), is_infinite(true),
|
|
||||||
callbackfunc(callback_input) {}
|
|
||||||
|
|
||||||
//limited timer
|
|
||||||
timer(std::string name, size_t interval_input, int triggeramount,
|
|
||||||
std::function<void(const timer &)> callback)
|
|
||||||
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
|
||||||
callbackfunc(callback) {}
|
|
||||||
|
|
||||||
// callbackfunc once enough time has accumulated
|
|
||||||
void updatetime(double DeltaTime) {
|
|
||||||
accumulator += DeltaTime;
|
|
||||||
if (!is_expired) {
|
|
||||||
if (accumulator >= time_interval) {
|
|
||||||
callbackfunc(*this);
|
|
||||||
accumulator -= time_interval;
|
|
||||||
triggercounter++;
|
|
||||||
if (!is_infinite && triggercounter >= triggeramount) {
|
|
||||||
is_expired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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: █");
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage collection of timers
|
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
||||||
class timermanager {
|
int ticnt = timer_input.triggercounter;
|
||||||
std::vector<std::unique_ptr<timer>> timers;
|
mvwvline_set(stdscr, LINES-ticnt, ticnt, &input_char, ticnt);
|
||||||
|
}
|
||||||
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());
|
|
||||||
}
|
|
||||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
|
||||||
[](auto &timer) { return timer->is_expired; }),
|
|
||||||
timers.end());
|
|
||||||
// implement delete timers
|
|
||||||
;
|
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
@@ -100,14 +41,13 @@ 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! 🌍 テスト 测试");
|
||||||
const wchar_t wc = L'█';
|
const wchar_t mywch = L'█';
|
||||||
cchar_t c_char;
|
cchar_t myc_char;
|
||||||
setcchar(&c_char, &wc, A_NORMAL, 0, NULL);
|
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
||||||
mvwvline_set(stdscr, 1, 1, &c_char, 5);
|
|
||||||
// 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, myupdatedisplay);
|
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, [&myc_char](const timer& t){createbalk(t,myc_char);});
|
||||||
timermanager mytimermanager;
|
timermanager mytimermanager;
|
||||||
mytimermanager.addtimer(std::move(clocktimer));
|
mytimermanager.addtimer(std::move(clocktimer));
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
// 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>
|
||||||
|
|
||||||
|
// infinite timer
|
||||||
|
timer::timer(std::string name_input, size_t interval_input,
|
||||||
|
std::function<void(const timer &)> callback_input)
|
||||||
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
||||||
|
callbackfunc(callback_input) {}
|
||||||
|
|
||||||
|
// limited timer
|
||||||
|
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
||||||
|
std::function<void(const timer &)> callback)
|
||||||
|
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
||||||
|
callbackfunc(callback) {}
|
||||||
|
|
||||||
|
// callbackfunc once enough time has accumulated
|
||||||
|
void timer::updatetime(double DeltaTime) {
|
||||||
|
accumulator += DeltaTime;
|
||||||
|
if (!is_expired) {
|
||||||
|
if (accumulator >= time_interval) {
|
||||||
|
callbackfunc(*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();
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef TIMER_HPP
|
||||||
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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::function<void(const timer&)> callbackfunc;
|
||||||
|
bool is_expired = false;
|
||||||
|
|
||||||
|
|
||||||
|
//infinite timer
|
||||||
|
timer(std::string name_input, size_t interval_input,
|
||||||
|
std::function<void(const timer &)> callback_input);
|
||||||
|
|
||||||
|
//limited timer
|
||||||
|
timer(std::string name, size_t interval_input, int triggeramount,
|
||||||
|
std::function<void(const timer &)> callback);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user