app now just coordinates the components initialized in main
reduced responsibilities of constructors started using more pointers. sessions through app rather than app itself. mostly split view away from app (still hard coded) haven't decoupled sorts from timers as their actuator yet
This commit is contained in:
+22
-37
@@ -1,69 +1,54 @@
|
||||
#include "./app.hpp"
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include "./app.hpp"
|
||||
|
||||
bool displaySortInput(SortInput input) {
|
||||
cout << "name: " + input.name + " values: ";
|
||||
for (size_t i = 0; i < input.values.size(); ++i) {
|
||||
printf("%d | ",input.values[i]);
|
||||
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(SortInput input) {
|
||||
string arraytostr(shared_ptr<SortInput> input) {
|
||||
string myoutput;
|
||||
ostringstream stringrep;
|
||||
for (size_t i = 0; i < input.values.size(); ++i) {
|
||||
stringrep << input.values[i] <<" | ";
|
||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||
stringrep << input->values[i] <<" | ";
|
||||
}
|
||||
return stringrep.str();
|
||||
}
|
||||
|
||||
App::App(vector<SortInput> inputs):
|
||||
mywindow(sf::VideoMode({800,600}), "mywindow"),
|
||||
myfont(load_font("assets/NS-R.ttf")),
|
||||
text(myfont),
|
||||
inputs(std::move(inputs)),
|
||||
mysortsession(this->inputs)
|
||||
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))
|
||||
{
|
||||
text.setFont(myfont);
|
||||
text.setString("Hello world");
|
||||
text.setCharacterSize(24);
|
||||
text.setFillColor(sf::Color::Red);
|
||||
}
|
||||
|
||||
|
||||
sf::Font App::load_font(const std::string &filename){
|
||||
sf::Font font;
|
||||
if (!font.openFromFile(filename)){
|
||||
fprintf(stderr, "failed to load font");
|
||||
std::exit(1);
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
void App::run(){
|
||||
//set up ui
|
||||
while (mywindow.isOpen()){
|
||||
while (const optional event = mywindow.pollEvent()){
|
||||
while (myviewsession->mywindow.isOpen()){
|
||||
while (const optional event = myviewsession->mywindow.pollEvent()){
|
||||
if (event->is<sf::Event::Closed>())
|
||||
mywindow.close();
|
||||
myviewsession->mywindow.close();
|
||||
}
|
||||
|
||||
mywindow.clear(sf::Color::Black);
|
||||
mywindow.draw(text);
|
||||
text.setString(arraytostr(inputs[0]));
|
||||
myviewsession->mywindow.clear(sf::Color::Black);
|
||||
myviewsession->mywindow.draw(myviewsession->text);
|
||||
myviewsession->text.setString(arraytostr(inputs[0]));
|
||||
|
||||
mysortsession.update_sorts();
|
||||
mysortsession->update_sorts();
|
||||
|
||||
for (SortInput input : inputs) {
|
||||
for (shared_ptr<SortInput> input : inputs) {
|
||||
displaySortInput(input);
|
||||
}
|
||||
|
||||
mywindow.display();
|
||||
myviewsession->mywindow.display();
|
||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -5,19 +5,19 @@
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "../sorters.hpp"
|
||||
#include "../sessions.hpp"
|
||||
|
||||
//should connect the different parts
|
||||
//for now data, mutation, and rendering
|
||||
struct App{
|
||||
sf::RenderWindow mywindow;
|
||||
sf::Font myfont;
|
||||
sf::Text text;
|
||||
vector<SortInput> inputs;
|
||||
SortSession mysortsession;
|
||||
vector<shared_ptr<SortInput>> inputs;
|
||||
unique_ptr<SortSession> mysortsession;
|
||||
unique_ptr<ViewSession> myviewsession;
|
||||
|
||||
sf::Font load_font(const std::string &filename);
|
||||
|
||||
App(vector<SortInput> inputs);
|
||||
App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession);
|
||||
|
||||
void run();
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+20
-10
@@ -1,26 +1,32 @@
|
||||
#ifndef SESSIONS_HPP
|
||||
#define SESSIONS_HPP
|
||||
|
||||
//list of inputs, manually assign them to sorts
|
||||
#include "sorters.hpp"
|
||||
#include "timer.hpp"
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#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<SortInput> &inputs;
|
||||
unique_ptr<Steppable> sorts;
|
||||
vector<unique_ptr<Steppable>> sortsessions;
|
||||
TimerManager SortTimer;
|
||||
|
||||
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
|
||||
add_session(sorts);
|
||||
SortSession(){
|
||||
}
|
||||
|
||||
|
||||
void add_session(unique_ptr<Steppable> &mysteppable){
|
||||
//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,
|
||||
[&mysteppable]() {return mysteppable->step();},
|
||||
[prs]() {return prs->step();},
|
||||
std::chrono::milliseconds(250));
|
||||
}
|
||||
void update_sorts(){
|
||||
@@ -28,10 +34,14 @@ struct SortSession {
|
||||
}
|
||||
};
|
||||
|
||||
//sfml/render
|
||||
struct ViewSession{
|
||||
sf::RenderWindow mywindow;
|
||||
sf::Font myfont;
|
||||
sf::Text text;
|
||||
|
||||
ViewSession();
|
||||
sf::Font load_font(const std::string &filename);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+6
-5
@@ -1,27 +1,28 @@
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include "sorters.hpp"
|
||||
using namespace std;
|
||||
|
||||
|
||||
SteppedBsort::SteppedBsort(SortInput &input): data(input) {};
|
||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
|
||||
|
||||
bool SteppedBsort:: step(){
|
||||
if(is_finished==true) {
|
||||
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]);
|
||||
swap(data->values[j], data->values[j+1]);
|
||||
}
|
||||
j++;
|
||||
if(j>=data.values.size()-1 -i){
|
||||
if(j>=data->values.size()-1 -i){
|
||||
j=0;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i==data.values.size()-1){
|
||||
if (i==data->values.size()-1){
|
||||
is_finished=true;
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -10,6 +10,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
//ground truth domain data
|
||||
struct SortInput {
|
||||
string name;
|
||||
vector<int> values;
|
||||
@@ -18,6 +19,7 @@ struct SortInput {
|
||||
: name(name), values(data) {}
|
||||
};
|
||||
|
||||
//ro rendering of data
|
||||
struct SortView {
|
||||
shared_ptr<const SortInput> values;
|
||||
size_t currenttarget = 0;
|
||||
@@ -26,16 +28,16 @@ struct SortView {
|
||||
SortView(shared_ptr<const SortInput> in): values(in) {}
|
||||
};
|
||||
|
||||
|
||||
//mutates the domain data
|
||||
class SteppedBsort: public Steppable {
|
||||
public:
|
||||
SortInput &data;
|
||||
shared_ptr<SortInput> data;
|
||||
size_t stepcount = 0;
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
bool is_finished = false;
|
||||
|
||||
SteppedBsort(SortInput &data);
|
||||
SteppedBsort(shared_ptr<SortInput> data);
|
||||
|
||||
bool step();
|
||||
};
|
||||
|
||||
+14
-4
@@ -1,4 +1,5 @@
|
||||
// for now juist CLI, print an arr, then print each it
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -11,6 +12,7 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "core/app.hpp"
|
||||
#include "sessions.hpp"
|
||||
#include "sorters.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
@@ -22,13 +24,21 @@ vector<int> dataprovider(){
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<SortInput> inputs;
|
||||
SortInput myinput("arr1", dataprovider());
|
||||
SortInput myinput2("arr2", dataprovider());
|
||||
//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);
|
||||
|
||||
App myapp(inputs);
|
||||
//connect data with sorter
|
||||
auto myssession = make_unique<SortSession>();
|
||||
myssession->add_session(myinput, "bsort");
|
||||
|
||||
auto myviewsession = make_unique<ViewSession>();
|
||||
|
||||
//create app with initialized dependencies
|
||||
App myapp(std::move(inputs), std::move(myssession), std::move(myviewsession));
|
||||
myapp.run();
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,15 +1,16 @@
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#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) {}
|
||||
:triggeramount(triggeramount)
|
||||
, timeraction(myaction)
|
||||
, triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
@@ -25,7 +26,7 @@ void Timer::add_time(milliseconds deltatime) {
|
||||
|
||||
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
||||
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
||||
timers.push_back(mynewtimer);
|
||||
timers.push_back(std::move(mynewtimer));
|
||||
};
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
|
||||
+4
-8
@@ -4,14 +4,10 @@
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
//check if the timer is still valid, check if enough time has passed and if so, actuate action
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
size_t triggeramount;
|
||||
@@ -19,9 +15,9 @@ public:
|
||||
bool isexpired = false;
|
||||
bool isinfinite = false;
|
||||
size_t triggercount = 0;
|
||||
function<bool()> timeraction;
|
||||
std::function<bool()> timeraction;
|
||||
|
||||
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
Timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
void add_time(std::chrono::milliseconds deltatime);
|
||||
|
||||
private:
|
||||
@@ -33,9 +29,9 @@ private:
|
||||
//CRUD timers and give them dt on update
|
||||
class TimerManager {
|
||||
public:
|
||||
vector<Timer> timers;
|
||||
std::vector<Timer> timers;
|
||||
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
||||
void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
void delete_timer();
|
||||
void update_timers();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user