Allegro 位图命令返回黑屏
我是 allegro 和 c++ 的初学者。我正在尝试使用位图命令。我使用这个简单的程序来测试它:
#include <allegro.h>
BITMAP *red;
int main(){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
red = load_bitmap( "frago.png", NULL);
acquire_screen();
blit(red, screen, 0, 0, 0, 0, 480, 360);
release_screen();
readkey();
destroy_bitmap(red);
return 0;
}
END_OF_MAIN();
有问题的文件“frago.png”位于我的桌面上,是一个红色的大矩形。该颜色支持颜色深度 32。我在 Mac 上使用 Xcode 4。有人可以帮助我吗?
I am a beginner at allegro and c++. I am trying to use the bitmap commands. I used this simple program to test it:
#include <allegro.h>
BITMAP *red;
int main(){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
red = load_bitmap( "frago.png", NULL);
acquire_screen();
blit(red, screen, 0, 0, 0, 0, 480, 360);
release_screen();
readkey();
destroy_bitmap(red);
return 0;
}
END_OF_MAIN();
The file "frago.png" in question is located on my desktop and is a big red rectangle. The color is supported in color depth 32. I am using Xcode 4 on a Mac. Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Allegro 库默认无法读取 .png 文件。您必须使用其他一些库/插件(libpng、zlib、loadpng)。 loadpng 从版本 4.3.10 开始与 Allegro 捆绑在一起,但您需要在编译器中安装 libpng 和 zlib。
您必须在 load_bitmap() 之前使用 register_png_file_type()。
Allegro 4.4 的 loadpng 插件包含在其源代码中:
https://alleg.svn.sourceforge.net/ svnroot/alleg/allegro/branches/4.4/addons/loadpng/
如果 PNG 是 8bpp 图像,请记住加载其调色板:
无论如何,我认为 Allegro 应该自动将您的图像转换为 32bpp,尝试使用 set_color_conversion 在 load_bitmap() 之前以防万一:
最后你可以尝试使用 load_png() 函数直接(用load_png替换load_bitmap)。
Allegro library cannot read .png files by default. You must use some other libraries/addons (libpng, zlib, loadpng). loadpng is bundled with Allegro from version 4.3.10, but you need libpng and zlib installed in your compiler.
You must use register_png_file_type() before load_bitmap().
The loadpng addon of Allegro 4.4 is included in its source code:
https://alleg.svn.sourceforge.net/svnroot/alleg/allegro/branches/4.4/addons/loadpng/
If the PNG is 8bpp image, remember to load its color palette:
Anyway I think Allegro should convert your image to 32bpp automatically, try using set_color_conversion before load_bitmap() just in case:
Finally you could try to use load_png() function directly (replace load_bitmap with load_png).
如果程序没有在与图像相同的文件夹中运行,它将找不到该图像。
例如,如果程序在 c:\temp\MyProgram\ 中运行,则图像应位于同一文件夹中。
此外,某些 IDE 允许您指定程序在从 IDE 运行或调试时将运行的文件夹,您可以将此路径设置为桌面或将图像复制到程序文件夹。
另一种选择是在 load_bitmap 调用中指定完整的图像路径,但在我看来这是最糟糕的解决方案,因为只有当图像恰好位于该位置时程序才会工作。
我还建议添加对 null 的检查:
If the program is not running in the same folder as the image, it will not find the image.
For example, if the program is running in c:\temp\MyProgram\, the image should be located in this same folder.
Also, some IDEs allow you to specify the folder that the program will run when running or debugging from the IDE, you can set this path to your desktop or copy the image to the program folder.
Another option is to specify the full image path in the load_bitmap call, but this is the worst solution in my opinion, because the program will only works when the image is exactly in this location.
Also I suggest adding a check for null: