-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
235 lines (196 loc) · 8.17 KB
/
main.cpp
File metadata and controls
235 lines (196 loc) · 8.17 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
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
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <unordered_map>
#include "include/Array.hpp"
#include "include/rectangle/AllRectangles.hpp"
#include "include/sorting/AllSorts.hpp"
// =====================================================================================================================
/*
* Arg Parser for Sorting visualizer
*/
struct Args {
static const std::array<std::string, 11> algorithms;
static const std::unordered_map<std::string, int> algo_ids;
int width = 1000, height = 600, array_size = 100;
std::string sorting_algorithm = "Bubblesort";
int algo_id = 2;
};
const std::array<std::string, 11> Args::algorithms{"Bogosort", "Bubblesort", "Cyclesort", "Heapsort", "Insertionsort",
"Mergesort", "Quicksort", "Radixsort", "Selectionsort", "Shellsort",
"Stoogesort"};
const std::unordered_map<std::string, int> Args::algo_ids{{"Bogosort", 1},
{"Bubblesort", 2},
{"Cyclesort", 3},
{"Heapsort", 4},
{"Insertionsort", 5},
{"Mergesort", 6},
{"Quicksort", 7},
{"Radixsort", 8},
{"Selectionsort", 9},
{"Shellsort", 10},
{"Stoogesort", 11}};
void parse_args(int argc, char *argv[], Args *args) {
for (int i = 1; i < argc; ++i) {
std::string arg{argv[i]};
if (arg.length() == 2 and arg[0] == '-') {
switch (arg[1]) {
case 'w':
args->width = atoi(argv[++i]);
continue;
case 'h':
args->height = atoi(argv[++i]);
continue;
case 'n':
args->array_size = atoi(argv[++i]);
continue;
case 'a':
const std::string &algo{argv[++i]};
if (std::find(Args::algorithms.begin(), Args::algorithms.end(), algo) != Args::algorithms.end()) {
args->algo_id = Args::algo_ids.at(algo);
args->sorting_algorithm = algo;
} else {
std::cerr << "Not supported sorting algorithm!\n";
std::cerr << "Currently implemented!\n";
for (const auto &algo: Args::algorithms) {
std::cerr << "\t-" << algo << "\n";
}
}
continue;
}
} else {
std::cerr << "Invalid parameter <" << arg
<< ">. Sorting Visualizer only supports single character parameters:\n";
std::cerr << "\t-w width of the opened window.\n";
std::cerr << "\t-h height of the opened window.\n";
std::cerr << "\t-n number of elements in the array to sort.\n";
std::cerr << "\t-a name of the sorting algorithm.\n";
}
}
}
// =====================================================================================================================
struct AppState {
const unsigned int MAX_DELAY = 5;
int window_width, window_height;
SDL_Window *window{nullptr};
SDL_Renderer *renderer{nullptr};
Array array;
AppState(const int width, const int height, const int array_size) : window_width(width), window_height(height),
array(RAINBOW, array_size,
static_cast<float>(width),
static_cast<float>(height)) {}
};
Sort *sorter;
static inline void print_welcome() {
std::cout << "===============================================\n";
std::cout << " SDL SORTING VISUALIZER - v1.0\n";
std::cout << "===============================================\n";
std::cout << " Welcome! Watch your algorithms in real-time.\n\n";
std::cout << " CONTROLS:\n";
std::cout << " [S] : Shuffle the list and reset\n";
std::cout << " [B] : Boost speed (skips ~100 steps)\n\n";
std::cout << " Status: Ready to sort...\n";
std::cout << "-----------------------------------------------\n";
}
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
print_welcome();
if (!SDL_Init(SDL_INIT_VIDEO)) return SDL_APP_FAILURE;
Args args{};
parse_args(argc, argv, &args);
auto *state = new AppState(args.width, args.height, args.array_size);
std::string window_title{"Sorting Visualizer " + args.sorting_algorithm};
SDL_CreateWindowAndRenderer(window_title.c_str(), state->window_width, state->window_height, SDL_WINDOW_RESIZABLE,
&state->window,
&state->renderer);
if (!state->window || !state->renderer) {
SDL_Log("Window or Renderer creation failed: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
// setup Random Seed
std::srand((unsigned) std::time(nullptr));
switch (args.algo_id) {
case 1:
sorter = new Bogosort(&state->array);
break;
case 2:
sorter = new Bubblesort(&state->array);
break;
case 3:
sorter = new Cyclesort(&state->array);
break;
case 4:
sorter = new Heapsort(&state->array);
break;
case 5:
sorter = new Insertionsort(&state->array);
break;
case 6:
sorter = new Mergesort(&state->array);
break;
case 7:
sorter = new Quicksort(&state->array);
break;
case 8:
sorter = new Radixsort(&state->array);
break;
case 9:
sorter = new Selectionsort(&state->array);
break;
case 10:
sorter = new Shellsort(&state->array);
break;
case 11:
sorter = new Stoogesort(&state->array);
break;
}
*appstate = state;
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
auto *state = static_cast<AppState *>(appstate);
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS;
}
if (event->type == SDL_EVENT_WINDOW_RESIZED) {
const int new_width = event->window.data1;
const int new_height = event->window.data2;
state->array.window_resize(state->window_width, state->window_height, new_width, new_height);
state->window_width = new_width;
state->window_height = new_height;
}
if (event->type == SDL_EVENT_KEY_DOWN) {
if (event->key.key == 115) {
// Key: s
state->array.shuffle();
sorter->reset();
}
// std::cout << event->key.key<< std::endl;
if (event->key.key == 98) {
// Key: b
sorter->sort(1000);
}
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void *appstate) {
auto *state = static_cast<AppState *>(appstate);
SDL_RenderClear(state->renderer);
SDL_SetRenderDrawColor(state->renderer, 69, 69, 69, 255);
SDL_RenderFillRect(state->renderer, nullptr);
for (int i = 0; i < state->array.size(); ++i) {
state->array[i]->draw(state->renderer);
}
SDL_RenderPresent(state->renderer);
sorter->sort(1);
SDL_Delay(state->MAX_DELAY);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
const auto *state = static_cast<AppState *>(appstate);
delete state;
delete sorter;
SDL_Quit();
}