diff --git a/src/sorters.hpp b/src/sorters.hpp index d4b7031..ab28c43 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -1,11 +1,12 @@ -#ifndef _SORTERS_ -#define _SORTERS_ +#ifndef SORTERS_HPP +#define SORTERS_HPP #include #include #include #include -#include "timer.hpp" + +#include "steppable.hpp" using namespace std; @@ -29,10 +30,10 @@ struct SortView { class SteppedBsort: Steppable { public: SortInput &data; - int stepcount = 0; + size_t stepcount = 0; size_t i = 0; size_t j = 0; - bool is_finished; + bool is_finished = false; SteppedBsort(SortInput &data); diff --git a/src/sortui.cpp b/src/sortui.cpp index 6ad602c..801218b 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -1,9 +1,4 @@ // for now juist CLI, print an arr, then print each it -#include -#include -#include -#include -#include #include #include #include @@ -15,6 +10,11 @@ #include #include +#include +#include +#include +#include +#include #include #include @@ -53,7 +53,6 @@ void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){ mytimermanager.add_timer(std::move(mytimer)); } - struct App{ sf::RenderWindow mywindow; sf::Font myfont; @@ -87,6 +86,7 @@ struct App{ } void run(){ + //set up ui while (mywindow.isOpen()){ while (const optional event = mywindow.pollEvent()){ if (event->is()) @@ -96,6 +96,7 @@ struct App{ mywindow.clear(sf::Color::Black); mywindow.draw(text); text.setString(arraytostr(input)); + mytimermanager.update_timers(); displaynumbers(input.values); diff --git a/src/steppable.hpp b/src/steppable.hpp new file mode 100644 index 0000000..8371fa9 --- /dev/null +++ b/src/steppable.hpp @@ -0,0 +1,14 @@ +#ifndef STEPPABLE_HPP +#define STEPPABLE_HPP + +#include + +class Steppable { +public: + virtual ~Steppable() = default; + bool is_finished = 0; + size_t stepcount = 0; + virtual bool step() = 0; +}; + +#endif diff --git a/src/timer.cpp b/src/timer.cpp index 35fa659..bdf4bd4 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -15,11 +15,9 @@ void Timer::add_time(milliseconds deltatime) { accumulator += deltatime; if (accumulator >= triggerinterval) { accumulator -= triggerinterval; - if (timeraction()==true){ - isdone = true; - }; - triggercounter++; - if (!isinfinite && triggercounter >= triggeramount){ + timeraction(); + triggercount++; + if (!isinfinite && triggercount >= triggeramount){ isexpired=true; } } diff --git a/src/timer.hpp b/src/timer.hpp index 0e9b944..1ef0b6b 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,5 +1,5 @@ -#ifndef _TIMERS_ -#define _TIMERS_ +#ifndef TIMER_HPP +#define TIMER_HPP #include #include @@ -9,48 +9,39 @@ #include #include -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 +//check if the timer is still valid, check if enough time has passed and if so, actuate action class Timer { public: string name; size_t triggeramount; bool isexpired = false; - bool isdone = false; bool isinfinite = false; - size_t triggercounter = 0; + size_t triggercount = 0; function timeraction; - Timer(string name, int triggercount, function myaction, milliseconds triggerinterval); - void add_time(milliseconds deltatime); + Timer(string name, int triggercount, function myaction, std::chrono::milliseconds triggerinterval); + void add_time(std::chrono::milliseconds deltatime); private: - milliseconds accumulator = milliseconds(0); - milliseconds triggerinterval = milliseconds(1000); //default to 1 sec + std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); + std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec }; -//CRUD timers and give them dt +//CRUD timers and give them dt on update class TimerManager { public: vector> timers; - TimerManager() { previoustime = steady_clock::now(); } + TimerManager() { previoustime = std::chrono::steady_clock::now(); } void add_timer(unique_ptr newtimer); void delete_timer(); void update_timers(); private: - steady_clock::time_point previoustime; + std::chrono::steady_clock::time_point previoustime; }; #endif