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