From ad94f758fc36851767b085894d43044e80615faf Mon Sep 17 00:00:00 2001 From: my name Date: Wed, 12 Aug 2026 02:05:51 +0200 Subject: [PATCH] split app into seperate files to clean main --- src/core/app.cpp | 69 +++++++++++++++++++++++++++++++ src/core/app.hpp | 25 +++++++++++ src/sessions.hpp | 38 +++++++++++++++++ src/sortui.cpp | 105 +---------------------------------------------- 4 files changed, 133 insertions(+), 104 deletions(-) create mode 100644 src/core/app.cpp create mode 100644 src/core/app.hpp create mode 100644 src/sessions.hpp diff --git a/src/core/app.cpp b/src/core/app.cpp new file mode 100644 index 0000000..4aa11ec --- /dev/null +++ b/src/core/app.cpp @@ -0,0 +1,69 @@ +#include "./app.hpp" +#include +#include +#include + + +bool displaySortInput(SortInput input) { + cout << "name: " + input.name + " values: "; + for (size_t i = 0; i < input.values.size(); ++i) { + printf("%d | ",input.values[i]); + } + printf("\n"); + return false; +} + +string arraytostr(SortInput input) { + string myoutput; + ostringstream stringrep; + for (size_t i = 0; i < input.values.size(); ++i) { + stringrep << input.values[i] <<" | "; + } + return stringrep.str(); +} + +App::App(vector inputs): + mywindow(sf::VideoMode({800,600}), "mywindow"), + myfont(load_font("assets/NS-R.ttf")), + text(myfont), + 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(){ + //set up ui + while (mywindow.isOpen()){ + while (const optional event = mywindow.pollEvent()){ + if (event->is()) + mywindow.close(); + } + + mywindow.clear(sf::Color::Black); + mywindow.draw(text); + text.setString(arraytostr(inputs[0])); + + mysortsession.update_sorts(); + + for (SortInput input : inputs) { + displaySortInput(input); + } + + mywindow.display(); + this_thread::sleep_for(std::chrono::milliseconds(250)); + } +} diff --git a/src/core/app.hpp b/src/core/app.hpp new file mode 100644 index 0000000..3018334 --- /dev/null +++ b/src/core/app.hpp @@ -0,0 +1,25 @@ +#ifndef APP_HPP +#define APP_HPP + + +#include +#include +#include + +#include "../sorters.hpp" +#include "../sessions.hpp" +struct App{ + sf::RenderWindow mywindow; + sf::Font myfont; + sf::Text text; + vector inputs; + SortSession mysortsession; + + sf::Font load_font(const std::string &filename); + + App(vector inputs); + + void run(); +}; + +#endif diff --git a/src/sessions.hpp b/src/sessions.hpp new file mode 100644 index 0000000..ed6fd90 --- /dev/null +++ b/src/sessions.hpp @@ -0,0 +1,38 @@ +#ifndef SESSIONS_HPP +#define SESSIONS_HPP + +//list of inputs, manually assign them to sorts +#include "sorters.hpp" +#include "timer.hpp" +#include +#include +#include + +struct SortSession { + vector &inputs; + unique_ptr sorts; + TimerManager SortTimer; + + SortSession(vector &inputs): inputs(inputs), sorts(make_unique(inputs[0])) { + //setupsorttimers(SortTimer, sorts); + add_session(sorts); + } + + + void add_session(unique_ptr &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::Font myfont; + sf::Text text; +}; + +#endif diff --git a/src/sortui.cpp b/src/sortui.cpp index 332a014..3f1dce4 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -1,14 +1,7 @@ // for now juist CLI, print an arr, then print each it #include -#include -#include -#include -#include #include -#include #include -#include -#include #include #include @@ -19,6 +12,7 @@ #include #include +#include "core/app.hpp" #include "steppable.hpp" #include "timer.hpp" #include "sorters.hpp" @@ -26,23 +20,6 @@ using namespace std::chrono; using namespace std; -bool displaySortInput(SortInput input) { - cout << "name: " + input.name + " values: "; - for (size_t i = 0; i < input.values.size(); ++i) { - printf("%d | ",input.values[i]); - } - printf("\n"); - return false; -} - -string arraytostr(SortInput input) { - string myoutput; - ostringstream stringrep; - for (size_t i = 0; i < input.values.size(); ++i) { - stringrep << input.values[i] <<" | "; - } - return stringrep.str(); -} vector dataprovider(){ return vector {9,8,1,2,7,3,6,4,5}; @@ -55,86 +32,6 @@ void setupsorttimers(TimerManager &mytimermanager, unique_ptr &mysess milliseconds(250)); } -//list of inputs, manually assign them to sorts -struct SortSession { - vector &inputs; - unique_ptr sorts; - TimerManager SortTimer; - - SortSession(vector &inputs): inputs(inputs), sorts(make_unique(inputs[0])) { - //setupsorttimers(SortTimer, sorts); - add_session(sorts); - } - - - void add_session(unique_ptr &mysteppable){ - SortTimer.add_timer(0, - [&mysteppable]() {return mysteppable->step();}, - milliseconds(250)); - } - void update_sorts(){ - SortTimer.update_timers(); - } -}; - -struct ViewSession{ - sf::RenderWindow mywindow; - sf::Font myfont; - sf::Text text; -}; - -struct App{ - sf::RenderWindow mywindow; - sf::Font myfont; - sf::Text text; - vector inputs; - SortSession mysortsession; - - sf::Font load_font(const string &filename) { - sf::Font font; - if (!font.openFromFile(filename)){ - fprintf(stderr, "failed to load font"); - std::exit(1); - } - return font; - } - - App(vector inputs): - mywindow(sf::VideoMode({800,600}), "mywindow"), - myfont(load_font("assets/NS-R.ttf")), - text(myfont), - inputs(std::move(inputs)), - mysortsession(this->inputs) - { - text.setFont(myfont); - text.setString("Hello world"); - text.setCharacterSize(24); - text.setFillColor(sf::Color::Red); - } - - void run(){ - //set up ui - while (mywindow.isOpen()){ - while (const optional event = mywindow.pollEvent()){ - if (event->is()) - mywindow.close(); - } - - mywindow.clear(sf::Color::Black); - mywindow.draw(text); - text.setString(arraytostr(inputs[0])); - - mysortsession.update_sorts(); - - for (SortInput input : inputs) { - displaySortInput(input); - } - - mywindow.display(); - this_thread::sleep_for(milliseconds(250)); - } - } -}; int main() { vector inputs;