loading bitmaps with allegro
blackwind87

Hello!
I'd written a programm to show a bitmap on the screen. After compiling there where no errors or warnings. But each time I start the exe-file there comes an error-message. The bitmap is in the same directory like the .cpp and the .exe and I can't find the error in the programm, I hope you can help me.

#include <allegro.h>

int set_graphic_mode()
{
set_color_depth(32);
if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0) >= 0)
{
return 1;
}
set_color_depth(24);
if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0) >= 0)
{
return 1;
}
set_color_depth(32);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0) >= 0)
{
return 1;
}
return 0;
}

int main(int argc, char **argv)
{
allegro_init();
install_keyboard();

if (!set_graphic_mode())
{
allegro_message("Unable to set graphic mode!");
exit(0);
}

BITMAP *bild = load_bitmap("ffx-2.bmp", NULL);

acquire_screen();
clear(screen);
blit(bild, screen, 0, 0, (SCREEN_W - bild->w)/2, (SCREEN_H - bild->h)/2, bild->w, bild->h);
release_screen();

while (!keypressed());
}
END_OF_MAIN();

gnolam

1) Edit your post to use code tags.

2)

Quote:

But each time I start the exe-file there comes an error-message.

What message? Always include all the relevant information when asking for help.

3)

Quote:

BITMAP *bild = load_bitmap("ffx-2.bmp", NULL);

You need error checking.

BITMAP *bild = load_bitmap("ffx-2.bmp", NULL);
if (!bild) {
    allegro_message("Couldn't find ffx-2.bmp!\n");
    exit(1);
   }

4)

Quote:

The bitmap is in the same directory like the .cpp and the .exe and I can't find the error in the programm, I hope you can help me.

And are you running the program through the commandline or through an IDE of some kind? IDEs are notorious for using weird working directories. This will, however, be caught by 3) above.

5) Main should return 0.

6) Minor points:
a)

Quote:

END_OF_MAIN();

Remove the semicolon (counter-intuitive, I know, but it's how the macro is constructed).

b) acquire_screen() and release_screen() aren't necessary. Here, they just clutter up the code (and if you put anything not graphics related in between them, Bad ThingsTM happen to your program).

Evert

You should check the return value of load_bitmap().

Quote:

The bitmap is in the same directory like the .cpp and the .exe

That's probably irrelevant. The file needs to be in the current working directory when the program is run.
To find the directory where the binary is stored, use get_executable_name().

LennyLen

What is the error message?

Also, why on earth have you written your set_graphic_mode function the way you have? END_OF_MAIN() also does not have a semicolon at the end of it.

blackwind87

This program was an example in a book i buied and I thought it should work...
Thanks for your help, I will see if I can solve the problem now!

LennyLen

Which book?... [cue spooky music]

blackwind87

It's a german book about programming with allegro but it is from the year 2003. I don't know the actual titel...
I have fixed some things like described above, but now there comes the message that ffx-2.bmp couldn't be found. The bitmap is in the same directory and the name is correct and so I don't understand, why it can't be found!?

LennyLen
Quote:

It's a german book about programming with allegro but it is from the year 2003.

Oh, I was expecting it to be another book notorious for bad coding practices. But hmmm, a German book on Allegro - Spellcaster's? (This question is directed at established a.cc members, not you blackwind.)

Quote:

The bitmap is in the same directory and the name is correct and so I don't understand, why it can't be found!?

Well, as gnolam has already mentioned, some IDEs create their own working directories, so if you're attempting to run the program from the IDE, it may be looking in the working directory.

Evert
Quote:

why on earth have you written your set_graphic_mode function the way you have?

What's wrong with it?
It's good practice to try different graphics modes if one doesn't work, and putting the code in a function makes it clear and readable. That code is fine.

Quote:

[cue spooky music]

Nah, the programming style is different. Besides, people who use GPAIO2E #include <conio.h> for no good reason.

Quote:

It's a german book about programming with allegro

Ah, that would be spelly's book.

Quote:

buied

bought. ;)

Quote:

but now there comes the message that ffx-2.bmp couldn't be found. The bitmap is in the same directory and the name is correct and so I don't understand, why it can't be found!?

Because it needs to be in the active working directory when the program is run,which is not nescessarily the same as the directory that contains the executable.
Try (almost a direct copy&paste from the manual)

   char name[200];
   BITMAP *bild;

   get_executable_name(name, sizeof(name));
   replace_filename(name, name, "ffx-2.bmp", sizeof(name));
   bild = load_bitmap(name, NULL);

CGamesPlay
Quote:

It's a german book about programming with allegro but it is from the year 2003. I don't know the actual titel...

Spielenprogrammen mit Allegro (Lennart Steinke), isn't it? At least, that is the only one anyone at this forum will be familiar with ;)

Quote:

I have fixed some things like described above, but now there comes the message that ffx-2.bmp couldn't be found. The bitmap is in the same directory and the name is correct and so I don't understand, why it can't be found!?

What is the compiler you are using? As said, you have to actually run the program from the same directory as the image file is in. That means that if you double-click on the program icon, make sure the image is also in that Explorer window. Also, double-click on the image and make sure it loads properly.

[append]

Quote:

What's wrong with it?
It's good practice to try different graphics modes if one doesn't work, and putting the code in a function makes it clear and readable. That code is fine.

His code sets 3 different graphics modes, and 2 of them are the same mode :)

LennyLen
Quote:

What's wrong with it?

Actually, nothing, I just REALLY didn't read it properly and somehow missed all the "return 1"'s (my excuse is that I've been awake for about 70 hours now and the lack of formatting was making my eyes bleed). It appeared he was setting the graphics mode to three different settings back-to-back.

[edit]

Quote:

His code sets 3 different graphics modes, and 2 of them are the same mode

Look again.

blackwind87

Okay, now there is the first message again:

An unhandled win32 exception occured in prog4.exe[2988]. Just-In-Time debugging this exception failed with the following error: No installed debugger has Just-In-Time debugging enabled. In Visual Studio, Just-In-Time debugging can be enabled from Tools/Options/Debugging/Just-In-Time.

But I doesn't have Visual Studio:o

William Labbett

I've got the same problem tonight.

I'm using Dev-C++ and I'm trying to load a bitmap and display it (shouldn't be too hard), but the program can't load it. Like Blackwind it's in the same dir as the .exe. Other programs have managed to load bitmaps like this (from the same dir as the .exe). But it's not working this time..???

Evert
Quote:

it's in the same dir as the .exe

Once again, it doesn't matter if it's in the same directory as the binary, it needs to be in the working directory. Check that it is and if it is, post the program and the image.

William Labbett

actually i've found where the error is. i had the file name wrong in the code.
sorry for being stupid :-[

Thread #587322. Printed from Allegro.cc