diff --git a/CMakeLists.txt b/CMakeLists.txt index 6edaaa0..9e18f3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 4.0) project(cppout LANGUAGES CXX) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + file(GLOB SOURCE_FILES "src/*.cpp") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/src/sorters.cpp b/src/sorters.cpp new file mode 100644 index 0000000..e4a0d6d --- /dev/null +++ b/src/sorters.cpp @@ -0,0 +1,29 @@ +#include +#include +#include "sorters.hpp" +using namespace std; + + +SteppedBsort::SteppedBsort(SortInput &input): data(input) {}; + +bool SteppedBsort:: step(){ + if(is_finished==true) { + printf("finished"); + return true; + } + if(data.values[j]>data.values[j+1]) + { + swap(data.values[j], data.values[j+1]); + } + j++; + if(j>=data.values.size()-1 -i){ + j=0; + i++; + } + + if (i==data.values.size()-1){ + is_finished=true; + } + + return false; +} diff --git a/src/sorters.hpp b/src/sorters.hpp index 13aadf8..d4b7031 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -1,12 +1,42 @@ +#ifndef _SORTERS_ +#define _SORTERS_ + +#include +#include #include #include +#include "timer.hpp" -struct SortingData { - std::string name; - int target; - std::vector data; +using namespace std; - SortingData(std::string name, std::vector data) - : name(name), data(data) {} - +struct SortInput { + string name; + vector values; + + SortInput(string name, vector data) + : name(name), values(data) {} }; + +struct SortView { + shared_ptr values; + size_t currenttarget = 0; + int x,y; + int color; + SortView(shared_ptr in): values(in) {} +}; + + +class SteppedBsort: Steppable { +public: + SortInput &data; + int stepcount = 0; + size_t i = 0; + size_t j = 0; + bool is_finished; + + SteppedBsort(SortInput &data); + + bool step(); +}; + +#endif diff --git a/src/sortui.cpp b/src/sortui.cpp index de9800c..6ad602c 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -9,8 +9,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -22,40 +24,43 @@ using namespace std::chrono; using namespace std; -void displaynumbers(SortingData in_arr) { - for (size_t i = 0; i < in_arr.data.size(); ++i) { - printf("array:%d ", in_arr.data[i]); +bool displaynumbers(vector input) { + for (size_t i = 0; i < input.size(); ++i) { + printf("array:%d ", input[i]); } printf("\n"); + return false; } -void stepbubblesort(vector &data, size_t step) { - data[step%data.size()]++; +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}; + return vector {9,8,1,2,7,3,6,4,5}; } -void setuptimers(TimerManager &mytimermanager, SortingData &mysd){ - +void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){ unique_ptr mytimer = make_unique( "sort1", 0, - [&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); }, - milliseconds(1000)); - unique_ptr mydisplaytimer = make_unique("display", 0, - [&mysd](Timer &t){displaynumbers(mysd);}, - milliseconds(1000)); - mytimermanager.add_timer(move(mytimer)); - mytimermanager.add_timer(move(mydisplaytimer)); + [&mysession](){return mysession.step();}, + milliseconds(250)); + mytimermanager.add_timer(std::move(mytimer)); } + struct App{ sf::RenderWindow mywindow; sf::Font myfont; sf::Text text; TimerManager mytimermanager; - SortingData mysortdata; + SortInput input; + SteppedBsort mystb; sf::Font load_font(const string &filename) { sf::Font font; @@ -65,18 +70,20 @@ struct App{ } return font; } - - App():mywindow(sf::VideoMode({800,600}), "mywindow"), - myfont(load_font("assets/NS-R.ttf")), - text(myfont), - mysortdata("arr1", dataprovider()) { + + App(SortInput input): + mywindow(sf::VideoMode({800,600}), "mywindow"), + myfont(load_font("assets/NS-R.ttf")), + text(myfont), + input(std::move(input)), + mystb(this->input) + { text.setFont(myfont); text.setString("Hello world"); text.setCharacterSize(24); text.setFillColor(sf::Color::Red); - setuptimers(mytimermanager, mysortdata); - + setupsorttimers(mytimermanager, mystb); } void run(){ @@ -85,17 +92,22 @@ struct App{ if (event->is()) mywindow.close(); } - + mywindow.clear(sf::Color::Black); mywindow.draw(text); + text.setString(arraytostr(input)); mytimermanager.update_timers(); + + displaynumbers(input.values); + mywindow.display(); - this_thread::sleep_for(milliseconds(1000)); + this_thread::sleep_for(milliseconds(250)); } } }; int main() { - App myapp; + SortInput myinput("arr1", dataprovider()); + App myapp(myinput); myapp.run(); } diff --git a/src/timer.cpp b/src/timer.cpp index 15b7896..35fa659 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -4,30 +4,37 @@ #include #include #include "timer.hpp" -Timer::Timer(std::string name, int triggercount, std::function myaction, std::chrono::milliseconds triggerinterval) + +using namespace std::chrono; +using namespace std; + +Timer::Timer(string name, int triggercount, function myaction, milliseconds triggerinterval) : name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {} -void Timer::add_time(std::chrono::milliseconds deltatime) { +void Timer::add_time(milliseconds deltatime) { accumulator += deltatime; if (accumulator >= triggerinterval) { accumulator -= triggerinterval; - timeraction(*this); + if (timeraction()==true){ + isdone = true; + }; triggercounter++; - if (!isinfinite && triggercounter >= triggeramount){ - isexpired=true; - } + if (!isinfinite && triggercounter >= triggeramount){ + isexpired=true; + } } }; -void TimerManager::add_timer(std::unique_ptr newtimer) { +void TimerManager::add_timer(unique_ptr newtimer) { timers.push_back(std::move(newtimer)); }; void TimerManager::update_timers() { - std::chrono::steady_clock::time_point currenttime = - std::chrono::steady_clock::now(); - std::chrono::milliseconds deltatime = std::chrono::duration_cast(currenttime-previoustime); - for(std::unique_ptr &mytimer: timers){ - mytimer->add_time(deltatime); + steady_clock::time_point currenttime = + steady_clock::now(); + milliseconds deltatime = duration_cast(currenttime-previoustime); + for(unique_ptr &mytimer: timers){ + mytimer->add_time(deltatime); } + previoustime = currenttime; } diff --git a/src/timer.hpp b/src/timer.hpp index a295815..0e9b944 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,3 +1,5 @@ +#ifndef _TIMERS_ +#define _TIMERS_ #include #include @@ -7,34 +9,48 @@ #include #include -class Timer { -public: - std::string name; - std::size_t triggeramount; +using namespace std::chrono; +using namespace std; + +class Steppable { +public: + bool is_finished = 0; + virtual bool step() = 0; +}; + +//check if the timer is valid, check if enough time has passed and if so, actuate +class Timer { +public: + string name; + size_t triggeramount; + + bool isexpired = false; + bool isdone = false; + bool isinfinite = false; + size_t triggercounter = 0; + function timeraction; + + Timer(string name, int triggercount, function myaction, milliseconds triggerinterval); + void add_time(milliseconds deltatime); + - bool isexpired; - bool isinfinite; - std::size_t triggercounter = 0; - - Timer(std::string name, int triggercount, std::function myaction, std::chrono::milliseconds triggerinterval); - void add_time(std::chrono::milliseconds deltatime); - std::function timeraction; private: - std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); - std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec + milliseconds accumulator = milliseconds(0); + milliseconds triggerinterval = milliseconds(1000); //default to 1 sec }; +//CRUD timers and give them dt class TimerManager { +public: + vector> timers; + TimerManager() { previoustime = steady_clock::now(); } + void add_timer(unique_ptr newtimer); + void delete_timer(); + void update_timers(); - -public: - std::vector> timers; - TimerManager() { previoustime = std::chrono::steady_clock::now(); } - void add_timer(std::unique_ptr newtimer); - void delete_timer(); - void update_timers(); - -private: - std::chrono::steady_clock::time_point previoustime; +private: + steady_clock::time_point previoustime; }; + +#endif