22- Sort Types -
330: Bubble Sort
441: ...
5+
6+ Due to the 1ms resolution of timers on Windows, the speed scale is smaller. The Linux version has a higher time resoltion < 1ms!
57*/
68
79#include < SFML/Graphics.hpp>
810#include < iostream>
911#include < thread>
1012#include " SortController.h"
1113
12- void test (int num) {
13- std::cout << " Hi, this is a test and this is my number: " << num << std::endl;
14- }
15-
1614int main ()
1715{
18- sf::RenderWindow window (sf::VideoMode (600 , 400 ), " Sorting visualizer v0.1 -alpha" );
16+ sf::RenderWindow window (sf::VideoMode (600 , 400 ), " Sorting visualizer v0.2 -alpha" );
1917
20- int numOfElements = 10 ;
21- int timeSleep = 1 ;
18+ int numOfElements = 50 ;
19+ int timeSleep = 1 ; // milliseconds
2220 int sortType = 0 ;
2321
2422 std::thread sortingThread;
@@ -39,13 +37,23 @@ int main()
3937 // Randomize and start sorting
4038 case sf::Keyboard::Space:
4139 if (!sortController.isSorting ) {
42- std::cout << " Randomizing..." << std::endl;
40+ std::cout << " Starting sort!" << std::endl;
41+ sortController.clear ();
42+ sortController.populate (numOfElements);
4343 sortController.randomize ();
4444
4545 sortingThread = std::thread (&SortController::startSort, &sortController, sortType);
4646 sortingThread.detach ();
4747 }
48+ break ;
4849
50+ // Stop sort
51+ case sf::Keyboard::Backspace:
52+ if (sortController.isSorting ) {
53+ std::cout << " Sort stopped!" << std::endl;
54+ sortController.clear ();
55+ sortController.populate (numOfElements);
56+ }
4957 break ;
5058
5159 // Change sort type (increase)
@@ -76,6 +84,25 @@ int main()
7684 std::cout << " Num of elements changed to: " << numOfElements << std::endl;
7785 break ;
7886
87+ // Change number of sortables through console
88+ case sf::Keyboard::F1:
89+ std::cout << " Number of sortables: " ;
90+ std::cin >> numOfElements;
91+ std::cout << std::endl;
92+
93+ sortController.clear ();
94+ sortController.populate (numOfElements);
95+ break ;
96+
97+ // Change time between iterations
98+ case sf::Keyboard::F2:
99+ std::cout << " Time between iterations (milliseconds): " ;
100+ std::cin >> timeSleep;
101+ std::cout << std::endl;
102+
103+ sortController.setTimeSleep (timeSleep);
104+ break ;
105+
79106 default :
80107 break ;
81108 }
@@ -91,6 +118,7 @@ int main()
91118 int index = 0 ;
92119 for (auto sortable : sortController.sortElements ) {
93120 sf::RectangleShape shape = sortable.shape ();
121+ shape.setFillColor (sortable.color );
94122 shape.setPosition (sf::Vector2f (sortable.width * index++, sortController.winHeight - sortable.height ));
95123 window.draw (shape);
96124 }
0 commit comments