Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5 - al_load_bitmap() not working!

This thread is locked; no one can reply to it. rss feed Print
Allegro 5 - al_load_bitmap() not working!
Callum Gregg
Member #12,995
July 2011

Hi guys,

I have spent the past 5 hours fiddling around searching the web and the wiki to try and solve this problem I am having with al_load_bitmap.

I have moderate experience with C++ but this is my first time using the allegro library so I dont know a great deal about this but regardless of what I do I cannot get al_load_bitmap() to work.

At the moment I'm just trying to place bitmaps on the screen and regardless how basic I make the code I can't get it to work, can anyone spot something i'm doing wrong perhaps?

Even this basic code won't work:

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_image.h> 4 5int main(int argc, char **argv) 6{ 7 al_init(); 8 9 al_init_image_addon(); 10 11 ALLEGRO_BITMAP *bmp = al_load_bitmap("LowerFull.bmp"); 12 if(!bmp) 13 fprintf(stderr,"failed to load bitmap.\n"); 14 15 while(true){ 16 ... 17 } 18}

Any help would be greatly appreciated, Thanks! :)

Edit: I have also tried using an absolute path and that does not work either.

Matthew Leverton
Supreme Loser
January 1999
avatar

What part doesn't work?

Callum Gregg
Member #12,995
July 2011

I always receive NULL for my bmp variable.

Edit:

Aha! I have finally got it to work!
I saw another post by you discussing a similar topic where you suggested the following code:

#SelectExpand
1ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 2al_set_path_filename(path, "LowerFull.bmp"); 3 4bmp = al_load_bitmap(al_path_cstr(path, '/'));

I'm assuming then the error was simply in pointing to the file? Yet this seems odd as an absolute address didn't work either!

North~
Member #12,192
August 2010

I've seen that question and solution so many times. Someone just needs to put that in the manual somewhere. ALWAYS USE al_get_standard_path(ALLEGRO_RESOURCES_PATH);

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Matthew Leverton
Supreme Loser
January 1999
avatar

Yet this seems odd as an absolute address didn't work either!

You probably used back slashes like "c:\foo\bar.png" when it should be "c:/foo/bar.png".

Note that you can also do this:

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
chdir(al_path_cstr(path, '/'));
al_destroy_path(path);

bmp = al_load_bitmap("LowerFull.bmp");

Callum Gregg
Member #12,995
July 2011

Hmm, I am actually still having issues with this, it works for loading one of my bitmaps but not the others, the code is practically identical I'm not sure where I could be going wrong!

I can load in LowerFull.bmp but it wont let me load in LowerHalf.bmp, they are both in the same directory so I can't understand where the problem is.

If I print out the string it shows the correct directory for both yet the second returns false and if I try to draw it gives me an Access Violation error.

Any more ideas?

Matthew Leverton
Supreme Loser
January 1999
avatar

How big are the bitmaps?

Callum Gregg
Member #12,995
July 2011

The bitmaps are 100kb and 200kb in size, the larger one has no problem loading, while the bigger one does.

Arthur Kalliokoski
Second in Command
February 2005
avatar

The bitmaps are 100kb and 200kb in size.

I thought they had to be powers-of-two for dimensions, at least on some older cards. Those sizes don't strike me as fitting that criteria.

They all watch too much MSNBC... they get ideas.

Callum Gregg
Member #12,995
July 2011

Ah, perhaps, I have quite a modern graphics card however, but how would I go about optimizing the bitmaps for that then?

Oh sorry, the exact size is 332kb for LowerFull.bmp and 111kb for LowerHalf.bmp

Arthur Kalliokoski
Second in Command
February 2005
avatar

You'd set the dimensions in the bitmap editor to have the vertical and horizontal sizes to be powers-of-two, preferably 2048 or less. (MSPaint, paint.net or whatever you're using)

They all watch too much MSNBC... they get ideas.

J-Gamer
Member #12,491
January 2011
avatar

EDIT: beaten...

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Callum Gregg
Member #12,995
July 2011

Should I also destroy every bmp and path after I have used it? (I know its good practise to free up memory but are there any more implications caused by not deleting a bitmap/path from memory and trying to replace the same variable with another bitmap/path?)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I thought Allegro 5 automatically took care of any problems with NPOT (non power of two) textures by making them powers of two if NPOT is not supported.

Yes, you should destroy bitmaps when you are done with them. You can ignore this, but then you are leaking memory that you may need later.

Matthew Leverton
Supreme Loser
January 1999
avatar

The bitmaps are 100kb and 200kb in size, the larger one has no problem loading, while the bigger one does.

File size doesn't really matter, but the dimensions do. The height and width must be no larger than whatever your video card supports. Otherwise, you will have to load it as a memory bitmap (which is very slow).

They don't have to be a power of two.

Arthur Kalliokoski
Second in Command
February 2005
avatar

OTOH, 2048 x 2048 = 4,194,304 (well over 4000k) even for 8 bit color without header info.

They all watch too much MSNBC... they get ideas.

Thomas Fjellstrom
Member #476
June 2000
avatar

They don't have to be a power of two.

Some cards/drivers are moronic though, by advertising NPOT support, but doing it in software.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: