0

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?

RedX
  • 14,749
  • 1
  • 53
  • 76
Gabe
  • 85
  • 1
  • 8

2 Answers2

2

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).

dacap
  • 478
  • 3
  • 19
  • register_png_file_type() is not an allegro command, and I have libpng, zlib, and loadpng. I also tried .jpg and .bmp. – Gabe Apr 15 '11 at 21:19
  • register_png_file_type() is a function defined in loadpng.h, you must #include "loadpng.h" file. See https://alleg.svn.sourceforge.net/svnroot/alleg/allegro/branches/4.4/addons/loadpng/loadpng.h Also you could use load_png() directly (instead of register_png_file_type & load_bitmap). – dacap Apr 15 '11 at 23:55
  • Well jpg format is not supported by Allegro without jpgalleg addon. Are you linking your executable with these addons? (loadpng, jpgalleg, etc.) Or are you just linking with allegro library? (e.g. only -lalleg flag). – dacap Apr 15 '11 at 23:58
  • How do I link with loadpng, libpng, and zlib? – Gabe Apr 16 '11 at 20:14
  • That is another topic, maybe these answers can help you: http://stackoverflow.com/questions/5684373/what-is-the-correct-way-to-link-to-a-c-library-a-in-xcode http://stackoverflow.com/questions/1300339/libpng-framework-on-os-x http://stackoverflow.com/questions/2117620/linking-a-framework-to-project – dacap Apr 16 '11 at 23:48
  • Um... could you just tell me? :/ – Gabe Apr 18 '11 at 00:00
  • No, because I've never used Xcode. I just can tell you that the command line arguments for gcc could be something like -lalleg -lpng -lzlib -lloadpng (but I don't know if you are using libpng as a framework, or dynamic library, or a static one). Now you've other problem, so I think that if you ask other question like "how do I link/compile a program with libpng/zlib in MacOS X?" you'll get more help. – dacap Apr 18 '11 at 02:23
  • Well, maybe the PNG is an 8-bit image. I'll edit the answer to give you an example of how to load an 8-bit image. – dacap Apr 19 '11 at 13:25
  • @dacap And by the way, it's a 1-bit image. I changed it from red to white because I thought it would help. – Gabe Apr 19 '11 at 20:25
  • Do you test with different png files? Do you try a .bmp file? Can you share the exact code and image you are using? I would like to compile it manually. – dacap Apr 19 '11 at 21:16
  • here's the program: | `>#include >#include >#include >#include >BITMAP *red; >int main(int argc, char *argv[]){ >allegro_init(); >install_keyboard(); >set_color_depth(32); >set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); >loadpng_init(); >PALETTE palette;` (CONT) – Gabe Apr 20 '11 at 01:05
  • `set_color_conversion(COLORCONV_TOTAL); red = load_png("frago.png", palette); select_palette(palette); if (red == NULL) { printf("The image could not be loaded.\n"); return 1; } acquire_screen(); select_palette(palette); blit(red, screen, 0, 0, 0, 0, red->w, red->h); printf("The image was loaded!"); release_screen(); readkey(); destroy_bitmap(red); return 0; } END_OF_MAIN();` I am using a 454x340 white (FFFFFF) rectangle. I tried a bmp, but it didn't work. I'm going to try again. And sorry for the code – Gabe Apr 20 '11 at 01:08
  • It's working here (with a .bmp, Allegro 4.4). Try to execute the "exbitmap" Allegro example loading your bmp, e.g. `exbitmap frago.bmp` (use the bmp, not the png). exbitmap examples does exactly the same as your code, but checking every function. Some questions, did you compile Allegro? What exact version are you using? – dacap Apr 20 '11 at 20:03
  • I use Allegro 5.0(.1?). I linked to Allegro. This is not my first program with Allegro; others have worked and I did the same things for both (architecture: 32 bit intel, other linker flags: -framework Cocoa -lalleg -lalleg-main -lpng -zlib -lloadpng, header search paths: /opt/local/include, library search paths: /opt/local/lib). And how would I implement exbitmap here? – Gabe Apr 21 '11 at 11:08
  • 1
    Allegro 5.0? But the API you're using is from Allegro 4. Please see the [Allegro 5's ex_bitmap example](http://alleg.svn.sourceforge.net/viewvc/alleg/allegro/branches/5.0/examples/ex_bitmap.c?revision=14133&view=markup) (which is completely different to [Allegro 4's exbitmap](http://alleg.svn.sourceforge.net/viewvc/alleg/allegro/branches/4.4/examples/exbitmap.c?revision=12081&view=markup)) and read [Allegro 5 documentation](http://alleg.sourceforge.net/a5docs/5.0.2/). The API has changed entirely, and I think you should be linking your program with -lallegro and -lallegro_main. – dacap Apr 21 '11 at 13:30
  • Yes! When I changed it to allegro instead of alleg it worked! Thanks. – Gabe Apr 22 '11 at 12:08
  • Anyway still I don't get it. If you're using Allegro 5, why you are using the Allegro 4 API? That is not possible!, if you include it means you must to link to Allegro 4, not 5. If you were able to compile other programs as you said, you should use the same compiler/linker flags here (+addons). I recommend you to ask this question in Allegro forums, and please provide them a lot more of information (specific Allegro version, OS & compiler versions, how you compile it and how you link, if you were able to compile the examples and how you compile them, etc.). – dacap May 01 '11 at 14:01
  • I'll try, but whats the api (.h) for allegro 5? – Gabe May 05 '11 at 00:58
  • See my comment of April 21, here the links again: [Allegro 5's ex_bitmap example](http://alleg.svn.sourceforge.net/viewvc/alleg/allegro/branches/5.0/examples/ex_bitmap.c?revision=14133&view=markup) and [Allegro 5 docs](http://alleg.sourceforge.net/a5docs/5.0.2/). Also could you please tell me if you were able to compile the Allegro 5 examples? – dacap May 06 '11 at 01:24
  • I couldn't compile the examples; the compiler couldn't find allegro/allegro5.h, allegro/allegro_image.h, or common.c. It must be a problem with the API. – Gabe May 08 '11 at 11:13
  • 1
    You didn't compile Allegro 5 yet. Start downloading [the latest version of Allegro 5](http://sourceforge.net/projects/alleg/files/allegro/5.0.2.1/), follow the instructions of README_macosx.txt. You'll need to install [cmake](http://cmake.org/cmake/resources/software.html) because it's the building system used by Allegro 5. Then you should [read this page](http://wiki.allegro.cc/index.php?title=OSX,_Xcode_3,_Framework). Good luck, and you don't even try to compile your own programs until you're able to compile the whole library+examples using the README steps. – dacap May 08 '11 at 14:15
0

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;
}
bcsanches
  • 2,362
  • 21
  • 32
  • I tried using your first suggestion and the check for null. The check for null works and gets me out of the program, but it still can't load the image! – Gabe Apr 15 '11 at 11:50
  • Check the program execution folder as I commented and check dacap answer about png support. – bcsanches Apr 15 '11 at 14:32
  • I put the file in the execution folder. – Gabe Apr 15 '11 at 21:20
  • Did you load up the add on for png images? – bcsanches Apr 20 '11 at 11:36
  • Take a look on @dacap answer, he explains about an add-on for adding png images support to allegro, did you added the add-on code to your project? – bcsanches Apr 21 '11 at 12:56
  • For me to help you, I need you answer me if you loaded up the png add-on for allegro or not. – bcsanches May 01 '11 at 15:16
  • @bcsanches I loaded loadpng, zlib, and libpng. – Gabe May 05 '11 at 00:59
  • Did you check for errors? No errors occurred during this process? On the worst case I would suggest you grab the source code and debug to it where it exactly fails. But I think on the possibilites: 1 - the png addon load failed due to not finding the file, 2 - the file load failed because the file was not found, 3 - the file is somehow corrupt and the load fails. – bcsanches May 05 '11 at 12:43
  • @bsanches what do you suggest i do? – Gabe May 07 '11 at 22:54
  • Did you check for errors as I mentioned? IF you think you did everything right, I suggest you to grab the source and the debug it to see exactly where it fails to try to understand the problem. – bcsanches May 08 '11 at 22:38
  • @bcsanches it doesn't fail, it runs, but the picture doesn't load. – Gabe May 13 '11 at 22:51
  • sorry, I have run out of ideas. – bcsanches May 25 '11 at 17:22