Compare commits

...

2 Commits

Author SHA1 Message Date
lyrgb 9dbc24efc1 visualization of bsort, manager now responsible for updates and time
split timeractions from timers into seperate header
integrated the experiments into an improved displaynumbers, which
includes the vector as well as bars above them
outsourced the calculation of delta time to the manager to clean up main()
2026-07-17 00:04:28 +02:00
lyrgb db8fc15560 time keeping / deltatime now handled by timermanager instead of main 2026-07-16 18:49:11 +02:00
5 changed files with 147 additions and 71 deletions
+33 -39
View File
@@ -1,14 +1,11 @@
#include "timer.hpp"
#include <algorithm>
#include "timeractions.hpp"
#include <chrono>
#include <clocale>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <memory>
#include <ncurses.h>
#include <string>
#include <thread>
#include <unistd.h>
#include <utility>
@@ -17,7 +14,6 @@
/*
todo
figure out namespaces, using, and auto
formalize timer struct
read about std function and std bind
try to do with objects / classes instead
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> myothernumbers = {1, 4, 9, 8, 6, 2, 3, 5, 7};
void swapnumbers(std::vector<int> &input_numbers) {
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){
void displaynumbers(int startx, int starty, std::vector<int> &input_numbers, cchar_t &input_char) {
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() {
setlocale(LC_ALL, "");
initscr();
nodelay(stdscr, FALSE);
refresh();
start_color();
init_pair(1, COLOR_BLUE, COLOR_WHITE);
init_pair(1, COLOR_RED, COLOR_GREEN);
attron(COLOR_PAIR(1));
printw("Hello World! 🌍 テスト 测试");
}
int main() {
myinitialize();
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
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 myaction = std::make_unique<swapaction>(mynumbers);
auto clocktimer = std::make_unique<timer>(
"secondsprinter", 1, 10,
std::move(myaction));
auto clocktimer =
std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
auto myothersort = std::make_unique<sortaction>(myothernumbers);
auto sorttimer =
std::make_unique<timer>("sorttimer", 0.1, std::move(myothersort));
timermanager mytimermanager;
mytimermanager.addtimer(std::move(clocktimer));
//swapnumbers(mynumbers);
displaynumbers(2,mynumbers);
mvprintw(5, 5, "timer added");
// calculate delta between current and previous time
mytimermanager.addtimer(std::move(sorttimer));
while (true) {
auto currenttime = std::chrono::steady_clock::now();
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
double dtime = myDeltaTime.count();
mytimermanager.updatetimers(dtime);
displaynumbers(3,mynumbers);
starttime = currenttime;
erase();
mytimermanager.updatetimers();
displaynumbers(0,0,mynumbers, my_c_char);
displaynumbers(10, 10, myothernumbers, my_c_char);
refresh();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
+11 -12
View File
@@ -1,8 +1,8 @@
// calls callbackfunc on specified interval based on accumulated deltatime
#include "timer.hpp"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <functional>
#include <memory>
#include <ncurses.h>
#include <string>
@@ -39,11 +39,19 @@ void timermanager::addtimer(std::unique_ptr<timer> 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) {
mytimer->updatetime(dtime);
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(),
[](auto &timer) { return timer->is_expired; }),
timers.end());
@@ -51,14 +59,5 @@ void timermanager::updatetimers(double dtime) {
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
View File
@@ -1,6 +1,7 @@
#ifndef TIMER_HPP
#define TIMER_HPP
#include <chrono>
#include <cstddef>
#include <functional>
#include <memory>
@@ -10,6 +11,12 @@
class timer;
struct balkjes {
int startx;
int starty;
int yhight;
};
class timeraction {
public:
virtual ~timeraction() = default;
@@ -47,23 +54,14 @@ public:
// manage collection of timers
class timermanager {
std::vector<std::unique_ptr<timer>> timers;
std::chrono::steady_clock::time_point prevtime; // initial starttime
public:
timermanager();
void addtimer(std::unique_ptr<timer> input_timer);
void updatetimers(double dtime);
void updatetimers();
};
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
+53
View File
@@ -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;
}
}
}
+32
View File
@@ -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