Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » How to load a image

This thread is locked; no one can reply to it. rss feed Print
 1   2 
How to load a image
kyle motherwell
Member #11,111
July 2009

Well, i have a lot of trouble on trying to load a images that i have, how exactly do i load one? I presume its like this

BITMAP *john = NULL;
john = load_bitmap(C:\pictures\john.bmp,NULL);

Is this correct, please help. ???

Indeterminatus
Member #737
November 2000
avatar

You need to get yourself a book on C. String literals are usually surrounded by ".

Is this correct

The answer to that question would be given to you any compiler; it will throw this back at you with an error.

edit: Resolved ambiguity.

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

kyle motherwell
Member #11,111
July 2009

Ah shit sorry
nah ive already done that, i just forgot to do it then.
put it this way the compiler always compains about not being able to find it.

didint quite explain that clearly, i just forgot to put "" there, i ussualy do. I just typed it in s an example. The program can never find the image.

Indeterminatus
Member #737
November 2000
avatar

You give an absolute path, so you must make sure the image is at the position specified. Note that when using \, you need to escape them in string literals ("C:\\pictures\\john.bmp"). Alternatively, you can use slashes ("C:/pictures/john.bmp").

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

kyle motherwell
Member #11,111
July 2009

OK here is the original code that me and others had trouble with

#include <allegro.h>

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

set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,860,0,0);

BITMAP *buffer = NULL;
BITMAP *John = NULL;
BITMAP *John_IronMan = NULL;
BITMAP *hand = NULL;
BITMAP *backdrop = NULL;

buffer = create_bitmap(100,100);
John = load_bitmap("C:/Documents and Settings/Kairu/Desktop/VC++ misc/cube/Test_Allegro/Debug/JOHN_PESTER_CURSOR.bmp",NULL);
John_IronMan = load_bitmap("C:/Documents and Settings/Kairu/Desktop/VC++ misc/cube/Test_Allegro/Debug/JOHN_HEAD_IRONMAN.bmp",NULL);
hand = load_bitmap("C:/Documents and Settings/Kairu/Desktop/VC++ misc/cube/Test_Allegro/Debug/JOHN_HEAD.bmp",NULL);

draw_sprite(screen,John_IronMan,mouse_x,mouse_y);

readkey();

return 0;
}
END_OF_MAIN();

i don't think there's nothing wrong with it, but it cant seem to find the image

Indeterminatus
Member #737
November 2000
avatar

OK, for one, please use the code tags. Secondly, a path with spaces might be problematic, so to exclude any other errors, try loading a bitmap from a path that has no spaces in it.

What exactly is the problem here? Does your program crash, does it just not show anything ... ? You don't check for errors of any of the functions you're calling, but you ought to do that.

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

kyle motherwell
Member #11,111
July 2009

I made a new directory for the images, heres the error i get which is the same one as before,

Unhandled exception at 0x67a0130a in Test_Allegro.exe: 0xC0000005: Access violation reading location 0x0000001c.

this is the directorys i set

John = load_bitmap("C:\\Pictures4alleg\\JOHN_HEAD.bmp",NULL);

John_IronMan = load_bitmap("C:\\Pictures4alleg\\JOHN_HEAD_IRONMAN.bmp",NULL);

hand = load_bitmap("C:\\Pictures4alleg\\JOHN_PESTER_CURSOR.bmp",NULL);

When it breaks a little green return arrow appears at the side of this line of code

draw_sprite(screen,John,mouse_x,mouse_y);

is there something wrong with my compiler or something cause i already made a post and nobody really seemed to know how to fix it.

OnlineCop
Member #7,919
October 2006
avatar

Do you have error checking in place?

  BITMAP *john = NULL;
  john = load_bitmap("C:/pictures/john.bmp", NULL);
  if (!john) {
    // Bitmap not found, show an error
  }

Do this before ANY call to draw_sprite(), blit(), etc. That will probably give you a lot better error message than a generic "Unhandled exception..."

EDIT: I would also recommend compiling with debug info, and then running it through a debugger (like gdb):

  gcc -g -W -Wall -o filename source.c
// ...or...
  g++ -g -W -Wall -o filename source.c

kyle motherwell
Member #11,111
July 2009

Yes i have done this and it returns as NULL

I jst had a thought, i looked at the images im trying to use, and the one i used that worked, they are both 24 bitmaps, so it is not the image or its destination, also when it loads the program the entire background was bright pink then it breaks.
I started having image loading programs when i tried to do a separate project using allegro, i might of messed up one of the settings perhaps, i don't know but hope these details helped

Thomas Fjellstrom
Member #476
June 2000
avatar

That means its failing to find, or load your images.

try adding the following in your error handling:

allegro_message(allegro_error);

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

kyle motherwell
Member #11,111
July 2009

#include <allegro.h>

