-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
259 lines (202 loc) · 5.44 KB
/
main.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Standard headers
#include <numeric>
#include <random>
// 3rd party headers
#include <SDL2/SDL.h>
#include <imgui.h>
#include <imgui_impl_sdl2.h>
#include <imgui_impl_sdlrenderer2.h>
// Project headers
#include "sorting_algo.hpp"
struct sorting_visualizer final
{
static int run();
private:
sorting_visualizer() noexcept;
~sorting_visualizer() noexcept;
bool handle(SDL_Event const &e) noexcept;
void update() noexcept;
void render() noexcept;
void imgui() noexcept;
SDL_Window *win;
SDL_Renderer *ren;
std::default_random_engine gen;
unsigned numbers[50] = {};
tnt::colors cs; // red and blue pivot indices
bool paused = true;
int active_algo = 0;
tnt::sorting_task sort;
};
int main(int, char **)
{
return sorting_visualizer::run();
}
int sorting_visualizer::run()
{
sorting_visualizer app{};
SDL_Event e;
bool running = true;
while (running)
{
while (SDL_PollEvent(&e))
running = app.handle(e);
app.update();
app.render();
}
return 0;
}
sorting_visualizer::sorting_visualizer() noexcept
: sort{sortviz::bubble_sort(numbers)}
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
std::exit(1);
}
win = SDL_CreateWindow(
"The TnT Sorting Visualizer",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1280, 720,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI //
);
if (win == nullptr)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
std::exit(1);
}
ren = SDL_CreateRenderer(
win, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC //
);
if (ren == nullptr)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
std::exit(1);
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplSDL2_InitForSDLRenderer(win, ren);
ImGui_ImplSDLRenderer2_Init(ren);
std::random_device rd;
gen.seed(rd());
std::iota(std::begin(numbers), std::end(numbers), 1);
std::ranges::shuffle(numbers, gen);
}
sorting_visualizer::~sorting_visualizer() noexcept
{
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
}
bool sorting_visualizer::handle(SDL_Event const &e) noexcept
{
if (e.type == SDL_QUIT)
return false;
else if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)
return false;
ImGui_ImplSDL2_ProcessEvent(&e);
return true;
}
void sorting_visualizer::update() noexcept
{
if (!paused)
{
if (!sort.done())
{
sort.resume();
cs = sort.value();
}
}
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
imgui();
}
void sorting_visualizer::render() noexcept
{
SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0xff);
SDL_RenderClear(ren);
SDL_SetRenderDrawColor(ren, 0xff, 0xff, 0xff, 0xff);
for (int i = 0; i < std::size(numbers); ++i)
{
auto const area = SDL_Rect{
.x = 12 + i * 25,
.y = int(720 - numbers[i] * 10),
.w = 25,
.h = int(numbers[i] * 10),
};
SDL_RenderFillRect(ren, &area);
}
{
SDL_SetRenderDrawColor(ren, 0xff, 0x00, 0x00, 0xff);
auto const area = SDL_Rect{
.x = int(12 + cs.red * 25),
.y = int(720 - numbers[cs.red] * 10),
.w = 25,
.h = int(numbers[cs.red] * 10),
};
SDL_RenderFillRect(ren, &area);
}
{
SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0xff, 0xff);
auto const area = SDL_Rect{
.x = int(12 + cs.blue * 25),
.y = int(720 - numbers[cs.blue] * 10),
.w = 25,
.h = int(numbers[cs.blue] * 10),
};
SDL_RenderFillRect(ren, &area);
}
ImGui::Render();
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(ren);
}
static constexpr const char *algo_names[] = {
"Bubble Sort",
"Selection Sort",
"Quick Sort",
"Heap Sort",
};
static constexpr tnt::sorting_task (*algo_ptr[])(std::span<unsigned>) = {
sortviz::bubble_sort,
sortviz::selection_sort,
sortviz::quick_sort,
sortviz::heap_sort,
};
void sorting_visualizer::imgui() noexcept
{
if (ImGui::Begin("Controls"))
{
if (ImGui::Button(paused ? "Play" : "Pause"))
paused = !paused;
ImGui::SameLine();
if (ImGui::Button("Shuffle"))
{
std::ranges::shuffle(numbers, gen);
sort = algo_ptr[active_algo](numbers);
}
if (paused)
{
if (ImGui::BeginCombo("Algorithm", algo_names[active_algo]))
{
for (int i = 0; i < std::size(algo_names); ++i)
{
bool const selected = active_algo == i;
if (ImGui::Selectable(algo_names[i], selected))
{
active_algo = i;
sort = algo_ptr[i](numbers);
}
if (selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
}
}
ImGui::End();
}