Search code examples
c++allegroallegro5

How do I get the error reason in Allegro?


I come from SDL and there I had a function called SDL_GetError(), which returned magically a const char * with the internal error. Here's my piece of code on Allegro 5:

#include "game.h"

ALLEGRO_BITMAP *load_bmp(const char *s) {
    ALLEGRO_BITMAP *bmp = nullptr;
    bmp = al_load_bitmap(s);
    if (!bmp) {

        al_show_native_message_box(display,
            "Fatal Error!",
            "Failed to load: " ,
            s,
            NULL,
            ALLEGRO_MESSAGEBOX_ERROR);

        al_destroy_display(display);
        return nullptr;

    }

    return bmp;
}

ALLEGRO_BITMAP *player = load_bmp("GFX\\player_up.bmp"); 

The file is ok and I can load directly from al_load_bitmap, but, since I've added my personal function, Allegro crashes and gives me its error dialog (So, bmp must be a nullptr). The problem is that this error for me is absolutely useless, and I need to know what's happening inside Allegro (So, something like al_get_error() would be awesome). How do I know what happened?

enter image description here


Solution

  • Is the variable player at file scope; in other words, is it initialised before your main runs and has a chance to initialise the allegro library?

    Assuming this is the case, try changing the line to

    ALLEGRO_BITMAP *player = nullptr;
    

    and initialise it in a function that you call at the appropriate time during program start-up, like:

    void init_game_bitmaps()
    {
       player = load_bmp("GFX\\player_up.bmp");
       // Other initialisation here ...
    }
    

    calling it like:

    // Somewhere in main, or an appropriate function call:
    init_game_bitmaps();