app now just coordinates the components initialized in main

reduced responsibilities of constructors
started using more pointers.
sessions through app rather than app itself.
mostly split view away from app (still hard coded)
haven't decoupled sorts from timers as their actuator yet
This commit is contained in:
2026-08-15 03:04:32 +02:00
parent afdf73ca2e
commit 6e71aeebb4
9 changed files with 123 additions and 98 deletions
+25 -40
View File
@@ -1,69 +1,54 @@
#include "./app.hpp"
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <iostream> #include <iostream>
#include <memory>
#include <thread> #include <thread>
#include "./app.hpp"
bool displaySortInput(SortInput input) { bool displaySortInput(shared_ptr<SortInput> input) {
cout << "name: " + input.name + " values: "; cout << "name: " + input->name + " values: ";
for (size_t i = 0; i < input.values.size(); ++i) { for (size_t i = 0; i < input->values.size(); ++i) {
printf("%d | ",input.values[i]); printf("%d | ",input->values[i]);
} }
printf("\n"); printf("\n");
return false; return false;
} }
string arraytostr(SortInput input) { string arraytostr(shared_ptr<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(vector<SortInput> inputs): App::App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession)
mywindow(sf::VideoMode({800,600}), "mywindow"), : inputs(std::move(inputs))
myfont(load_font("assets/NS-R.ttf")), , mysortsession(std::move(mysortsession))
text(myfont), , myviewsession(std::move(myviewsession))
inputs(std::move(inputs)), {
mysortsession(this->inputs) }
{
text.setFont(myfont);
text.setString("Hello world");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
}
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(){ void App::run(){
//set up ui //set up ui
while (mywindow.isOpen()){ while (myviewsession->mywindow.isOpen()){
while (const optional event = mywindow.pollEvent()){ while (const optional event = myviewsession->mywindow.pollEvent()){
if (event->is<sf::Event::Closed>()) if (event->is<sf::Event::Closed>())
mywindow.close(); myviewsession->mywindow.close();
} }
mywindow.clear(sf::Color::Black); myviewsession->mywindow.clear(sf::Color::Black);
mywindow.draw(text); myviewsession->mywindow.draw(myviewsession->text);
text.setString(arraytostr(inputs[0])); myviewsession->text.setString(arraytostr(inputs[0]));
mysortsession.update_sorts(); mysortsession->update_sorts();
for (SortInput input : inputs) { for (shared_ptr<SortInput> input : inputs) {
displaySortInput(input); displaySortInput(input);
} }
mywindow.display(); myviewsession->mywindow.display();
this_thread::sleep_for(std::chrono::milliseconds(250)); this_thread::sleep_for(std::chrono::milliseconds(250));
} }
} }
+8 -8
View File
@@ -5,19 +5,19 @@
#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 "../sorters.hpp" #include "../sorters.hpp"
#include "../sessions.hpp" #include "../sessions.hpp"
//should connect the different parts
//for now data, mutation, and rendering
struct App{ struct App{
sf::RenderWindow mywindow; vector<shared_ptr<SortInput>> inputs;
sf::Font myfont; unique_ptr<SortSession> mysortsession;
sf::Text text; unique_ptr<ViewSession> myviewsession;
vector<SortInput> inputs;
SortSession mysortsession;
sf::Font load_font(const std::string &filename); App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession);
App(vector<SortInput> inputs);
void run(); void run();
}; };
+20
View File
@@ -0,0 +1,20 @@
#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;
}
+20 -10
View File
@@ -1,26 +1,32 @@
#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"
#include "timer.hpp"
//owns and manages the sorters that mutate data, but does not own the data itself
//steppables/sorts are currently updated by timermanager(should be decoupled)
struct SortSession { struct SortSession {
vector<SortInput> &inputs; vector<unique_ptr<Steppable>> sortsessions;
unique_ptr<Steppable> sorts;
TimerManager SortTimer; TimerManager SortTimer;
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) { SortSession(){
add_session(sorts);
} }
//connects data with a sorter and controls it through a timer
void add_session(unique_ptr<Steppable> &mysteppable){ void add_session(shared_ptr<SortInput> input, string sorttype){
if(sorttype == "bsort"){
sortsessions.push_back(make_unique<SteppedBsort>(input));
}
auto prs = sortsessions.back().get();
SortTimer.add_timer(0, SortTimer.add_timer(0,
[&mysteppable]() {return mysteppable->step();}, [prs]() {return prs->step();},
std::chrono::milliseconds(250)); std::chrono::milliseconds(250));
} }
void update_sorts(){ void update_sorts(){
@@ -28,10 +34,14 @@ struct SortSession {
} }
}; };
//sfml/render
struct ViewSession{ 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
+6 -5
View File
@@ -1,27 +1,28 @@
#include <cstdio> #include <cstdio>
#include <memory>
#include <utility> #include <utility>
#include "sorters.hpp" #include "sorters.hpp"
using namespace std; using namespace std;
SteppedBsort::SteppedBsort(SortInput &input): data(input) {}; SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
bool SteppedBsort:: step(){ bool SteppedBsort:: step(){
if(is_finished==true) { if(is_finished==true) {
printf("finished"); printf("finished");
return true; 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;
} }
+5 -3
View File
@@ -10,6 +10,7 @@
using namespace std; using namespace std;
//ground truth domain data
struct SortInput { struct SortInput {
string name; string name;
vector<int> values; vector<int> values;
@@ -18,6 +19,7 @@ struct SortInput {
: name(name), values(data) {} : name(name), values(data) {}
}; };
//ro rendering of data
struct SortView { struct SortView {
shared_ptr<const SortInput> values; shared_ptr<const SortInput> values;
size_t currenttarget = 0; size_t currenttarget = 0;
@@ -26,16 +28,16 @@ struct SortView {
SortView(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:
SortInput &data; shared_ptr<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(SortInput &data); SteppedBsort(shared_ptr<SortInput> data);
bool step(); bool step();
}; };
+14 -4
View File
@@ -1,4 +1,5 @@
// for now juist CLI, print an arr, then print each it // for now juist CLI, print an arr, then print each it
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -11,6 +12,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "core/app.hpp" #include "core/app.hpp"
#include "sessions.hpp"
#include "sorters.hpp" #include "sorters.hpp"
using namespace std::chrono; using namespace std::chrono;
@@ -22,13 +24,21 @@ vector<int> dataprovider(){
} }
int main() { int main() {
vector<SortInput> inputs; //create data
SortInput myinput("arr1", dataprovider()); vector<shared_ptr<SortInput>> inputs;
SortInput myinput2("arr2", dataprovider()); auto myinput = make_shared<SortInput>("arr1", dataprovider());
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
inputs.push_back(myinput); inputs.push_back(myinput);
inputs.push_back(myinput2); inputs.push_back(myinput2);
App myapp(inputs); //connect data with sorter
auto myssession = make_unique<SortSession>();
myssession->add_session(myinput, "bsort");
auto myviewsession = make_unique<ViewSession>();
//create app with initialized dependencies
App myapp(std::move(inputs), std::move(myssession), std::move(myviewsession));
myapp.run(); myapp.run();
} }
+5 -4
View File
@@ -1,15 +1,16 @@
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <memory>
#include <string>
#include <utility> #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, function<bool()> myaction, milliseconds triggerinterval) Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
:triggeramount(triggeramount), timeraction(myaction), triggerinterval(triggerinterval) {} :triggeramount(triggeramount)
, timeraction(myaction)
, triggerinterval(triggerinterval) {}
void Timer::add_time(milliseconds deltatime) { void Timer::add_time(milliseconds deltatime) {
accumulator += deltatime; accumulator += deltatime;
@@ -25,7 +26,7 @@ void Timer::add_time(milliseconds deltatime) {
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) { void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
Timer mynewtimer(triggeramount, myaction, triggerinterval); Timer mynewtimer(triggeramount, myaction, triggerinterval);
timers.push_back(mynewtimer); timers.push_back(std::move(mynewtimer));
}; };
void TimerManager::update_timers() { void TimerManager::update_timers() {
+4 -8
View File
@@ -4,14 +4,10 @@
#include <cstddef> #include <cstddef>
#include <ctime> #include <ctime>
#include <functional> #include <functional>
#include <memory>
#include <string>
#include <chrono> #include <chrono>
#include <vector> #include <vector>
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 { class Timer {
public: public:
size_t triggeramount; size_t triggeramount;
@@ -19,9 +15,9 @@ public:
bool isexpired = false; bool isexpired = false;
bool isinfinite = false; bool isinfinite = false;
size_t triggercount = 0; size_t triggercount = 0;
function<bool()> timeraction; std::function<bool()> timeraction;
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval); Timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void add_time(std::chrono::milliseconds deltatime); void add_time(std::chrono::milliseconds deltatime);
private: private:
@@ -33,9 +29,9 @@ private:
//CRUD timers and give them dt on update //CRUD timers and give them dt on update
class TimerManager { class TimerManager {
public: public:
vector<Timer> timers; std::vector<Timer> timers;
TimerManager(){previoustime = std::chrono::steady_clock::now();} TimerManager(){previoustime = std::chrono::steady_clock::now();}
void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval); void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void delete_timer(); void delete_timer();
void update_timers(); void update_timers();