1+
2+ #include " algorithm0824.h"
3+ #include " info0824.h"
4+
5+ int runBubbleSort () {
6+
7+ sf::RenderWindow window (sf::VideoMode (960 , 540 ), " Bubble Sort" );
8+ window.setFramerateLimit (targetFrameRate);
9+
10+ bool sorted = false ;
11+ int lines = linesCount;
12+ int iteration = 0 ;
13+ int limit = lines - 2 ;
14+
15+ sf::RectangleShape rectangle (sf::Vector2f (1 .f , 1 .f ));
16+ rectangle.setOutlineThickness (1 );
17+ rectangle.setOutlineColor (sf::Color (143 , 114 , 74 ));
18+
19+ sf::Texture texture;
20+ if (!texture.loadFromFile (" Resources/Textures/background.jpg" ))
21+ {
22+ return 1 ;
23+ }
24+ float scaleX = window.getSize ().x / 1920 .f ;
25+ float scaleY = window.getSize ().y / 1080 .f ;
26+
27+ sf::Sprite background;
28+ background.setTexture (texture);
29+ background.setScale (sf::Vector2f (scaleX, scaleY));
30+
31+ int Bar_width = window.getSize ().x / lines;
32+ for (int i = 0 ; i < lines; ++i)
33+ {
34+ line[i].height = (std::rand () % (window.getSize ().y - 30 ) + 10 );
35+ line[i].pos = Bar_width * i + Bar_width;
36+ }
37+
38+ sf::Font font;
39+ if (!font.loadFromFile (" Resources/Font/coolvetica_rg_it.otf" )) {
40+ std::cerr << " Failed to load font." << std::endl;
41+ return 1 ;
42+ }
43+ // Navigation Buttons
44+ constexpr int sortedButtonCount = 3 ;
45+ sf::RectangleShape sortedButton[sortedButtonCount];
46+ sf::Text sortedText[sortedButtonCount];
47+ sortedText[0 ].setString (" Open The Menu" );
48+ sortedText[1 ].setString (" Restart The Algorithm" );
49+ sortedText[2 ].setString (" Exit The Application" );
50+ sortedText[0 ].setPosition (sf::Vector2f (window.getSize ().x * 0.05 , window.getSize ().y / 8.0 ));
51+ sortedText[1 ].setPosition (sf::Vector2f (window.getSize ().x * 0.05 , window.getSize ().y / 8.0 * 2.0 ));
52+ sortedText[2 ].setPosition (sf::Vector2f (window.getSize ().x * 0.05 , window.getSize ().y / 8.0 * 3.0 ));
53+ for (int i = 0 ; i < sortedButtonCount; ++i)
54+ {
55+ sortedText[i].setFont (font);
56+ sortedText[i].setCharacterSize (24 );
57+ sortedText[i].setFillColor (sf::Color (118 , 74 , 255 )); // B = 255 galas good visai 161
58+ sortedText[i].setOutlineColor (sf::Color (94 , 0 , 135 ));
59+ sortedText[i].setOutlineThickness (2 );
60+ sortedButton[i].setPosition (sf::Vector2f (sortedText[i].getPosition ().x - 5 , sortedText[i].getPosition ().y - 5 ));
61+ sortedButton[i].setSize (sf::Vector2f (sortedText[i].getGlobalBounds ().width * 1.05 , 50 ));
62+ sortedButton[i].setFillColor (sf::Color (88 , 69 , 97 , 127 ));
63+ }
64+ constexpr int infoTextCount = 6 ;
65+ sf::RectangleShape infoBackground;
66+ sf::Text infoText[infoTextCount];
67+ bool infoAppear = false ;
68+ for (int i = 0 ; i < infoTextCount; ++i)
69+ {
70+ infoText[i].setFillColor (sf::Color (118 , 74 , 255 ));
71+ infoText[i].setOutlineColor (sf::Color::Black);
72+ infoText[i].setOutlineThickness (1.3 );
73+ infoText[i].setFont (font);
74+ infoText[i].setCharacterSize (28 );
75+ }
76+ infoText[0 ].setString (" About:" );
77+ infoText[1 ].setString (" Bubble Sort is the simplest sorting" );
78+ infoText[2 ].setString (" algorithm that works by repeatedly " );
79+ infoText[3 ].setString (" swapping the adjacent elements " );
80+ infoText[4 ].setString (" if they are in the wrong order." );
81+
82+ infoBackground.setFillColor (sf::Color (88 , 69 , 97 , 127 ));
83+ infoBackground.setSize (sf::Vector2f (infoText[3 ].getGlobalBounds ().width * 1.14 , 240 ));
84+ infoBackground.setPosition (sf::Vector2f (window.getSize ().x - infoBackground.getGlobalBounds ().width ,
85+ infoBackground.getGlobalBounds ().height * 0.01 ));
86+ infoBackground.setOutlineColor (sf::Color (94 , 0 , 135 ));
87+ infoBackground.setOutlineThickness (3 );
88+
89+ infoText[0 ].setPosition (infoBackground.getPosition ().x + infoBackground.getGlobalBounds ().width * 0.35 ,
90+ infoBackground.getPosition ().y + 5 );
91+ for (int i = 1 ; i < infoTextCount; i++)
92+ {
93+
94+ infoText[i].setPosition (infoBackground.getPosition ().x * 1.02 , infoText[0 ].getPosition ().y + 45 * i);
95+ }
96+
97+ sf::RectangleShape infoButton;
98+ infoButton.setSize (sf::Vector2f (window.getSize ().y / 16 , window.getSize ().y / 16 ));
99+ infoButton.setFillColor (sf::Color (90 , 66 , 110 )); // -3, Because OutlineThickness = 3
100+ infoButton.setPosition (sf::Vector2f (window.getSize ().x - infoButton.getGlobalBounds ().width - 3 , 3 ));
101+ infoButton.setOutlineColor (sf::Color::Black);
102+ infoButton.setOutlineThickness (3 );
103+
104+ sf::Texture infoTexture;
105+ if (!infoTexture.loadFromFile (" Resources/Textures/infoButton.png" ))
106+ {
107+ std::cerr << " error loading info button texture" ;
108+ std::exit (1 );
109+ }
110+ sf::Sprite infoSprite;
111+ infoSprite.setTexture (infoTexture);
112+ infoSprite.setScale (0.068 , 0.068 );
113+ infoSprite.setPosition (infoButton.getPosition ().x , infoButton.getPosition ().y );
114+
115+ while (window.isOpen ())
116+ {
117+ sf::Event event;
118+ while (window.pollEvent (event))
119+ {
120+ if (event.type == sf::Event::Closed)
121+ window.close ();
122+ else if (event.type == sf::Event::MouseButtonPressed && sorted)
123+ {
124+ sf::Vector2i mousePosition = sf::Mouse::getPosition (window);
125+ for (int i = 0 ; i < 3 ; ++i)
126+ {
127+ if (sortedButton[i].getGlobalBounds ().contains (static_cast <float >(mousePosition.x ), static_cast <float >(mousePosition.y )))
128+ {
129+ switch (i)
130+ {
131+ case 0 :
132+ window.close ();
133+ break ;
134+ case 1 :
135+ window.close ();
136+ runBubbleSort ();
137+ break ;
138+ case 2 :
139+ std::exit (0 );
140+ break ;
141+ }
142+ }
143+ }
144+ if (infoButton.getGlobalBounds ().contains (static_cast <float >(mousePosition.x ), static_cast <float >(mousePosition.y )) && infoAppear == false )
145+ {
146+ infoAppear = true ;
147+ }
148+ else if (infoButton.getGlobalBounds ().contains (static_cast <float >(mousePosition.x ), static_cast <float >(mousePosition.y )) && infoAppear == true )
149+ {
150+ infoAppear = false ;
151+ }
152+ }
153+ }
154+ if (line[iteration].height > line[iteration + 1 ].height ) {
155+ int temp = line[iteration].height ;
156+ line[iteration].height = line[iteration + 1 ].height ;
157+ line[iteration + 1 ].height = temp;
158+ }
159+ window.clear ();
160+ window.draw (background);
161+
162+ for (int i = 0 ; i < lines; i++)
163+ {
164+ if (!sorted)
165+ rectangle.setFillColor (sf::Color (76 , 50 , 95 )); // Unsorted - Color
166+ if (iteration >= 0 && i > limit + 1 )
167+ rectangle.setFillColor (sf::Color (77 , 96 , 41 )); // Sorted - Color
168+ if ((iteration+1 == i) && !sorted)
169+ rectangle.setFillColor (sf::Color (131 , 130 , 119 )); // Moving bar - Color
170+
171+ rectangle.setPosition (sf::Vector2f (line[i].pos , window.getSize ().y ));
172+ rectangle.setSize (sf::Vector2f ((window.getSize ().x / lines) / 1 .1f , line[i].height ));
173+ rectangle.setRotation (180 );
174+
175+ window.draw (rectangle);
176+ }
177+ if (sorted)
178+ {
179+ for (int i = 0 ; i < 3 ; i++)
180+ {
181+ window.draw (sortedButton[i]);
182+ window.draw (sortedText[i]);
183+ }
184+ if (infoAppear == true )
185+ {
186+ window.draw (infoBackground);
187+ for (int i = 0 ; i < 5 ; i++)
188+ {
189+ window.draw (infoText[i]);
190+ }
191+
192+ }
193+ window.draw (infoButton);
194+ window.draw (infoSprite);
195+ }
196+
197+ window.display ();
198+ ++iteration;
199+ if (iteration > limit)
200+ {
201+ iteration = 0 ;
202+ --limit;
203+
204+ if (limit == 0 )
205+ sorted = true ;
206+
207+ }
208+ }
209+ return 0 ;
210+ }
0 commit comments