comparison main.c @ 18:6a5c5b137348

implement method to run the algo function given the selected algo
author Dennis C. M. <dennis@denniscm.com>
date Sun, 25 Jun 2023 19:23:49 +0100
parents fba66d02f1cf
children a027304ec75d
comparison
equal deleted inserted replaced
17:fba66d02f1cf 18:6a5c5b137348
18 /* Global variables */ 18 /* Global variables */
19 19
20 FT_Library ft_library; 20 FT_Library ft_library;
21 FT_Face ft_face; 21 FT_Face ft_face;
22 22
23 char algos[4][50] = { 23 struct Algo {
24 "Bubble sort", 24 char name[50];
25 "Selection sort", 25 void (*function)();
26 "Insertion sort",
27 "Quick sort"
28 }; 26 };
27
28 struct Algo algos[1];
29 29
30 int selected_algo = 0; 30 int selected_algo = 0;
31 int speed = 5; 31 int speed = 5;
32 int refresh_counter = 0; 32 int refresh_counter = 0;
33 int iter_counter = 0; 33 int iter_counter = 0;
160 int adjusted_y = y + (slot->bitmap_top - glyph_bitmap->rows); 160 int adjusted_y = y + (slot->bitmap_top - glyph_bitmap->rows);
161 161
162 glRasterPos2f(x, adjusted_y); 162 glRasterPos2f(x, adjusted_y);
163 glDrawPixels(glyph_bitmap->width, glyph_bitmap->rows, GL_LUMINANCE, GL_UNSIGNED_BYTE, glyph_bitmap->buffer); 163 glDrawPixels(glyph_bitmap->width, glyph_bitmap->rows, GL_LUMINANCE, GL_UNSIGNED_BYTE, glyph_bitmap->buffer);
164 164
165 x += 15; 165 x += slot->advance.x / 64;
166 } 166 }
167 } 167 }
168 168
169 169
170 void display() { 170 void display() {
193 glEnd(); 193 glEnd();
194 194
195 // Render text 195 // Render text
196 char text[256]; 196 char text[256];
197 197
198
199 // Top: Column 1 198 // Top: Column 1
200 sprintf(text, "Algorithm: %s", algos[selected_algo]); 199 sprintf(text, "Algorithm: %s", algos[selected_algo].name);
201 render_text(20, WINDOW_HEIGHT - 50, text); 200 render_text(20, WINDOW_HEIGHT - 50, text);
202 201
203 sprintf(text, "Speed: %i", speed); 202 sprintf(text, "Speed: %i", speed);
204 render_text(20, WINDOW_HEIGHT - 80, text); 203 render_text(20, WINDOW_HEIGHT - 80, text);
205 204
225 224
226 /* Refresh function */ 225 /* Refresh function */
227 226
228 void idle() { 227 void idle() {
229 if (run) { 228 if (run) {
230 bubble_sort(); 229 algos[selected_algo].function();
231 refresh_counter++; 230 refresh_counter++;
232 iter_counter++; 231 iter_counter++;
233 232
234 if (refresh_counter == speed) { 233 if (refresh_counter == speed) {
235 glutPostRedisplay(); 234 glutPostRedisplay();
270 run = false; 269 run = false;
271 270
272 // Reset algo steps 271 // Reset algo steps
273 bs = (struct BubbleSortInfo){0, 0}; 272 bs = (struct BubbleSortInfo){0, 0};
274 } 273 }
275 274
275 // u: Increase speed
276 if (key == 117) {
277 speed++;
278 }
279
280 // d: reduce speed
281 if (key == 100) {
282 speed--;
283 }
284
276 // enter: Run program 285 // enter: Run program
277 if (key == 13) { 286 if (key == 13) {
278 run = true; 287 run = true;
279 } 288 }
280 289
281 // u: Increase speed 290
282 if (key == 117) {
283 speed++;
284 }
285
286 // d: reduce speed
287 if (key == 100) {
288 speed--;
289 }
290 } 291 }
291 292
292 293
293 /* Set up functions */ 294 /* Set up functions */
294 295
347 } 348 }
348 } 349 }
349 350
350 351
351 int main(int argc, char** argv) { 352 int main(int argc, char** argv) {
353 strcpy(algos[0].name, "Bubble sort");
354 algos[0].function = &bubble_sort;
355
352 create_array(); 356 create_array();
353 357
354 glutInit(&argc, argv); 358 glutInit(&argc, argv);
355 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 359 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
356 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); 360 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);