Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 4 doesn't load bitmaps and program crashes!

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Allegro 4 doesn't load bitmaps and program crashes!
coderthatcancode
Member #16,679
May 2017

Hey guys, I'm new with the allegro community and I know C programming.

I've recently started doing allegro Code::blocks development, and that's been a real drag lately. I cant seem to load a bitmap image (mario8.bmp), and yes I'am totally aware of the allegro 5 library, but I prefer to use allegro 4 for now, and its much easier. Here down below I will provide the code that I used from a tutorial ( https://www.youtube.com/watch?v=ceOTjZOD6wo&t=1s ) I'm running on a 32 bit machine with windows vista, it appears in this tutorial he uses something older. Anyway, I'm not sure what's going on, and just to answer any sample questions, yes the image is named correctly, the file is in the folder, the image is where the .exe file is. Every time I run the program it goes to the 8-bit color scheme on windows vista then crashes and says "Allegro Game.exe has stopped working", then returns to its normal color scheme. Please help me, I don't know how to solve this or what to do.

Kind regards coderthatcancode

The code that I have used:

#include <allegro.h>

int main()
{
allegro_init();
install_keyboard();

set_color_depth(8); // 8, 15, 16, 32
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

BITMAP *bmp = create_bitmap(640, 480);
clear_bitmap(bmp);

BITMAP *character = load_bitmap("mario8.bmp", NULL);

while(!key[KEY_ESC])
{
blit(character, bmp, 0, 0, 0, 0, character->w, character->h);
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
}

destroy_bitmap(bmp);
destroy_bitmap(character);

return 0;
}
END_OF_MAIN()

LennyLen
Member #5,313
December 2004
avatar

Try changing the colour depth like this and see if it makes a difference:

set_color_depth(desktop_color_depth());

EDIT:

If it still isn't working, try using the bmp that I attached. Using that combined with your code and using desktop_color_depth() worked for me.

coderthatcancode
Member #16,679
May 2017

+LennyLen Yeah I did that but it doesn't seem to work and still crashes..btw I'm using Code::Blocks as the IDE

Response to edit: Yeah, the image doesnt seem to work, do you think it could be possibly to do with the image being next to the executable??

LennyLen
Member #5,313
December 2004
avatar

Did you try the bmp I uploaded?

coderthatcancode
Member #16,679
May 2017

yeah, and quick question here? are you using codeblocks? if so, what did you do to make it work?

LennyLen
Member #5,313
December 2004
avatar

If you're running it from within C::B, then the image needs to be with your .cbp file, not with the .exe file.

EDIT:

Here's a more robust version of that code:

#SelectExpand
1#include <allegro.h> 2 3int main() 4{ 5 allegro_init(); 6 install_keyboard(); 7 8 set_color_depth(desktop_color_depth()); // 8, 15, 16, 32 9 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); 10 11 BITMAP *bmp = create_bitmap(640, 480); 12 clear_bitmap(bmp); 13 14 BITMAP *character = load_bitmap("mario8.bmp", NULL); 15 if (!character) { 16 17 if (file_exists("mario8.bmp", 0, NULL)) { 18 19 allegro_message("mario8.bmp was located but could not be loaded."); 20 exit(EXIT_FAILURE); 21 22 } 23 else { 24 25 allegro_message("could not locate mario8.bmp"); 26 exit(EXIT_FAILURE); 27 } 28 29 } 30 31 while(!key[KEY_ESC]) 32 { 33 blit(character, bmp, 0, 0, 0, 0, character->w, character->h); 34 blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h); 35 } 36 37 destroy_bitmap(bmp); 38 destroy_bitmap(character); 39 40 return 0; 41} 42END_OF_MAIN()

coderthatcancode
Member #16,679
May 2017

Thanks so much it worked, would I do this for all of my allegro games, but what about the 8-bit image how would I load that.

EDIT: yeah i will also use to use the little check if the character file is in the folder, quick question though, how would I do it, if I was going to create my own bitmap?

LennyLen
Member #5,313
December 2004
avatar

Yes, for each C::B project, if you're running it from within the IDE you need to have all your files (images, dlls, etc) in the project's base directory. When the project is finished and you want to share it, then everything needs to be with the .exe.

You don't need to be in 8-bit mode to load 8-bit images. Allegro will automatically convert them to the bit depth you're using when it loads the image.

edit:

When you say create your own bitmaps, do you mean you want to load ones you've created in a paint program, or ones you've created in your code?

coderthatcancode
Member #16,679
May 2017

When I said create your own bitmaps, I mean as in create your own bitmap image using the paint software or GIMP for the game. So, How would you accomplish this?

NOTE: I don't mean in code just to confirm that

Neil Roy
Member #2,229
April 2002
avatar

Just a note, newer versions of Windows don't do 8bit well anymore, they tend to mess up the palette, so I strongly recommend moving up to 24 or 32bit (32bit is just 24 with an alpha channel). You can still use the pink colour for transparent sections in Allegro 4 at these depths.

