Compare commits
7 Commits
6e71aeebb4
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a8894a51cd | |||
| c330f13bb8 | |||
| d98c29ecbb | |||
| 273ff07a00 | |||
| 54faab2a28 | |||
| 2185d2858a | |||
| b5309278cf |
@@ -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
|
||||
+24
-15
@@ -1,12 +1,16 @@
|
||||
#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]);
|
||||
}
|
||||
@@ -23,32 +27,37 @@ string arraytostr(shared_ptr<SortInput> input) {
|
||||
return stringrep.str();
|
||||
}
|
||||
|
||||
App::App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession)
|
||||
: inputs(std::move(inputs))
|
||||
, mysortsession(std::move(mysortsession))
|
||||
, myviewsession(std::move(myviewsession))
|
||||
{
|
||||
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 (myviewsession->mywindow.isOpen()){
|
||||
while (const optional event = myviewsession->mywindow.pollEvent()){
|
||||
if (event->is<sf::Event::Closed>())
|
||||
myviewsession->mywindow.close();
|
||||
while (viewsession->mywindow.isOpen()) {
|
||||
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
|
||||
}
|
||||
|
||||
myviewsession->mywindow.clear(sf::Color::Black);
|
||||
myviewsession->mywindow.draw(myviewsession->text);
|
||||
myviewsession->text.setString(arraytostr(inputs[0]));
|
||||
|
||||
mysortsession->update_sorts();
|
||||
// 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();
|
||||
|
||||
myviewsession->mywindow.display();
|
||||
// update logic
|
||||
timermanager->update_timers();
|
||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
}
|
||||
}
|
||||
|
||||
+9
-6
@@ -1,24 +1,27 @@
|
||||
#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 "../sorters.hpp"
|
||||
#include "../sessions.hpp"
|
||||
#include "../sorters.hpp"
|
||||
#include "../timer.hpp"
|
||||
|
||||
// should connect the different parts
|
||||
// for now data, mutation, and rendering
|
||||
struct App {
|
||||
vector<shared_ptr<SortInput>> inputs;
|
||||
unique_ptr<SortSession> mysortsession;
|
||||
unique_ptr<ViewSession> myviewsession;
|
||||
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||
std::vector<SortBinding> sortbindings;
|
||||
std::unique_ptr<ViewSession> viewsession;
|
||||
std::unique_ptr<TimerManager> timermanager;
|
||||
|
||||
App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession);
|
||||
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
||||
|
||||
void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
|
||||
void run();
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
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.setString("Hello world");
|
||||
text.setCharacterSize(24);
|
||||
|
||||
@@ -7,32 +7,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include "sorters.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
|
||||
// owns and manages the sorters that mutate data, but does not own the data itself
|
||||
//steppables/sorts are currently updated by timermanager(should be decoupled)
|
||||
struct SortSession {
|
||||
vector<unique_ptr<Steppable>> sortsessions;
|
||||
TimerManager SortTimer;
|
||||
|
||||
SortSession(){
|
||||
}
|
||||
|
||||
//connects data with a sorter and controls it through a timer
|
||||
void add_session(shared_ptr<SortInput> input, string sorttype){
|
||||
if(sorttype == "bsort"){
|
||||
sortsessions.push_back(make_unique<SteppedBsort>(input));
|
||||
}
|
||||
auto prs = sortsessions.back().get();
|
||||
SortTimer.add_timer(0,
|
||||
[prs]() {return prs->step();},
|
||||
std::chrono::milliseconds(250));
|
||||
}
|
||||
void update_sorts(){
|
||||
SortTimer.update_timers();
|
||||
}
|
||||
};
|
||||
|
||||
// sfml/render
|
||||
struct ViewSession {
|
||||
|
||||
+18
-9
@@ -1,19 +1,18 @@
|
||||
#include "sorters.hpp"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include "sorters.hpp"
|
||||
using namespace std;
|
||||
|
||||
|
||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input) : data(input) {};
|
||||
|
||||
bool SteppedBsort:: step(){
|
||||
if(is_finished==true) {
|
||||
void SteppedBsort::step() {
|
||||
cout << "step in SteppedBsort\n";
|
||||
if (is_finished) {
|
||||
printf("finished");
|
||||
return true;
|
||||
}
|
||||
if(data->values[j]>data->values[j+1])
|
||||
{
|
||||
if (data->values[j] > data->values[j + 1]) {
|
||||
swap(data->values[j], data->values[j + 1]);
|
||||
}
|
||||
j++;
|
||||
@@ -25,6 +24,16 @@ bool SteppedBsort:: step(){
|
||||
if (i == data->values.size() - 1) {
|
||||
is_finished = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
SortBinding::SortBinding(
|
||||
std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker)
|
||||
: input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)) {}
|
||||
|
||||
// connects data with the correct sort
|
||||
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
||||
if (sorttype == "bsort") {
|
||||
return make_shared<SteppedBsort>(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
+19
-11
@@ -6,40 +6,48 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Invoker.hpp"
|
||||
#include "steppable.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// ground truth domain data
|
||||
struct SortInput {
|
||||
string name;
|
||||
vector<int> values;
|
||||
std::string name;
|
||||
std::vector<int> values;
|
||||
|
||||
SortInput(string name, vector<int> data)
|
||||
: name(name), values(data) {}
|
||||
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {}
|
||||
};
|
||||
|
||||
// ro rendering of data
|
||||
struct SortView {
|
||||
shared_ptr<const SortInput> values;
|
||||
std::shared_ptr<const SortInput> values;
|
||||
size_t currenttarget = 0;
|
||||
int x, y;
|
||||
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:
|
||||
shared_ptr<SortInput> data;
|
||||
std::shared_ptr<SortInput> data;
|
||||
size_t stepcount = 0;
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
bool is_finished = false;
|
||||
|
||||
SteppedBsort(shared_ptr<SortInput> data);
|
||||
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||
|
||||
bool step();
|
||||
void 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
|
||||
|
||||
+21
-15
@@ -1,44 +1,50 @@
|
||||
// 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/Font.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#include <SFML/Window/Window.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Window/Window.hpp>
|
||||
|
||||
#include "core/app.hpp"
|
||||
#include "sessions.hpp"
|
||||
#include "sorters.hpp"
|
||||
#include "steppable.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
|
||||
vector<int> dataprovider(){
|
||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
||||
}
|
||||
vector<int> dataprovider() { return vector<int>{9, 8, 1, 2, 7, 3, 6, 4, 5}; }
|
||||
|
||||
int main() {
|
||||
// create data
|
||||
vector<shared_ptr<SortInput>> inputs;
|
||||
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
||||
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
||||
|
||||
inputs.push_back(myinput);
|
||||
inputs.push_back(myinput2);
|
||||
|
||||
//connect data with sorter
|
||||
auto myssession = make_unique<SortSession>();
|
||||
myssession->add_session(myinput, "bsort");
|
||||
|
||||
auto myviewsession = make_unique<ViewSession>();
|
||||
auto mytimermanager = make_unique<TimerManager>();
|
||||
|
||||
// create app with initialized dependencies
|
||||
App myapp(std::move(inputs), std::move(myssession), std::move(myviewsession));
|
||||
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();
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ public:
|
||||
virtual ~Steppable() = default;
|
||||
bool is_finished = 0;
|
||||
size_t stepcount = 0;
|
||||
virtual bool step() = 0;
|
||||
virtual void step() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+13
-15
@@ -1,37 +1,35 @@
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "Invoker.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
|
||||
:triggeramount(triggeramount)
|
||||
, timeraction(myaction)
|
||||
, triggerinterval(triggerinterval) {}
|
||||
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||
: triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
if (accumulator >= triggerinterval) {
|
||||
accumulator -= triggerinterval;
|
||||
timeraction();
|
||||
cout << "request in timer\n";
|
||||
if (auto sp = invoker.lock()) {
|
||||
sp->request();
|
||||
}
|
||||
triggercount++;
|
||||
if (!isinfinite && triggercount >= triggeramount){
|
||||
if (triggeramount != 0 && triggercount >= triggeramount) {
|
||||
isexpired = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
||||
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
||||
timers.push_back(std::move(mynewtimer));
|
||||
};
|
||||
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); }
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
steady_clock::time_point currenttime =
|
||||
steady_clock::now();
|
||||
steady_clock::time_point currenttime = steady_clock::now();
|
||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime - previoustime);
|
||||
for (Timer &mytimer : timers) {
|
||||
mytimer.add_time(deltatime);
|
||||
|
||||
+7
-8
@@ -1,23 +1,22 @@
|
||||
#ifndef TIMER_HPP
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "Invoker.hpp"
|
||||
// check if the timer is still valid, check if enough time has passed and if so, actuate action
|
||||
class Timer {
|
||||
public:
|
||||
size_t triggeramount;
|
||||
|
||||
bool isexpired = false;
|
||||
bool isinfinite = false;
|
||||
size_t triggercount = 0;
|
||||
std::function<bool()> timeraction;
|
||||
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
|
||||
|
||||
Timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||
void add_time(std::chrono::milliseconds deltatime);
|
||||
|
||||
private:
|
||||
@@ -25,13 +24,13 @@ private:
|
||||
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); // default to 1 sec
|
||||
};
|
||||
|
||||
|
||||
// CRUD timers and give them dt on update
|
||||
class TimerManager {
|
||||
public:
|
||||
std::vector<Timer> timers;
|
||||
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 delete_timer();
|
||||
void update_timers();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user