简体   繁体   中英

Allegro bitmap commands return black screen

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?

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:

PALETTE palette;
BITMAP* red = load_bitmap("frago.png", palette);
select_palette(palette);
blit(red, screen, 0, 0, 0, 0, red->w, red->h);
unselect_palette();

Anyway I think Allegro should convert your image to 32bpp automatically, try using set_color_conversion before load_bitmap() just in case:

set_color_conversion(COLORCONV_TOTAL);

Finally you could try to use load_png() function directly (replace load_bitmap with load_png).

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:

red = load_bitmap("frago.png", NULL);
if(red == NULL)
{
    printf("Cannot load frago.png\n");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM