-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (144 loc) Β· 3.98 KB
/
main.cpp
File metadata and controls
168 lines (144 loc) Β· 3.98 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @name Sorting visualizer
* @author alesbe - github.com/alesbe/
* @brief A lightweight sorting visualizer made with C++ and SFML.
* @version v1.1
*
* @copyright License - MIT [alesbe 2022]
*
*/
#ifdef _WIN32
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
#include <iostream>
#include <thread>
#include <SFML/Graphics.hpp>
#include "SortController.h"
#include "Utils.h"
int main()
{
// Window settings
sf::RenderWindow window(sf::VideoMode(600, 400), "Sorting visualizer v1.1");
window.setFramerateLimit(60);
// Visualizer settings (can be changed on runtime through the key events)
int numOfElements = 150;
int timeSleep = 1;
int sortType = 0;
// Initialize SortController
SortController sortController(window.getSize(), timeSleep);
sortController.populate(numOfElements);
// Main loop
while (window.isOpen())
{
// Events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed) {
switch (event.key.code)
{
// Start sort
case sf::Keyboard::Space:
if (!sortController.isSorting()) {
sortController.clear();
sortController.populate(numOfElements);
sortController.randomize();
sortController.startSort(sortType);
}
break;
// Stop sort
case sf::Keyboard::Backspace:
if (sortController.isSorting()) {
system(CLEAR);
std::cout << "Sort stopped!" << std::endl;
sortController.stopSort();
}
break;
// Display help
case sf::Keyboard::H:
system(CLEAR);
std::cout << "[Space]: Start sort" << std::endl;
std::cout << "[Backspace]: Stop sort" << std::endl;
std::cout << "[Up/Down Arrows]: Change sort type" << std::endl;
std::cout << "[F1]: Change number of elements" << std::endl;
std::cout << "[F2]: Change time between comparisons" << std::endl;
break;
// Change sort type (increase)
case sf::Keyboard::Up:
if (sortType >= 0 && Utils::hasNextSortType(sortType)) {
sortType++;
system(CLEAR);
std::cout << "Sort changed to: " << Utils::getSortType(sortType) << std::endl;
}
else {
//Go to the first sorting algorithm
sortType = 0;
system(CLEAR);
std::cout << "Sort changed to " << Utils::getSortType(sortType) << std::endl;
}
break;
// Change sort type (decrease)
case sf::Keyboard::Down:
if (sortType > 0) {
sortType--;
system(CLEAR);
std::cout << "Sort changed to: " << Utils::getSortType(sortType) << std::endl;
}
else {
//Go to the last sorting algorithm
while(Utils::hasNextSortType(sortType))
{
sortType++;
}
system(CLEAR);
std::cout << "Sort changed to: " << Utils::getSortType(sortType) << std::endl;
}
break;
// Change number of elements
case sf::Keyboard::F1:
if(!sortController.isSorting()) {
system(CLEAR);
std::cout << "Number of elements: ";
std::cin >> numOfElements;
std::cout << std::endl;
sortController.clear();
sortController.populate(numOfElements);
break;
}
// Change time between comparisons
case sf::Keyboard::F2:
if(!sortController.isSorting()) {
system(CLEAR);
std::cout << "Time between comparisons (milliseconds): ";
std::cin >> timeSleep;
std::cout << std::endl;
sortController.setTimeSleep(timeSleep);
break;
}
default:
break;
}
}
// Close window
if (event.type == sf::Event::Closed) {
sortController.stopSort();
window.close();
}
}
// Clear screen between each frame
window.clear(sf::Color::Black);
// Draw array elements
int index = 0;
for (auto sortable : sortController.sortElements()) {
sf::RectangleShape shape = sortable.shape();
shape.setFillColor(sortable.color);
shape.setPosition(sf::Vector2f(sortable.width * index++, sortController.winHeight() - sortable.height));
window.draw(shape);
}
// Display drawed content on screen
window.display();
}
return 0;
}