My Deluxe Pacman 1 (link in my sig) uses Allegro 4 and used to be 8 bit many moons ago. Switching it to use 32bit with Allegro 4 was easy and ensures it looks proper on all systems.

My Deluxe Pacman 2 uses Allegro 5. If you take the time to learn to use Allegro 5 it is well worth switching to.

---
“I love you too.” - last words of Wanda Roy

coderthatcancode
Member #16,679
May 2017

+Neil Roy so good to see you sir, I didn't know you use allegro, I remember seeing you on VertoStudio's video in the comment section advising him a lot, thanks for the recommendation and I just recently moved on from SDL 2, I like allegro 4 better than allegro 5, I find that they completely changed the whole API and I wasn't happy at all with that, plus its way easier for me to get my way around. I'm not just focusing on the performance of it, I'm also looking out for the rate/ difficulty of it I must say it is way easier than SDL and in allegro, you could write int main() with out any argument counters with in the parentheses of it. Its just the way I learnt it with the simple int main(). No doubt about it I will probably use allegro 5 for my CPP projects but as I am doing C right now, I thinks its best to play it allegro 4 safe and plus if I couldn't even load a bitmap image in allegro 4 (now fixed) then how could I load an bitmap image in allegro 5.

Anyway, I'm just saying, after all I know C and its rather fun to write apps in it, and I am aware of course that C can do allegro 5, but I still prefer allegro 4 just because of its simplicity. Thanks again though :)

EDIT: After all I did just start with allegro 4 the first time I saw allegro in a video was this: https://www.youtube.com/watch?v=F9WtUbyZI3E&t=648s so thats what got me to allegro 4.

Also, I'm having problems with my bitmap, what did you do for the other bitmap to go well with allegro I tried using another mario image, what did you do for the other one, and why isn't this one showing up?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

gah, please don't litter your project directory with data files. Put them in their own folder under a single folder where your executable resides. C::B has a setting to change the working directory when you use C::B to run it. Go to Menu->Project->Properties. Go to the Build targets tab and change the execution working directory and output filename. Done. Voila.

As for your bitmap, it's corrupt, or A4 doesn't recognize its particular format.

