Compare commits
2 Commits
d98c29ecbb
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a8894a51cd | |||
| c330f13bb8 |
+7
-26
@@ -10,6 +10,7 @@ using namespace std;
|
||||
|
||||
bool displaySortInput(shared_ptr<SortInput> input) {
|
||||
cout << "name: " + input->name + " values: ";
|
||||
|
||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||
printf("%d | ", input->values[i]);
|
||||
}
|
||||
@@ -27,42 +28,22 @@ string arraytostr(shared_ptr<SortInput> input) {
|
||||
}
|
||||
|
||||
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
||||
: viewsession(std::move(myviewsession)),
|
||||
timermanager(std::move(mytimermanager)) {}
|
||||
: viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {}
|
||||
|
||||
//connects data with the correct sort
|
||||
shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
||||
if (sorttype == "bsort") {
|
||||
return make_shared<SteppedBsort>(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//creates a Sortbinding, actually does too much
|
||||
void App::add_sort(shared_ptr<SortInput> myinput, string sorttype) {
|
||||
cout << "adding sort " << sorttype << "\n";
|
||||
// just pushes to App for now
|
||||
void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) {
|
||||
cout << "adding sort " << myinput->name << "\n";
|
||||
// insert data
|
||||
inputs.push_back(myinput);
|
||||
//connect data with logic into a sort
|
||||
std::shared_ptr<Steppable> mysorter = make_sort(myinput, sorttype);
|
||||
|
||||
//connect the sort with its invoker
|
||||
unique_ptr<Invoker> myinvoker = make_unique<Invoker>(mysorter);
|
||||
|
||||
// move the sort into app
|
||||
sortbindings.push_back(SortBinding{myinput, std::move(mysorter)});
|
||||
|
||||
//create the timer with the invoker and add it to app
|
||||
Timer timer(0, std::move(myinvoker), chrono::milliseconds(250));
|
||||
timermanager->add_timer(std::move(timer));
|
||||
sortbindings.push_back(mysortbinding);
|
||||
}
|
||||
|
||||
void App::run() {
|
||||
// set up ui
|
||||
while (viewsession->mywindow.isOpen()) {
|
||||
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
viewsession->mywindow.close();
|
||||
if (event->is<sf::Event::Closed>()) viewsession->mywindow.close();
|
||||
}
|
||||
|
||||
// draw
|
||||
|
||||
+3
-4
@@ -1,16 +1,15 @@
|
||||
#ifndef APP_HPP
|
||||
#define APP_HPP
|
||||
|
||||
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "../timer.hpp"
|
||||
#include "../sorters.hpp"
|
||||
#include "../sessions.hpp"
|
||||
#include "../sorters.hpp"
|
||||
#include "../timer.hpp"
|
||||
|
||||
// should connect the different parts
|
||||
// for now data, mutation, and rendering
|
||||
@@ -22,7 +21,7 @@ struct App{
|
||||
|
||||
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
||||
|
||||
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
||||
void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
|
||||
void run();
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
ViewSession::ViewSession(): mywindow(sf::VideoMode({800,600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont)
|
||||
{
|
||||
ViewSession::ViewSession()
|
||||
: mywindow(sf::VideoMode({800, 600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) {
|
||||
text.setFont(myfont);
|
||||
text.setString("Hello world");
|
||||
text.setCharacterSize(24);
|
||||
|
||||
+14
-4
@@ -1,11 +1,10 @@
|
||||
#include "sorters.hpp"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include "sorters.hpp"
|
||||
using namespace std;
|
||||
|
||||
|
||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input) : data(input) {};
|
||||
|
||||
void SteppedBsort::step() {
|
||||
@@ -13,8 +12,7 @@ void SteppedBsort:: step(){
|
||||
if (is_finished) {
|
||||
printf("finished");
|
||||
}
|
||||
if(data->values[j]>data->values[j+1])
|
||||
{
|
||||
if (data->values[j] > data->values[j + 1]) {
|
||||
swap(data->values[j], data->values[j + 1]);
|
||||
}
|
||||
j++;
|
||||
@@ -27,3 +25,15 @@ void SteppedBsort:: step(){
|
||||
is_finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
SortBinding::SortBinding(
|
||||
std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker)
|
||||
: input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)) {}
|
||||
|
||||
// connects data with the correct sort
|
||||
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
||||
if (sorttype == "bsort") {
|
||||
return make_shared<SteppedBsort>(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
+7
-4
@@ -14,8 +14,7 @@ struct SortInput {
|
||||
std::string name;
|
||||
std::vector<int> values;
|
||||
|
||||
SortInput(std::string name, std::vector<int> data)
|
||||
: name(name), values(data) {}
|
||||
SortInput(std::string name, std::vector<int> data) : name(name), values(data) {}
|
||||
};
|
||||
|
||||
// ro rendering of data
|
||||
@@ -41,10 +40,14 @@ public:
|
||||
void step();
|
||||
};
|
||||
|
||||
// working unit
|
||||
struct SortBinding {
|
||||
std::shared_ptr<SortInput> input;
|
||||
std::shared_ptr<Steppable> sort;
|
||||
//SortBinding(std::shared_ptr<SortInput> input, std::unique_ptr<Steppable> sort, std::unique_ptr<Invoker> invoker);
|
||||
|
||||
std::shared_ptr<Invoker> invoker;
|
||||
SortBinding(std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker);
|
||||
};
|
||||
|
||||
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype);
|
||||
|
||||
#endif
|
||||
|
||||
+15
-7
@@ -1,27 +1,27 @@
|
||||
// for now juist CLI, print an arr, then print each it
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#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/Graphics.hpp>
|
||||
#include <SFML/Window/Window.hpp>
|
||||
|
||||
#include "core/app.hpp"
|
||||
#include "sessions.hpp"
|
||||
#include "sorters.hpp"
|
||||
#include "steppable.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
vector<int> dataprovider(){
|
||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
||||
}
|
||||
vector<int> dataprovider() { return vector<int>{9, 8, 1, 2, 7, 3, 6, 4, 5}; }
|
||||
|
||||
int main() {
|
||||
// create data
|
||||
@@ -34,8 +34,16 @@ int main() {
|
||||
// create app with initialized dependencies
|
||||
App myapp(std::move(myviewsession), std::move(mytimermanager));
|
||||
|
||||
//add and connect data
|
||||
myapp.add_sort(myinput, "bsort");
|
||||
// create binding
|
||||
shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
|
||||
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(asort);
|
||||
SortBinding mysortbinding(myinput, asort, myinvoker);
|
||||
|
||||
Timer timer(0, myinvoker, chrono::milliseconds(250));
|
||||
myapp.timermanager->add_timer(std::move(timer));
|
||||
|
||||
// add data and binding
|
||||
myapp.add_sort(myinput, mysortbinding);
|
||||
|
||||
// actually start the app
|
||||
myapp.run();
|
||||
|
||||
+8
-11
@@ -2,23 +2,23 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "Invoker.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||
:triggeramount(triggeramount)
|
||||
, invoker(std::move(invoker))
|
||||
, triggerinterval(triggerinterval){}
|
||||
Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||
: triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {}
|
||||
|
||||
void Timer::add_time(milliseconds deltatime) {
|
||||
accumulator += deltatime;
|
||||
if (accumulator >= triggerinterval) {
|
||||
accumulator -= triggerinterval;
|
||||
cout << "request in timer\n";
|
||||
invoker->request();
|
||||
if (auto sp = invoker.lock()) {
|
||||
sp->request();
|
||||
}
|
||||
triggercount++;
|
||||
if (triggeramount != 0 && triggercount >= triggeramount) {
|
||||
isexpired = true;
|
||||
@@ -26,13 +26,10 @@ void Timer::add_time(milliseconds deltatime) {
|
||||
}
|
||||
}
|
||||
|
||||
void TimerManager::add_timer(Timer timer){
|
||||
timers.push_back(std::move(timer));
|
||||
}
|
||||
void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); }
|
||||
|
||||
void TimerManager::update_timers() {
|
||||
steady_clock::time_point currenttime =
|
||||
steady_clock::now();
|
||||
steady_clock::time_point currenttime = steady_clock::now();
|
||||
milliseconds deltatime = duration_cast<milliseconds>(currenttime - previoustime);
|
||||
for (Timer &mytimer : timers) {
|
||||
mytimer.add_time(deltatime);
|
||||
|
||||
+3
-4
@@ -1,9 +1,9 @@
|
||||
#ifndef TIMER_HPP
|
||||
#define TIMER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -14,9 +14,9 @@ public:
|
||||
size_t triggeramount;
|
||||
bool isexpired = false;
|
||||
size_t triggercount = 0;
|
||||
std::unique_ptr<Invoker> invoker;
|
||||
std::weak_ptr<Invoker> invoker; // if no longer exists, then should clean up timer
|
||||
|
||||
Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||
Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||
void add_time(std::chrono::milliseconds deltatime);
|
||||
|
||||
private:
|
||||
@@ -24,7 +24,6 @@ private:
|
||||
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); // default to 1 sec
|
||||
};
|
||||
|
||||
|
||||
// CRUD timers and give them dt on update
|
||||
class TimerManager {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user