|
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> int main() { // Inicialización del addon de fuentes // Inicialización del addon de imágenes (necesario para algunas fuentes) // Crear una ventana // Cargar una fuente // Limpiar la pantalla con un color blanco // Configurar el color del texto // Dibujar el texto en la pantalla // Mostrar el contenido del buffer // Esperar 5 segundos // Liberar recursos return 0; I appreciate your help. Thank you. |
torhu
Member #2,727
September 2002
|
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
|
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 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
|
Congratulations, AceBlkwell, you've earned your first kill! Consider that bug squashed!
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
AceBlkwell
Member #13,038
July 2011
|
Thanks Edgar. You are too kind. I guess even a blind squirrel can find a nut once in a while . Or maybe it's the Allegro company I keep that is starting to wear off on me. In any case thanks again. |
|