Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Problem with function 'al_draw_text'

This thread is locked; no one can reply to it. rss feed Print
Problem with function 'al_draw_text'
miloarias
Member #27,110
July 2024

Hi, I am new to "Allegro", I have the following code to display some text in the allegro window, although the program does not show errors, the text is not displayed in the window.

#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>

int main() {
// Inicialización de Allegro
if (!al_init()) {
fprintf(stderr, "Failed to initialize Allegro.\n");
return -1;
}

// Inicialización del addon de fuentes
if (!al_init_font_addon()) {
fprintf(stderr, "Failed to initialize font addon.\n");
return -1;
}

// Inicialización del addon de imágenes (necesario para algunas fuentes)
if (!al_init_image_addon()) {
fprintf(stderr, "Failed to initialize image addon.\n");
return -1;
}

// Crear una ventana
ALLEGRO_DISPLAY *display = al_create_display(800, 600);
if (!display) {
fprintf(stderr, "Failed to create display.\n");
return -1;
}

// Cargar una fuente
ALLEGRO_FONT *font = al_load_font("arial.ttf", 36, 0); // Asegúrate de tener arial.ttf en tu directorio
if (!font) {
fprintf(stderr, "Failed to load font.\n");
al_destroy_display(display);
return -1;
}

// Limpiar la pantalla con un color blanco
al_clear_to_color(al_map_rgb(255, 255, 255));

// Configurar el color del texto
ALLEGRO_COLOR text_color = al_map_rgb(0, 0, 0); // Color negro

// Dibujar el texto en la pantalla
al_draw_text(font, text_color, 400, 300, ALLEGRO_ALIGN_CENTRE, "¡Hola, Allegro!");

// Mostrar el contenido del buffer
al_flip_display();

// Esperar 5 segundos
al_rest(5.0);

// Liberar recursos
al_destroy_font(font);
al_destroy_display(display);

return 0;
}

I appreciate your help.

Thank you.

torhu
Member #2,727
September 2002
avatar

How you know it doesn't show errors, are you running it on the command line?

You can put your code between <code> </code> tags to give it better formatting here.

AceBlkwell
Member #13,038
July 2011
avatar

Miloarias,

I got the code running and found one issue really. You initialize font but not ttf. I added it and also added cstdio header. Worked fine. I had to use a font I had on my computer, PressStart2P.tff. Just change back to arial.ttf

#SelectExpand
1 2#include <cstdio> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6#include <allegro5/allegro_primitives.h> 7#include <allegro5/allegro_image.h> 8 9int main() { 10// Inicialización de Allegro 11if (!al_init()) { 12fprintf(stderr, "Failed to initialize Allegro.\n"); 13return -1; 14} 15// Inicialización del addon de fuentes 16if (!al_init_font_addon()) { 17fprintf(stderr, "Failed to initialize font addon.\n"); 18return -1; 19} 20 21/************ Changed image addon to ttf. Image not needed for this program ****/ 22// Inicialización del addon de imágenes (necesario para algunas fuentes) 23if (!al_init_ttf_addon()) { 24fprintf(stderr, "Failed to initialize ttf addon.\n"); 25return -1; 26} 27 28// Crear una ventana 29ALLEGRO_DISPLAY *display = al_create_display(800, 600); 30if (!display) { 31fprintf(stderr, "Failed to create display.\n"); 32return -1; 33} 34// Cargar una fuente 35ALLEGRO_FONT *font = al_load_font("PressStart2P.ttf", 36, 0); // Asegúrate de tener arial.ttf en tu directorio 36if (!font) { 37fprintf(stderr, "Failed to load font.\n"); 38al_destroy_display(display); 39return -1; 40} 41// Limpiar la pantalla con un color blanco 42al_clear_to_color(al_map_rgb(255, 255, 255)); 43// Configurar el color del texto 44ALLEGRO_COLOR text_color = al_map_rgb(0, 0, 0); // Color negro 45// Dibujar el texto en la pantalla 46al_draw_text(font, text_color, 400, 300, ALLEGRO_ALIGN_CENTRE, "¡Hola, Allegro!"); 47// Mostrar el contenido del buffer 48al_flip_display(); 49// Esperar 5 segundos 50al_rest(5.0); 51// Liberar recursos 52al_destroy_font(font); 53al_destroy_display(display); 54return 0; 55}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Edgar. You are too kind. I guess even a blind squirrel can find a nut once in a while ;D.

Or maybe it's the Allegro company I keep that is starting to wear off on me.

In any case thanks again. :)

Go to: