comparison src/main.c @ 32:b1a605eb721a

accept user inputs
author Dennis C. M. <dennis@denniscm.com>
date Thu, 29 Jun 2023 19:45:49 +0100
parents 61104b22a25d
children 8ed1256dd518
comparison
equal deleted inserted replaced
31:61104b22a25d 32:b1a605eb721a
2 2
3 3
4 int window_width = 1920; 4 int window_width = 1920;
5 int window_height = 1080; 5 int window_height = 1080;
6 int vpadding = 150; 6 int vpadding = 150;
7 int rect_width = 30; 7 int rect_width = 5;
8 int space = 1; 8 int space = 1;
9 9
10 struct Algo algos[2]; 10 struct Algo algos[2];
11 int selected_algo = 0; 11 int selected_algo = 0;
12 int algos_size; 12 int algos_size;
246 exit(1); 246 exit(1);
247 } 247 }
248 } 248 }
249 249
250 250
251 int main(int argc, char** argv) { 251 int main(int argc, char *argv[]) {
252 if (argc - 1 == 4) {
253 int args[4];
254
255 printf("Values provided: ");
256
257 for (int i = 1; i < argc; i++) {
258 printf("%s ", argv[i]);
259
260 char* end_ptr;
261 long value = strtol(argv[i], &end_ptr, 10);
262
263 if (end_ptr == argv[i]) {
264 printf("Invalid argument\n");
265 exit(1);
266 }
267
268 if ((value == LONG_MAX || value == LONG_MIN)) {
269 printf("Integer out of range\n");
270 exit(1);
271 }
272
273 args[i - 1] = (int)value;
274 }
275
276 printf("\n");
277
278 printf("Window width: %i\n", args[0]);
279 window_width = args[0];
280
281 printf("Window height: %i\n", args[1]);
282 window_height = args[1];
283
284 printf("Rectangle width: %i\n", args[2]);
285 rect_width = args[2];
286
287 printf("Space: %i\n", args[3]);
288 space = args[3];
289
290 } else {
291 printf("Using default values\n");
292 }
293
252 algo_args.arr_size = window_width / (rect_width + space); 294 algo_args.arr_size = window_width / (rect_width + space);
253 algo_args.arr = malloc(algo_args.arr_size * sizeof(struct Element)); 295 algo_args.arr = malloc(algo_args.arr_size * sizeof(struct Element));
254 algo_args.comparisons = 0; 296 algo_args.comparisons = 0;
255 algo_args.pause = false; 297 algo_args.pause = false;
256 algo_args.sequentially = false; 298 algo_args.sequentially = false;
257 algo_args.delay = 100; 299 algo_args.delay = 100;
258 300
259 strcpy(algos[0].name, "Bubble sort"); 301 strcpy(algos[0].name, "Bubble sort");
260 algos[0].function = bubble_sort; 302 algos[0].function = bubble_sort;
261 303
262 strcpy(algos[1].name, "Selection sort"); 304 strcpy(algos[1].name, "Selection sort");
263 algos[1].function = selection_sort; 305 algos[1].function = selection_sort;
264 306
265 algos_size = sizeof(algos) / sizeof(algos[0]); 307 algos_size = sizeof(algos) / sizeof(algos[0]);
266 308
268 randomize_array(algo_args.arr, algo_args.arr_size); 310 randomize_array(algo_args.arr, algo_args.arr_size);
269 311
270 glutInit(&argc, argv); 312 glutInit(&argc, argv);
271 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 313 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
272 glutInitWindowSize(window_width, window_height); 314 glutInitWindowSize(window_width, window_height);
273 glutCreateWindow("Algorithm animator"); 315 glutCreateWindow("Visualization of sorting algorithms | Developed by Dennis CM");
274 316
275 setup_gl(); 317 setup_gl();
276 setup_freetype(); 318 setup_freetype();
277 319
278 glutDisplayFunc(display); 320 glutDisplayFunc(display);