split app into seperate files to clean main
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
#include "./app.hpp"
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
|
bool displaySortInput(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 myoutput;
|
||||||
|
ostringstream stringrep;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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()){
|
||||||
|
if (event->is<sf::Event::Closed>())
|
||||||
|
mywindow.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
mywindow.clear(sf::Color::Black);
|
||||||
|
mywindow.draw(text);
|
||||||
|
text.setString(arraytostr(inputs[0]));
|
||||||
|
|
||||||
|
mysortsession.update_sorts();
|
||||||
|
|
||||||
|
for (SortInput input : inputs) {
|
||||||
|
displaySortInput(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
mywindow.display();
|
||||||
|
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef APP_HPP
|
||||||
|
#define APP_HPP
|
||||||
|
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
|
||||||
|
#include "../sorters.hpp"
|
||||||
|
#include "../sessions.hpp"
|
||||||
|
struct App{
|
||||||
|
sf::RenderWindow mywindow;
|
||||||
|
sf::Font myfont;
|
||||||
|
sf::Text text;
|
||||||
|
vector<SortInput> inputs;
|
||||||
|
SortSession mysortsession;
|
||||||
|
|
||||||
|
sf::Font load_font(const std::string &filename);
|
||||||
|
|
||||||
|
App(vector<SortInput> inputs);
|
||||||
|
|
||||||
|
void run();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#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>
|
||||||
|
|
||||||
|
struct SortSession {
|
||||||
|
vector<SortInput> &inputs;
|
||||||
|
unique_ptr<Steppable> sorts;
|
||||||
|
TimerManager SortTimer;
|
||||||
|
|
||||||
|
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
|
||||||
|
add_session(sorts);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void add_session(unique_ptr<Steppable> &mysteppable){
|
||||||
|
SortTimer.add_timer(0,
|
||||||
|
[&mysteppable]() {return mysteppable->step();},
|
||||||
|
std::chrono::milliseconds(250));
|
||||||
|
}
|
||||||
|
void update_sorts(){
|
||||||
|
SortTimer.update_timers();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ViewSession{
|
||||||
|
sf::RenderWindow mywindow;
|
||||||
|
sf::Font myfont;
|
||||||
|
sf::Text text;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+1
-116
@@ -1,14 +1,5 @@
|
|||||||
// for now juist CLI, print an arr, then print each it
|
// for now juist CLI, print an arr, then print each it
|
||||||
#include <chrono>
|
|
||||||
#include <cstddef>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <SFML/Graphics/Color.hpp>
|
#include <SFML/Graphics/Color.hpp>
|
||||||
@@ -19,123 +10,17 @@
|
|||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include "steppable.hpp"
|
#include "core/app.hpp"
|
||||||
#include "timer.hpp"
|
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool displaySortInput(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 myoutput;
|
|
||||||
ostringstream stringrep;
|
|
||||||
for (size_t i = 0; i < input.values.size(); ++i) {
|
|
||||||
stringrep << input.values[i] <<" | ";
|
|
||||||
}
|
|
||||||
return stringrep.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
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};
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
|
|
||||||
mytimermanager.add_timer(
|
|
||||||
0,
|
|
||||||
[&mysession](){return mysession->step();},
|
|
||||||
milliseconds(250));
|
|
||||||
}
|
|
||||||
|
|
||||||
//list of inputs, manually assign them to sorts
|
|
||||||
struct SortSession {
|
|
||||||
vector<SortInput> &inputs;
|
|
||||||
unique_ptr<Steppable> sorts;
|
|
||||||
TimerManager SortTimer;
|
|
||||||
|
|
||||||
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
|
|
||||||
//setupsorttimers(SortTimer, sorts);
|
|
||||||
add_session(sorts);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void add_session(unique_ptr<Steppable> &mysteppable){
|
|
||||||
SortTimer.add_timer(0,
|
|
||||||
[&mysteppable]() {return mysteppable->step();},
|
|
||||||
milliseconds(250));
|
|
||||||
}
|
|
||||||
void update_sorts(){
|
|
||||||
SortTimer.update_timers();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ViewSession{
|
|
||||||
sf::RenderWindow mywindow;
|
|
||||||
sf::Font myfont;
|
|
||||||
sf::Text text;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct App{
|
|
||||||
sf::RenderWindow mywindow;
|
|
||||||
sf::Font myfont;
|
|
||||||
sf::Text text;
|
|
||||||
vector<SortInput> inputs;
|
|
||||||
SortSession mysortsession;
|
|
||||||
|
|
||||||
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(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)
|
|
||||||
{
|
|
||||||
text.setFont(myfont);
|
|
||||||
text.setString("Hello world");
|
|
||||||
text.setCharacterSize(24);
|
|
||||||
text.setFillColor(sf::Color::Red);
|
|
||||||
}
|
|
||||||
|
|
||||||
void run(){
|
|
||||||
//set up ui
|
|
||||||
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(inputs[0]));
|
|
||||||
|
|
||||||
mysortsession.update_sorts();
|
|
||||||
|
|
||||||
for (SortInput input : inputs) {
|
|
||||||
displaySortInput(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
mywindow.display();
|
|
||||||
this_thread::sleep_for(milliseconds(250));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
vector<SortInput> inputs;
|
vector<SortInput> inputs;
|
||||||
SortInput myinput("arr1", dataprovider());
|
SortInput myinput("arr1", dataprovider());
|
||||||
|
|||||||
Reference in New Issue
Block a user