523dac1f4d
implemented reversing array and "animating" it
65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
// 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>
|
|
#include <vector>
|
|
|
|
// infinite timer
|
|
timer::timer(std::string name_input, size_t interval_input,
|
|
std::unique_ptr<timeraction> action_input)
|
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
|
action(std::move(action_input)) {}
|
|
|
|
// limited timer
|
|
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
|
std::unique_ptr<timeraction> action_input)
|
|
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
|
action(std::move(action_input)) {}
|
|
|
|
// callbackfunc once enough time has accumulated
|
|
void timer::updatetime(double DeltaTime) {
|
|
accumulator += DeltaTime;
|
|
if (!is_expired) {
|
|
if (accumulator >= time_interval) {
|
|
action->update(*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();
|
|
}
|
|
|
|
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
|
|
|
void swapaction::update(const timer &input_timer){
|
|
if (!iscomplete) {
|
|
int temp = input_vector[it_i];
|
|
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
|
input_vector[input_vector.size() - 1 - it_i] = temp;
|
|
it_i++;
|
|
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
|
}
|
|
}
|