switched from raw lambda to stepable
couldn't figure out how to handle sortstate, eventually put it into bsort class. should be split into timeraction and steppable. and there shouldn't be a random steppedbsort in App
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
cmake_minimum_required(VERSION 4.0)
|
cmake_minimum_required(VERSION 4.0)
|
||||||
project(cppout LANGUAGES CXX)
|
project(cppout LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
file(GLOB SOURCE_FILES "src/*.cpp")
|
file(GLOB SOURCE_FILES "src/*.cpp")
|
||||||
|
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <utility>
|
||||||
|
#include "sorters.hpp"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
SteppedBsort::SteppedBsort(SortInput &input): data(input) {};
|
||||||
|
|
||||||
|
bool SteppedBsort:: step(){
|
||||||
|
if(is_finished==true) {
|
||||||
|
printf("finished");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i==data.values.size()-1){
|
||||||
|
is_finished=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
+37
-7
@@ -1,12 +1,42 @@
|
|||||||
|
#ifndef _SORTERS_
|
||||||
|
#define _SORTERS_
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
|
||||||
struct SortingData {
|
using namespace std;
|
||||||
std::string name;
|
|
||||||
int target;
|
|
||||||
std::vector<int> data;
|
|
||||||
|
|
||||||
SortingData(std::string name, std::vector<int> data)
|
struct SortInput {
|
||||||
: name(name), data(data) {}
|
string name;
|
||||||
|
vector<int> values;
|
||||||
|
|
||||||
|
SortInput(string name, vector<int> data)
|
||||||
|
: name(name), values(data) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct SortView {
|
||||||
|
shared_ptr<const SortInput> values;
|
||||||
|
size_t currenttarget = 0;
|
||||||
|
int x,y;
|
||||||
|
int color;
|
||||||
|
SortView(shared_ptr<const SortInput> in): values(in) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class SteppedBsort: Steppable {
|
||||||
|
public:
|
||||||
|
SortInput &data;
|
||||||
|
int stepcount = 0;
|
||||||
|
size_t i = 0;
|
||||||
|
size_t j = 0;
|
||||||
|
bool is_finished;
|
||||||
|
|
||||||
|
SteppedBsort(SortInput &data);
|
||||||
|
|
||||||
|
bool step();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+38
-26
@@ -9,8 +9,10 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
@@ -22,40 +24,43 @@
|
|||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void displaynumbers(SortingData in_arr) {
|
bool displaynumbers(vector<int> input) {
|
||||||
for (size_t i = 0; i < in_arr.data.size(); ++i) {
|
for (size_t i = 0; i < input.size(); ++i) {
|
||||||
printf("array:%d ", in_arr.data[i]);
|
printf("array:%d ", input[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stepbubblesort(vector<int> &data, size_t step) {
|
string arraytostr(SortInput input) {
|
||||||
data[step%data.size()]++;
|
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 setuptimers(TimerManager &mytimermanager, SortingData &mysd){
|
void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){
|
||||||
|
|
||||||
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
unique_ptr<Timer> mytimer = make_unique<Timer>(
|
||||||
"sort1", 0,
|
"sort1", 0,
|
||||||
[&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); },
|
[&mysession](){return mysession.step();},
|
||||||
milliseconds(1000));
|
milliseconds(250));
|
||||||
unique_ptr<Timer> mydisplaytimer = make_unique<Timer>("display", 0,
|
mytimermanager.add_timer(std::move(mytimer));
|
||||||
[&mysd](Timer &t){displaynumbers(mysd);},
|
|
||||||
milliseconds(1000));
|
|
||||||
mytimermanager.add_timer(move(mytimer));
|
|
||||||
mytimermanager.add_timer(move(mydisplaytimer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct App{
|
struct App{
|
||||||
sf::RenderWindow mywindow;
|
sf::RenderWindow mywindow;
|
||||||
sf::Font myfont;
|
sf::Font myfont;
|
||||||
sf::Text text;
|
sf::Text text;
|
||||||
TimerManager mytimermanager;
|
TimerManager mytimermanager;
|
||||||
SortingData mysortdata;
|
SortInput input;
|
||||||
|
SteppedBsort mystb;
|
||||||
|
|
||||||
sf::Font load_font(const string &filename) {
|
sf::Font load_font(const string &filename) {
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
@@ -65,18 +70,20 @@ struct App{
|
|||||||
}
|
}
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
App():mywindow(sf::VideoMode({800,600}), "mywindow"),
|
App(SortInput input):
|
||||||
myfont(load_font("assets/NS-R.ttf")),
|
mywindow(sf::VideoMode({800,600}), "mywindow"),
|
||||||
text(myfont),
|
myfont(load_font("assets/NS-R.ttf")),
|
||||||
mysortdata("arr1", dataprovider()) {
|
text(myfont),
|
||||||
|
input(std::move(input)),
|
||||||
|
mystb(this->input)
|
||||||
|
{
|
||||||
text.setFont(myfont);
|
text.setFont(myfont);
|
||||||
text.setString("Hello world");
|
text.setString("Hello world");
|
||||||
text.setCharacterSize(24);
|
text.setCharacterSize(24);
|
||||||
text.setFillColor(sf::Color::Red);
|
text.setFillColor(sf::Color::Red);
|
||||||
|
|
||||||
setuptimers(mytimermanager, mysortdata);
|
setupsorttimers(mytimermanager, mystb);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void run(){
|
void run(){
|
||||||
@@ -85,17 +92,22 @@ struct App{
|
|||||||
if (event->is<sf::Event::Closed>())
|
if (event->is<sf::Event::Closed>())
|
||||||
mywindow.close();
|
mywindow.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
mywindow.clear(sf::Color::Black);
|
mywindow.clear(sf::Color::Black);
|
||||||
mywindow.draw(text);
|
mywindow.draw(text);
|
||||||
|
text.setString(arraytostr(input));
|
||||||
mytimermanager.update_timers();
|
mytimermanager.update_timers();
|
||||||
|
|
||||||
|
displaynumbers(input.values);
|
||||||
|
|
||||||
mywindow.display();
|
mywindow.display();
|
||||||
this_thread::sleep_for(milliseconds(1000));
|
this_thread::sleep_for(milliseconds(250));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
App myapp;
|
SortInput myinput("arr1", dataprovider());
|
||||||
|
App myapp(myinput);
|
||||||
myapp.run();
|
myapp.run();
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-12
@@ -4,30 +4,37 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
Timer::Timer(std::string name, int triggercount, std::function<void(Timer &t)> myaction, std::chrono::milliseconds triggerinterval)
|
|
||||||
|
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) {}
|
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
||||||
|
|
||||||
void Timer::add_time(std::chrono::milliseconds deltatime) {
|
void Timer::add_time(milliseconds deltatime) {
|
||||||
accumulator += deltatime;
|
accumulator += deltatime;
|
||||||
if (accumulator >= triggerinterval) {
|
if (accumulator >= triggerinterval) {
|
||||||
accumulator -= triggerinterval;
|
accumulator -= triggerinterval;
|
||||||
timeraction(*this);
|
if (timeraction()==true){
|
||||||
|
isdone = true;
|
||||||
|
};
|
||||||
triggercounter++;
|
triggercounter++;
|
||||||
if (!isinfinite && triggercounter >= triggeramount){
|
if (!isinfinite && triggercounter >= triggeramount){
|
||||||
isexpired=true;
|
isexpired=true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void TimerManager::add_timer(std::unique_ptr<Timer> newtimer) {
|
void TimerManager::add_timer(unique_ptr<Timer> newtimer) {
|
||||||
timers.push_back(std::move(newtimer));
|
timers.push_back(std::move(newtimer));
|
||||||
};
|
};
|
||||||
|
|
||||||
void TimerManager::update_timers() {
|
void TimerManager::update_timers() {
|
||||||
std::chrono::steady_clock::time_point currenttime =
|
steady_clock::time_point currenttime =
|
||||||
std::chrono::steady_clock::now();
|
steady_clock::now();
|
||||||
std::chrono::milliseconds deltatime = std::chrono::duration_cast<std::chrono::milliseconds>(currenttime-previoustime);
|
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
||||||
for(std::unique_ptr<Timer> &mytimer: timers){
|
for(unique_ptr<Timer> &mytimer: timers){
|
||||||
mytimer->add_time(deltatime);
|
mytimer->add_time(deltatime);
|
||||||
}
|
}
|
||||||
|
previoustime = currenttime;
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-23
@@ -1,3 +1,5 @@
|
|||||||
|
#ifndef _TIMERS_
|
||||||
|
#define _TIMERS_
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@@ -7,34 +9,48 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Timer {
|
using namespace std::chrono;
|
||||||
public:
|
using namespace std;
|
||||||
std::string name;
|
|
||||||
std::size_t triggeramount;
|
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;
|
||||||
|
function<bool()> timeraction;
|
||||||
|
|
||||||
|
Timer(string name, int triggercount, function<bool()> myaction, milliseconds triggerinterval);
|
||||||
|
void add_time(milliseconds deltatime);
|
||||||
|
|
||||||
|
|
||||||
bool isexpired;
|
|
||||||
bool isinfinite;
|
|
||||||
std::size_t triggercounter = 0;
|
|
||||||
|
|
||||||
Timer(std::string name, int triggercount, std::function<void(Timer &t)> myaction, std::chrono::milliseconds triggerinterval);
|
|
||||||
void add_time(std::chrono::milliseconds deltatime);
|
|
||||||
std::function<void(Timer&)> timeraction;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
milliseconds accumulator = milliseconds(0);
|
||||||
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
milliseconds triggerinterval = milliseconds(1000); //default to 1 sec
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//CRUD timers and give them dt
|
||||||
class TimerManager {
|
class TimerManager {
|
||||||
|
public:
|
||||||
|
vector<unique_ptr<Timer>> timers;
|
||||||
|
TimerManager() { previoustime = steady_clock::now(); }
|
||||||
|
void add_timer(unique_ptr<Timer> newtimer);
|
||||||
|
void delete_timer();
|
||||||
|
void update_timers();
|
||||||
|
|
||||||
|
private:
|
||||||
public:
|
steady_clock::time_point previoustime;
|
||||||
std::vector<std::unique_ptr<Timer>> timers;
|
|
||||||
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
|
||||||
void add_timer(std::unique_ptr<Timer> newtimer);
|
|
||||||
void delete_timer();
|
|
||||||
void update_timers();
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::chrono::steady_clock::time_point previoustime;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user