updated naming
This commit is contained in:
@@ -19,24 +19,24 @@
|
|||||||
centralize into event sender and subscriber
|
centralize into event sender and subscriber
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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,
|
mvprintw(timer_input.trigger_counter, timer_input.trigger_counter + x_offset,
|
||||||
"test: █");
|
"test: █");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
std::vector<int> my_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
std::vector<int> myothernumbers = {4, 9, 8, 6, 2, 1, 10, 3, 5, 7};
|
std::vector<int> my_other_numbers = {4, 9, 8, 6, 2, 1, 10, 3, 5, 7};
|
||||||
|
|
||||||
void displaynumbers(int startx, int starty, std::vector<int> &input_numbers, cchar_t &input_char) {
|
void displaynumbers(int start_x, int start_y, std::vector<int> &input_numbers, cchar_t &input_char) {
|
||||||
for (size_t i = 0; i < input_numbers.size(); i++) {
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
/**
|
/**
|
||||||
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
||||||
above that, I want a line of bars.
|
above that, I want a line of bars.
|
||||||
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
||||||
**/
|
**/
|
||||||
mvprintw(LINES -1 - starty, i + startx, "%d,", input_numbers[i]);
|
mvprintw(LINES -1 - start_y, i + start_x, "%d,", input_numbers[i]);
|
||||||
mvwvline_set(stdscr, LINES-1-input_numbers[i]-starty, i+startx, &input_char, input_numbers[i]);
|
mvwvline_set(stdscr, LINES-1-input_numbers[i]-start_y, i+start_x, &input_char, input_numbers[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,27 +58,27 @@ int main() {
|
|||||||
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||||
|
|
||||||
//create actions, then put them in timers, then put them into the timermanager
|
//create actions, then put them in timers, then put them into the timermanager
|
||||||
|
|
||||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
|
||||||
auto clocktimer =
|
|
||||||
std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
|
||||||
|
|
||||||
auto myothersort = std::make_unique<sortaction>(myothernumbers);
|
auto my_action = std::make_unique<SwapAction>(my_numbers);
|
||||||
auto sorttimer =
|
auto clock_timer =
|
||||||
std::make_unique<timer>("sorttimer", 0.5, std::move(myothersort));
|
std::make_unique<Timer>("secondsprinter", 1, 10, std::move(my_action));
|
||||||
|
|
||||||
timermanager mytimermanager;
|
auto my_other_sort = std::make_unique<SortAction>(my_other_numbers);
|
||||||
mytimermanager.addtimer(std::move(clocktimer));
|
auto sort_timer =
|
||||||
mytimermanager.addtimer(std::move(sorttimer));
|
std::make_unique<Timer>("sorttimer", 0.5, std::move(my_other_sort));
|
||||||
|
|
||||||
|
TimerManager my_timer_manager;
|
||||||
|
my_timer_manager.addtimer(std::move(clock_timer));
|
||||||
|
my_timer_manager.addtimer(std::move(sort_timer));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
erase();
|
erase();
|
||||||
|
|
||||||
mytimermanager.updatetimers();
|
|
||||||
|
|
||||||
displaynumbers(0,0,mynumbers, my_c_char);
|
my_timer_manager.updatetimers();
|
||||||
displaynumbers(10, 10, myothernumbers, my_c_char);
|
|
||||||
|
displaynumbers(0,0,my_numbers, my_c_char);
|
||||||
|
displaynumbers(10, 10, my_other_numbers, my_c_char);
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-16
@@ -2,47 +2,46 @@
|
|||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstddef>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// infinite timer
|
// infinite timer
|
||||||
timer::timer(std::string name_input, double interval_input,
|
Timer::Timer(std::string name_input, double interval_input,
|
||||||
std::unique_ptr<timeraction> action_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),
|
||||||
action(std::move(action_input)) {}
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
// limited timer
|
// limited timer
|
||||||
timer::timer(std::string name, double interval_input, int triggeramount,
|
Timer::Timer(std::string name, double interval_input, int trigger_amount,
|
||||||
std::unique_ptr<timeraction> action_input)
|
std::unique_ptr<TimerAction> action_input)
|
||||||
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
: trigger_amount(trigger_amount), time_interval(interval_input), name(name),
|
||||||
action(std::move(action_input)) {}
|
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 delta_time) {
|
||||||
accumulator += DeltaTime;
|
accumulator += delta_time;
|
||||||
if (!is_expired) {
|
if (!is_expired) {
|
||||||
if (accumulator >= time_interval) {
|
if (accumulator >= time_interval) {
|
||||||
action->update(*this);
|
action->update(*this);
|
||||||
accumulator -= time_interval;
|
accumulator -= time_interval;
|
||||||
triggercounter++;
|
trigger_counter++;
|
||||||
if (!is_infinite && triggercounter >= triggeramount) {
|
if (!is_infinite && trigger_counter >= trigger_amount) {
|
||||||
is_expired = true;
|
is_expired = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
|
void TimerManager::addtimer(std::unique_ptr<Timer> input_timer) {
|
||||||
timers.push_back(std::move(input_timer));
|
timers.push_back(std::move(input_timer));
|
||||||
}
|
}
|
||||||
|
|
||||||
void timermanager::updatetimers() {
|
void TimerManager::updatetimers() {
|
||||||
//get dtime
|
//get dtime
|
||||||
auto currenttime = std::chrono::steady_clock::now();
|
auto current_time = std::chrono::steady_clock::now();
|
||||||
std::chrono::duration<double> myDeltaTime = currenttime - prevtime;
|
std::chrono::duration<double> myDeltaTime = current_time - prev_time;
|
||||||
double dtime = myDeltaTime.count();
|
double dtime = myDeltaTime.count();
|
||||||
|
|
||||||
//update timers with dtime
|
//update timers with dtime
|
||||||
@@ -51,7 +50,7 @@ void timermanager::updatetimers() {
|
|||||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
}
|
}
|
||||||
//reset for next tick
|
//reset for next tick
|
||||||
prevtime = currenttime;
|
prev_time = current_time;
|
||||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||||
[](auto &timer) { return timer->is_expired; }),
|
[](auto &timer) { return timer->is_expired; }),
|
||||||
timers.end());
|
timers.end());
|
||||||
@@ -59,5 +58,5 @@ void timermanager::updatetimers() {
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
TimerManager::TimerManager(): prev_time(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
|
|||||||
+20
-29
@@ -2,63 +2,54 @@
|
|||||||
#define TIMER_HPP
|
#define TIMER_HPP
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstddef>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class timer;
|
class Timer;
|
||||||
|
|
||||||
struct balkjes {
|
class TimerAction {
|
||||||
int startx;
|
|
||||||
int starty;
|
|
||||||
int yhight;
|
|
||||||
};
|
|
||||||
|
|
||||||
class timeraction {
|
|
||||||
public:
|
public:
|
||||||
virtual ~timeraction() = default;
|
virtual ~TimerAction() = default;
|
||||||
virtual void update(const timer &t) = 0;
|
virtual void update(const Timer &t) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class timer {
|
class Timer {
|
||||||
private:
|
private:
|
||||||
double accumulator = 0;
|
double accumulator = 0;
|
||||||
int triggeramount = 0;
|
int trigger_amount = 0;
|
||||||
double time_interval = 1; // only display every 1 seconds
|
double time_interval = 1; // only display every 1 seconds
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string name;
|
std::string name;
|
||||||
int triggercounter = 0;
|
int trigger_counter = 0;
|
||||||
bool is_infinite = false;
|
bool is_infinite = false;
|
||||||
std::unique_ptr<timeraction> action;
|
std::unique_ptr<TimerAction> action;
|
||||||
bool is_expired = false;
|
bool is_expired = false;
|
||||||
|
|
||||||
|
|
||||||
//infinite timer
|
//infinite timer
|
||||||
timer(std::string name_input, double interval_input,
|
Timer(std::string name_input, double interval_input,
|
||||||
std::unique_ptr<timeraction> action_input
|
std::unique_ptr<TimerAction> action_input
|
||||||
);
|
);
|
||||||
|
|
||||||
//limited timer
|
//limited timer
|
||||||
timer(std::string name, double interval_input, int triggeramount,
|
Timer(std::string name, double interval_input, int trigger_amount,
|
||||||
std::unique_ptr<timeraction> action
|
std::unique_ptr<TimerAction> action
|
||||||
);
|
);
|
||||||
// callbackfunc once enough time has accumulated
|
// callbackfunc once enough time has accumulated
|
||||||
void updatetime(double DeltaTime);
|
void updatetime(double delta_time);
|
||||||
};
|
};
|
||||||
|
|
||||||
// manage collection of timers
|
// manage collection of timers
|
||||||
class timermanager {
|
class TimerManager {
|
||||||
std::vector<std::unique_ptr<timer>> timers;
|
std::vector<std::unique_ptr<Timer>> timers;
|
||||||
std::chrono::steady_clock::time_point prevtime; // initial starttime
|
std::chrono::steady_clock::time_point prev_time; // initial starttime
|
||||||
|
|
||||||
public:
|
public:
|
||||||
timermanager();
|
TimerManager();
|
||||||
void addtimer(std::unique_ptr<timer> input_timer);
|
void addtimer(std::unique_ptr<Timer> input_timer);
|
||||||
void updatetimers();
|
void updatetimers();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,52 +2,48 @@
|
|||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include "timeractions.hpp"
|
#include "timeractions.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
|
||||||
#include <cstddef>
|
|
||||||
#include <memory>
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
SwapAction::SwapAction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||||
|
|
||||||
void swapaction::update(const timer &input_timer){
|
void SwapAction::update(const Timer &input_timer){
|
||||||
if (!iscomplete) {
|
if (!is_complete) {
|
||||||
int temp = input_vector[it_i];
|
int temp = input_vector[it_i];
|
||||||
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
||||||
input_vector[input_vector.size() - 1 - it_i] = temp;
|
input_vector[input_vector.size() - 1 - it_i] = temp;
|
||||||
it_i++;
|
it_i++;
|
||||||
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
if(it_i>=input_vector.size() / 2){is_complete=true;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sortaction::sortaction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
SortAction::SortAction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
||||||
|
|
||||||
//sorts the included array using bsort and external iterators rather than for loops
|
//sorts the included array using bsort and external iterators rather than for loops
|
||||||
void sortaction::update(const timer &input_timer) {
|
void SortAction::update(const Timer &input_timer) {
|
||||||
if (!iscomplete) {
|
if (!is_complete) {
|
||||||
/**
|
/**
|
||||||
we take a number, it_i loop
|
we take a number, it_i loop
|
||||||
compare it with its neighbour, it_j loop
|
compare it with its neighbour, it_j loop
|
||||||
if it's larger swap it
|
if it's larger swap it
|
||||||
keep going until it_i == length - sorted numbers
|
keep going until it_i == length - sorted numbers
|
||||||
once that is true, reset it_j and advance it_i
|
once that is true, reset it_j and advance it_i
|
||||||
**/
|
**/
|
||||||
if (input_vector[it_j]>input_vector[it_j+1]) {
|
if (input_vector[it_j]>input_vector[it_j+1]) {
|
||||||
// int temp = input_vector[it_j + 1];
|
// int temp = input_vector[it_j + 1];
|
||||||
// input_vector[it_j + 1] = input_vector[it_j];
|
// input_vector[it_j + 1] = input_vector[it_j];
|
||||||
// input_vector[it_j] = temp;
|
// input_vector[it_j] = temp;
|
||||||
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
||||||
}
|
}
|
||||||
it_j++;
|
it_j++;
|
||||||
|
|
||||||
if (it_j >= input_vector.size() - 1 - it_i) {
|
if (it_j >= input_vector.size() - 1 - it_i) {
|
||||||
it_j = 0;
|
it_j = 0;
|
||||||
it_i++;
|
it_i++;
|
||||||
}
|
}
|
||||||
if (it_i>=input_vector.size()) {
|
if (it_i>=input_vector.size()) {
|
||||||
iscomplete=true;
|
is_complete=true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,23 +10,23 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class swapaction : public timeraction {
|
class SwapAction : public TimerAction {
|
||||||
public:
|
public:
|
||||||
size_t it_i = 0;
|
size_t it_i = 0;
|
||||||
bool iscomplete = false;
|
bool is_complete = false;
|
||||||
std::vector<int> &input_vector;
|
std::vector<int> &input_vector;
|
||||||
swapaction(std::vector<int> &input_vector);
|
SwapAction(std::vector<int> &input_vector);
|
||||||
void update(const timer &t);
|
void update(const Timer &t);
|
||||||
};
|
};
|
||||||
|
|
||||||
class sortaction : public timeraction {
|
class SortAction : public TimerAction {
|
||||||
public:
|
public:
|
||||||
size_t it_i = 0;
|
size_t it_i = 0;
|
||||||
size_t it_j = 0;
|
size_t it_j = 0;
|
||||||
bool iscomplete = false;
|
bool is_complete = false;
|
||||||
std::vector<int> &input_vector;
|
std::vector<int> &input_vector;
|
||||||
sortaction(std::vector<int> &input_vector);
|
SortAction(std::vector<int> &input_vector);
|
||||||
void update(const timer &t);
|
void update(const Timer &t);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user