Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » load_bitmap not working

Credits go to CGamesPlay, Dustin Dettmer, gnolam, Indeterminatus, Kikaru, LennyLen, and someone972 for helping out!
This thread is locked; no one can reply to it. rss feed Print
load_bitmap not working
Charles256
Member #8,370
February 2007

I really hate asking this question because this seems like some little bug that's probably been posted a few dozen times. Either way, relevant code. The load_bitmap is returning null. I'm using MSVC 2005 pro edition. I put the images folder under the project folder and in it is a title.bmp file, would it be looking for the file somewhere else by any chance?

1void main(void)
2{
3 setup();
4
5 BITMAP *title=NULL;
6 title = load_bitmap("images/title.bmp", NULL);
7 bool test=false;
8 while(!key[KEY_ESC])
9 {
10 if(title!=NULL)
11 {
12 draw_sprite(screen,title,250,250);
13 }
14
15 rest(120);
16 }
17 destroy_bitmap(title);
18 allegro_exit();
19}
20END_OF_MAIN()

and for those wondering setup does the following...

and setup screen does

void setup_screen()
{
  int ret=set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0);
  if (ret!=0)
  {
    allegro_message(allegro_error);
    return;
  }
  textprintf(screen,font,1,1,makecol(255,189,73),"Extreme Racing - %dx%d",1024,768);
  rect(screen,0,12,1023,767,makecol(255,242,169));
  rect(screen,1,13,1022,765,makecol(255,242,169));
}

Edit: i actually had an end of main.;D Sorry about the lack of formatting, used to devnetwork.net which has a button to click for code editing. My mistake. :-D I am clicking the debug button which means it's supposed to look in the project folder.

CGamesPlay
Member #2,559
July 2002
avatar

Read this before posting code

Quote:

would it be looking for the file somewhere else by any chance?

Are you using Debug->Start Without Debugging, or double clicking on the EXE? In the case of the former, it will look under the project folder. In the case of the latter, it will look for project/Debug/images/title.bmp.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

gnolam
Member #2,030
March 2002
avatar

Edit your post with code tags so it's actually readable. Click the "Help" icon for info.

Quote:

I put the images folder under the project folder and in it is a title.bmp file, would it be looking for the file somewhere else by any chance?

Check the working directory for your project.

Also, "void main" is an illegal construction, and you lack an END_OF_MAIN(). That's all I'm willing to read in the unformatted code.

[EDIT]
DAMN YOU, CGAMESPLAY! >:(

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Charles256
Member #8,370
February 2007

Doh. You said in unformatted code. At first I took it as a strike upon my spacing. :-D Which is a bit off with the code tags... Oh well.. Sorry. :-D Honest, I know c++. Only reason I used a void main is because of the book I'm using to learn allegro. I normally do a good ole standard int main(){ return 0;} format but since the code didn't work the first time through I made the code more and more like the book so I could eliminate assumption errors.

gnolam
Member #2,030
March 2002
avatar

Quote:

Only reason I used a void main is because of the book I'm using to learn allegro.

... it's "Game Programming All In One", isn't it? :P

Anyway, on to the code:
First off, as a general rule, don't call any* Allegro functions before allegro_init(). It might be safe in this case, but in others your program might die a horrible death.

Quote:

set_color_conversion(16);

That's not how you do it. It possibly does something useful, but even in that case it's unreadable: set_color_conversion() takes as its argument an bitfield with your choice of conversion modes ORed together.

Quote:

textprintf

textprintf() is deprecated. Use textprintf_ex() instead.

Other than that it looks ok. :)

  • the exception being set_uformat()

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Charles256
Member #8,370
February 2007

yep. looks just fine, doesn't it? It's still not working though..... help? :(

gnolam
Member #2,030
March 2002
avatar

Well, how do you start your program? And what are your working directory settings in MSVS?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

LennyLen
Member #5,313
December 2004
avatar

Does it load the file correctly if you give the full path name?

someone972
Member #7,719
August 2006
avatar

See if the attached file works, it only has minor changes to the code you originally gave.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown

Charles256
Member #8,370
February 2007

I start the program by starting debugging which means it should search under my project folder for the image. And what do you mean by working directory settings? Been programming in c++ for a bit but it's all been command line stuff that very rarely acts this poorly so you'll have to forgive me. :-D Be a bit more specific and I'll dig up the info for you.

edit: the attached file did not work and even if I give the full path name it does not load teh file however I can load the file in any old image viewer.

someone972
Member #7,719
August 2006
avatar

Are you linking with -lalleg or -lalld, it might not even make a difference.
Oh ok.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown

Charles256
Member #8,370
February 2007

i have an additional dependency of alld.lib

gnolam
Member #2,030
March 2002
avatar

someone972: As I've said to others: if you have absolutely no idea, keep silent. Incorrect linking would show up immediately as a linking error. :P

Quote:

And what do you mean by working directory settings?

Somewhere in the myriad settings in MSVS you can set the working directory (= the directory your executable starts in) for your project.

Quote:

the attached file did not work and even if I give the full path name it does not load teh file however I can load the file in any old image viewer.

How did you load it with the full path?
And could you attach the BMP? It might be some obscure or broken variant that Allegro just won't load.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Charles256
Member #8,370
February 2007

this working directory, as you put it, as far as I can tell is the standard directory. all MSVC projects assume the project folder. there's two of em. the original one i tried to use is titled test now

gnolam
Member #2,030
March 2002
avatar

Those BMPs are kosher.
Two things just (hey, I'm tired :P) occurred to me however:
1) You should be exiting with an error message if the bitmap doesn't load instead of just not drawing it - otherwise, it might actually be loaded and drawn but (due to, say, erroneous color conversion*) blend in with the background.
2) (Since you do have a habit of ignoring parts of posts :P) ... you did change your call to set_color_conversion() to something proper, right? Drawing a non-8-bit BITMAP onto a different-depth-bit-BITMAP would be a Bad ThingTM.

  • cue dramatic foreshadowing music

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Kikaru
Member #7,616
August 2006
avatar

