Compare commits
5 Commits
6f7bd9d964
...
ad94f758fc
| Author | SHA1 | Date | |
|---|---|---|---|
| ad94f758fc | |||
| 005d4282a9 | |||
| 8a60e45cd1 | |||
| e6e170a42a | |||
| d2d9ef6c70 |
+1
-1
@@ -3,7 +3,7 @@ project(cppout LANGUAGES CXX)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -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_
|
||||
#define _SORTERS_
|
||||
#ifndef SORTERS_HPP
|
||||
#define SORTERS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "timer.hpp"
|
||||
|
||||
#include "steppable.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -26,13 +27,13 @@ struct SortView {
|
||||
};
|
||||
|
||||
|
||||
class SteppedBsort: Steppable {
|
||||
class SteppedBsort: public Steppable {
|
||||
public:
|
||||
SortInput &data;
|
||||
int stepcount = 0;
|
||||
size_t stepcount = 0;
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
bool is_finished;
|
||||
bool is_finished = false;
|
||||
|
||||
SteppedBsort(SortInput &data);
|
||||
|
||||
|
||||
+18
-85
@@ -1,113 +1,46 @@
|
||||
// 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/Font.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Text.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/Graphics.hpp>
|
||||
|
||||
#include "core/app.hpp"
|
||||
#include "steppable.hpp"
|
||||
#include "timer.hpp"
|
||||
#include "sorters.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
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(){
|
||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
||||
}
|
||||
|
||||
void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){
|
||||
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
||||
"sort1", 0,
|
||||
[&mysession](){return mysession.step();},
|
||||
void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
|
||||
mytimermanager.add_timer(
|
||||
0,
|
||||
[&mysession](){return mysession->step();},
|
||||
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() {
|
||||
vector<SortInput> inputs;
|
||||
SortInput myinput("arr1", dataprovider());
|
||||
App myapp(myinput);
|
||||
SortInput myinput2("arr2", dataprovider());
|
||||
|
||||
inputs.push_back(myinput);
|
||||
inputs.push_back(myinput2);
|
||||
|
||||
App myapp(inputs);
|
||||
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;
|
||||
|
||||
Timer::Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval)
|
||||
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
||||
Timer::Timer(size_t triggeramount, function<bool()> myaction, milliseconds triggerinterval)
|
||||
:triggeramount(triggeramount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
if (accumulator >= triggerinterval) {
|
||||
accumulator -= triggerinterval;
|
||||
if (timeraction()==true){
|
||||
isdone = true;
|
||||
};
|
||||
triggercounter++;
|
||||
if (!isinfinite && triggercounter >= triggeramount){
|
||||
timeraction();
|
||||
triggercount++;
|
||||
if (!isinfinite && triggercount >= triggeramount){
|
||||
isexpired=true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
|
||||
timers.push_back(std::move(newtimer));
|
||||
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
||||
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
||||
timers.push_back(mynewtimer);
|
||||
};
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
steady_clock::time_point currenttime =
|
||||
steady_clock::now();
|
||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
||||
for(unique_ptr<Timer> &mytimer: timers){
|
||||
mytimer->add_time(deltatime);
|
||||
for(Timer &mytimer: timers){
|
||||
mytimer.add_time(deltatime);
|
||||
}
|
||||
previoustime = currenttime;
|
||||
}
|
||||
|
||||
+14
-24
@@ -1,5 +1,5 @@
|
||||
#ifndef _TIMERS_
|
||||
#define _TIMERS_
|
||||
#ifndef TIMER_HPP
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
@@ -9,48 +9,38 @@
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
using namespace std::chrono;
|
||||
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 {
|
||||
public:
|
||||
string name;
|
||||
size_t triggeramount;
|
||||
|
||||
bool isexpired = false;
|
||||
bool isdone = false;
|
||||
bool isinfinite = false;
|
||||
size_t triggercounter = 0;
|
||||
size_t triggercount = 0;
|
||||
function<bool()> timeraction;
|
||||
|
||||
Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval);
|
||||
void add_time(milliseconds deltatime);
|
||||
|
||||
|
||||
Timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
void add_time(std::chrono::milliseconds deltatime);
|
||||
|
||||
private:
|
||||
milliseconds accumulator = milliseconds(0);
|
||||
milliseconds triggerinterval = milliseconds(1000); //default to 1 sec
|
||||
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||
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 {
|
||||
public:
|
||||
vector<unique_ptr<Timer>> timers;
|
||||
TimerManager() { previoustime = steady_clock::now(); }
|
||||
void add_timer(unique_ptr<Timer> newtimer);
|
||||
vector<Timer> timers;
|
||||
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
||||
void add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||
void delete_timer();
|
||||
void update_timers();
|
||||
|
||||
private:
|
||||
steady_clock::time_point previoustime;
|
||||
std::chrono::steady_clock::time_point previoustime;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user