Search code examples
c++imageallegroallegro5

Loading .png Image file with Allegro 5


I am writing a game in allegro and would like to load some image files. However, whenever I call al_load_bitmap, I am getting a null pointer. I am using XCode 4.1 as my IDE. I would try compiling using g++ (in case it is a path issue) however I don't know what I need to do in order to compile it in the command line with g++ (simply g++ main.cpp does not work). Anyways, here is my code:

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);

for (int i = 0; i < NUM_TILES; i++)
{
    switch (static_cast<Tile>(i)) {
        case GRASS:
            al_set_path_filename(path, "grass.png");
            tileFiles[i] = al_load_bitmap(al_path_cstr(path, '/'));
            if (!tileFiles[i])
            {
                std::cerr<<"grass.png not initialized"<<std::endl;
            }
            break;
        case DIRT:
            al_set_path_filename(path, "dirt.png");
            tileFiles[i] = al_load_bitmap(al_path_cstr(path, '/'));
            if (!tileFiles[i])
            {
                std::cerr<<"dirt.png not initialized"<<std::endl;
            }
            break;
        default:
            std::cerr 
                << "Missing case statement for datatype Tile numbered at " 
                << i
                << " in Board::Board (float mw, float mh, int tst)"
                << " declared in Board.cpp."
                << std::endl;
            break;
    }
}

I have already run:

if(!al_init_image_addon()) {
    al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", 
                               NULL, ALLEGRO_MESSAGEBOX_ERROR);
    return -1;
}

and I have also put:

#include "allegro5/allegro_image.h"
#include "allegro5/allegro_native_dialog.h"

at the top of my file. Neither grass.png, nor dirt.png load and they are in the exact same directory as my main.cpp file. I get no compilation errors, but I consistently get the null pointer when I try to load my images, so when it comes time to draw them to the display, they do not show. Please help!


Solution

  • Neither grass.png, nor dirt.png load and they are in the exact same directory as my main.cpp file

    Just a debugging tip... If you were to output the result of al_path_cstr(path, '/') to the console, it should be extremely obvious why the call is failing.

    ALLEGRO_RESOURCES_PATH is the location of "bundled resources," which on OS X means the directory of the executable file. (If you were to use an app bundle, then it would be the resource folder of the bundle.) As a quick check, just copy the images into the same directory that your executable file is being built.

    Most IDEs have very odd directory structures, IMO. I would ultimately set it up so that you are building into something like:

    /src/main.c
    /include/main.h
    /obj/release
    /obj/debug
    /bin/game.exe
    /bin/game-debug.exe
    /bin/image.png
    

    But that's just my preference. Use whatever you like, but you should read the docs again to get a clear picture of the different locations that al_get_standard_path() reveals.