ic working example
setup with working timers and timermanager
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "timer.hpp"
|
||||
Timer::Timer(std::string name, int triggercount, std::function<void(Timer &t)> myaction, std::chrono::milliseconds triggerinterval)
|
||||
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(std::chrono::milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
if (accumulator >= triggerinterval) {
|
||||
accumulator -= triggerinterval;
|
||||
timeraction(*this);
|
||||
triggercounter++;
|
||||
if (!isinfinite && triggercounter >= triggeramount){
|
||||
isexpired=true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void TimerManager::add_timer(std::unique_ptr<Timer> 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<std::chrono::milliseconds>(currenttime-previoustime);
|
||||
for(std::unique_ptr<Timer> &mytimer: timers){
|
||||
mytimer->add_time(deltatime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user