timermanager now creates its own timers
This commit is contained in:
+18
-7
@@ -49,11 +49,10 @@ vector<int> dataprovider(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
|
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
|
||||||
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
mytimermanager.add_timer(
|
||||||
"sort1", 0,
|
0,
|
||||||
[&mysession](){return mysession->step();},
|
[&mysession](){return mysession->step();},
|
||||||
milliseconds(250));
|
milliseconds(250));
|
||||||
mytimermanager.add_timer(std::move(mytimer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//list of inputs, manually assign them to sorts
|
//list of inputs, manually assign them to sorts
|
||||||
@@ -63,14 +62,27 @@ struct SortSession {
|
|||||||
TimerManager SortTimer;
|
TimerManager SortTimer;
|
||||||
|
|
||||||
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
|
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
|
||||||
setupsorttimers(SortTimer, sorts);
|
//setupsorttimers(SortTimer, sorts);
|
||||||
|
add_session(sorts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void add_session(unique_ptr<Steppable> &mysteppable){
|
||||||
|
SortTimer.add_timer(0,
|
||||||
|
[&mysteppable]() {return mysteppable->step();},
|
||||||
|
milliseconds(250));
|
||||||
|
}
|
||||||
void update_sorts(){
|
void update_sorts(){
|
||||||
SortTimer.update_timers();
|
SortTimer.update_timers();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ViewSession{
|
||||||
|
sf::RenderWindow mywindow;
|
||||||
|
sf::Font myfont;
|
||||||
|
sf::Text text;
|
||||||
|
};
|
||||||
|
|
||||||
struct App{
|
struct App{
|
||||||
sf::RenderWindow mywindow;
|
sf::RenderWindow mywindow;
|
||||||
sf::Font myfont;
|
sf::Font myfont;
|
||||||
@@ -118,7 +130,6 @@ struct App{
|
|||||||
displaySortInput(input);
|
displaySortInput(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mywindow.display();
|
mywindow.display();
|
||||||
this_thread::sleep_for(milliseconds(250));
|
this_thread::sleep_for(milliseconds(250));
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -8,8 +8,8 @@
|
|||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Timer::Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval)
|
Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
|
||||||
: name(name), triggeramount(triggercount), 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;
|
||||||
@@ -23,16 +23,17 @@ void Timer::add_time(milliseconds deltatime) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
|
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
||||||
timers.push_back(std::move(newtimer));
|
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
||||||
|
timers.push_back(mynewtimer);
|
||||||
};
|
};
|
||||||
|
|
||||||
void TimerManager::update_timers() {
|
void TimerManager::update_timers() {
|
||||||
steady_clock::time_point currenttime =
|
steady_clock::time_point currenttime =
|
||||||
steady_clock::now();
|
steady_clock::now();
|
||||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
||||||
for(unique_ptr<Timer> &mytimer: timers){
|
for(Timer &mytimer: timers){
|
||||||
mytimer->add_time(deltatime);
|
mytimer.add_time(deltatime);
|
||||||
}
|
}
|
||||||
previoustime = currenttime;
|
previoustime = currenttime;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-7
@@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
using namespace std;
|
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:
|
||||||
string name;
|
|
||||||
size_t triggeramount;
|
size_t triggeramount;
|
||||||
|
|
||||||
bool isexpired = false;
|
bool isexpired = false;
|
||||||
@@ -21,22 +21,21 @@ public:
|
|||||||
size_t triggercount = 0;
|
size_t triggercount = 0;
|
||||||
function<bool()> timeraction;
|
function<bool()> timeraction;
|
||||||
|
|
||||||
Timer(string name, int triggercount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void add_time(std::chrono::milliseconds deltatime);
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||||
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//CRUD timers and give them dt on update
|
//CRUD timers and give them dt on update
|
||||||
class TimerManager {
|
class TimerManager {
|
||||||
public:
|
public:
|
||||||
vector<unique_ptr<Timer>> timers;
|
vector<Timer> timers;
|
||||||
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
||||||
void add_timer(unique_ptr<Timer> newtimer);
|
void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void delete_timer();
|
void delete_timer();
|
||||||
void update_timers();
|
void update_timers();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user