Compare commits

..

1 Commits

Author SHA1 Message Date
lyrgb ad94f758fc split app into seperate files to clean main 2026-08-12 02:06:24 +02:00
11 changed files with 216 additions and 254 deletions
-18
View File
@@ -1,18 +0,0 @@
#ifndef INVOKER_HPP
#define INVOKER_HPP
#include "steppable.hpp"
#include <memory>
class Invoker {
public:
std::weak_ptr<Steppable> target;
Invoker(std::shared_ptr<Steppable> t) { target = t; }
void request() {
if (auto sp = target.lock()) {
sp->step();
}
}
};
#endif
+44 -38
View File
@@ -1,63 +1,69 @@
#include "./app.hpp"
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <chrono>
#include <iostream> #include <iostream>
#include <memory>
#include <thread> #include <thread>
#include "./app.hpp"
using namespace std; bool displaySortInput(SortInput input) {
cout << "name: " + input.name + " values: ";
bool displaySortInput(shared_ptr<SortInput> input) { for (size_t i = 0; i < input.values.size(); ++i) {
cout << "name: " + input->name + " values: "; printf("%d | ",input.values[i]);
for (size_t i = 0; i < input->values.size(); ++i) {
printf("%d | ", input->values[i]);
} }
printf("\n"); printf("\n");
return false; return false;
} }
string arraytostr(shared_ptr<SortInput> input) { string arraytostr(SortInput input) {
string myoutput; string myoutput;
ostringstream stringrep; ostringstream stringrep;
for (size_t i = 0; i < input->values.size(); ++i) { for (size_t i = 0; i < input.values.size(); ++i) {
stringrep << input->values[i] << " | "; stringrep << input.values[i] <<" | ";
} }
return stringrep.str(); return stringrep.str();
} }
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager) App::App(vector<SortInput> inputs):
: viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {} mywindow(sf::VideoMode({800,600}), "mywindow"),
myfont(load_font("assets/NS-R.ttf")),
// just pushes to App for now text(myfont),
void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) { inputs(std::move(inputs)),
cout << "adding sort " << myinput->name << "\n"; mysortsession(this->inputs)
// insert data {
inputs.push_back(myinput); text.setFont(myfont);
// move the sort into app text.setString("Hello world");
sortbindings.push_back(mysortbinding); text.setCharacterSize(24);
} text.setFillColor(sf::Color::Red);
void App::run() {
// set up ui
while (viewsession->mywindow.isOpen()) {
while (const optional event = viewsession->mywindow.pollEvent()) {
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
} }
// draw
viewsession->mywindow.clear(sf::Color::Black);
viewsession->mywindow.draw(viewsession->text);
viewsession->text.setString(arraytostr(inputs[0]));
for (shared_ptr<SortInput> input : inputs) { sf::Font App::load_font(const std::string &filename){
sf::Font font;
if (!font.openFromFile(filename)){
fprintf(stderr, "failed to load font");
std::exit(1);
}
return font;
}
void App::run(){
//set up ui
while (mywindow.isOpen()){
while (const optional event = mywindow.pollEvent()){
if (event->is<sf::Event::Closed>())
mywindow.close();
}
mywindow.clear(sf::Color::Black);
mywindow.draw(text);
text.setString(arraytostr(inputs[0]));
mysortsession.update_sorts();
for (SortInput input : inputs) {
displaySortInput(input); displaySortInput(input);
} }
viewsession->mywindow.display();
// update logic mywindow.display();
timermanager->update_timers();
this_thread::sleep_for(std::chrono::milliseconds(250)); this_thread::sleep_for(std::chrono::milliseconds(250));
} }
} }
+10 -13
View File
@@ -1,27 +1,24 @@
#ifndef APP_HPP #ifndef APP_HPP
#define APP_HPP #define APP_HPP
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include <memory>
#include <vector>
#include "../sessions.hpp"
#include "../sorters.hpp" #include "../sorters.hpp"
#include "../timer.hpp" #include "../sessions.hpp"
struct App{
sf::RenderWindow mywindow;
sf::Font myfont;
sf::Text text;
vector<SortInput> inputs;
SortSession mysortsession;
// should connect the different parts sf::Font load_font(const std::string &filename);
// for now data, mutation, and rendering
struct App {
std::vector<std::shared_ptr<SortInput>> inputs;
std::vector<SortBinding> sortbindings;
std::unique_ptr<ViewSession> viewsession;
std::unique_ptr<TimerManager> timermanager;
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager); App(vector<SortInput> inputs);
void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
void run(); void run();
}; };
-20
View File
@@ -1,20 +0,0 @@
#include "sessions.hpp"
using namespace std;
ViewSession::ViewSession()
: mywindow(sf::VideoMode({800, 600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) {
text.setFont(myfont);
text.setString("Hello world");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
};
sf::Font ViewSession::load_font(const std::string &filename) {
sf::Font font;
if (!font.openFromFile(filename)) {
fprintf(stderr, "failed to load font");
std::exit(1);
}
return font;
}
+23 -8
View File
@@ -1,23 +1,38 @@
#ifndef SESSIONS_HPP #ifndef SESSIONS_HPP
#define SESSIONS_HPP #define SESSIONS_HPP
//list of inputs, manually assign them to sorts
#include "sorters.hpp"
#include "timer.hpp"
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include <vector>
#include "sorters.hpp" struct SortSession {
vector<SortInput> &inputs;
unique_ptr<Steppable> sorts;
TimerManager SortTimer;
// owns and manages the sorters that mutate data, but does not own the data itself SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
//setupsorttimers(SortTimer, sorts);
add_session(sorts);
}
// sfml/render
struct ViewSession { void add_session(unique_ptr<Steppable> &mysteppable){
SortTimer.add_timer(0,
[&mysteppable]() {return mysteppable->step();},
std::chrono::milliseconds(250));
}
void update_sorts(){
SortTimer.update_timers();
}
};
struct ViewSession{
sf::RenderWindow mywindow; sf::RenderWindow mywindow;
sf::Font myfont; sf::Font myfont;
sf::Text text; sf::Text text;
ViewSession();
sf::Font load_font(const std::string &filename);
}; };
#endif #endif
+14 -24
View File
@@ -1,39 +1,29 @@
#include "sorters.hpp"
#include <cstdio> #include <cstdio>
#include <iostream>
#include <memory>
#include <utility> #include <utility>
#include "sorters.hpp"
using namespace std; using namespace std;
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input) : data(input) {};
void SteppedBsort::step() { SteppedBsort::SteppedBsort(SortInput &input): data(input) {};
cout << "step in SteppedBsort\n";
if (is_finished) { bool SteppedBsort:: step(){
if(is_finished==true) {
printf("finished"); printf("finished");
return true;
} }
if (data->values[j] > data->values[j + 1]) { if(data.values[j]>data.values[j+1])
swap(data->values[j], data->values[j + 1]); {
swap(data.values[j], data.values[j+1]);
} }
j++; j++;
if (j >= data->values.size() - 1 - i) { if(j>=data.values.size()-1 -i){
j = 0; j=0;
i++; i++;
} }
if (i == data->values.size() - 1) { if (i==data.values.size()-1){
is_finished = true; is_finished=true;
} }
}
SortBinding::SortBinding( return false;
std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker)
: input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)) {}
// connects data with the correct sort
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
if (sorttype == "bsort") {
return make_shared<SteppedBsort>(input);
}
return nullptr;
} }
+15 -25
View File
@@ -6,48 +6,38 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Invoker.hpp"
#include "steppable.hpp" #include "steppable.hpp"
// ground truth domain data using namespace std;
struct SortInput { struct SortInput {
std::string name; string name;
std::vector<int> values; vector<int> values;
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {} SortInput(string name, vector<int> data)
: name(name), values(data) {}
}; };
// ro rendering of data
struct SortView { struct SortView {
std::shared_ptr<const SortInput> values; shared_ptr<const SortInput> values;
size_t currenttarget = 0; size_t currenttarget = 0;
int x, y; int x,y;
int color; int color;
SortView(std::shared_ptr<const SortInput> in) : values(in) {} SortView(shared_ptr<const SortInput> in): values(in) {}
}; };
// mutates the domain data
class SteppedBsort : public Steppable { class SteppedBsort: public Steppable {
public: public:
std::shared_ptr<SortInput> data; SortInput &data;
size_t stepcount = 0; size_t stepcount = 0;
size_t i = 0; size_t i = 0;
size_t j = 0; size_t j = 0;
bool is_finished = false; bool is_finished = false;
SteppedBsort(std::shared_ptr<SortInput> data); SteppedBsort(SortInput &data);
void step(); bool step();
}; };
// working unit
struct SortBinding {
std::shared_ptr<SortInput> input;
std::shared_ptr<Steppable> sort;
std::shared_ptr<Invoker> invoker;
SortBinding(std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker);
};
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype);
#endif #endif
+22 -26
View File
@@ -1,50 +1,46 @@
// for now juist CLI, print an arr, then print each it // for now juist CLI, print an arr, then print each it
#include <chrono>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include <SFML/Window.hpp>
#include <SFML/Window/Window.hpp> #include <SFML/Window/Window.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "core/app.hpp" #include "core/app.hpp"
#include "sessions.hpp"
#include "sorters.hpp"
#include "steppable.hpp" #include "steppable.hpp"
#include "timer.hpp" #include "timer.hpp"
#include "sorters.hpp"
using namespace std::chrono; using namespace std::chrono;
using namespace std; using namespace std;
vector<int> dataprovider() { return vector<int>{9, 8, 1, 2, 7, 3, 6, 4, 5}; }
vector<int> dataprovider(){
return vector<int> {9,8,1,2,7,3,6,4,5};
}
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
mytimermanager.add_timer(
0,
[&mysession](){return mysession->step();},
milliseconds(250));
}
int main() { int main() {
// create data vector<SortInput> inputs;
auto myinput = make_shared<SortInput>("arr1", dataprovider()); SortInput myinput("arr1", dataprovider());
auto myinput2 = make_shared<SortInput>("arr2", dataprovider()); SortInput myinput2("arr2", dataprovider());
auto myviewsession = make_unique<ViewSession>(); inputs.push_back(myinput);
auto mytimermanager = make_unique<TimerManager>(); inputs.push_back(myinput2);
// create app with initialized dependencies App myapp(inputs);
App myapp(std::move(myviewsession), std::move(mytimermanager));
// create binding
shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(asort);
SortBinding mysortbinding(myinput, asort, myinvoker);
Timer timer(0, myinvoker, chrono::milliseconds(250));
myapp.timermanager->add_timer(std::move(timer));
// add data and binding
myapp.add_sort(myinput, mysortbinding);
// actually start the app
myapp.run(); myapp.run();
} }
+2 -2
View File
@@ -4,11 +4,11 @@
#include <cstddef> #include <cstddef>
class Steppable { class Steppable {
public: public:
virtual ~Steppable() = default; virtual ~Steppable() = default;
bool is_finished = 0; bool is_finished = 0;
size_t stepcount = 0; size_t stepcount = 0;
virtual void step() = 0; virtual bool step() = 0;
}; };
#endif #endif
+17 -16
View File
@@ -1,37 +1,38 @@
#include <chrono> #include <chrono>
#include <iostream> #include <functional>
#include <memory> #include <memory>
#include <string>
#include "Invoker.hpp" #include <utility>
#include "timer.hpp" #include "timer.hpp"
using namespace std::chrono; using namespace std::chrono;
using namespace std; using namespace std;
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval) Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
: triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {} :triggeramount(triggeramount), timeraction(myaction), triggerinterval(triggerinterval) {}
void Timer::add_time(milliseconds deltatime) { void Timer::add_time(milliseconds deltatime) {
accumulator += deltatime; accumulator += deltatime;
if (accumulator >= triggerinterval) { if (accumulator >= triggerinterval) {
accumulator -= triggerinterval; accumulator -= triggerinterval;
cout << "request in timer\n"; timeraction();
if (auto sp = invoker.lock()) {
sp->request();
}
triggercount++; triggercount++;
if (triggeramount != 0 && triggercount >= triggeramount) { if (!isinfinite && triggercount >= triggeramount){
isexpired = true; isexpired=true;
} }
} }
} };
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); } void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
Timer mynewtimer(triggeramount, myaction, triggerinterval);
timers.push_back(mynewtimer);
};
void TimerManager::update_timers() { void TimerManager::update_timers() {
steady_clock::time_point currenttime = steady_clock::now(); steady_clock::time_point currenttime =
milliseconds deltatime = duration_cast<milliseconds>(currenttime - previoustime); steady_clock::now();
for (Timer &mytimer : timers) { milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
for(Timer &mytimer: timers){
mytimer.add_time(deltatime); mytimer.add_time(deltatime);
} }
previoustime = currenttime; previoustime = currenttime;
+24 -19
View File
@@ -1,40 +1,45 @@
#ifndef TIMER_HPP #ifndef TIMER_HPP
#define TIMER_HPP #define TIMER_HPP
#include <chrono>
#include <cstddef> #include <cstddef>
#include <ctime> #include <ctime>
#include <functional>
#include <memory> #include <memory>
#include <string>
#include <chrono>
#include <vector> #include <vector>
#include "Invoker.hpp" using namespace std;
// check if the timer is still valid, check if enough time has passed and if so, actuate action //check if the timer is still valid, check if enough time has passed and if so, actuate action
class Timer {
public:
size_t triggeramount;
bool isexpired = false;
size_t triggercount = 0;
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval); class Timer {
public:
size_t triggeramount;
bool isexpired = false;
bool isinfinite = false;
size_t triggercount = 0;
function<bool()> timeraction;
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void add_time(std::chrono::milliseconds deltatime); void add_time(std::chrono::milliseconds deltatime);
private: private:
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); // default to 1 sec std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
}; };
// CRUD timers and give them dt on update
//CRUD timers and give them dt on update
class TimerManager { class TimerManager {
public: public:
std::vector<Timer> timers; vector<Timer> timers;
TimerManager() { previoustime = std::chrono::steady_clock::now(); } TimerManager(){previoustime = std::chrono::steady_clock::now();}
// void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval); void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void add_timer(Timer timer);
void delete_timer(); void delete_timer();
void update_timers(); void update_timers();
private: private:
std::chrono::steady_clock::time_point previoustime; std::chrono::steady_clock::time_point previoustime;
}; };