Allegro5 无法在 Ubuntu 20.04 上解码 png 图像(使用 cmake)

Posted

技术标签:

【中文标题】Allegro5 无法在 Ubuntu 20.04 上解码 png 图像(使用 cmake)【英文标题】:Allegro5 cannot decode png images on Ubuntu 20.04 (using cmake) 【发布时间】:2021-08-15 23:40:18 【问题描述】:

Allegro5 似乎无法为我的游戏项目解码 png 图像。这个函数:

ALLEGRO_BITMAP* BrushCollection::LoadBitmapFromFile(const char* fileName)

    ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_EXENAME_PATH);

    al_append_path_component(path, "assets");
    al_set_path_filename(path, fileName);

    ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));
    allegro_init(bitmap, L"UI Bitmap");

    return bitmap;

返回 NULL。我知道图像路径是正确的,因为相同的代码适用于 Windows。

与本题相关:here

我在 Ubuntu 20.04 中使用预编译的 allegro 5.2.7 包也遇到了同样的问题!我将png 添加为项目的外部库。我的系统中确实安装了 libpng16-16 和 libpng-dev,但是 allegro 似乎无法解码 png 图像!

这是cmake顺便生成的命令,链接中好像包含了libpng:

/usr/bin/c++  -g   CMakeFiles/Planetaire.dir/src/main.cpp.o CMakeFiles/Planetaire.dir/src/Game.cpp.o CMakeFiles/Planetaire.dir/src/brush/BrushCollection.cpp.o CMakeFiles/Planetaire.dir/src/events/EventStack.cpp.o CMakeFiles/Planetaire.dir/src/events/MouseEvents.cpp.o CMakeFiles/Planetaire.dir/src/text/FontCollection.cpp.o CMakeFiles/Planetaire.dir/src/tools/AllegroInit.cpp.o CMakeFiles/Planetaire.dir/src/tools/Debug.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ContinueGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitToDesktop.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/StartNewGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/AbstractComponent.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/InGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/MainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/AbstractUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ButtonElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ImageElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/MainUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/PanelElement.cpp.o CMakeFiles/Planetaire.dir/src/world-generator/Terrain.cpp.o  -o Planetaire  -lallegro -lallegro_main -lallegro_font -lallegro_ttf -lallegro_image -lallegro_primitives /usr/lib/x86_64-linux-gnu/libpng.so

我想知道我在这里做错了什么。这在 Windows 上开箱即用。

【问题讨论】:

【参考方案1】:

我想我知道答案了!

执行时路径完全错误:

    ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));

这是建立一个反斜杠路径。 Linux/Mac 找不到这个文件。当我更正这条路径时,应用程序工作了!

【讨论】:

以上是关于Allegro5 无法在 Ubuntu 20.04 上解码 png 图像(使用 cmake)的主要内容,如果未能解决你的问题,请参考以下文章

fgets 无法在 Ubuntu 20.04 上从管道读取两次

无法在 linux ubuntu 20.04 中安装 libgraph

无法在 WSL 2 Ubuntu20.04 上通过 Ansible 启动服务

无法登录 Ubuntu 20.04 LTS [关闭]

SoapUI 5.6.0 无法在 Ubuntu 20.04 LTS 上安装

无法在 ubuntu 20.04 上通过 apt 安装 curl