【发布时间】:2017-05-02 20:46:35
【问题描述】:
我开始使用 Allegro 5 在 C++ 中的 Visual Studio 2017 中创建游戏。为了更容易管理图像,我创建了一个 ImageLoader 类,它将加载和存储所有活动图像,并在必要时销毁它们。它使用将文件名与相应图像匹配的映射来执行此操作。 目前我的 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()) 将其存储在地图中时相同的文件名,但即使这返回 false,ALLEGRO_BITMAP 指针仍然为空。我尝试让地图存储位图而不是指针,但是当我尝试向地图添加元素时,这给了我一个错误,因为我不允许像这样修改地图值。为什么这些指针在我设置它们的时间和我检索它们的时间之间会变为空?对于如何存储位图,我也愿意接受其他建议。
编辑:我修改了我的项目,将文件名的实例作为 char 数组更改为 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() 以便在找不到搜索的文件名时不再添加到地图后,我发现在一行中加载图像时我也犯了一个错误 I'我最初发布问题时忘记包括:
current_screen.load_images(image_loader);
事实证明,我是这样写的load_images():
void MainGameScreen::load_images(ImageLoader loader)
...这意味着函数对loader 的调用实际上并未应用于我传入的 ImageLoader。我将其更改为:
void MainGameScreen::load_images(ImageLoader& loader)
...现在一切正常。
【问题讨论】:
标签: c++ visual-studio bitmap allegro