If you are running on Windows this: load_bitmap("images/title.bmp", NULL);
should be: load_bitmap("images//title.bmp", NULL);

Dustin Dettmer
Member #3,935
October 2003
avatar

Quote:

If you are running on Windows this:

What you are saying is false.

  1. Navigate to the directory containing your executable file

  2. Ensure that the images directory is in this directory

  3. Double click the executable

If load_bitmap is still returning NULL, you know the issue is something else.

Charles256
Member #8,370
February 2007

current code. it's an 8 bit bmp. i am exiting with an error message if it doesn't load. because if it wasn't null it wouldn't get to the else. :-D i took out color conversion. I also went to the folder with the exe, ran it, and it still didn't load the file. Thanks for all the help guys but we're still short. Please help. I can program but if I can't get this working I'll have to do thigns the hard way and learn direct x :-/

1#include <allegro.h>
2 
3void setup_screen()
4{
5 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0)!=0)
6 {
7 set_gfx_mode(GFX_TEXT,0,0,0,0);
8 allegro_message(allegro_error);
9 exit(1);
10 }
11 textprintf_ex(screen,font,1,1,makecol(255,189,73),0,"Extreme Racing - %dx%d",1024,768);
12 rect(screen,0,12,1023,767,makecol(255,242,169));
13 rect(screen,1,13,1022,765,makecol(255,242,169));
14}
15 
16void setup()
17{
18 allegro_init();
19 set_color_depth(8);
20 set_color_conversion(8);
21 install_keyboard();
22 install_mouse();
23 install_timer(); /*don't know if you need this*/
24 setup_screen();
25 show_mouse(screen);
26}
27 
28int main(void)
29{
30 setup();
31
32 BITMAP *title;
33 title = load_bitmap("title.bmp", NULL);
34 if(!title)
35 {
36 allegro_message("Error loading title.bmp");
37 return 1;
38 }
39 while(!key[KEY_ESC])
40 {
41 draw_sprite(screen,title,250,250);
42 rest(120);
43 }
44 destroy_bitmap(title);
45 allegro_exit();
46 return 0;
47}
48END_OF_MAIN()

LennyLen
Member #5,313
December 2004
avatar

The combination of your code and the title.bmp file you attached earlier works fine for me. The only thing different I'm doing is using MinGW instead of MSVC.

I've attached the .exe to this post. Check to see that it works when it's in the same directory as the .bmp.

gnolam
Member #2,030
March 2002
avatar

You're STILL not doing it right! >:(

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Charles256
Member #8,370
February 2007

i deleted the set color conversion entirely and it still didn't work. i didn't follow what you wanted me to do originally when you said i wasn't doing it right.

LennyLen
Member #5,313
December 2004
avatar

Quote:

i deleted the set color conversion entirely and it still didn't work. i didn't follow what you wanted me to do originally when you said i wasn't doing it right.

You supply a mode that says how conversions are specified. To see a list of the valid modes, see the manual.

And did the .exe I provided work for you?

Indeterminatus
Member #737
November 2000
avatar

Quoting the manual on allegro_message: "Outputs a message, using a printf() format string. Usually you want to use this to report messages to the user in an OS independant way when some Allegro subsystem cannot be initialised. But you must not use this function if you are in a graphic mode, only before calling set_gfx_mode(), or after a set_gfx_mode(GFX_TEXT). Also, this function depends on a system driver being installed, which means that it won't display the message at all on some platforms if Allegro has not been initialised correctly." [emphasis added, ed.]

That said, LennyLen's exe with your bitmaps (I tried them all by renaming them to title.bmp sequentially) displayed a white rectangle in the middle of the window for the two title.bmps, and some weird pixels for test.bmp that look like the bitmap was loaded correctly, but drawn with the wrong palette set.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Charles256
Member #8,370
February 2007

: sighs : it still didn't work for me.... I have a small theory... Windows Vista no longer sees it's directory as C:\PROGRA~1\micro~\blah\blah any more. It actually now acknowledges it as literally "C:\Program Files\Microsoft Visual Studio" maybe it's getting lost? Somehow I doubt that but it was the problem I ran into when I tried to compile allegro. I eventually gave in and just used the pre-compiled files. :-D So, to someone who has this working. Can you please tell me your exact system setup. I'll miss my parameter pop up on functions but I'll get over it and c++ is c++ anywhere so I'm to the point I'm willing to try a different compiler. And a continuing thanks to all those that contniue to provide input.

Edit: Installed XP Pro. Set up everything. At first it didn't work. Rebooted my computer, worked like a charm with my existing code. You shall see a remake and improvement on New Horizons: Uncharter Waters from me shortly. ( SNES, Sega, PC, and Nintendo version of aforementioned games exist)

Go to: