Allegro 5:在标准映射中存储位图时遇到问题

Allegro 5: trouble storing bitmaps in a std map

我在 Visual Studio 2017 年开始使用 Allegro 5 在 C++ 中创建游戏。为了更容易管理图像,我创建了一个 ImageLoader class 来加载和存储所有活动图像,并在必要时销毁它们。它使用将文件名与相应图像匹配的映射来实现这一点。 目前我的 main() 代码如下所示:

int main(){

if (!al_init()) {
    al_show_native_message_box(NULL, NULL, NULL, "Could not intialize allegro 5.", NULL, NULL);
    return -1;
}

ALLEGRO_DISPLAY *display = al_create_display(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
al_set_window_title(display, "Game title");

// make usre the display was created correctly
if (!display) {
    al_show_native_message_box(display, "Title", "settings", "Could not create Allegro window.", NULL, NULL);
}

// intialize fonts, primitives, keyboard,etc.
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
al_install_keyboard();
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;}
ImageLoader image_loader;

ImageLoader 然后为播放器加载图像:

ALLEGRO_BITMAP *image = al_load_bitmap(filename);

if (image == NULL) {
    std::cout << filename << std::endl;
    std::cout << "loader failed to load image" << std::endl;
}
image_map[filename] = image;

当我测试这部分时,它似乎工作正常,因为图像不为空。请注意,image_map 的声明方式与 ImageLoader.h:

类似
std::map<const char *, ALLEGRO_BITMAP*> image_map;

当我尝试从 ImageLoader 获取图像时,问题出现在我的主游戏循环中:

for (GameImage image : imageList) {
            ALLEGRO_BITMAP *draw_image = image_loader.get_current_image(image);
            if (draw_image == NULL) {
                //TODO: error handling
                std::cout << "no image" << std::endl;
            }
            else
                al_draw_bitmap(draw_image, image.get_x(), image.get_y(), NULL);

        }

我的检查总是显示 draw_image 为空。这是 get_current_image:

的代码
ALLEGRO_BITMAP * ImageLoader::get_current_image(GameImage image)
{
return image_map[image.get_image_filename()];
}

我已经通过检查 if (image_map.find(image.get_image_filename()) == image_map.end()) 进行测试以确保我在此处使用与加载图像并将其存储在地图中时相同的文件名,但即使此 returns 错误ALLEGRO_BITMAP 指针仍然为空。我已经尝试让地图存储位图而不是指针,但是当我尝试向地图添加元素时这给了我一个错误,因为我不允许像这样修改地图值。为什么这些指针在我设置它们的时间和我检索它们的时间之间会变为空?对于如何存储位图,我也乐于接受其他建议。

编辑:我修改了我的项目,将作为字符数组的文件名实例更改为 std::strings。图像映射现在在 ImageLoader.h 中声明为 std::map<std::string, ALLEGRO_BITMAP*> image_map,而 GameImage 存储的文件名现在是 std::string image_filename。 ImageLoader.cpp 中的 load_image 现在看起来像这样:

ALLEGRO_BITMAP *image = al_load_bitmap(filename.c_str());

if (image == NULL) {
    std::cout << filename << std::endl;
    std::cout << "loader failed to load image" << std::endl;
}
image_map[filename] = image;

最后,get_current_image还是老样子:

ALLEGRO_BITMAP * ImageLoader::get_current_image(GameImage image)
{
return image_map[image.get_image_filename()];
}

但是,同样的问题仍然存在。我还检查了图像映射的大小,它在我的程序的整个持续时间内保持在 1,我插入的图像的文件名作为键,值从指向位图的非空指针开始,并且看起来变成在某些时候为空。

编辑 2:

在将 char 数组更改为字符串并修复 get_current_image() 以便在找不到搜索到的文件名时它不再添加到地图后,我发现我在加载图像时也犯了一个错误我最初发布问题时忘记包括的一行:

current_screen.load_images(image_loader);

事实证明,我是这样写 load_images() 的:

void MainGameScreen::load_images(ImageLoader loader)

...意味着函数对 loader 的调用实际上并未应用于我传入的 ImageLoader。我将其更改为:

void MainGameScreen::load_images(ImageLoader& loader)

...现在一切正常。

你的问题是使用 const char * 作为你的键意味着 map 将执行直接地址比较,即 "Is the address of the string equal to the address of the string I'm holding in memory"。这几乎肯定不是您想要执行字符串比较的方式。

这个问题实际上有两种解决方案,具体取决于您的用例。第一种是简单地将 const char * 更改为 std::string,这是最简单的解决方案,默认情况下您应该这样做。

std::map<std::string, ALLEGRO_BITMAP*> image_map;

而且,从广义上讲,在任何使用字符串的地方,您都应该使用 std::stringstd::string const&。没有理由使用其他任何东西。

...除非您关心性能。如果您正在编写游戏,性能几乎肯定是您关心的事情,这将我们带到了第二个解决方案。必须对地图进行大量查找将调用大量比较,虽然这通常不是一个大问题,但它在这里是因为每个比较都是一个成熟的基于字符串的相等性检查。

解决方案是,当您加载图像时,为每个图像分配一个唯一的 ID(如 int64_tuint64_t),并将这些值分配给 GameImage 对象而不是文件名或路径名。然后,当您进行查找时,使用该 ID 执行查找。

由你决定。将 const char * 换成 std::string 几乎肯定会修复代码中的逻辑错误并使其按预期方式工作。如果您发现必须进行所有这些字符串比较会显着降低您的程序速度,那么剩下的更像是一个优化问题。

编辑:

你的新问题是 std::mapoperator[] 函数会自动插入一个默认值(在本例中,nullptr)如果它找不到任何预先存在的图像请求的名称。您想要的代码看起来更像这样:

ALLEGRO_BITMAP * ImageLoader::get_current_image(GameImage image)
{
    auto it = image_map.find(image.get_image_filename());
    if(it == image_map.end()) return nullptr;
    else return it->second;
    //return image_map[image.get_image_filename()];
}

这样,找不到图像就不会诱使您使用的任何调试工具认为该位置存储了有效(空)值。

如果您想改用内置异常工具,也可以将其简化为:

ALLEGRO_BITMAP * ImageLoader::get_current_image(GameImage image)
{
    //Will throw an exception if nothing is found
    return image_map.at(image.get_image_filename());
}