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)
|
||||
project(cppout LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
file(GLOB SOURCE_FILES "src/*.cpp")
|
||||
|
||||
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 <vector>
|
||||
#include "timer.hpp"
|
||||
|
||||
struct SortingData {
|
||||
std::string name;
|
||||
int target;
|
||||
std::vector<int> data;
|
||||
using namespace std;
|
||||
|
||||
SortingData(std::string name, std::vector<int> data)
|
||||
: name(name), data(data) {}
|
||||
|
||||
struct SortInput {
|
||||
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 <cstdlib>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <SFML/Window.hpp>
|
||||
@@ -22,40 +24,43 @@
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
void displaynumbers(SortingData in_arr) {
|
||||
for (size_t i = 0; i < in_arr.data.size(); ++i) {
|
||||
printf("array:%d ", in_arr.data[i]);
|
||||
bool displaynumbers(vector<int> input) {
|
||||
for (size_t i = 0; i < input.size(); ++i) {
|
||||
printf("array:%d ", input[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
void stepbubblesort(vector<int> &data, size_t step) {
|
||||
data[step%data.size()]++;
|
||||
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};
|
||||
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>(
|
||||
"sort1", 0,
|
||||
[&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); },
|
||||
milliseconds(1000));
|
||||
unique_ptr<Timer> mydisplaytimer = make_unique<Timer>("display", 0,
|
||||
[&mysd](Timer &t){displaynumbers(mysd);},
|
||||
milliseconds(1000));
|
||||
mytimermanager.add_timer(move(mytimer));
|
||||
mytimermanager.add_timer(move(mydisplaytimer));
|
||||
[&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;
|
||||
SortingData mysortdata;
|
||||
SortInput input;
|
||||
SteppedBsort mystb;
|
||||
|
||||
sf::Font load_font(const string &filename) {
|
||||
sf::Font font;
|
||||
@@ -65,18 +70,20 @@ struct App{
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
App():mywindow(sf::VideoMode({800,600}), "mywindow"),
|
||||
myfont(load_font("assets/NS-R.ttf")),
|
||||
text(myfont),
|
||||
mysortdata("arr1", dataprovider()) {
|
||||
|
||||
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);
|
||||
|
||||
setuptimers(mytimermanager, mysortdata);
|
||||
|
||||
setupsorttimers(mytimermanager, mystb);
|
||||
}
|
||||
|
||||
void run(){
|
||||
@@ -85,17 +92,22 @@ struct App{
|
||||
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(1000));
|
||||
this_thread::sleep_for(milliseconds(250));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
App myapp;
|
||||
SortInput myinput("arr1", dataprovider());
|
||||
App myapp(myinput);
|
||||
myapp.run();
|
||||
}
|
||||
|
||||
+19
-12
@@ -4,30 +4,37 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#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) {}
|
||||
|
||||
void Timer::add_time(std::chrono::milliseconds deltatime) {
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
if (accumulator >= triggerinterval) {
|
||||
accumulator -= triggerinterval;
|
||||
timeraction(*this);
|
||||
if (timeraction()==true){
|
||||
isdone = true;
|
||||
};
|
||||
triggercounter++;
|
||||
if (!isinfinite && triggercounter >= triggeramount){
|
||||
isexpired=true;
|
||||
}
|
||||
if (!isinfinite && triggercounter >= triggeramount){
|
||||
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));
|
||||
};
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
std::chrono::steady_clock::time_point currenttime =
|
||||
std::chrono::steady_clock::now();
|
||||
std::chrono::milliseconds deltatime = std::chrono::duration_cast<std::chrono::milliseconds>(currenttime-previoustime);
|
||||
for(std::unique_ptr<Timer> &mytimer: timers){
|
||||
mytimer->add_time(deltatime);
|
||||
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);
|
||||
}
|
||||
previoustime = currenttime;
|
||||
}
|
||||
|
||||
+39
-23
@@ -1,3 +1,5 @@
|
||||
#ifndef _TIMERS_
|
||||
#define _TIMERS_
|
||||
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
@@ -7,34 +9,48 @@
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
std::string name;
|
||||
std::size_t triggeramount;
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
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:
|
||||
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
||||
milliseconds accumulator = milliseconds(0);
|
||||
milliseconds triggerinterval = milliseconds(1000); //default to 1 sec
|
||||
};
|
||||
|
||||
//CRUD timers and give them dt
|
||||
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();
|
||||
|
||||
|
||||
public:
|
||||
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;
|
||||
private:
|
||||
steady_clock::time_point previoustime;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user