Bugster
Bugster

Reputation: 1594

Allegro 5 crashing when loading the font

I've recently gotten started with Allegro 5, and I tried loading a font. This is my code:

#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_native_dialog.h>

int main()
{
    al_init_font_addon();
    al_init_ttf_addon();
    ALLEGRO_DISPLAY *display = NULL;
    if(!al_init())
    {
        al_show_native_message_box(NULL, NULL, NULL, "Error", NULL, NULL);
        return -1;
    }
    display = al_create_display(800, 800);
    ALLEGRO_FONT *font1 = al_load_font("arial.ttf", 12, 0);

    if(!font1)
    {
        al_show_native_message_box(NULL, NULL, NULL, "Error 3", NULL, NULL);
        return -3;
    }
    al_clear_to_color(al_map_rgb(0, 0, 0));
    al_draw_text(font1, al_map_rgb(255, 0, 255), 50, 50, ALLEGRO_ALIGN_CENTRE, "Hello font size: 12");
    al_flip_display();

    al_rest(3.0);
    al_destroy_font(font1);
    al_destroy_display(display);
    return 0;
}

However upon running the code it returns -3 which means there was an error loading the font. What am I doing wrong? I'm using Codeblocks IDE with Windows XP SP 3 mingw compiler. What am I doing wrong?

EDIT: I fixed it by adding the ttf file to my project directory. Vote to close

Upvotes: 1

Views: 1557

Answers (1)

Matthew
Matthew

Reputation: 48294

You have two problems, one that you already discovered. The other is that you are initializing the add-ons before you call al_init().

You should read this troubleshooting guide to learn how to correctly load resources from relative locations in a cross-platform way.

Upvotes: 2

Related Questions