Mercurial > public > algo-animator
annotate main.c @ 16:2ded5f1f544a
refactor variable name
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 25 Jun 2023 16:52:44 +0100 |
parents | e2fcfcb43fee |
children | fba66d02f1cf |
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 |
12 | 23 char algos[4][50] = { |
24 "Bubble sort", | |
25 "Selection sort", | |
26 "Insertion sort", | |
27 "Quick sort" | |
28 }; | |
29 | |
9 | 30 int selected_algo = 0; |
12 | 31 int refresh_counter = 0; |
32 int iter_counter = 0; | |
33 int arr_size; | |
9 | 34 |
10 | 35 int* arr; |
12 | 36 |
37 bool run; | |
0 | 38 |
39 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
40 /* Algorithms */ |
12 | 41 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
42 // Bubble sort |
12 | 43 struct BubbleSortInfo { |
44 int step; | |
45 int i; | |
46 }; | |
47 | |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
48 struct BubbleSortInfo bs = {1, 0}; |
12 | 49 |
9 | 50 void bubble_sort() { |
12 | 51 if (bs.i < arr_size - bs.step - 1) { |
52 int current = arr[bs.i]; | |
53 int next = arr[bs.i + 1]; | |
9 | 54 |
11 | 55 if (current > next) { |
12 | 56 arr[bs.i + 1] = current; |
57 arr[bs.i] = next; | |
11 | 58 } |
9 | 59 |
12 | 60 bs.i++; |
11 | 61 } else { |
12 | 62 bs.step++; |
63 bs.i = 0; | |
9 | 64 } |
65 } | |
66 | |
67 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
68 /* Helper functions */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
69 |
10 | 70 void create_array() { |
71 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; | |
72 arr = (int*)malloc(arr_size * sizeof(int)); | |
73 | |
12 | 74 srand(time(NULL)); |
10 | 75 |
16 | 76 int min = VPADDING; |
77 int max = WINDOW_HEIGHT - VPADDING; | |
10 | 78 |
79 for (int i = 0; i < arr_size; i++) { | |
80 arr[i] = rand() % ((max - min) + 1) + min; | |
81 } | |
82 } | |
83 | |
84 | |
12 | 85 bool array_sorted() { |
86 for (int i = 0; i < arr_size - 1; i++) { | |
87 if (arr[i] > arr[i + 1]) { | |
88 return false; | |
89 } | |
10 | 90 } |
12 | 91 |
92 return true; | |
9 | 93 } |
94 | |
95 | |
96 void algo_selector(int direction) { | |
97 int selection = selected_algo + direction; | |
98 int lower = 0; | |
99 int upper = (sizeof(algos) / sizeof(algos[0])) - 1; | |
100 | |
101 if (selection >= lower && selection <= upper) { | |
102 selected_algo = selection; | |
103 } | |
104 } | |
105 | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
106 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
107 /* Render functions */ |
8 | 108 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
109 unsigned char* flipBitmapVertically(unsigned char* bitmap, int width, int height) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
110 unsigned char* flippedBitmap = (unsigned char*)malloc(width * height); |
8 | 111 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
112 for (int row = 0; row < height; row++) { |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
113 unsigned char* srcRow = bitmap + (row * width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
114 unsigned char* destRow = flippedBitmap + ((height - row - 1) * width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
115 memcpy(destRow, srcRow, width); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
116 } |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
117 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
118 return flippedBitmap; |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
119 } |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
120 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
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 |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
165 x += 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 | |
182 glVertex2f(x, arr[i]); | |
183 | |
184 // Top right | |
185 glVertex2f(x + RECT_WIDTH, arr[i]); | |
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 |
198 sprintf(text, "Algorithm: %s", algos[selected_algo]); | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
199 render_text(20, WINDOW_HEIGHT - 50, text); |
8 | 200 |
10 | 201 sprintf(text, "Number of elements: %i", arr_size); |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
202 render_text(20, WINDOW_HEIGHT - 80, text); |
9 | 203 |
12 | 204 sprintf(text, "Iterations: %i", iter_counter); |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
205 render_text(20, WINDOW_HEIGHT - 110, text); |
12 | 206 |
16 | 207 render_text(20, VPADDING - 50, "Press a or s to select an algorithm"); |
208 render_text(20, VPADDING - 80, "Press enter to run the algorithm"); | |
209 render_text(20, VPADDING - 110, "Press r to reset array"); | |
8 | 210 |
211 glutSwapBuffers(); | |
212 } | |
213 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
214 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
215 /* Refresh function */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
216 |
12 | 217 void idle() { |
218 if (run) { | |
219 bubble_sort(); | |
220 refresh_counter++; | |
221 iter_counter++; | |
222 | |
223 if (refresh_counter == 90) { | |
224 glutPostRedisplay(); | |
225 refresh_counter = 0; | |
226 } | |
227 | |
228 } else { | |
229 glutPostRedisplay(); | |
230 } | |
231 | |
232 if (array_sorted()) { | |
233 run = false; | |
234 } | |
8 | 235 } |
236 | |
237 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
238 /* User input handler */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
239 |
8 | 240 void keyboard(unsigned char key, int x, int y) { |
241 | |
10 | 242 // s |
9 | 243 if (key == 115) { |
244 algo_selector(1); | |
245 } | |
246 | |
10 | 247 // a |
9 | 248 if (key == 97) { |
249 algo_selector(-1); | |
250 } | |
251 | |
12 | 252 // r |
253 if (key == 114) { | |
16 | 254 |
255 // Reset array | |
12 | 256 create_array(); |
16 | 257 |
258 // Reset state | |
12 | 259 iter_counter = 0; |
260 refresh_counter = 0; | |
261 run = false; | |
16 | 262 |
263 // Reset algo steps | |
12 | 264 bs = (struct BubbleSortInfo){0, 0}; |
265 } | |
266 | |
10 | 267 // enter |
8 | 268 if (key == 13) { |
12 | 269 run = true; |
7 | 270 } |
0 | 271 } |
272 | |
273 | |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
274 /* Set up functions */ |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
275 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
276 void setup_gl() { |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
277 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
278 // Set background dark |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
279 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
|
280 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
281 // 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
|
282 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
|
283 glPointSize(5.0); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
284 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
285 // 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
|
286 glMatrixMode(GL_PROJECTION); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
287 glLoadIdentity(); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
288 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
289 /* |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
290 * Creates projection matrix |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
291 * 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
|
292 * 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
|
293 */ |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
294 |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
295 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
296 |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
297 /* |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
298 * 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
|
299 * 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
|
300 * does not match the padding expected by GL. |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
301 */ |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
302 |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
303 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
|
304 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
305 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
306 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
307 void setup_freetype() { |
13
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 // Init library |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
310 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
|
311 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
|
312 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
313 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
314 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
315 // Load font |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
316 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
|
317 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
|
318 exit(1); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
319 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
320 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
321 // Set font size |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
322 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
|
323 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
|
324 FT_Done_Face(ft_face); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
325 FT_Done_FreeType(ft_library); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
326 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
327 exit(1); |
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 } |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
330 |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
331 |
0 | 332 int main(int argc, char** argv) { |
10 | 333 create_array(); |
7 | 334 |
0 | 335 glutInit(&argc, argv); |
8 | 336 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
337 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
|
338 glutCreateWindow("Algorithm animator"); |
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
339 |
14
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
340 setup_gl(); |
d055228ca9a6
printing text with custom font
Dennis C. M. <dennis@denniscm.com>
parents:
13
diff
changeset
|
341 setup_freetype(); |
13
074bde2db09a
printing text with custom font but incorrect letters
Dennis C. M. <dennis@denniscm.com>
parents:
12
diff
changeset
|
342 |
0 | 343 glutDisplayFunc(display); |
8 | 344 glutKeyboardFunc(keyboard); |
12 | 345 glutIdleFunc(idle); |
0 | 346 glutMainLoop(); |
347 | |
10 | 348 free(arr); |
7 | 349 |
15
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
350 FT_Done_Face(ft_face); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
351 FT_Done_FreeType(ft_library); |
e2fcfcb43fee
fix vertical character alignment
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
352 |
0 | 353 return 0; |
354 } |