Mercurial > public > algo-animator
comparison main.c @ 14:d055228ca9a6
printing text with custom font
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 25 Jun 2023 15:51:39 +0100 |
parents | 074bde2db09a |
children | e2fcfcb43fee |
comparison
equal
deleted
inserted
replaced
13:074bde2db09a | 14:d055228ca9a6 |
---|---|
43 struct BubbleSortInfo { | 43 struct BubbleSortInfo { |
44 int step; | 44 int step; |
45 int i; | 45 int i; |
46 }; | 46 }; |
47 | 47 |
48 struct BubbleSortInfo bs = {0, 0}; | 48 struct BubbleSortInfo bs = {1, 0}; |
49 | 49 |
50 void bubble_sort() { | 50 void bubble_sort() { |
51 if (bs.i < arr_size - bs.step - 1) { | 51 if (bs.i < arr_size - bs.step - 1) { |
52 int current = arr[bs.i]; | 52 int current = arr[bs.i]; |
53 int next = arr[bs.i + 1]; | 53 int next = arr[bs.i + 1]; |
105 | 105 |
106 | 106 |
107 /* Render functions */ | 107 /* Render functions */ |
108 | 108 |
109 void render_text(int x, int y, char* text) { | 109 void render_text(int x, int y, char* text) { |
110 glRasterPos2f(x, y); | |
111 | 110 |
112 // Get glyph index from character code | 111 // Get glyph index from character code |
113 FT_UInt glyph_index = FT_Get_Char_Index(ft_face, 'o'); | 112 FT_UInt glyph_index = FT_Get_Char_Index(ft_face, 'A'); |
113 | |
114 if (glyph_index == 0) { | |
115 fprintf(stderr, "Given character code has no glyph image in the face\n"); | |
116 exit(1); | |
117 } | |
114 | 118 |
115 // Load glyph image | 119 // Load glyph image |
116 if (FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_DEFAULT)) { | 120 if (FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_DEFAULT)) { |
117 fprintf(stderr, "Failed to load glyph.\n"); | 121 fprintf(stderr, "Failed to load glyph.\n"); |
118 exit(1); | 122 exit(1); |
119 } | 123 } |
120 | 124 |
121 | |
122 // Render glyph | 125 // Render glyph |
123 if (FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL)) { | 126 if (FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL)) { |
124 fprintf(stderr, "Failed to load glyph.\n"); | 127 fprintf(stderr, "Failed to render glyph.\n"); |
125 exit(1); | 128 exit(1); |
126 } | 129 } |
127 | 130 |
128 FT_GlyphSlot ft_slot = ft_face->glyph; | 131 FT_GlyphSlot slot = ft_face->glyph; |
129 FT_Bitmap* glyph_bitmap = &ft_slot->bitmap; | 132 FT_Bitmap* glyph_bitmap = &slot->bitmap; |
130 | 133 |
131 glDrawPixels(glyph_bitmap->width, glyph_bitmap->rows, GL_LUMINANCE, GL_UNSIGNED_BYTE, glyph_bitmap->buffer); | 134 glRasterPos2f(x, y); |
135 glDrawPixels( | |
136 glyph_bitmap->width, | |
137 glyph_bitmap->rows, | |
138 GL_LUMINANCE, | |
139 GL_UNSIGNED_BYTE, | |
140 glyph_bitmap->buffer | |
141 ); | |
132 } | 142 } |
133 | 143 |
134 | 144 |
135 void display() { | 145 void display() { |
136 glClear(GL_COLOR_BUFFER_BIT); | 146 glClear(GL_COLOR_BUFFER_BIT); |
147 | |
137 glBegin(GL_QUADS); | 148 glBegin(GL_QUADS); |
138 | 149 |
139 int x = 0; | 150 int x = 0; |
140 for (int i = 0; i < arr_size; i++) { | 151 for (int i = 0; i < arr_size; i++) { |
141 | 152 |
229 } | 240 } |
230 | 241 |
231 | 242 |
232 /* Set up functions */ | 243 /* Set up functions */ |
233 | 244 |
234 void init_gl() { | 245 void setup_gl() { |
235 | 246 |
236 // Set background dark | 247 // Set background dark |
237 glClearColor(0.0, 0.0, 0.0, 1.0); | 248 glClearColor(0.0, 0.0, 0.0, 1.0); |
238 | 249 |
239 // Set point color and size to 1 pixel | 250 // Set point color and size to 1 pixel |
248 * Creates projection matrix | 259 * Creates projection matrix |
249 * x increases from left to right (0 to WINDOW_WIDTH) | 260 * x increases from left to right (0 to WINDOW_WIDTH) |
250 * y increases from bottom to top (0 to WINDOW_HEIGHT) | 261 * y increases from bottom to top (0 to WINDOW_HEIGHT) |
251 */ | 262 */ |
252 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); | 263 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); |
253 } | 264 |
254 | 265 /* |
255 | 266 * This fucking line... I spent a day rendering random symbols |
256 void init_freetype() { | 267 * because the padding that adds FreeType to each row of the bitmap |
268 * does not match the padding expected by GL. | |
269 */ | |
270 | |
271 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
272 } | |
273 | |
274 | |
275 void setup_freetype() { | |
257 | 276 |
258 // Init library | 277 // Init library |
259 if (FT_Init_FreeType(&ft_library)) { | 278 if (FT_Init_FreeType(&ft_library)) { |
260 fprintf(stderr, "Failed to initialize FreeType library\n"); | 279 fprintf(stderr, "Failed to initialize FreeType library\n"); |
261 exit(1); | 280 exit(1); |
262 } | 281 } |
263 | 282 |
264 // Load font | 283 // Load font |
265 if (FT_New_Face(ft_library, "JetBrainsMono-Medium.ttf", 0, &ft_face)) { | 284 if (FT_New_Face(ft_library, "fonts/JetBrainsMono-Regular.ttf", 0, &ft_face)) { |
266 fprintf(stderr, "Failed to load font\n"); | 285 fprintf(stderr, "Failed to load font\n"); |
267 exit(1); | 286 exit(1); |
268 } | 287 } |
269 | 288 |
270 // Set font size | 289 // Set font size |
284 glutInit(&argc, argv); | 303 glutInit(&argc, argv); |
285 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | 304 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
286 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | 305 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
287 glutCreateWindow("Algorithm animator"); | 306 glutCreateWindow("Algorithm animator"); |
288 | 307 |
289 init_gl(); | 308 setup_gl(); |
290 init_freetype(); | 309 setup_freetype(); |
291 | 310 |
292 glutDisplayFunc(display); | 311 glutDisplayFunc(display); |
293 glutKeyboardFunc(keyboard); | 312 glutKeyboardFunc(keyboard); |
294 glutIdleFunc(idle); | 313 glutIdleFunc(idle); |
295 glutMainLoop(); | 314 glutMainLoop(); |