Compare commits
9 Commits
02a12622c2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ccd216591a | |||
| 8ff0e89bb1 | |||
| 37c919df90 | |||
| 9dbc24efc1 | |||
| db8fc15560 | |||
| 523dac1f4d | |||
| f3b7de0489 | |||
| 3834d764c1 | |||
| 0cc5d69c0a |
@@ -7,4 +7,15 @@ find_package(Curses REQUIRED)
|
|||||||
include_directories(${CURSES_INCLUDE_DIR})
|
include_directories(${CURSES_INCLUDE_DIR})
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_option(${PROJECT_NAME} PRIVATE /W4)
|
||||||
|
else()
|
||||||
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
|
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
|
||||||
|
|||||||
@@ -1,105 +1,86 @@
|
|||||||
#include <algorithm>
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <clocale>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <functional>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
todo
|
todo
|
||||||
figure out namespaces, using, and auto
|
figure out namespaces, using, and auto
|
||||||
formalize timer struct
|
|
||||||
read about std function and std bind
|
read about std function and std bind
|
||||||
try to do with objects / classes instead
|
try to do with objects / classes instead
|
||||||
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.trigger_counter, timer_input.trigger_counter + x_offset,
|
||||||
*/
|
"test: █");
|
||||||
|
|
||||||
struct mysecondsdisplay {
|
|
||||||
int intervalcount = 10;
|
|
||||||
void myupdatedisplay() {
|
|
||||||
intervalcount -= 1;
|
|
||||||
mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
|
||||||
if (intervalcount < 1) {
|
|
||||||
intervalcount = 10;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
std::vector<int> my_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
class timer {
|
std::vector<int> my_other_numbers = {4, 9, 8, 6, 2, 1, 10, 3, 5, 7};
|
||||||
public:
|
|
||||||
std::string name;
|
|
||||||
std::function<void()> callbackfunc;
|
|
||||||
|
|
||||||
timer(std::string name, size_t interval_input, std::function<void()> callback)
|
void displaynumbers(int start_x, int start_y, std::vector<int> &input_numbers, cchar_t &input_char) {
|
||||||
: name(name), callbackfunc(callback), time_interval(interval_input) {}
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
|
/**
|
||||||
// callbackfunc once enough time has accumulated
|
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
||||||
void updatetime(double DeltaTime) {
|
above that, I want a line of bars.
|
||||||
accumulator += DeltaTime;
|
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
||||||
if (accumulator >= time_interval) {
|
**/
|
||||||
accumulator -= time_interval;
|
mvprintw(LINES -1 - start_y, i + start_x, "%d,", input_numbers[i]);
|
||||||
callbackfunc();
|
mvwvline_set(stdscr, LINES-1-input_numbers[i]-start_y, i+start_x, &input_char, input_numbers[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
void myinitialize() {
|
||||||
double accumulator = 0;
|
setlocale(LC_ALL, "");
|
||||||
double time_interval = 1; // only display every 1 seconds
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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() {
|
|
||||||
initscr();
|
initscr();
|
||||||
nodelay(stdscr, FALSE);
|
nodelay(stdscr, FALSE);
|
||||||
refresh();
|
refresh();
|
||||||
// printf("verification\n");
|
start_color();
|
||||||
|
init_pair(1, COLOR_RED, COLOR_GREEN);
|
||||||
|
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));
|
|
||||||
|
|
||||||
mvprintw(5, 5, "timer added");
|
|
||||||
// calculate delta between current and previous time
|
int main() {
|
||||||
|
myinitialize();
|
||||||
|
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
|
||||||
|
|
||||||
|
auto my_action = std::make_unique<SwapAction>(my_numbers);
|
||||||
|
auto clock_timer =
|
||||||
|
std::make_unique<Timer>("secondsprinter", 1, 10, std::move(my_action));
|
||||||
|
|
||||||
|
auto my_other_sort = std::make_unique<SortAction>(my_other_numbers);
|
||||||
|
auto sort_timer =
|
||||||
|
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) {
|
||||||
auto currenttime = std::chrono::system_clock::now();
|
erase();
|
||||||
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
||||||
double dtime = myDeltaTime.count();
|
|
||||||
// mvprintw(11, 11, "dtime: %f", dtime);
|
|
||||||
// clocktimer.updatetime(dtime);
|
|
||||||
mytimermanager.updatetimers(dtime);
|
|
||||||
|
|
||||||
starttime = currenttime;
|
my_timer_manager.updatetimers();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
||||||
|
displaynumbers(0,0,my_numbers, my_c_char);
|
||||||
|
displaynumbers(10, 10, my_other_numbers, my_c_char);
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
}
|
}
|
||||||
|
|
||||||
getch();
|
getch();
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// infinite timer
|
||||||
|
Timer::Timer(std::string name_input, double 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, double interval_input, int trigger_amount,
|
||||||
|
std::unique_ptr<TimerAction> action_input)
|
||||||
|
: trigger_amount(trigger_amount), time_interval(interval_input), name(name),
|
||||||
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
|
// callbackfunc once enough time has accumulated
|
||||||
|
void Timer::updatetime(double delta_time) {
|
||||||
|
accumulator += delta_time;
|
||||||
|
if (!is_expired) {
|
||||||
|
if (accumulator >= time_interval) {
|
||||||
|
action->update(*this);
|
||||||
|
accumulator -= time_interval;
|
||||||
|
trigger_counter++;
|
||||||
|
if (!is_infinite && trigger_counter >= trigger_amount) {
|
||||||
|
is_expired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerManager::addtimer(std::unique_ptr<Timer> input_timer) {
|
||||||
|
timers.push_back(std::move(input_timer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerManager::updatetimers() {
|
||||||
|
//get dtime
|
||||||
|
auto current_time = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = current_time - prev_time;
|
||||||
|
double dtime = myDeltaTime.count();
|
||||||
|
|
||||||
|
//update timers with dtime
|
||||||
|
for (auto &mytimer : timers) {
|
||||||
|
mytimer->updatetime(dtime);
|
||||||
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
|
}
|
||||||
|
//reset for next tick
|
||||||
|
prev_time = current_time;
|
||||||
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||||
|
[](auto &timer) { return timer->is_expired; }),
|
||||||
|
timers.end());
|
||||||
|
// implement delete timers
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerManager::TimerManager(): prev_time(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#ifndef TIMER_HPP
|
||||||
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#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 trigger_amount = 0;
|
||||||
|
double time_interval = 1; // only display every 1 seconds
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
int trigger_counter = 0;
|
||||||
|
bool is_infinite = false;
|
||||||
|
std::unique_ptr<TimerAction> action;
|
||||||
|
bool is_expired = false;
|
||||||
|
|
||||||
|
|
||||||
|
//infinite timer
|
||||||
|
Timer(std::string name_input, double interval_input,
|
||||||
|
std::unique_ptr<TimerAction> action_input
|
||||||
|
);
|
||||||
|
|
||||||
|
//limited timer
|
||||||
|
Timer(std::string name, double interval_input, int trigger_amount,
|
||||||
|
std::unique_ptr<TimerAction> action
|
||||||
|
);
|
||||||
|
// callbackfunc once enough time has accumulated
|
||||||
|
void updatetime(double delta_time);
|
||||||
|
};
|
||||||
|
|
||||||
|
// manage collection of timers
|
||||||
|
class TimerManager {
|
||||||
|
std::vector<std::unique_ptr<Timer>> timers;
|
||||||
|
std::chrono::steady_clock::time_point prev_time; // initial starttime
|
||||||
|
|
||||||
|
public:
|
||||||
|
TimerManager();
|
||||||
|
void addtimer(std::unique_ptr<Timer> input_timer);
|
||||||
|
void updatetimers();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
externalize state
|
||||||
|
*/
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
SwapAction::SwapAction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||||
|
|
||||||
|
void SwapAction::update(const Timer &input_timer){
|
||||||
|
if (!is_complete) {
|
||||||
|
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){is_complete=true;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SortAction::SortAction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
||||||
|
|
||||||
|
//sorts the included array using bsort and external iterators rather than for loops
|
||||||
|
void SortAction::update(const Timer &input_timer) {
|
||||||
|
if (!is_complete) {
|
||||||
|
/**
|
||||||
|
we take a number, it_i loop
|
||||||
|
compare it with its neighbour, it_j loop
|
||||||
|
if it's larger swap it
|
||||||
|
keep going until it_i == length - sorted numbers
|
||||||
|
once that is true, reset it_j and advance it_i
|
||||||
|
**/
|
||||||
|
if (input_vector[it_j]>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] = temp;
|
||||||
|
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
||||||
|
}
|
||||||
|
it_j++;
|
||||||
|
|
||||||
|
if (it_j >= input_vector.size() - 1 - it_i) {
|
||||||
|
it_j = 0;
|
||||||
|
it_i++;
|
||||||
|
}
|
||||||
|
if (it_i>=input_vector.size()) {
|
||||||
|
is_complete=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef TIMERACTIONS_HPP
|
||||||
|
#define TIMERACTIONS_HPP
|
||||||
|
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class SwapAction : public TimerAction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
bool is_complete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
SwapAction(std::vector<int> &input_vector);
|
||||||
|
void update(const Timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
class SortAction : public TimerAction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
size_t it_j = 0;
|
||||||
|
bool is_complete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
SortAction(std::vector<int> &input_vector);
|
||||||
|
void update(const Timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user