-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (84 loc) · 3.41 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (84 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <thread>
#include "headers/sorting.hpp"
#include "headers/draw.hpp"
#include "headers/initialize.hpp"
#include "headers/stepStruct.hpp"
#include "headers/sortingVisualiser.hpp"
#include "headers/button.hpp"
enum class Windows {menu, sorting, graph};
extern const sf::Font font("C:/Windows/fonts/arial.ttf");
int main()
{
Windows currentWindow = Windows::menu;
SortingAlgorithm algoToRun = SortingAlgorithm::insertionSort;
sf::RenderWindow window(sf::VideoMode({800, 600}), "Algorithm visualiser");
sf::Vector2f buttonSize = {200.f, 100.f};
Button insertionSortButton(buttonSize, {100.f, 50.f}, "insertion sort");
Button mergeSortButton(buttonSize, {100.f, 200.f}, "merge sort");
Button graphButton(buttonSize, {400.f, 50.f}, "Graph builder");
//loop to keep open
while(window.isOpen()){
while (const std::optional event = window.pollEvent()){
if(event->is<sf::Event::Closed>()){
window.close();
}
}
switch (currentWindow){
case Windows::menu: {
window.clear(sf::Color(100,100,100));
sf::Text text(font); // a font is required to make a text object
text.setString("menu ");
text.setCharacterSize(24); // in pixels, not points!
text.setFillColor(sf::Color::Red);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(window));
if(insertionSortButton.clicked(mouse_position)){
currentWindow = Windows::sorting;
algoToRun = SortingAlgorithm::insertionSort;
}
if(mergeSortButton.clicked(mouse_position)){
currentWindow = Windows::sorting;
algoToRun = SortingAlgorithm::mergeSort;
}
if(graphButton.clicked(mouse_position)){
currentWindow = Windows::graph;
}
window.draw(insertionSortButton);
window.draw(mergeSortButton);
window.draw(graphButton);
window.draw(text);
window.display();
break;
}
case Windows::graph:{
bool wantToExit = false;
while(!wantToExit && window.isOpen()){
window.clear({0,0,0});
while (const std::optional event = window.pollEvent()){
if(event->is<sf::Event::Closed>()){
window.close();
}
else if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
if (keyPressed->scancode == sf::Keyboard::Scancode::Escape){
wantToExit = true;
}
}
}
window.display();
}
currentWindow = Windows::menu;
break;
}
case Windows::sorting: {
//run sorting algo and when it is done, go back to menu
runSortAlgo(window, algoToRun);
currentWindow = Windows::menu;
break;
}
}
}
}