Compare commits

..

2 Commits

Author SHA1 Message Date
lyrgb 2185d2858a App now uses separate add_session to insert data into it
improved the hub and spoke-ish model of App by making it more clear
that its components have specific responsibilities, which App then
glues together.

moved timermanager to App to seperate the invoker from receiver so we
can eventually add interactivity

also fixed the using namespace std error from a while ago
2026-08-20 01:47:38 +02:00
lyrgb b5309278cf rename myaction to callback 2026-08-20 00:45:52 +02:00
7 changed files with 59 additions and 60 deletions
+28 -14
View File
@@ -5,6 +5,8 @@
#include "./app.hpp"
using namespace std;
bool displaySortInput(shared_ptr<SortInput> input) {
cout << "name: " + input->name + " values: ";
for (size_t i = 0; i < input->values.size(); ++i) {
@@ -23,32 +25,44 @@ string arraytostr(shared_ptr<SortInput> input) {
return stringrep.str();
}
App::App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession)
: inputs(std::move(inputs))
, mysortsession(std::move(mysortsession))
, myviewsession(std::move(myviewsession))
{
App::App(unique_ptr<ViewSession> myviewsession)
: viewsession(std::move(myviewsession)) {}
//connects data with logic and invoker
void App::setup_session(shared_ptr<SortInput> myinput, string sorttype) {
cout << "adding sort " << sorttype << "\n";
//push data to app
inputs.push_back(myinput);
//connect data to logic
if (sorttype == "bsort") {
sortsession.add_session(myinput, sorttype);
}
//connect logic to timer (should not be hardcoded to timers)
auto prs = sortsession.sortsessions.back().get();
timermanager.add_timer(
0, [prs]() { return prs->step(); }, std::chrono::milliseconds(250));
}
void App::run() {
// set up ui
while (myviewsession->mywindow.isOpen()){
while (const optional event = myviewsession->mywindow.pollEvent()){
while (viewsession->mywindow.isOpen()) {
while (const optional event = viewsession->mywindow.pollEvent()) {
if (event->is<sf::Event::Closed>())
myviewsession->mywindow.close();
viewsession->mywindow.close();
}
myviewsession->mywindow.clear(sf::Color::Black);
myviewsession->mywindow.draw(myviewsession->text);
myviewsession->text.setString(arraytostr(inputs[0]));
mysortsession->update_sorts();
//draw
viewsession->mywindow.clear(sf::Color::Black);
viewsession->mywindow.draw(viewsession->text);
viewsession->text.setString(arraytostr(inputs[0]));
for (shared_ptr<SortInput> input : inputs) {
displaySortInput(input);
}
viewsession->mywindow.display();
myviewsession->mywindow.display();
//update logic
timermanager.update_timers();
this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
+7 -4
View File
@@ -7,18 +7,21 @@
#include <SFML/Graphics/Text.hpp>
#include <memory>
#include "../timer.hpp"
#include "../sorters.hpp"
#include "../sessions.hpp"
//should connect the different parts
//for now data, mutation, and rendering
struct App{
vector<shared_ptr<SortInput>> inputs;
unique_ptr<SortSession> mysortsession;
unique_ptr<ViewSession> myviewsession;
std::vector<std::shared_ptr<SortInput>> inputs;
SortSession sortsession;
std::unique_ptr<ViewSession> viewsession;
TimerManager timermanager;
App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession);
App(std::unique_ptr<ViewSession> myviewsession);
void setup_session(std::shared_ptr<SortInput> myinput, std::string sorttype);
void run();
};
+3 -14
View File
@@ -7,30 +7,19 @@
#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 {
vector<unique_ptr<Steppable>> sortsessions;
TimerManager SortTimer;
std::vector<std::unique_ptr<Steppable>> sortsessions;
SortSession(){
}
//connects data with a sorter and controls it through a timer
void add_session(shared_ptr<SortInput> input, string sorttype){
void add_session(std::shared_ptr<SortInput> input, std::string sorttype){
if(sorttype == "bsort"){
sortsessions.push_back(make_unique<SteppedBsort>(input));
}
auto prs = sortsessions.back().get();
SortTimer.add_timer(0,
[prs]() {return prs->step();},
std::chrono::milliseconds(250));
}
void update_sorts(){
SortTimer.update_timers();
};
}
};
+7 -9
View File
@@ -8,36 +8,34 @@
#include "steppable.hpp"
using namespace std;
//ground truth domain data
struct SortInput {
string name;
vector<int> values;
std::string name;
std::vector<int> values;
SortInput(string name, vector<int> data)
SortInput(std::string name, std::vector<int> data)
: name(name), values(data) {}
};
//ro rendering of data
struct SortView {
shared_ptr<const SortInput> values;
std::shared_ptr<const SortInput> values;
size_t currenttarget = 0;
int x,y;
int color;
SortView(shared_ptr<const SortInput> in): values(in) {}
SortView(std::shared_ptr<const SortInput> in): values(in) {}
};
//mutates the domain data
class SteppedBsort: public Steppable {
public:
shared_ptr<SortInput> data;
std::shared_ptr<SortInput> data;
size_t stepcount = 0;
size_t i = 0;
size_t j = 0;
bool is_finished = false;
SteppedBsort(shared_ptr<SortInput> data);
SteppedBsort(std::shared_ptr<SortInput> data);
bool step();
};
+4 -10
View File
@@ -18,27 +18,21 @@
using namespace std::chrono;
using namespace std;
vector<int> dataprovider(){
return vector<int> {9,8,1,2,7,3,6,4,5};
}
int main() {
//create data
vector<shared_ptr<SortInput>> inputs;
auto myinput = make_shared<SortInput>("arr1", dataprovider());
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
inputs.push_back(myinput);
inputs.push_back(myinput2);
//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));
App myapp(std::move(myviewsession));
//add and connect data
myapp.setup_session(myinput, "bsort");
//actually start the app
myapp.run();
}
+2 -2
View File
@@ -9,14 +9,14 @@ using namespace std;
Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
:triggeramount(triggeramount)
, timeraction(myaction)
, callback(myaction)
, triggerinterval(triggerinterval) {}
void Timer::add_time(milliseconds deltatime) {
accumulator += deltatime;
if (accumulator >= triggerinterval) {
accumulator -= triggerinterval;
timeraction();
callback();
triggercount++;
if (!isinfinite && triggercount >= triggeramount){
isexpired=true;
+3 -2
View File
@@ -11,13 +11,14 @@
class Timer {
public:
size_t triggeramount;
bool isexpired = false;
bool isinfinite = false;
size_t triggercount = 0;
std::function<bool()> timeraction;
std::function<bool()> callback;
Timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void add_time(std::chrono::milliseconds deltatime);
private: