Skip to content

Commit 0d67d7d

Browse files
committed
Initial commit
0 parents  commit 0d67d7d

15 files changed

+2157
-0
lines changed

BubbleSort0824.cpp

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
}

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# CMakeList.txt : CMake project for CMakeSFMLProject, include source and define
2+
cmake_minimum_required(VERSION 3.8)
3+
project(CMakeSFMLProject)
4+
5+
include(FetchContent)
6+
FetchContent_Declare(SFML
7+
GIT_REPOSITORY https://github.com/SFML/SFML.git
8+
GIT_TAG 2.6.x)
9+
FetchContent_MakeAvailable(SFML)
10+
11+
add_executable(CMakeSFMLProject "CMakeSFMLProject.cpp" "BubbleSort0824.cpp" "InsertionSort0824.cpp" "ShakerSort0824.cpp" "SelectionSort0824.cpp" "DoubleSelectionSort0824.cpp" "info0824.h" "algorithm0824.h" )
12+
target_link_libraries(CMakeSFMLProject PRIVATE sfml-graphics)
13+
target_compile_features(CMakeSFMLProject PRIVATE cxx_std_17)
14+
add_custom_command(
15+
TARGET CMakeSFMLProject POST_BUILD
16+
COMMAND ${CMAKE_COMMAND} -E copy_directory
17+
${CMAKE_SOURCE_DIR}/Resources
18+
${CMAKE_CURRENT_BINARY_DIR}/Release)
19+
20+
add_custom_command(
21+
TARGET CMakeSFMLProject POST_BUILD
22+
COMMAND ${CMAKE_COMMAND} -E copy_directory
23+
${CMAKE_SOURCE_DIR}/Resources
24+
${CMAKE_CURRENT_BINARY_DIR}/Debug)
25+
26+
if (WIN32 AND BUILD_SHARED_LIBS)
27+
add_custom_command(TARGET CMakeSFMLProject POST_BUILD
28+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:CMakeSFMLProject> $<TARGET_FILE_DIR:CMakeSFMLProject> COMMAND_EXPAND_LISTS)
29+
endif()
30+
31+
install(TARGETS CMakeSFMLProject)

CMakePresets.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "windows-base",
6+
"hidden": true,
7+
"generator": "Ninja",
8+
"binaryDir": "${sourceDir}/out/build/${presetName}",
9+
"installDir": "${sourceDir}/out/install/${presetName}",
10+
"cacheVariables": {
11+
"CMAKE_C_COMPILER": "cl.exe",
12+
"CMAKE_CXX_COMPILER": "cl.exe"
13+
},
14+
"condition": {
15+
"type": "equals",
16+
"lhs": "${hostSystemName}",
17+
"rhs": "Windows"
18+
}
19+
},
20+
{
21+
"name": "x64-debug",
22+
"displayName": "x64 Debug",
23+
"inherits": "windows-base",
24+
"architecture": {
25+
"value": "x64",
26+
"strategy": "external"
27+
},
28+
"cacheVariables": {
29+
"CMAKE_BUILD_TYPE": "Debug"
30+
}
31+
},
32+
{
33+
"name": "x64-release",
34+
"displayName": "x64 Release",
35+
"inherits": "x64-debug",
36+
"cacheVariables": {
37+
"CMAKE_BUILD_TYPE": "Release"
38+
}
39+
},
40+
{
41+
"name": "x86-debug",
42+
"displayName": "x86 Debug",
43+
"inherits": "windows-base",
44+
"architecture": {
45+
"value": "x86",
46+
"strategy": "external"
47+
},
48+
"cacheVariables": {
49+
"CMAKE_BUILD_TYPE": "Debug"
50+
}
51+
},
52+
{
53+
"name": "x86-release",
54+
"displayName": "x86 Release",
55+
"inherits": "x86-debug",
56+
"cacheVariables": {
57+
"CMAKE_BUILD_TYPE": "Release"
58+
}
59+
}
60+
]
61+
}

0 commit comments

Comments
 (0)