Mercurial > public > algo-animator
annotate src/main.c @ 51:8a9a7d020197
Update algorithms.h
Added function prototype for merge sort
committer: GitHub <noreply@github.com>
author | Pietro Molendys <89810437+Pedritos22@users.noreply.github.com> |
---|---|
date | Wed, 02 Apr 2025 01:12:40 +0200 |
parents | 62b7c457510d |
children | 468f82bd24ac |
rev | line source |
---|---|
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
1 #include "algorithms.h" |
50
62b7c457510d
Update main.c
Pietro Molendys <89810437+Pedritos22@users.noreply.github.com>
parents:
39
diff
changeset
|
2 #define GL_SILENCE_DEPRECATION |
62b7c457510d
Update main.c
Pietro Molendys <89810437+Pedritos22@users.noreply.github.com>
parents:
39
diff
changeset
|
3 |
0 | 4 |
5 | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
6 int window_width = 1920; |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
7 int window_height = 1080; |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
8 int vpadding = 150; |
32 | 9 int rect_width = 5; |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
10 int space = 1; |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
11 |
38
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
12 struct Algo algos[4]; |
9 | 13 int selected_algo = 0; |
30 | 14 int algos_size; |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
15 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
16 struct AlgoArgs algo_args; |
30 | 17 struct ThreadState thread_state; |
8 | 18 |
21 | 19 FT_Library ft_library; |
20 FT_Face ft_face; | |
21 | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
22 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
23 void render_text(int x, int y, char* text) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
24 for (const char *c = text; *c; c++) { |
21 | 25 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
26 // Get glyph index from character code |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
27 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
|
28 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
29 if (glyph_index == 0) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
30 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
|
31 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
32 } |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
33 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
34 // Load glyph image |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
35 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
|
36 fprintf(stderr, "Failed to load glyph.\n"); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
37 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
38 } |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
39 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
40 // Render glyph |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
41 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
|
42 fprintf(stderr, "Failed to render glyph.\n"); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
43 exit(1); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
44 } |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
45 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
46 FT_GlyphSlot slot = ft_face->glyph; |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
47 FT_Bitmap* glyph_bitmap = &slot->bitmap; |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
48 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
49 // Flip the bitmap vertically |
30 | 50 unsigned char* flipped_bitmap = (unsigned char*)malloc( |
51 glyph_bitmap->width * glyph_bitmap->rows); | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
52 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
53 for (int row = 0; row < glyph_bitmap->rows; row++) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
54 unsigned char* src_row = glyph_bitmap->buffer + (row * glyph_bitmap->width); |
30 | 55 unsigned char* dest_row = flipped_bitmap + ((glyph_bitmap->rows - row - 1) * |
56 glyph_bitmap->width); | |
57 | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
58 memcpy(dest_row, src_row, glyph_bitmap->width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
59 } |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
60 |
21 | 61 glyph_bitmap->buffer = flipped_bitmap; |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
62 |
21 | 63 // Calculate the adjusted y position based on the glyph's bearing |
64 int adjusted_y = y + (slot->bitmap_top - glyph_bitmap->rows); | |
65 | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
66 glRasterPos2f(x, adjusted_y); |
30 | 67 glDrawPixels(glyph_bitmap->width, glyph_bitmap->rows, GL_LUMINANCE, |
68 GL_UNSIGNED_BYTE, glyph_bitmap->buffer); | |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
69 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
70 x += slot->advance.x / 64; |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
71 } |
0 | 72 } |
73 | |
74 | |
75 void display() { | |
76 glClear(GL_COLOR_BUFFER_BIT); | |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
77 |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
78 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
79 |
10 | 80 int x = 0; |
38
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
81 for (int i = 0; i < algo_args.arr_size; i++) { |
21 | 82 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
83 if (algo_args.arr[i].current) { |
20
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
84 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
|
85 } else { |
fc44102980fb
change rectangle color to white when algorithm is selecting it
Dennis C. M. <dennis@denniscm.com>
parents:
19
diff
changeset
|
86 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
|
87 } |
10 | 88 |
89 // Bottom left | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
90 glVertex2f(x, vpadding); |
10 | 91 |
92 // Top left | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
93 glVertex2f(x, vpadding + algo_args.arr[i].value); |
10 | 94 |
95 // Top right | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
96 glVertex2f(x + rect_width, vpadding + algo_args.arr[i].value); |
10 | 97 |
98 // Bottom right | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
99 glVertex2f(x + rect_width, vpadding); |
10 | 100 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
101 x += rect_width + space; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
102 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
103 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
104 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
105 |
7 | 106 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
107 char text[256]; |
9 | 108 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
109 // 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
|
110 sprintf(text, "Algorithm: %s", algos[selected_algo].name); |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
111 render_text(20, window_height - 50, text); |
21 | 112 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
113 sprintf(text, "Delay: %u microseconds", algo_args.delay); |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
114 render_text(20, window_height - 80, text); |
21 | 115 |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
116 // Top: Column 2 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
117 sprintf(text, "Number of elements: %i", algo_args.arr_size); |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
118 render_text(500, window_height - 50, text); |
9 | 119 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
120 sprintf(text, "Comparisons: %i", algo_args.comparisons); |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
121 render_text(500, window_height - 80, text); |
12 | 122 |
30 | 123 // Top: Column 3 |
124 if (algo_args.pause && !algo_args.sequentially) { | |
125 sprintf(text, "PAUSED"); | |
126 render_text(window_width - 400, window_height - 50, text); | |
127 } | |
128 | |
129 if (algo_args.sequentially) { | |
130 sprintf(text, "SEQUENTIAL MODE"); | |
131 render_text(window_width - 400, window_height - 80, text); | |
132 } | |
133 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
134 // Bottom: Column 1 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
135 render_text(20, vpadding - 50, "Press a or s to select an algorithm."); |
31
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
136 render_text(20, vpadding - 80, "Press u or d to change the delay."); |
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
137 render_text(20, vpadding - 110, "Press r to reset."); |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
138 |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
139 // Bottom: Column 2 |
31
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
140 render_text(800, vpadding - 50, "Press enter to run or resume the algorithm."); |
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
141 render_text(800, vpadding - 80, "Press p to pause."); |
30 | 142 render_text(800, vpadding - 110, "Press q to enable or disable sequential mode."); |
8 | 143 |
144 glutSwapBuffers(); | |
145 } | |
146 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
147 |
12 | 148 void idle() { |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
149 glutPostRedisplay(); |
8 | 150 } |
151 | |
152 | |
153 void keyboard(unsigned char key, int x, int y) { | |
154 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
155 // s: Next algorithm |
9 | 156 if (key == 115) { |
30 | 157 algorithm_selector(algos, algos_size, 1, &selected_algo); |
9 | 158 } |
159 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
160 // a: Previous algorithm |
9 | 161 if (key == 97) { |
30 | 162 algorithm_selector(algos, algos_size, -1, &selected_algo); |
9 | 163 } |
164 | |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
165 // r: Reset state |
12 | 166 if (key == 114) { |
30 | 167 reset_state(&algo_args, &thread_state); |
12 | 168 } |
21 | 169 |
39 | 170 // u: Increase delay |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
171 if (key == 117) { |
31
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
172 change_delay(&algo_args, 10); |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
173 } |
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
174 |
39 | 175 // d: reduce delay |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
176 if (key == 100) { |
31
61104b22a25d
I think it is working now...
Dennis C. M. <dennis@denniscm.com>
parents:
30
diff
changeset
|
177 change_delay(&algo_args, -10); |
17
fba66d02f1cf
add randomize func and refactor UI
Dennis C. M. <dennis@denniscm.com>
parents:
16
diff
changeset
|
178 } |
21 | 179 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
180 // 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
|
181 if (key == 13) { |
30 | 182 run(&algo_args, algos, selected_algo, &thread_state); |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
183 } |
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
184 |
19 | 185 // p: Pause program |
186 if (key == 112) { | |
30 | 187 algo_args.pause = true; |
188 } | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
189 |
30 | 190 // q: Enable sequential mode |
191 if (key == 113) { | |
192 algo_args.sequentially = !algo_args.sequentially; | |
19 | 193 } |
0 | 194 } |
195 | |
196 | |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
197 void setup_gl() { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
198 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
199 // Set background dark |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
200 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
|
201 |
39 | 202 // Set point color and size |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
203 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
|
204 glPointSize(5.0); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
205 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
206 // 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
|
207 glMatrixMode(GL_PROJECTION); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
208 glLoadIdentity(); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
209 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
210 /* |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
211 * Creates projection matrix |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
212 * 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
|
213 * 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
|
214 */ |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
215 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
216 gluOrtho2D(0, window_width, 0, window_height); |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
217 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
218 /* |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
219 * 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
|
220 * 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
|
221 * does not match the padding expected by GL. |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
222 */ |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
223 |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
224 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
|
225 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
226 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
227 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
228 void setup_freetype() { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
229 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
230 // Init library |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
231 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
|
232 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
|
233 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
234 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
235 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
236 // Load font |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
237 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
|
238 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
|
239 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
240 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
241 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
242 // Set font size |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
243 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
|
244 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
|
245 FT_Done_Face(ft_face); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
246 FT_Done_FreeType(ft_library); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
247 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
248 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
249 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
250 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
251 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
252 |
32 | 253 int main(int argc, char *argv[]) { |
254 if (argc - 1 == 4) { | |
255 int args[4]; | |
256 | |
257 printf("Values provided: "); | |
258 | |
259 for (int i = 1; i < argc; i++) { | |
260 printf("%s ", argv[i]); | |
261 | |
262 char* end_ptr; | |
263 long value = strtol(argv[i], &end_ptr, 10); | |
264 | |
265 if (end_ptr == argv[i]) { | |
266 printf("Invalid argument\n"); | |
267 exit(1); | |
268 } | |
269 | |
270 if ((value == LONG_MAX || value == LONG_MIN)) { | |
271 printf("Integer out of range\n"); | |
272 exit(1); | |
273 } | |
274 | |
275 args[i - 1] = (int)value; | |
276 } | |
277 | |
278 printf("\n"); | |
279 | |
280 printf("Window width: %i\n", args[0]); | |
281 window_width = args[0]; | |
282 | |
283 printf("Window height: %i\n", args[1]); | |
284 window_height = args[1]; | |
285 | |
286 printf("Rectangle width: %i\n", args[2]); | |
287 rect_width = args[2]; | |
288 | |
289 printf("Space: %i\n", args[3]); | |
290 space = args[3]; | |
291 | |
292 } else { | |
293 printf("Using default values\n"); | |
294 } | |
295 | |
30 | 296 algo_args.arr_size = window_width / (rect_width + space); |
297 algo_args.arr = malloc(algo_args.arr_size * sizeof(struct Element)); | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
298 algo_args.comparisons = 0; |
30 | 299 algo_args.pause = false; |
300 algo_args.sequentially = false; | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
301 algo_args.delay = 100; |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
302 |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
303 strcpy(algos[0].name, "Bubble sort"); |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
304 algos[0].function = bubble_sort; |
32 | 305 |
30 | 306 strcpy(algos[1].name, "Selection sort"); |
307 algos[1].function = selection_sort; | |
18
6a5c5b137348
implement method to run the algo function given the selected algo
Dennis C. M. <dennis@denniscm.com>
parents:
17
diff
changeset
|
308 |
38
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
309 strcpy(algos[2].name, "Quick sort"); |
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
310 algos[2].function = quick_sort; |
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
311 |
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
312 strcpy(algos[3].name, "Insertion sort"); |
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
313 algos[3].function = insertion_sort; |
8ed1256dd518
finish quick sort and add insertion sort
Dennis C. M. <dennis@denniscm.com>
parents:
32
diff
changeset
|
314 |
30 | 315 algos_size = sizeof(algos) / sizeof(algos[0]); |
21 | 316 |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
317 create_array(algo_args.arr, algo_args.arr_size, window_height, vpadding); |
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
318 randomize_array(algo_args.arr, algo_args.arr_size); |
7 | 319 |
0 | 320 glutInit(&argc, argv); |
8 | 321 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
322 glutInitWindowSize(window_width, window_height); |
39 | 323 glutCreateWindow("Visualization of sorting algorithms"); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
324 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
325 setup_gl(); |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
326 setup_freetype(); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
327 |
0 | 328 glutDisplayFunc(display); |
8 | 329 glutKeyboardFunc(keyboard); |
12 | 330 glutIdleFunc(idle); |
0 | 331 glutMainLoop(); |
332 | |
29
dae463bbf5ca
implementing multi-thread and refactoring
Dennis C. M. <dennis@denniscm.com>
parents:
27
diff
changeset
|
333 free(algo_args.arr); |
7 | 334 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
335 FT_Done_Face(ft_face); |
21 | 336 FT_Done_FreeType(ft_library); |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
337 |
0 | 338 return 0; |
339 } |