Compare commits
13 Commits
6f7bd9d964
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a8894a51cd | |||
| c330f13bb8 | |||
| d98c29ecbb | |||
| 273ff07a00 | |||
| 54faab2a28 | |||
| 2185d2858a | |||
| b5309278cf | |||
| 6e71aeebb4 | |||
| afdf73ca2e | |||
| 005d4282a9 | |||
| 8a60e45cd1 | |||
| e6e170a42a | |||
| d2d9ef6c70 |
+1
-1
@@ -3,7 +3,7 @@ project(cppout LANGUAGES CXX)
|
|||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
file(GLOB SOURCE_FILES "src/*.cpp")
|
file(GLOB SOURCE_FILES "src/*.cpp" "src/*/*.cpp")
|
||||||
|
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef INVOKER_HPP
|
||||||
|
#define INVOKER_HPP
|
||||||
|
|
||||||
|
#include "steppable.hpp"
|
||||||
|
#include <memory>
|
||||||
|
class Invoker {
|
||||||
|
public:
|
||||||
|
std::weak_ptr<Steppable> target;
|
||||||
|
|
||||||
|
Invoker(std::shared_ptr<Steppable> t) { target = t; }
|
||||||
|
void request() {
|
||||||
|
if (auto sp = target.lock()) {
|
||||||
|
sp->step();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "./app.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
bool displaySortInput(shared_ptr<SortInput> input) {
|
||||||
|
cout << "name: " + input->name + " values: ";
|
||||||
|
|
||||||
|
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||||
|
printf("%d | ", input->values[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string arraytostr(shared_ptr<SortInput> input) {
|
||||||
|
string myoutput;
|
||||||
|
ostringstream stringrep;
|
||||||
|
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||||
|
stringrep << input->values[i] << " | ";
|
||||||
|
}
|
||||||
|
return stringrep.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
||||||
|
: viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {}
|
||||||
|
|
||||||
|
// just pushes to App for now
|
||||||
|
void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) {
|
||||||
|
cout << "adding sort " << myinput->name << "\n";
|
||||||
|
// insert data
|
||||||
|
inputs.push_back(myinput);
|
||||||
|
// move the sort into app
|
||||||
|
sortbindings.push_back(mysortbinding);
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::run() {
|
||||||
|
// set up ui
|
||||||
|
while (viewsession->mywindow.isOpen()) {
|
||||||
|
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||||
|
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw
|
||||||
|
viewsession->mywindow.clear(sf::Color::Black);
|
||||||
|
viewsession->mywindow.draw(viewsession->text);
|
||||||
|
viewsession->text.setString(arraytostr(inputs[0]));
|
||||||
|
|
||||||
|
for (shared_ptr<SortInput> input : inputs) {
|
||||||
|
displaySortInput(input);
|
||||||
|
}
|
||||||
|
viewsession->mywindow.display();
|
||||||
|
|
||||||
|
// update logic
|
||||||
|
timermanager->update_timers();
|
||||||
|
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef APP_HPP
|
||||||
|
#define APP_HPP
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../sessions.hpp"
|
||||||
|
#include "../sorters.hpp"
|
||||||
|
#include "../timer.hpp"
|
||||||
|
|
||||||
|
// should connect the different parts
|
||||||
|
// for now data, mutation, and rendering
|
||||||
|
struct App {
|
||||||
|
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||||
|
std::vector<SortBinding> sortbindings;
|
||||||
|
std::unique_ptr<ViewSession> viewsession;
|
||||||
|
std::unique_ptr<TimerManager> timermanager;
|
||||||
|
|
||||||
|
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
||||||
|
|
||||||
|
void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
|
||||||
|
void run();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include "sessions.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
ViewSession::ViewSession()
|
||||||
|
: mywindow(sf::VideoMode({800, 600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) {
|
||||||
|
text.setFont(myfont);
|
||||||
|
text.setString("Hello world");
|
||||||
|
text.setCharacterSize(24);
|
||||||
|
text.setFillColor(sf::Color::Red);
|
||||||
|
};
|
||||||
|
|
||||||
|
sf::Font ViewSession::load_font(const std::string &filename) {
|
||||||
|
sf::Font font;
|
||||||
|
if (!font.openFromFile(filename)) {
|
||||||
|
fprintf(stderr, "failed to load font");
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
return font;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef SESSIONS_HPP
|
||||||
|
#define SESSIONS_HPP
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "sorters.hpp"
|
||||||
|
|
||||||
|
// owns and manages the sorters that mutate data, but does not own the data itself
|
||||||
|
|
||||||
|
// sfml/render
|
||||||
|
struct ViewSession {
|
||||||
|
sf::RenderWindow mywindow;
|
||||||
|
sf::Font myfont;
|
||||||
|
sf::Text text;
|
||||||
|
|
||||||
|
ViewSession();
|
||||||
|
sf::Font load_font(const std::string &filename);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+33
-23
@@ -1,29 +1,39 @@
|
|||||||
#include <cstdio>
|
|
||||||
#include <utility>
|
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input) : data(input) {};
|
||||||
|
|
||||||
SteppedBsort::SteppedBsort(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++;
|
||||||
|
}
|
||||||
|
|
||||||
bool SteppedBsort:: step(){
|
if (i == data->values.size() - 1) {
|
||||||
if(is_finished==true) {
|
is_finished = true;
|
||||||
printf("finished");
|
}
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-23
@@ -1,42 +1,53 @@
|
|||||||
#ifndef _SORTERS_
|
#ifndef SORTERS_HPP
|
||||||
#define _SORTERS_
|
#define SORTERS_HPP
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "timer.hpp"
|
|
||||||
|
|
||||||
using namespace std;
|
#include "Invoker.hpp"
|
||||||
|
#include "steppable.hpp"
|
||||||
|
|
||||||
|
// ground truth domain data
|
||||||
struct SortInput {
|
struct SortInput {
|
||||||
string name;
|
std::string name;
|
||||||
vector<int> values;
|
std::vector<int> values;
|
||||||
|
|
||||||
SortInput(string name, vector<int> data)
|
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {}
|
||||||
: name(name), values(data) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ro rendering of data
|
||||||
struct SortView {
|
struct SortView {
|
||||||
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(shared_ptr<const SortInput> in): values(in) {}
|
SortView(std::shared_ptr<const SortInput> in) : values(in) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// mutates the domain data
|
||||||
|
class SteppedBsort : public Steppable {
|
||||||
|
public:
|
||||||
|
std::shared_ptr<SortInput> data;
|
||||||
|
size_t stepcount = 0;
|
||||||
|
size_t i = 0;
|
||||||
|
size_t j = 0;
|
||||||
|
bool is_finished = false;
|
||||||
|
|
||||||
class SteppedBsort: Steppable {
|
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||||
public:
|
|
||||||
SortInput &data;
|
|
||||||
int stepcount = 0;
|
|
||||||
size_t i = 0;
|
|
||||||
size_t j = 0;
|
|
||||||
bool is_finished;
|
|
||||||
|
|
||||||
SteppedBsort(SortInput &data);
|
void step();
|
||||||
|
|
||||||
bool step();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// working unit
|
||||||
|
struct SortBinding {
|
||||||
|
std::shared_ptr<SortInput> input;
|
||||||
|
std::shared_ptr<Steppable> sort;
|
||||||
|
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
|
||||||
|
|||||||
+35
-98
@@ -1,113 +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 <string>
|
||||||
|
#include <utility>
|
||||||
|
#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 <chrono>
|
|
||||||
#include <cstddef>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <memory>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Window/Window.hpp>
|
||||||
|
|
||||||
#include "timer.hpp"
|
#include "core/app.hpp"
|
||||||
|
#include "sessions.hpp"
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
#include "steppable.hpp"
|
||||||
|
#include "timer.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool displaynumbers(vector<int> input) {
|
vector<int> dataprovider() { return vector<int>{9, 8, 1, 2, 7, 3, 6, 4, 5}; }
|
||||||
for (size_t i = 0; i < input.size(); ++i) {
|
|
||||||
printf("array:%d ", input[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string arraytostr(SortInput input) {
|
|
||||||
string myoutput;
|
|
||||||
ostringstream stringrep;
|
|
||||||
for (size_t i = 0; i < input.values.size(); ++i) {
|
|
||||||
stringrep << input.values[i] <<" | ";
|
|
||||||
}
|
|
||||||
return stringrep.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<int> dataprovider(){
|
|
||||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
|
||||||
}
|
|
||||||
|
|
||||||
void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){
|
|
||||||
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
|
||||||
"sort1", 0,
|
|
||||||
[&mysession](){return mysession.step();},
|
|
||||||
milliseconds(250));
|
|
||||||
mytimermanager.add_timer(std::move(mytimer));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct App{
|
|
||||||
sf::RenderWindow mywindow;
|
|
||||||
sf::Font myfont;
|
|
||||||
sf::Text text;
|
|
||||||
TimerManager mytimermanager;
|
|
||||||
SortInput input;
|
|
||||||
SteppedBsort mystb;
|
|
||||||
|
|
||||||
sf::Font load_font(const string &filename) {
|
|
||||||
sf::Font font;
|
|
||||||
if (!font.openFromFile(filename)){
|
|
||||||
fprintf(stderr, "failed to load font");
|
|
||||||
std::exit(1);
|
|
||||||
}
|
|
||||||
return font;
|
|
||||||
}
|
|
||||||
|
|
||||||
App(SortInput input):
|
|
||||||
mywindow(sf::VideoMode({800,600}), "mywindow"),
|
|
||||||
myfont(load_font("assets/NS-R.ttf")),
|
|
||||||
text(myfont),
|
|
||||||
input(std::move(input)),
|
|
||||||
mystb(this->input)
|
|
||||||
{
|
|
||||||
text.setFont(myfont);
|
|
||||||
text.setString("Hello world");
|
|
||||||
text.setCharacterSize(24);
|
|
||||||
text.setFillColor(sf::Color::Red);
|
|
||||||
|
|
||||||
setupsorttimers(mytimermanager, mystb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void run(){
|
|
||||||
while (mywindow.isOpen()){
|
|
||||||
while (const optional event = mywindow.pollEvent()){
|
|
||||||
if (event->is<sf::Event::Closed>())
|
|
||||||
mywindow.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
mywindow.clear(sf::Color::Black);
|
|
||||||
mywindow.draw(text);
|
|
||||||
text.setString(arraytostr(input));
|
|
||||||
mytimermanager.update_timers();
|
|
||||||
|
|
||||||
displaynumbers(input.values);
|
|
||||||
|
|
||||||
mywindow.display();
|
|
||||||
this_thread::sleep_for(milliseconds(250));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SortInput myinput("arr1", dataprovider());
|
// create data
|
||||||
App myapp(myinput);
|
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
||||||
myapp.run();
|
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
||||||
|
|
||||||
|
auto myviewsession = make_unique<ViewSession>();
|
||||||
|
auto mytimermanager = make_unique<TimerManager>();
|
||||||
|
|
||||||
|
// create app with initialized dependencies
|
||||||
|
App myapp(std::move(myviewsession), std::move(mytimermanager));
|
||||||
|
|
||||||
|
// create binding
|
||||||
|
shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
|
||||||
|
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(asort);
|
||||||
|
SortBinding mysortbinding(myinput, asort, myinvoker);
|
||||||
|
|
||||||
|
Timer timer(0, myinvoker, chrono::milliseconds(250));
|
||||||
|
myapp.timermanager->add_timer(std::move(timer));
|
||||||
|
|
||||||
|
// add data and binding
|
||||||
|
myapp.add_sort(myinput, mysortbinding);
|
||||||
|
|
||||||
|
// actually start the app
|
||||||
|
myapp.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef STEPPABLE_HPP
|
||||||
|
#define STEPPABLE_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
class Steppable {
|
||||||
|
public:
|
||||||
|
virtual ~Steppable() = default;
|
||||||
|
bool is_finished = 0;
|
||||||
|
size_t stepcount = 0;
|
||||||
|
virtual void step() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+20
-22
@@ -1,40 +1,38 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
#include "Invoker.hpp"
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Timer::Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval)
|
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||||
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
: triggeramount(triggeramount), invoker(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;
|
||||||
if (timeraction()==true){
|
cout << "request in timer\n";
|
||||||
isdone = true;
|
if (auto sp = invoker.lock()) {
|
||||||
};
|
sp->request();
|
||||||
triggercounter++;
|
}
|
||||||
if (!isinfinite && triggercounter >= triggeramount){
|
triggercount++;
|
||||||
isexpired=true;
|
if (triggeramount != 0 && triggercount >= triggeramount) {
|
||||||
}
|
isexpired = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
|
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); }
|
||||||
timers.push_back(std::move(newtimer));
|
|
||||||
};
|
|
||||||
|
|
||||||
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(unique_ptr<Timer> &mytimer: timers){
|
mytimer.add_time(deltatime);
|
||||||
mytimer->add_time(deltatime);
|
|
||||||
}
|
}
|
||||||
previoustime = currenttime;
|
previoustime = currenttime;
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-40
@@ -1,56 +1,41 @@
|
|||||||
#ifndef _TIMERS_
|
#ifndef TIMER_HPP
|
||||||
#define _TIMERS_
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
|
||||||
#include <chrono>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std::chrono;
|
#include "Invoker.hpp"
|
||||||
using namespace std;
|
// check if the timer is still valid, check if enough time has passed and if so, actuate action
|
||||||
|
|
||||||
class Steppable {
|
|
||||||
public:
|
|
||||||
bool is_finished = 0;
|
|
||||||
virtual bool step() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
//check if the timer is valid, check if enough time has passed and if so, actuate
|
|
||||||
class Timer {
|
class Timer {
|
||||||
public:
|
public:
|
||||||
string name;
|
size_t triggeramount;
|
||||||
size_t triggeramount;
|
bool isexpired = false;
|
||||||
|
size_t triggercount = 0;
|
||||||
|
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
|
||||||
|
|
||||||
bool isexpired = false;
|
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||||
bool isdone = false;
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
bool isinfinite = false;
|
|
||||||
size_t triggercounter = 0;
|
|
||||||
function<bool()> timeraction;
|
|
||||||
|
|
||||||
Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval);
|
private:
|
||||||
void add_time(milliseconds deltatime);
|
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||||
|
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); // default to 1 sec
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
milliseconds accumulator = milliseconds(0);
|
|
||||||
milliseconds triggerinterval = milliseconds(1000); //default to 1 sec
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//CRUD timers and give them dt
|
// CRUD timers and give them dt on update
|
||||||
class TimerManager {
|
class TimerManager {
|
||||||
public:
|
public:
|
||||||
vector<unique_ptr<Timer>> timers;
|
std::vector<Timer> timers;
|
||||||
TimerManager() { previoustime = steady_clock::now(); }
|
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
||||||
void add_timer(unique_ptr<Timer> newtimer);
|
// void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void delete_timer();
|
void add_timer(Timer timer);
|
||||||
void update_timers();
|
void delete_timer();
|
||||||
|
void update_timers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
steady_clock::time_point previoustime;
|
std::chrono::steady_clock::time_point previoustime;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user