Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Is using TTFs really that complicated?

This thread is locked; no one can reply to it. rss feed Print
Is using TTFs really that complicated?
AceBlkwell
Member #13,038
July 2011
avatar

Is there a simple example or tutorial for loading and using ttf fonts with Allegro 5? There is an example that comes with the library but seems really complex for a simple “hello world” type program.

Matthew Leverton
Supreme Loser
January 1999
avatar

Here's a simple example that should help you get started:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3#include <allegro5/allegro_ttf.h> 4 5int main() { 6 ALLEGRO_DISPLAY *display = NULL; 7 ALLEGRO_FONT *font = NULL; 8 9 // Initialize Allegro 10 al_init(); 11 al_init_font_addon(); 12 al_init_ttf_addon(); 13 14 // Create a display 15 display = al_create_display(640, 480); 16 if (!display) { 17 fprintf(stderr, "Failed to create display!\n"); 18 return -1; 19 } 20 21 // Load a TrueType font 22 font = al_load_ttf_font("font.ttf", 36, 0); 23 if (!font) { 24 fprintf(stderr, "Failed to load font!\n"); 25 return -1; 26 } 27 28 // Set the font color to white 29 al_set_target_backbuffer(display); 30 al_clear_to_color(al_map_rgb(0, 0, 0)); 31 al_draw_text(font, al_map_rgb(255, 255, 255), 320, 240, ALLEGRO_ALIGN_CENTER, "Hello, world!"); 32 33 // Flip the display to show the text 34 al_flip_display(); 35 36 // Wait for a few seconds 37 al_rest(5.0); 38 39 // Clean up resources 40 al_destroy_display(display); 41 al_destroy_font(font); 42 43 return 0; 44}

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Matthew. I found something similar on You-Tube. Allegro 5 Made Easy. There are videos for various aspects of A5. I think fonts was around 4 or 5. The videos are 11+ years but still applicable. His example closely resembled your's. Thanks Again.

Go to: