timermanager now creates its own timers

This commit is contained in:
2026-08-12 01:43:50 +02:00
parent e6e170a42a
commit 8a60e45cd1
3 changed files with 34 additions and 23 deletions
+16 -5
View File
@@ -49,11 +49,10 @@ vector<int> dataprovider(){
}
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
unique_ptr<Timer> mytimer = make_unique<Timer>(
"sort1", 0,
mytimermanager.add_timer(
0,
[&mysession](){return mysession->step();},
milliseconds(250));
mytimermanager.add_timer(std::move(mytimer));
}
//list of inputs, manually assign them to sorts
@@ -63,14 +62,27 @@ struct SortSession {
TimerManager SortTimer;
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(){
SortTimer.update_timers();
}
};
struct ViewSession{
sf::RenderWindow mywindow;
sf::Font myfont;
sf::Text text;
};
struct App{
sf::RenderWindow mywindow;
sf::Font myfont;
@@ -118,7 +130,6 @@ struct App{
displaySortInput(input);
}
mywindow.display();
this_thread::sleep_for(milliseconds(250));
}
+7 -6
View File
@@ -8,8 +8,8 @@
using namespace std::chrono;
using namespace std;
Timer::Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval)
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
:triggeramount(triggeramount), timeraction(myaction), triggerinterval(triggerinterval) {}
void Timer::add_time(milliseconds deltatime) {
accumulator += deltatime;
@@ -23,16 +23,17 @@ void Timer::add_time(milliseconds deltatime) {
}
};
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
timers.push_back(std::move(newtimer));
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
Timer mynewtimer(triggeramount, myaction, triggerinterval);
timers.push_back(mynewtimer);
};
void TimerManager::update_timers() {
steady_clock::time_point currenttime =
steady_clock::now();
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
for(unique_ptr<Timer> &mytimer: timers){
mytimer->add_time(deltatime);
for(Timer &mytimer: timers){
mytimer.add_time(deltatime);
}
previoustime = currenttime;
}
+6 -7
View File
@@ -11,9 +11,9 @@
using namespace std;
//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;
@@ -21,22 +21,21 @@ public:
size_t triggercount = 0;
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);
private:
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 on update
class TimerManager {
public:
vector<unique_ptr<Timer>> timers;
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
void add_timer(unique_ptr<Timer> newtimer);
vector<Timer> timers;
TimerManager(){previoustime = std::chrono::steady_clock::now();}
void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
void delete_timer();
void update_timers();