Compare commits
2 Commits
d98c29ecbb
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a8894a51cd | |||
| c330f13bb8 |
+8
-8
@@ -4,15 +4,15 @@
|
|||||||
#include "steppable.hpp"
|
#include "steppable.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
class Invoker {
|
class Invoker {
|
||||||
public:
|
public:
|
||||||
std::weak_ptr<Steppable> target;
|
std::weak_ptr<Steppable> target;
|
||||||
|
|
||||||
Invoker(std::shared_ptr<Steppable> t) { target = t;}
|
Invoker(std::shared_ptr<Steppable> t) { target = t; }
|
||||||
void request() {
|
void request() {
|
||||||
if (auto sp = target.lock()) {
|
if (auto sp = target.lock()) {
|
||||||
sp->step();
|
sp->step();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+38
-57
@@ -9,74 +9,55 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool displaySortInput(shared_ptr<SortInput> input) {
|
bool displaySortInput(shared_ptr<SortInput> input) {
|
||||||
cout << "name: " + input->name + " values: ";
|
cout << "name: " + input->name + " values: ";
|
||||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
|
||||||
printf("%d | ", input->values[i]);
|
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||||
}
|
printf("%d | ", input->values[i]);
|
||||||
printf("\n");
|
}
|
||||||
return false;
|
printf("\n");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string arraytostr(shared_ptr<SortInput> input) {
|
string arraytostr(shared_ptr<SortInput> input) {
|
||||||
string myoutput;
|
string myoutput;
|
||||||
ostringstream stringrep;
|
ostringstream stringrep;
|
||||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||||
stringrep << input->values[i] << " | ";
|
stringrep << input->values[i] << " | ";
|
||||||
}
|
}
|
||||||
return stringrep.str();
|
return stringrep.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
||||||
: viewsession(std::move(myviewsession)),
|
: viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {}
|
||||||
timermanager(std::move(mytimermanager)) {}
|
|
||||||
|
|
||||||
//connects data with the correct sort
|
// just pushes to App for now
|
||||||
shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) {
|
||||||
if (sorttype == "bsort") {
|
cout << "adding sort " << myinput->name << "\n";
|
||||||
return make_shared<SteppedBsort>(input);
|
// insert data
|
||||||
}
|
inputs.push_back(myinput);
|
||||||
return nullptr;
|
// move the sort into app
|
||||||
}
|
sortbindings.push_back(mysortbinding);
|
||||||
|
|
||||||
//creates a Sortbinding, actually does too much
|
|
||||||
void App::add_sort(shared_ptr<SortInput> myinput, string sorttype) {
|
|
||||||
cout << "adding sort " << sorttype << "\n";
|
|
||||||
//insert data
|
|
||||||
inputs.push_back(myinput);
|
|
||||||
//connect data with logic into a sort
|
|
||||||
std::shared_ptr<Steppable> mysorter = make_sort(myinput, sorttype);
|
|
||||||
|
|
||||||
//connect the sort with its invoker
|
|
||||||
unique_ptr<Invoker> myinvoker = make_unique<Invoker>(mysorter);
|
|
||||||
|
|
||||||
//move the sort into app
|
|
||||||
sortbindings.push_back(SortBinding{myinput, std::move(mysorter)});
|
|
||||||
|
|
||||||
//create the timer with the invoker and add it to app
|
|
||||||
Timer timer(0, std::move(myinvoker), chrono::milliseconds(250));
|
|
||||||
timermanager->add_timer(std::move(timer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::run() {
|
void App::run() {
|
||||||
// set up ui
|
// set up ui
|
||||||
while (viewsession->mywindow.isOpen()) {
|
while (viewsession->mywindow.isOpen()) {
|
||||||
while (const optional event = viewsession->mywindow.pollEvent()) {
|
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||||
if (event->is<sf::Event::Closed>())
|
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
|
||||||
viewsession->mywindow.close();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// draw
|
// draw
|
||||||
viewsession->mywindow.clear(sf::Color::Black);
|
viewsession->mywindow.clear(sf::Color::Black);
|
||||||
viewsession->mywindow.draw(viewsession->text);
|
viewsession->mywindow.draw(viewsession->text);
|
||||||
viewsession->text.setString(arraytostr(inputs[0]));
|
viewsession->text.setString(arraytostr(inputs[0]));
|
||||||
|
|
||||||
for (shared_ptr<SortInput> input : inputs) {
|
for (shared_ptr<SortInput> input : inputs) {
|
||||||
displaySortInput(input);
|
displaySortInput(input);
|
||||||
}
|
}
|
||||||
viewsession->mywindow.display();
|
viewsession->mywindow.display();
|
||||||
|
|
||||||
// update logic
|
// update logic
|
||||||
timermanager->update_timers();
|
timermanager->update_timers();
|
||||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-13
@@ -1,29 +1,28 @@
|
|||||||
#ifndef APP_HPP
|
#ifndef APP_HPP
|
||||||
#define APP_HPP
|
#define APP_HPP
|
||||||
|
|
||||||
|
|
||||||
#include <SFML/Graphics/Font.hpp>
|
#include <SFML/Graphics/Font.hpp>
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../timer.hpp"
|
|
||||||
#include "../sorters.hpp"
|
|
||||||
#include "../sessions.hpp"
|
#include "../sessions.hpp"
|
||||||
|
#include "../sorters.hpp"
|
||||||
|
#include "../timer.hpp"
|
||||||
|
|
||||||
//should connect the different parts
|
// should connect the different parts
|
||||||
//for now data, mutation, and rendering
|
// for now data, mutation, and rendering
|
||||||
struct App{
|
struct App {
|
||||||
std::vector<std::shared_ptr<SortInput>> inputs;
|
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||||
std::vector<SortBinding> sortbindings;
|
std::vector<SortBinding> sortbindings;
|
||||||
std::unique_ptr<ViewSession> viewsession;
|
std::unique_ptr<ViewSession> viewsession;
|
||||||
std::unique_ptr<TimerManager> timermanager;
|
std::unique_ptr<TimerManager> timermanager;
|
||||||
|
|
||||||
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
||||||
|
|
||||||
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+13
-13
@@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
ViewSession::ViewSession(): mywindow(sf::VideoMode({800,600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont)
|
ViewSession::ViewSession()
|
||||||
{
|
: mywindow(sf::VideoMode({800, 600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) {
|
||||||
text.setFont(myfont);
|
text.setFont(myfont);
|
||||||
text.setString("Hello world");
|
text.setString("Hello world");
|
||||||
text.setCharacterSize(24);
|
text.setCharacterSize(24);
|
||||||
text.setFillColor(sf::Color::Red);
|
text.setFillColor(sf::Color::Red);
|
||||||
};
|
};
|
||||||
|
|
||||||
sf::Font ViewSession::load_font(const std::string &filename){
|
sf::Font ViewSession::load_font(const std::string &filename) {
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
if (!font.openFromFile(filename)){
|
if (!font.openFromFile(filename)) {
|
||||||
fprintf(stderr, "failed to load font");
|
fprintf(stderr, "failed to load font");
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -8,16 +8,16 @@
|
|||||||
|
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
|
||||||
//owns and manages the sorters that mutate data, but does not own the data itself
|
// owns and manages the sorters that mutate data, but does not own the data itself
|
||||||
|
|
||||||
//sfml/render
|
// sfml/render
|
||||||
struct ViewSession{
|
struct ViewSession {
|
||||||
sf::RenderWindow mywindow;
|
sf::RenderWindow mywindow;
|
||||||
sf::Font myfont;
|
sf::Font myfont;
|
||||||
sf::Text text;
|
sf::Text text;
|
||||||
|
|
||||||
ViewSession();
|
ViewSession();
|
||||||
sf::Font load_font(const std::string &filename);
|
sf::Font load_font(const std::string &filename);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+30
-20
@@ -1,29 +1,39 @@
|
|||||||
|
#include "sorters.hpp"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include "sorters.hpp"
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input) : data(input) {};
|
||||||
|
|
||||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
|
void SteppedBsort::step() {
|
||||||
|
cout << "step in SteppedBsort\n";
|
||||||
|
if (is_finished) {
|
||||||
|
printf("finished");
|
||||||
|
}
|
||||||
|
if (data->values[j] > data->values[j + 1]) {
|
||||||
|
swap(data->values[j], data->values[j + 1]);
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
if (j >= data->values.size() - 1 - i) {
|
||||||
|
j = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
void SteppedBsort:: step(){
|
if (i == data->values.size() - 1) {
|
||||||
cout << "step in SteppedBsort\n";
|
is_finished = true;
|
||||||
if(is_finished) {
|
}
|
||||||
printf("finished");
|
}
|
||||||
}
|
|
||||||
if(data->values[j]>data->values[j+1])
|
SortBinding::SortBinding(
|
||||||
{
|
std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker)
|
||||||
swap(data->values[j], data->values[j+1]);
|
: input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)) {}
|
||||||
}
|
|
||||||
j++;
|
// connects data with the correct sort
|
||||||
if(j>=data->values.size()-1 -i){
|
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
||||||
j=0;
|
if (sorttype == "bsort") {
|
||||||
i++;
|
return make_shared<SteppedBsort>(input);
|
||||||
}
|
}
|
||||||
|
return nullptr;
|
||||||
if (i==data->values.size()-1){
|
|
||||||
is_finished=true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-25
@@ -9,42 +9,45 @@
|
|||||||
#include "Invoker.hpp"
|
#include "Invoker.hpp"
|
||||||
#include "steppable.hpp"
|
#include "steppable.hpp"
|
||||||
|
|
||||||
//ground truth domain data
|
// ground truth domain data
|
||||||
struct SortInput {
|
struct SortInput {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::vector<int> values;
|
std::vector<int> values;
|
||||||
|
|
||||||
SortInput(std::string name, std::vector<int> data)
|
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {}
|
||||||
: name(name), values(data) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//ro rendering of data
|
// ro rendering of data
|
||||||
struct SortView {
|
struct SortView {
|
||||||
std::shared_ptr<const SortInput> values;
|
std::shared_ptr<const SortInput> values;
|
||||||
size_t currenttarget = 0;
|
size_t currenttarget = 0;
|
||||||
int x,y;
|
int x, y;
|
||||||
int color;
|
int color;
|
||||||
SortView(std::shared_ptr<const SortInput> in): values(in) {}
|
SortView(std::shared_ptr<const SortInput> in) : values(in) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
//mutates the domain data
|
// mutates the domain data
|
||||||
class SteppedBsort: public Steppable {
|
class SteppedBsort : public Steppable {
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<SortInput> data;
|
std::shared_ptr<SortInput> data;
|
||||||
size_t stepcount = 0;
|
size_t stepcount = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
bool is_finished = false;
|
bool is_finished = false;
|
||||||
|
|
||||||
SteppedBsort(std::shared_ptr<SortInput> data);
|
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||||
|
|
||||||
void step();
|
void step();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// working unit
|
||||||
struct SortBinding {
|
struct SortBinding {
|
||||||
std::shared_ptr<SortInput> input;
|
std::shared_ptr<SortInput> input;
|
||||||
std::shared_ptr<Steppable> sort;
|
std::shared_ptr<Steppable> sort;
|
||||||
//SortBinding(std::shared_ptr<SortInput> input, std::unique_ptr<Steppable> sort, std::unique_ptr<Invoker> invoker);
|
std::shared_ptr<Invoker> invoker;
|
||||||
|
SortBinding(std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+24
-16
@@ -1,42 +1,50 @@
|
|||||||
// for now juist CLI, print an arr, then print each it
|
// for now juist CLI, print an arr, then print each it
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
#include <SFML/Graphics/Color.hpp>
|
#include <SFML/Graphics/Color.hpp>
|
||||||
#include <SFML/Graphics/Font.hpp>
|
#include <SFML/Graphics/Font.hpp>
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <SFML/Window/Window.hpp>
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Window/Window.hpp>
|
||||||
|
|
||||||
#include "core/app.hpp"
|
#include "core/app.hpp"
|
||||||
#include "sessions.hpp"
|
#include "sessions.hpp"
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
#include "steppable.hpp"
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
vector<int> dataprovider(){
|
vector<int> dataprovider() { return vector<int>{9, 8, 1, 2, 7, 3, 6, 4, 5}; }
|
||||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
//create data
|
// create data
|
||||||
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
||||||
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
||||||
|
|
||||||
auto myviewsession = make_unique<ViewSession>();
|
auto myviewsession = make_unique<ViewSession>();
|
||||||
auto mytimermanager = make_unique<TimerManager>();
|
auto mytimermanager = make_unique<TimerManager>();
|
||||||
|
|
||||||
//create app with initialized dependencies
|
// create app with initialized dependencies
|
||||||
App myapp(std::move(myviewsession),std::move(mytimermanager));
|
App myapp(std::move(myviewsession), std::move(mytimermanager));
|
||||||
|
|
||||||
//add and connect data
|
// create binding
|
||||||
myapp.add_sort(myinput, "bsort");
|
shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
|
||||||
|
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(asort);
|
||||||
|
SortBinding mysortbinding(myinput, asort, myinvoker);
|
||||||
|
|
||||||
//actually start the app
|
Timer timer(0, myinvoker, chrono::milliseconds(250));
|
||||||
myapp.run();
|
myapp.timermanager->add_timer(std::move(timer));
|
||||||
|
|
||||||
|
// add data and binding
|
||||||
|
myapp.add_sort(myinput, mysortbinding);
|
||||||
|
|
||||||
|
// actually start the app
|
||||||
|
myapp.run();
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -4,11 +4,11 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
class Steppable {
|
class Steppable {
|
||||||
public:
|
public:
|
||||||
virtual ~Steppable() = default;
|
virtual ~Steppable() = default;
|
||||||
bool is_finished = 0;
|
bool is_finished = 0;
|
||||||
size_t stepcount = 0;
|
size_t stepcount = 0;
|
||||||
virtual void step() = 0;
|
virtual void step() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+22
-25
@@ -2,40 +2,37 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "timer.hpp"
|
|
||||||
#include "Invoker.hpp"
|
#include "Invoker.hpp"
|
||||||
|
#include "timer.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval)
|
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||||
:triggeramount(triggeramount)
|
: triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {}
|
||||||
, invoker(std::move(invoker))
|
|
||||||
, triggerinterval(triggerinterval){}
|
|
||||||
|
|
||||||
void Timer::add_time(milliseconds deltatime) {
|
void Timer::add_time(milliseconds deltatime) {
|
||||||
accumulator += deltatime;
|
accumulator += deltatime;
|
||||||
if (accumulator >= triggerinterval) {
|
if (accumulator >= triggerinterval) {
|
||||||
accumulator -= triggerinterval;
|
accumulator -= triggerinterval;
|
||||||
cout << "request in timer\n";
|
cout << "request in timer\n";
|
||||||
invoker->request();
|
if (auto sp = invoker.lock()) {
|
||||||
triggercount++;
|
sp->request();
|
||||||
if (triggeramount != 0 && triggercount >= triggeramount){
|
}
|
||||||
isexpired=true;
|
triggercount++;
|
||||||
}
|
if (triggeramount != 0 && triggercount >= triggeramount) {
|
||||||
}
|
isexpired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerManager::add_timer(Timer timer){
|
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); }
|
||||||
timers.push_back(std::move(timer));
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (Timer &mytimer : timers) {
|
||||||
for(Timer &mytimer: timers){
|
mytimer.add_time(deltatime);
|
||||||
mytimer.add_time(deltatime);
|
}
|
||||||
}
|
previoustime = currenttime;
|
||||||
previoustime = currenttime;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-23
@@ -1,42 +1,41 @@
|
|||||||
#ifndef TIMER_HPP
|
#ifndef TIMER_HPP
|
||||||
#define TIMER_HPP
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <chrono>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Invoker.hpp"
|
#include "Invoker.hpp"
|
||||||
//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:
|
||||||
size_t triggeramount;
|
size_t triggeramount;
|
||||||
bool isexpired = false;
|
bool isexpired = false;
|
||||||
size_t triggercount = 0;
|
size_t triggercount = 0;
|
||||||
std::unique_ptr<Invoker> invoker;
|
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
|
||||||
|
|
||||||
Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, 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:
|
||||||
std::vector<Timer> timers;
|
std::vector<Timer> timers;
|
||||||
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
||||||
//void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
// void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void add_timer(Timer timer);
|
void add_timer(Timer timer);
|
||||||
void delete_timer();
|
void delete_timer();
|
||||||
void update_timers();
|
void update_timers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::steady_clock::time_point previoustime;
|
std::chrono::steady_clock::time_point previoustime;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user