Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad94f758fc | |||
| 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,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,38 @@
|
|||||||
|
#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])) {
|
||||||
|
//setupsorttimers(SortTimer, sorts);
|
||||||
|
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
|
||||||
+7
-6
@@ -1,11 +1,12 @@
|
|||||||
#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"
|
|
||||||
|
#include "steppable.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -26,13 +27,13 @@ struct SortView {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SteppedBsort: Steppable {
|
class SteppedBsort: public Steppable {
|
||||||
public:
|
public:
|
||||||
SortInput &data;
|
SortInput &data;
|
||||||
int stepcount = 0;
|
size_t stepcount = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
bool is_finished;
|
bool is_finished = false;
|
||||||
|
|
||||||
SteppedBsort(SortInput &data);
|
SteppedBsort(SortInput &data);
|
||||||
|
|
||||||
|
|||||||
+18
-85
@@ -1,113 +1,46 @@
|
|||||||
// 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 <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#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 <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/Graphics.hpp>
|
||||||
|
|
||||||
|
#include "core/app.hpp"
|
||||||
|
#include "steppable.hpp"
|
||||||
#include "timer.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 displaynumbers(vector<int> input) {
|
|
||||||
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(){
|
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, SteppedBsort &mysession){
|
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
|
||||||
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
mytimermanager.add_timer(
|
||||||
"sort1", 0,
|
0,
|
||||||
[&mysession](){return mysession.step();},
|
[&mysession](){return mysession->step();},
|
||||||
milliseconds(250));
|
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() {
|
||||||
|
vector<SortInput> inputs;
|
||||||
SortInput myinput("arr1", dataprovider());
|
SortInput myinput("arr1", dataprovider());
|
||||||
App myapp(myinput);
|
SortInput myinput2("arr2", dataprovider());
|
||||||
|
|
||||||
|
inputs.push_back(myinput);
|
||||||
|
inputs.push_back(myinput2);
|
||||||
|
|
||||||
|
App myapp(inputs);
|
||||||
myapp.run();
|
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 bool step() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+10
-11
@@ -8,33 +8,32 @@
|
|||||||
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, function<bool()> myaction, milliseconds triggerinterval)
|
||||||
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
:triggeramount(triggeramount), timeraction(myaction), 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){
|
timeraction();
|
||||||
isdone = true;
|
triggercount++;
|
||||||
};
|
if (!isinfinite && triggercount >= triggeramount){
|
||||||
triggercounter++;
|
|
||||||
if (!isinfinite && triggercounter >= triggeramount){
|
|
||||||
isexpired=true;
|
isexpired=true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
|
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
||||||
timers.push_back(std::move(newtimer));
|
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
||||||
|
timers.push_back(mynewtimer);
|
||||||
};
|
};
|
||||||
|
|
||||||
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(unique_ptr<Timer> &mytimer: timers){
|
for(Timer &mytimer: timers){
|
||||||
mytimer->add_time(deltatime);
|
mytimer.add_time(deltatime);
|
||||||
}
|
}
|
||||||
previoustime = currenttime;
|
previoustime = currenttime;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-24
@@ -1,5 +1,5 @@
|
|||||||
#ifndef _TIMERS_
|
#ifndef TIMER_HPP
|
||||||
#define _TIMERS_
|
#define TIMER_HPP
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@@ -9,48 +9,38 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std::chrono;
|
|
||||||
using namespace std;
|
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;
|
bool isexpired = false;
|
||||||
bool isdone = false;
|
|
||||||
bool isinfinite = false;
|
bool isinfinite = false;
|
||||||
size_t triggercounter = 0;
|
size_t triggercount = 0;
|
||||||
function<bool()> timeraction;
|
function<bool()> timeraction;
|
||||||
|
|
||||||
Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval);
|
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void add_time(milliseconds deltatime);
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
milliseconds accumulator = milliseconds(0);
|
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||||
milliseconds triggerinterval = milliseconds(1000); //default to 1 sec
|
std::chrono::milliseconds triggerinterval = std::chrono::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;
|
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, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
void delete_timer();
|
void delete_timer();
|
||||||
void update_timers();
|
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