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