#SelectExpand
1int main(int argc, char *argv[]) 2{ 3allegro_init(); 4install_keyboard(); 5 6 7set_color_depth(32); 8set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,860,0,0); 9 10BITMAP *buffer = NULL; 11BITMAP *John = NULL; 12BITMAP *John_IronMan = NULL; 13BITMAP *hand = NULL; 14BITMAP *backdrop = NULL; 15 16buffer = create_bitmap(100,100); 17John = load_bitmap("C:\\Pictures4alleg\\JOHN_HEAD.bmp",NULL); 18John_IronMan = load_bitmap("C:\\Pictures4alleg\\JOHN_HEAD_IRONMAN.bmp",NULL); 19hand = load_bitmap("C:\\Pictures4alleg\\JOHN_PESTER_CURSOR.bmp",NULL); 20 21if (John = NULL) { 22 allegro_message(allegro_error); 23 } 24 25draw_sprite(screen,John,mouse_x,mouse_y); 26 27 28readkey(); 29 30 31 32return 0; 33} 34END_OF_MAIN();

I feel like im running in circles, i have added an error thing it does the same thing.

gnolam
Member #2,030
March 2002
avatar

Attach the BMP.
And as we said before, check the return value of every function that can fail.

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

kyle motherwell
Member #11,111
July 2009

Here it is.

So what now?

LennyLen
Member #5,313
December 2004
avatar

Here's the problem:

if (John = NULL) {

You're setting it to NULL after loading it. Instead, it should be: if (John == NULL) {

kyle motherwell
Member #11,111
July 2009

Ok done that, same thing again.

Thomas Fjellstrom
Member #476
June 2000
avatar

Ok done that, same thing again.

what does the popup from "allegro_message" say? It should give you the error.

try also:

if (John == NULL) {
   allegro_message(allegro_error);
   rest(10000);
}

And do that for every single image you're loading.

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

LennyLen
Member #5,313
December 2004
avatar

Well, with that change made, your code runs fine for me (after I removed the lines for loading the other bitmaps).

kyle motherwell
Member #11,111
July 2009

It doesnt give an error, it just break exception thingie, i removed the other lines. Still does it.

LennyLen
Member #5,313
December 2004
avatar

It doesnt give an error, it just break exception thingie

Say that again with real words please.

kyle motherwell
Member #11,111
July 2009

Unhanded exception, same shit as before. I tried what the other guy said worked for him. Did the same thing.

So i take it everyone is stumped.

Case CLOSED, im officially FUCKED xD??? :P

GullRaDriel
Member #3,861
September 2003
avatar

Aller,

That is always the same shit with guys like you Kyle. Anyway, as I am a kindly guy tonight:

#1 If you ever know how to compile in debug mode with your compiler, then do it and go to #2, else go to #3

#2 If you ever know how to launch your app in a debugger, then do it brainy you are, do it ! else go to #3

#3 ask google, and go back to the original question

Also, I am pretty sure that if you really check each return code of each allegro function you use and exit if it do not match the wanted value you would catch the error.

Please also consider watching the results of any malloc, new, create_bitmap, ... any pointer you fill.

All that is what LennyLen is asking to you, stone head ! >:( We know your program is crashing, but what does it says ? hmmmm ??

And the case isn't closed. You choose to close it because it's easier for you. >:(

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

kyle motherwell
Member #11,111
July 2009

Ok this is what i mean by circles, you've got guys like the one above me going "Oh, tell me what its saying"When i already have told you. So really your not listening. Ive already checked on Google and all i ask is what you think, not your criticism on a piece of code that looks fine. I appreciate you help just try to not to bite a beginners finger off for asking a question

EDIT: i cant seem to draw any images to the screen at all, what could this be?

OnlineCop
Member #7,919
October 2006
avatar

#SelectExpand
1#include <allegro.h> 2 3int main(int argc, char *argv[]) 4{ 5 BITMAP *buffer = NULL; 6 BITMAP *John = NULL; 7 BITMAP *John_IronMan = NULL; 8 BITMAP *hand = NULL; 9 BITMAP *backdrop = NULL; 10 11 char john_filename[] = "C:\\Pictures4alleg\\JOHN_HEAD.bmp"; 12 char iron_filename[] = "C:\\Pictures4alleg\\JOHN_HEAD_IRONMAN.bmp"; 13 char hand_filename[] = "C:\\Pictures4alleg\\JOHN_PESTER_CURSOR.bmp"; 14 15 allegro_init(); 16 install_keyboard(); 17 18 set_color_depth(32); 19 set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,860,0,0); 20 21 22 buffer = create_bitmap(100,100); 23 John = load_bitmap(john_filename, NULL); 24 John_IronMan = load_bitmap(iron_filename, NULL); 25 hand = load_bitmap(hand_filename, NULL); 26 27 if (NULL == John) { 28 allegro_message("Cannot load file \"%s\"! EXITING.", john_filename); 29 allegro_message(allegro_error); 30 return 1; 31 } 32 33 draw_sprite(screen, John, mouse_x, mouse_y); 34 35 readkey(); 36 37 return 0; 38} END_OF_MAIN()

If it can't load the image, or if the file can't be found, you'll receive the error. If that's the case, either the file is in the wrong place, or you need to save it with a different format.

Let us know what you're getting as a result of this. (Warning: I didn't try to compile this code. I don't make mistakes ::) )

LennyLen
Member #5,313
December 2004
avatar

OnlineCop said:

I don't make mistakes

Except you used = instead of == as well. :P

Kyle, try running the attached .exe and see if it works (it's just your code compiled by me).

OnlineCop
Member #7,919
October 2006
avatar

LennyLen said:

OnlineCop said:

I don't make mistakes ::)

Except you used = instead of == as well. :P

My point exactly ;)

 1   2 


Go to: