| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // calls callbackfunc on specified interval based on accumulated deltatime
- #include "timer.hpp"
- #include <algorithm>
- #include <cstddef>
- #include <functional>
- #include <memory>
- #include <ncurses.h>
- #include <string>
- // infinite timer
- timer::timer(std::string name_input, size_t interval_input,
- std::function<void(const timer &)> callback_input)
- : time_interval(interval_input), name(name_input), is_infinite(true),
- callbackfunc(callback_input) {}
- // limited timer
- timer::timer(std::string name, size_t interval_input, int triggeramount,
- std::function<void(const timer &)> callback)
- : triggeramount(triggeramount), time_interval(interval_input), name(name),
- callbackfunc(callback) {}
- // callbackfunc once enough time has accumulated
- void timer::updatetime(double DeltaTime) {
- accumulator += DeltaTime;
- if (!is_expired) {
- if (accumulator >= time_interval) {
- callbackfunc(*this);
- accumulator -= time_interval;
- triggercounter++;
- if (!is_infinite && triggercounter >= triggeramount) {
- is_expired = true;
- }
- }
- }
- }
- void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
- timers.push_back(std::move(input_timer));
- }
- void timermanager::updatetimers(double dtime) {
- for (auto &mytimer : timers) {
- mytimer->updatetime(dtime);
- mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
- }
- timers.erase(std::remove_if(timers.begin(), timers.end(),
- [](auto &timer) { return timer->is_expired; }),
- timers.end());
- // implement delete timers
- refresh();
- }
|