clang formatting
This commit is contained in:
+2
-2
@@ -4,10 +4,10 @@
|
||||
#include "steppable.hpp"
|
||||
#include <memory>
|
||||
class Invoker {
|
||||
public:
|
||||
public:
|
||||
std::weak_ptr<Steppable> target;
|
||||
|
||||
Invoker(std::shared_ptr<Steppable> t) { target = t;}
|
||||
Invoker(std::shared_ptr<Steppable> t) { target = t; }
|
||||
void request() {
|
||||
if (auto sp = target.lock()) {
|
||||
sp->step();
|
||||
|
||||
+6
-7
@@ -10,6 +10,7 @@ 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]);
|
||||
}
|
||||
@@ -27,15 +28,14 @@ string arraytostr(shared_ptr<SortInput> input) {
|
||||
}
|
||||
|
||||
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
||||
: viewsession(std::move(myviewsession)),
|
||||
timermanager(std::move(mytimermanager)) {}
|
||||
: viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {}
|
||||
|
||||
//just pushes to App for now
|
||||
// just pushes to App for now
|
||||
void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) {
|
||||
cout << "adding sort " << myinput->name << "\n";
|
||||
//insert data
|
||||
// insert data
|
||||
inputs.push_back(myinput);
|
||||
//move the sort into app
|
||||
// move the sort into app
|
||||
sortbindings.push_back(mysortbinding);
|
||||
}
|
||||
|
||||
@@ -43,8 +43,7 @@ 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();
|
||||
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
|
||||
}
|
||||
|
||||
// draw
|
||||
|
||||
+5
-6
@@ -1,20 +1,19 @@
|
||||
#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 "../timer.hpp"
|
||||
#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{
|
||||
// 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;
|
||||
|
||||
+4
-4
@@ -2,17 +2,17 @@
|
||||
|
||||
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);
|
||||
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;
|
||||
if (!font.openFromFile(filename)){
|
||||
if (!font.openFromFile(filename)) {
|
||||
fprintf(stderr, "failed to load font");
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
+3
-3
@@ -8,10 +8,10 @@
|
||||
|
||||
#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
|
||||
struct ViewSession{
|
||||
// sfml/render
|
||||
struct ViewSession {
|
||||
sf::RenderWindow mywindow;
|
||||
sf::Font myfont;
|
||||
sf::Text text;
|
||||
|
||||
+14
-16
@@ -1,38 +1,36 @@
|
||||
#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) {};
|
||||
|
||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input)
|
||||
: data(input) {};
|
||||
|
||||
void SteppedBsort:: step(){
|
||||
void SteppedBsort::step() {
|
||||
cout << "step in SteppedBsort\n";
|
||||
if(is_finished) {
|
||||
if (is_finished) {
|
||||
printf("finished");
|
||||
}
|
||||
if(data->values[j]>data->values[j+1])
|
||||
{
|
||||
swap(data->values[j], data->values[j+1]);
|
||||
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;
|
||||
if (j >= data->values.size() - 1 - i) {
|
||||
j = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i==data->values.size()-1){
|
||||
is_finished=true;
|
||||
if (i == data->values.size() - 1) {
|
||||
is_finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
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)){}
|
||||
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
|
||||
// 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);
|
||||
|
||||
+9
-11
@@ -9,27 +9,26 @@
|
||||
#include "Invoker.hpp"
|
||||
#include "steppable.hpp"
|
||||
|
||||
//ground truth domain data
|
||||
// ground truth domain data
|
||||
struct SortInput {
|
||||
std::string name;
|
||||
std::vector<int> values;
|
||||
|
||||
SortInput(std::string name, std::vector<int> data)
|
||||
: name(name), values(data) {}
|
||||
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {}
|
||||
};
|
||||
|
||||
//ro rendering of data
|
||||
// ro rendering of data
|
||||
struct SortView {
|
||||
std::shared_ptr<const SortInput> values;
|
||||
size_t currenttarget = 0;
|
||||
int x,y;
|
||||
int x, y;
|
||||
int color;
|
||||
SortView(std::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:
|
||||
// mutates the domain data
|
||||
class SteppedBsort : public Steppable {
|
||||
public:
|
||||
std::shared_ptr<SortInput> data;
|
||||
size_t stepcount = 0;
|
||||
size_t i = 0;
|
||||
@@ -41,13 +40,12 @@ public:
|
||||
void step();
|
||||
};
|
||||
|
||||
//working unit
|
||||
// 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);
|
||||
|
||||
+9
-13
@@ -4,13 +4,13 @@
|
||||
#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"
|
||||
@@ -21,22 +21,20 @@
|
||||
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
|
||||
// create data
|
||||
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
||||
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 app with initialized dependencies
|
||||
App myapp(std::move(myviewsession), std::move(mytimermanager));
|
||||
|
||||
//create binding
|
||||
// create binding
|
||||
shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
|
||||
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(asort);
|
||||
SortBinding mysortbinding(myinput, asort, myinvoker);
|
||||
@@ -44,11 +42,9 @@ int main() {
|
||||
Timer timer(0, myinvoker, chrono::milliseconds(250));
|
||||
myapp.timermanager->add_timer(std::move(timer));
|
||||
|
||||
//add data and binding
|
||||
// add data and binding
|
||||
myapp.add_sort(myinput, mysortbinding);
|
||||
|
||||
|
||||
|
||||
//actually start the app
|
||||
// actually start the app
|
||||
myapp.run();
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
#include <cstddef>
|
||||
|
||||
class Steppable {
|
||||
public:
|
||||
public:
|
||||
virtual ~Steppable() = default;
|
||||
bool is_finished = 0;
|
||||
size_t stepcount = 0;
|
||||
|
||||
+8
-13
@@ -2,16 +2,14 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "Invoker.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||
:triggeramount(triggeramount)
|
||||
, invoker(invoker)
|
||||
, triggerinterval(triggerinterval){}
|
||||
: triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
@@ -22,21 +20,18 @@ void Timer::add_time(milliseconds deltatime) {
|
||||
sp->request();
|
||||
}
|
||||
triggercount++;
|
||||
if (triggeramount != 0 && triggercount >= triggeramount){
|
||||
isexpired=true;
|
||||
if (triggeramount != 0 && triggercount >= triggeramount) {
|
||||
isexpired = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TimerManager::add_timer(Timer timer){
|
||||
timers.push_back(std::move(timer));
|
||||
}
|
||||
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); }
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
steady_clock::time_point currenttime =
|
||||
steady_clock::now();
|
||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
||||
for(Timer &mytimer: timers){
|
||||
steady_clock::time_point currenttime = steady_clock::now();
|
||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime - previoustime);
|
||||
for (Timer &mytimer : timers) {
|
||||
mytimer.add_time(deltatime);
|
||||
}
|
||||
previoustime = currenttime;
|
||||
|
||||
+11
-12
@@ -1,41 +1,40 @@
|
||||
#ifndef TIMER_HPP
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#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
|
||||
// check if the timer is still valid, check if enough time has passed and if so, actuate action
|
||||
class Timer {
|
||||
public:
|
||||
public:
|
||||
size_t triggeramount;
|
||||
bool isexpired = false;
|
||||
size_t triggercount = 0;
|
||||
std::weak_ptr<Invoker> invoker; //if no longer exists, then should clean up timer
|
||||
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
|
||||
|
||||
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||
void add_time(std::chrono::milliseconds deltatime);
|
||||
|
||||
private:
|
||||
private:
|
||||
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 {
|
||||
public:
|
||||
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);
|
||||
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
||||
// 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();
|
||||
|
||||
private:
|
||||
private:
|
||||
std::chrono::steady_clock::time_point previoustime;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user