headers, split steppable
This commit is contained in:
+6
-5
@@ -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;
|
||||||
|
|
||||||
@@ -29,10 +30,10 @@ struct SortView {
|
|||||||
class SteppedBsort: Steppable {
|
class SteppedBsort: 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);
|
||||||
|
|
||||||
|
|||||||
+7
-6
@@ -1,9 +1,4 @@
|
|||||||
// for now juist CLI, print an arr, then print each it
|
// for now juist CLI, print an arr, then print each it
|
||||||
#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 <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
@@ -15,6 +10,11 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#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 <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
@@ -53,7 +53,6 @@ void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){
|
|||||||
mytimermanager.add_timer(std::move(mytimer));
|
mytimermanager.add_timer(std::move(mytimer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct App{
|
struct App{
|
||||||
sf::RenderWindow mywindow;
|
sf::RenderWindow mywindow;
|
||||||
sf::Font myfont;
|
sf::Font myfont;
|
||||||
@@ -87,6 +86,7 @@ struct App{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void run(){
|
void run(){
|
||||||
|
//set up ui
|
||||||
while (mywindow.isOpen()){
|
while (mywindow.isOpen()){
|
||||||
while (const optional event = mywindow.pollEvent()){
|
while (const optional event = mywindow.pollEvent()){
|
||||||
if (event->is<sf::Event::Closed>())
|
if (event->is<sf::Event::Closed>())
|
||||||
@@ -96,6 +96,7 @@ struct App{
|
|||||||
mywindow.clear(sf::Color::Black);
|
mywindow.clear(sf::Color::Black);
|
||||||
mywindow.draw(text);
|
mywindow.draw(text);
|
||||||
text.setString(arraytostr(input));
|
text.setString(arraytostr(input));
|
||||||
|
|
||||||
mytimermanager.update_timers();
|
mytimermanager.update_timers();
|
||||||
|
|
||||||
displaynumbers(input.values);
|
displaynumbers(input.values);
|
||||||
|
|||||||
@@ -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
|
||||||
+3
-5
@@ -15,11 +15,9 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-20
@@ -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,39 @@
|
|||||||
#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;
|
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(string name, int triggercount, 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<unique_ptr<Timer>> timers;
|
||||||
TimerManager() { previoustime = steady_clock::now(); }
|
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
||||||
void add_timer(unique_ptr<Timer> newtimer);
|
void add_timer(unique_ptr<Timer> newtimer);
|
||||||
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