Compare commits
2 Commits
523dac1f4d
...
9dbc24efc1
| Author | SHA1 | Date | |
|---|---|---|---|
| 9dbc24efc1 | |||
| db8fc15560 |
@@ -1,14 +1,11 @@
|
|||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
#include "timeractions.hpp"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <clocale>
|
#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 <utility>
|
||||||
@@ -17,7 +14,6 @@
|
|||||||
/*
|
/*
|
||||||
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
|
||||||
@@ -30,62 +26,60 @@ void myupdatedisplay(const timer &timer_input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
std::vector<int> myothernumbers = {1, 4, 9, 8, 6, 2, 3, 5, 7};
|
||||||
|
|
||||||
void swapnumbers(std::vector<int> &input_numbers) {
|
void displaynumbers(int startx, int starty, std::vector<int> &input_numbers, cchar_t &input_char) {
|
||||||
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++) {
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
mvprintw(myy, 10+i, "%d,", input_numbers[i]);
|
/**
|
||||||
|
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
||||||
|
above that, I want a line of bars.
|
||||||
|
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
||||||
|
**/
|
||||||
|
mvprintw(LINES -1 - starty, i + startx, "%d,", input_numbers[i]);
|
||||||
|
mvwvline_set(stdscr, LINES-1-input_numbers[i]-starty, i+startx, &input_char, input_numbers[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
|
||||||
int ticnt = timer_input.triggercounter;
|
|
||||||
mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
|
|
||||||
}
|
|
||||||
|
|
||||||
void myinitialize() {
|
void myinitialize() {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
initscr();
|
initscr();
|
||||||
nodelay(stdscr, FALSE);
|
nodelay(stdscr, FALSE);
|
||||||
refresh();
|
refresh();
|
||||||
start_color();
|
start_color();
|
||||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
init_pair(1, COLOR_RED, COLOR_GREEN);
|
||||||
attron(COLOR_PAIR(1));
|
attron(COLOR_PAIR(1));
|
||||||
printw("Hello World! 🌍 テスト 测试");
|
printw("Hello World! 🌍 テスト 测试");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
myinitialize();
|
myinitialize();
|
||||||
const wchar_t mywch = L'█';
|
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||||
cchar_t myc_char;
|
|
||||||
setcchar(&myc_char, &mywch, A_NORMAL, 0, NULL);
|
//create actions, then put them in timers, then put them into the timermanager
|
||||||
// printf("verification\n");
|
|
||||||
auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
|
||||||
|
|
||||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
auto myaction = std::make_unique<swapaction>(mynumbers);
|
||||||
auto clocktimer = std::make_unique<timer>(
|
auto clocktimer =
|
||||||
"secondsprinter", 1, 10,
|
std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
||||||
std::move(myaction));
|
|
||||||
|
auto myothersort = std::make_unique<sortaction>(myothernumbers);
|
||||||
|
auto sorttimer =
|
||||||
|
std::make_unique<timer>("sorttimer", 0.1, std::move(myothersort));
|
||||||
|
|
||||||
timermanager mytimermanager;
|
timermanager mytimermanager;
|
||||||
mytimermanager.addtimer(std::move(clocktimer));
|
mytimermanager.addtimer(std::move(clocktimer));
|
||||||
//swapnumbers(mynumbers);
|
mytimermanager.addtimer(std::move(sorttimer));
|
||||||
displaynumbers(2,mynumbers);
|
|
||||||
mvprintw(5, 5, "timer added");
|
|
||||||
// calculate delta between current and previous time
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto currenttime = std::chrono::steady_clock::now();
|
erase();
|
||||||
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
||||||
double dtime = myDeltaTime.count();
|
mytimermanager.updatetimers();
|
||||||
mytimermanager.updatetimers(dtime);
|
|
||||||
displaynumbers(3,mynumbers);
|
displaynumbers(0,0,mynumbers, my_c_char);
|
||||||
starttime = currenttime;
|
displaynumbers(10, 10, myothernumbers, my_c_char);
|
||||||
|
|
||||||
|
refresh();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-12
@@ -1,8 +1,8 @@
|
|||||||
// calls callbackfunc on specified interval based on accumulated deltatime
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -39,11 +39,19 @@ 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(double dtime) {
|
void timermanager::updatetimers() {
|
||||||
|
//get dtime
|
||||||
|
auto currenttime = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = currenttime - prevtime;
|
||||||
|
double dtime = myDeltaTime.count();
|
||||||
|
|
||||||
|
//update timers with dtime
|
||||||
for (auto &mytimer : timers) {
|
for (auto &mytimer : timers) {
|
||||||
mytimer->updatetime(dtime);
|
mytimer->updatetime(dtime);
|
||||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
}
|
}
|
||||||
|
//reset for next tick
|
||||||
|
prevtime = currenttime;
|
||||||
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());
|
||||||
@@ -51,14 +59,5 @@ void timermanager::updatetimers(double dtime) {
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
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;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+10
-12
@@ -1,6 +1,7 @@
|
|||||||
#ifndef TIMER_HPP
|
#ifndef TIMER_HPP
|
||||||
#define TIMER_HPP
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -10,6 +11,12 @@
|
|||||||
|
|
||||||
class timer;
|
class timer;
|
||||||
|
|
||||||
|
struct balkjes {
|
||||||
|
int startx;
|
||||||
|
int starty;
|
||||||
|
int yhight;
|
||||||
|
};
|
||||||
|
|
||||||
class timeraction {
|
class timeraction {
|
||||||
public:
|
public:
|
||||||
virtual ~timeraction() = default;
|
virtual ~timeraction() = default;
|
||||||
@@ -47,23 +54,14 @@ public:
|
|||||||
// 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
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
timermanager();
|
||||||
void addtimer(std::unique_ptr<timer> input_timer);
|
void addtimer(std::unique_ptr<timer> input_timer);
|
||||||
|
void updatetimers();
|
||||||
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
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 (!iscomplete) {
|
||||||
|
/**
|
||||||
|
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()) {
|
||||||
|
iscomplete=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 iscomplete = 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 iscomplete = 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