Mercurial > public > algo-animator
annotate main.c @ 21:8a5a7aee69ce
add description
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 27 Jun 2023 20:04:25 +0100 |
parents | fc44102980fb |
children |
rev | line source |
---|---|
8 | 1 #include <stdio.h> |
9 | 2 #include <stdlib.h> |
7 | 3 #include <GL/glut.h> |
9 | 4 #include <math.h> |
10 | 5 #include <time.h> |
12 | 6 #include <stdbool.h> |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
7 #include <ft2build.h> |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
8 #include FT_FREETYPE_H |
0 | 9 |
10 | |
21 | 11 #define WINDOW_WIDTH 1920 |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
12 #define WINDOW_HEIGHT 1080 |
16 | 13 #define VPADDING 150 |
7 | 14 #define RECT_WIDTH 5 |
10 | 15 #define SPACE 1 |
7 | 16 |
17 | |
21 | 18 /* Helper functions */ |
19 | |
20 struct Element { | |
21 float value; | |
22 bool current; | |
23 }; | |
24 | |
25 struct Element *arr; | |
26 int arr_size; | |
27 | |
28 void create_array() { | |
29 arr_size = WINDOW_WIDTH / (RECT_WIDTH + SPACE); | |
30 arr = malloc(arr_size * sizeof(struct Element)); | |
31 | |
32 float rect_increase = (WINDOW_HEIGHT - VPADDING * 2) / (float)(arr_size - 1); | |
33 | |
34 for (int i = 1; i <= arr_size; i++) { | |
35 arr[i - 1].value = i * rect_increase; | |
36 arr[i - 1].current = false; | |
37 } | |
38 } | |
39 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
40 |
21 | 41 void swap_elements(int x, int y) { |
42 struct Element temp = arr[x]; | |
43 arr[x] = arr[y]; | |
44 arr[y] = temp; | |
45 } | |
46 | |
47 | |
48 void randomize_array() { | |
49 srand(time(NULL)); | |
50 | |
51 // Fisher-Yates shuffle | |
52 for (int i = arr_size - 1; i > 0; i--) { | |
53 int j = rand() % (i + 1); | |
54 | |
55 // Swap | |
56 swap_elements(i, j); | |
57 } | |
58 } | |
59 | |
60 | |
61 bool array_sorted() { | |
62 for (int i = 0; i < arr_size - 1; i++) { | |
63 if (arr[i].value > arr[i + 1].value) { | |
64 return false; | |
65 } | |
66 } | |
67 | |
68 return true; | |
69 } | |
70 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
71 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
72 struct Algo { |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
73 char name[50]; |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
74 void (*function)(); |
12 | 75 }; |
76 | |
19 | 77 struct Algo algos[2]; |
9 | 78 int selected_algo = 0; |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
79 |
21 | 80 void algo_selector(int direction) { |
81 int selection = selected_algo + direction; | |
82 int lower = 0; | |
83 int upper = (sizeof(algos) / sizeof(algos[0])) - 1; | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
84 |
21 | 85 if (selection >= lower && selection <= upper) { |
86 selected_algo = selection; | |
87 } | |
88 } | |
0 | 89 |
90 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
91 /* Algorithms */ |
12 | 92 |
19 | 93 // Just some variables to store the state of the running algorithm |
94 struct AlgoState { | |
95 int a; | |
96 int b; | |
97 int c; | |
12 | 98 }; |
99 | |
21 | 100 struct AlgoState as; |
19 | 101 |
21 | 102 void reset_state() { |
103 as.a = 0; | |
104 as.b = 0; | |
105 as.c = 0; | |
19 | 106 } |
107 | |
12 | 108 |
9 | 109 void bubble_sort() { |
110 | |
19 | 111 /* |
112 * a: Index of the current selection | |
113 * b: Index boundary of the sorted array | |
114 */ | |
115 | |
116 if (as.a < arr_size - 1 - as.b) { | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
117 arr[as.a].current = true; |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
118 |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
119 if (arr[as.a].value > arr[as.a + 1].value) { |
19 | 120 swap_elements(as.a + 1, as.a); |
11 | 121 } |
9 | 122 |
19 | 123 as.a++; |
11 | 124 } else { |
19 | 125 as.b++; |
126 as.a = 0; | |
127 } | |
128 } | |
129 | |
130 | |
131 void selection_sort() { | |
132 | |
133 /* | |
134 * a: Index of current selection | |
135 * b: Index of boundary of sorted array | |
136 * c: Index of the minimum element | |
137 */ | |
21 | 138 |
19 | 139 |
140 if (as.a < arr_size) { | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
141 arr[as.a].current = true; |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
142 |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
143 if (arr[as.a].value < arr[as.c].value) { |
21 | 144 |
19 | 145 // Save new minimum |
146 as.c = as.a; | |
147 } | |
148 | |
149 as.a++; | |
150 } else { | |
151 swap_elements(as.b, as.c); | |
152 | |
153 as.b++; | |
154 as.a = as.b; | |
155 as.c = as.a; | |
9 | 156 } |
157 } | |
158 | |
159 | |
21 | 160 void quick_sort() { |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
161 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
162 } |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
163 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
164 |
21 | 165 void insertion_sort() { |
10 | 166 |
167 } | |
168 | |
169 | |
21 | 170 void merge_sort() { |
9 | 171 |
172 } | |
173 | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
174 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
175 /* Render functions */ |
8 | 176 |
21 | 177 FT_Library ft_library; |
178 FT_Face ft_face; | |
179 | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
180 void render_text(int x, int y, char* text) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
181 for (const char *c = text; *c; c++) { |
21 | 182 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
183 // Get glyph index from character code |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
184 FT_UInt glyph_index = FT_Get_Char_Index(ft_face, *c); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
185 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
186 if (glyph_index == 0) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
187 fprintf(stderr, "Given character code has no glyph image in the face\n"); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
188 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
189 } |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
190 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
191 // Load glyph image |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
192 if (FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_DEFAULT)) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
193 fprintf(stderr, "Failed to load glyph.\n"); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
194 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
195 } |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
196 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
197 // Render glyph |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
198 if (FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL)) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
199 fprintf(stderr, "Failed to render glyph.\n"); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
200 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
201 } |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
202 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
203 FT_GlyphSlot slot = ft_face->glyph; |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
204 FT_Bitmap* glyph_bitmap = &slot->bitmap; |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
205 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
206 // Flip the bitmap vertically |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
207 unsigned char* flipped_bitmap = (unsigned char*)malloc(glyph_bitmap->width * glyph_bitmap->rows); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
208 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
209 for (int row = 0; row < glyph_bitmap->rows; row++) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
210 unsigned char* src_row = glyph_bitmap->buffer + (row * glyph_bitmap->width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
211 unsigned char* dest_row = flipped_bitmap + ((glyph_bitmap->rows - row - 1) * glyph_bitmap->width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
212 memcpy(dest_row, src_row, glyph_bitmap->width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
213 } |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
214 |
21 | 215 glyph_bitmap->buffer = flipped_bitmap; |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
216 |
21 | 217 // Calculate the adjusted y position based on the glyph's bearing |
218 int adjusted_y = y + (slot->bitmap_top - glyph_bitmap->rows); | |
219 | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
220 glRasterPos2f(x, adjusted_y); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
221 glDrawPixels(glyph_bitmap->width, glyph_bitmap->rows, GL_LUMINANCE, GL_UNSIGNED_BYTE, glyph_bitmap->buffer); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
222 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
223 x += slot->advance.x / 64; |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
224 } |
0 | 225 } |
226 | |
227 | |
21 | 228 int speed = 50; |
229 int iter_counter = 0; | |
230 | |
0 | 231 void display() { |
232 glClear(GL_COLOR_BUFFER_BIT); | |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
233 |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
234 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
235 |
10 | 236 int x = 0; |
237 for (int i = 0; i < arr_size; i++) { | |
21 | 238 |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
239 if (arr[i].current) { |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
240 glColor3f(1.0, 1.0, 1.0); |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
241 } else { |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
242 glColor3f(1.0, 0.7569, 0.0); |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
243 } |
10 | 244 |
245 // Bottom left | |
16 | 246 glVertex2f(x, VPADDING); |
10 | 247 |
248 // Top left | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
249 glVertex2f(x, VPADDING + arr[i].value); |
10 | 250 |
251 // Top right | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
252 glVertex2f(x + RECT_WIDTH, VPADDING + arr[i].value); |
10 | 253 |
254 // Bottom right | |
16 | 255 glVertex2f(x + RECT_WIDTH, VPADDING); |
10 | 256 |
257 x += RECT_WIDTH + SPACE; | |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
258 |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
259 arr[i].current = false; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
260 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
261 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
262 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
263 |
7 | 264 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
265 char text[256]; |
9 | 266 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
267 // Top: Column 1 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
268 sprintf(text, "Algorithm: %s", algos[selected_algo].name); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
269 render_text(20, WINDOW_HEIGHT - 50, text); |
21 | 270 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
271 sprintf(text, "Speed: %i", speed); |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
272 render_text(20, WINDOW_HEIGHT - 80, text); |
21 | 273 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
274 // Top: Column 2 |
10 | 275 sprintf(text, "Number of elements: %i", arr_size); |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
276 render_text(500, WINDOW_HEIGHT - 50, text); |
9 | 277 |
12 | 278 sprintf(text, "Iterations: %i", iter_counter); |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
279 render_text(500, WINDOW_HEIGHT - 80, text); |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
280 |
12 | 281 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
282 // Bottom: Column 1 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
283 render_text(20, VPADDING - 50, "Press a or s to select an algorithm."); |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
284 render_text(20, VPADDING - 80, "Press u or d to modify speed."); |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
285 render_text(20, VPADDING - 110, "Press r to randomize the array."); |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
286 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
287 // Bottom: Column 2 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
288 render_text(800, VPADDING - 50, "Press enter to run the algorithm."); |
21 | 289 render_text(800, VPADDING - 80, "Press p to pause the algorithm."); |
8 | 290 |
291 glutSwapBuffers(); | |
292 } | |
293 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
294 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
295 /* Refresh function */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
296 |
21 | 297 |
298 bool run; | |
299 int refresh_counter = 0; | |
300 | |
12 | 301 void idle() { |
302 if (run) { | |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
303 algos[selected_algo].function(); |
12 | 304 refresh_counter++; |
305 iter_counter++; | |
306 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
307 if (refresh_counter == speed) { |
12 | 308 glutPostRedisplay(); |
309 refresh_counter = 0; | |
310 } | |
311 | |
312 } else { | |
313 glutPostRedisplay(); | |
314 } | |
315 | |
316 if (array_sorted()) { | |
317 run = false; | |
318 } | |
8 | 319 } |
320 | |
321 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
322 /* User input handler */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
323 |
8 | 324 void keyboard(unsigned char key, int x, int y) { |
325 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
326 // s: Next algorithm |
9 | 327 if (key == 115) { |
328 algo_selector(1); | |
329 } | |
330 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
331 // a: Previous algorithm |
9 | 332 if (key == 97) { |
333 algo_selector(-1); | |
334 } | |
335 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
336 // r: Reset state |
12 | 337 if (key == 114) { |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
338 randomize_array(); |
16 | 339 |
340 // Reset state | |
12 | 341 iter_counter = 0; |
342 refresh_counter = 0; | |
343 run = false; | |
16 | 344 |
345 // Reset algo steps | |
21 | 346 reset_state(); |
12 | 347 } |
21 | 348 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
349 // u: Increase speed |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
350 if (key == 117) { |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
351 speed++; |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
352 } |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
353 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
354 // d: reduce speed |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
355 if (key == 100) { |
19 | 356 if (speed > 1) { |
357 speed--; | |
358 } | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
359 } |
21 | 360 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
361 // enter: Run program |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
362 if (key == 13) { |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
363 run = true; |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
364 } |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
365 |
19 | 366 // p: Pause program |
367 if (key == 112) { | |
368 run = false; | |
369 } | |
0 | 370 } |
371 | |
372 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
373 /* Set up functions */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
374 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
375 void setup_gl() { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
376 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
377 // Set background dark |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
378 glClearColor(0.0, 0.0, 0.0, 1.0); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
379 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
380 // Set point color and size to 1 pixel |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
381 glColor3f(1.0, 0.7569, 0.0); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
382 glPointSize(5.0); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
383 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
384 // Matrix projection and reset with identity |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
385 glMatrixMode(GL_PROJECTION); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
386 glLoadIdentity(); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
387 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
388 /* |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
389 * Creates projection matrix |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
390 * x increases from left to right (0 to WINDOW_WIDTH) |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
391 * y increases from bottom to top (0 to WINDOW_HEIGHT) |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
392 */ |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
393 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
394 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
395 |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
396 /* |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
397 * This fucking line... I spent a day rendering weird symbols |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
398 * because the padding that adds FreeType to each row of the bitmap |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
399 * does not match the padding expected by GL. |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
400 */ |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
401 |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
402 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
403 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
404 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
405 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
406 void setup_freetype() { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
407 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
408 // Init library |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
409 if (FT_Init_FreeType(&ft_library)) { |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
410 fprintf(stderr, "Failed to initialize FreeType library\n"); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
411 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
412 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
413 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
414 // Load font |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
415 if (FT_New_Face(ft_library, "fonts/JetBrainsMono-Regular.ttf", 0, &ft_face)) { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
416 fprintf(stderr, "Failed to load font\n"); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
417 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
418 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
419 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
420 // Set font size |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
421 if (FT_Set_Pixel_Sizes(ft_face, 0, 24)) { |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
422 fprintf(stderr, "Failed to set font size.\n"); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
423 FT_Done_Face(ft_face); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
424 FT_Done_FreeType(ft_library); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
425 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
426 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
427 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
428 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
429 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
430 |
0 | 431 int main(int argc, char** argv) { |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
432 strcpy(algos[0].name, "Bubble sort"); |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
433 algos[0].function = &bubble_sort; |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
434 |
19 | 435 strcpy(algos[1].name, "Selection sort"); |
436 algos[1].function = &selection_sort; | |
21 | 437 |
10 | 438 create_array(); |
7 | 439 |
0 | 440 glutInit(&argc, argv); |
8 | 441 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
442 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
443 glutCreateWindow("Algorithm animator"); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
444 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
445 setup_gl(); |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
446 setup_freetype(); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
447 |
0 | 448 glutDisplayFunc(display); |
8 | 449 glutKeyboardFunc(keyboard); |
12 | 450 glutIdleFunc(idle); |
0 | 451 glutMainLoop(); |
452 | |
10 | 453 free(arr); |
7 | 454 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
455 FT_Done_Face(ft_face); |
21 | 456 FT_Done_FreeType(ft_library); |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
457 |
0 | 458 return 0; |
459 } |