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