{"name":"610911","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/3\/e3c575f1e66a52bb266e0ce061bf2531.png","w":800,"h":629,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/3\/e3c575f1e66a52bb266e0ce061bf2531"}610911
{"name":"610912","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/d\/9d92d25c32a1af449a3602369c3a95ab.png","w":314,"h":188,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/d\/9d92d25c32a1af449a3602369c3a95ab"}610912

coderthatcancode
Member #16,679
May 2017

+Edgar Reynaldo Thanks for responding, how do you make the bitmap not corrupted so that I can load the Mario image?

I did set the working directory to what it should read but, what will that do also?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

{"name":"610914","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1c55d6d968f3305af0406bf1af5def9d.png","w":1113,"h":665,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1c55d6d968f3305af0406bf1af5def9d"}610914

Try this mario8a.bmp with exbitmap.exe. Run the Run443Examples.bat file in the allegro folder and then run "exbitmap.exe mario8a.bmp" after saving mario8a to the bin/examples folder. All I did was load it in paint.net and then save it again as a different 8 bit bitmap. Whatever paint program was used to create and save mario8.bmp apparently didn't save it in a format allegro 4 can load.

coderthatcancode
Member #16,679
May 2017

+Edgar Reynaldo I don't have the run thing, I installed allegro using this tutorial: https://www.youtube.com/watch?v=QPaiGhrGq6I and it showed me how to configure allegro for code::blocks in my MinGW folder for code::blocks it doesn't have the run thing that you suggest I use I didn't install it the way you last told me to do it.

EDIT: What do you mean you saved it again?

LennyLen
Member #5,313
December 2004
avatar

gah, please don't litter your project directory with data files. Put them in their own folder under a single folder where your executable resides.

Which executable? I have several build profiles, and thus have several executables.

What I actually do is have a data directory that is in the base directory. That way all builds use the one copy of the data.

coderthatcancode
Member #16,679
May 2017

+LennyLen how did you load the 8-bit mario image without it crashing mine seems to crash every time I load it and I set it to the desktop color in the allegro code, i don't get it. It should support my windows because I set it to my desktop color.
Please help!! D:

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Please help!! D:

Things like this don't help. What we need is details. Show your latest code, show us how you run the program, and show us the directory structure of your project.

As I said, the mario8.bmp image you posted is not understood by allegro. The image I posted is. Use mario8a.bmp. I simply saved it in paint.net, which uses a format Allegro 4 understands.

If you're using the updated image and your program is still crashing, then you have an error in your code, or you are not running the program from the correct directory.

Post details.

coderthatcancode
Member #16,679
May 2017

Okay, I'll try this again. with screenshots this time,

So load_bitmap in allegro 4 wont work and every time I try and load an image it crashes, I have my bitmap images in the same folder next to my code block files and I'm trying to load the 8-bit Mario, I provided an image of my folder as well as the code::blocks IDE in the Attachment.

I don't know why its not loading or what it means for it to be corrupted.

The code used:
#include <allegro.h>

int main()
{
allegro_init();
install_keyboard();

set_color_depth(desktop_color_depth()); // 8, 15, 16, 32
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

BITMAP *bmp = create_bitmap(640, 480);
clear_bitmap(bmp);

BITMAP *character = load_bitmap("mario32.bmp", NULL);

int x = 0;
int y = 0;

while(!key[KEY_ESC])
{
draw_sprite(bmp, character, x, y);
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
clear_to_color(bmp, makeacol(255, 255, 255, 255));
}

destroy_bitmap(bmp);
destroy_bitmap(character);

return 0;
}
END_OF_MAIN()

I don't get what's wrong with it I get 0 errors and 0 Warnings in code.
When I seem to load mario 8 its fine, I don't know if its the color or anything, whereas if mario 32 was loaded (which has no difference what so ever its just an 8-bit mario with no change in color scheme what so ever) it fails and I get "Allegro Game has stopped working", and the console returns a negative value, and other stuff...

I just don't know what to do now, I've provided images and stuff to help and make it easier. The main problem is when ever I load the image, it NEVER shows up.. well, only if its mario8.bmp which is just a purple square which Lenny gave me instead, because he said to try it.

QUESTION: How would you create your own bitmap images for allegro

Thanks again coderthatcancode

EDIT: It worked it turns out you need to edit the mario32.bmp in a paint program and then save it as an 24-bit bitmap, unblock the file as it may be blocked in properties

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You're not checking any return values. load_bitmap can, and sometimes does, fail. Your original mario8.bmp file was in a format that Allegro 4 cannot understand. That's why it fails, and load_bitmap would have returned a null pointer in that case. That's why Lenny's and my mario8a.bmp both display properly. Or else it is because your image file is not in the directory it is supposed to be in, or your current working directory is not the one with the image in it.

coderthatcancode
Member #16,679
May 2017

It worked it turns out you need to edit the mario32.bmp in a paint program and then save it as an 24-bit bitmap, unblock the file as it may be blocked in properties

Although when I edited in paint it came out really weird when I saved it as 24-bit image, which is what allegro understands. Do you think if I used a different paint program it would save differently if I saved it as 24 bit?

Chris Katko
Member #1,881
January 2002
avatar

FYI, Allegro 4 sucks. ;) Use Allegro 5. Or PNG.

Allegro 4 CANNOT LOAD certain types of BMP files. More accurately, a BMP file with a specific VERSION header. Merely loading the same bitmap in GIMP and resaving it as BMP can fix it.

If you have a choice for file formats, always use PNG. It's basically a compressed BMP. (Lossless. So it looks the same.) Nobody uses BMP anymore. There's basically zero advantage to using BMP, and PNG is supported in Allegro 4.

I ran into the exact same problem when I trying to use Allegro 4 for an image manipulation tool. It kept blowing up on bitmap files and I found it out the hard way the header issue.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

coderthatcancode
Member #16,679
May 2017

Nah, I still prefer allegro 4, its way easier than allegro 5 and for me it makes a lot more sense.

I will probably move on to allegro 5 when I learn C++, but I will take note of it, its still possible to develop a game in allegro 4 anyway.

Yes, I am aware of .png files I've used them a lot when it came to SDL2, which is why I moved on to allegro 4. Its way easier than SDL2 in my opinion and that's what I like about it, it also seems that there a lot of people on the allegro web site, so if I launch a new game people might know about it, plus allegro 5 in a way is kinda advanced, I've programmed in it before and I didn't like it because it just changed the allegro that we know. Why would I want that? I will most likely use if for C++ for sure, and I know it works for C as well, but if you ask me I think C++ would work better, Thanks anyway.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

FYI, Allegro 4 sucks.

I would have to disagree with this. Allegro 4 is good at what it does, which is software rendering. However if you want hardware acceleration, or an event based system that doesn't poll, then yes, A4 sucks compared to Allegro 5.

My game Skyline uses Allegro 4 and a custom function I wrote to draw gradient circles. It's quite fast. I had SiegeLord's knowledge and expertise to help me write it though. However, the modern way to do it would be to use a shader on the gpu. I don't have that knowledge yet, so for me and Skyline A4 is still a good choice. It has it's niches, but yes in the overall picture A4 is obsolete and should be replaced by A5 whenever possible.

You didn't mention whether your problem was solved yet.

LennyLen
Member #5,313
December 2004
avatar

You didn't mention whether your problem was solved yet.

It worked it turns out you need to edit the mario32.bmp in a paint program and then save it as an 24-bit bitmap

So basically, what you were telling him all along. And if he'd used the full version of the code I posted, it would have told him that the problem wasn't finding the file, but that the problem was loading it.

 1   2 


Go to: