2

我正在用 allegro 编写游戏,并想加载一些图像文件。但是,每当我调用 al_load_bitmap 时,都会得到一个空指针。我使用 XCode 4.1 作为我的 IDE。我会尝试使用 g++ 进行编译(以防它是路径问题)但是我不知道我需要做什么才能在命令行中使用 g++ 编译它(只是 g++ main.cpp 不起作用)。无论如何,这是我的代码:

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;
    }
}

我已经跑了:

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;
}

我还提出:

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

在我文件的顶部。既不加载grass.png,也不加载dirty.png,它们与我的main.cpp 文件位于完全相同的目录中。我没有编译错误,但是当我尝试加载我的图像时,我总是得到空指针,所以当需要将它们绘制到显示器时,它们不会显示。请帮忙!

4

2 回答 2

3

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.

于 2012-06-08T13:15:12.367 回答
0

Okay, I had been having the same problem, and I was absolutely positive that I was looking in the correct directory and that the resources for the program were there. I used al_path_cstr(path, '/') and allegro's working directory was as expected. Then I looked at the resource file sizes....

All my resources in my build directory were zero bytes. Copied them over myself and it solved the issue. I hope this helps some one out!

于 2012-12-30T03:32:07.777 回答