Load Bitmap in VC++
Skax459

OK if you saw my other topic, I remade it because I forgot to title the other own. Sorry, sorry. Anyway,

I'm trying to make a game and I'm trying to load some bitmaps. I'm using Visual C++ 8.0. I added the file as a reference, but it won't run. When I take out the little piece of bitmap loading code, it works fine. When its in, it returns this error: (The game is Zylin)

Unhandled exception at 0x10042eaa in Zylin.exe: 0xC0000005: Access violation reading location 0x00000000.

after that, it says:

No symbols are loaded for any call stack frame. The source code cannot be displayed.

The code is:

1//========================================
2//Zylin
3//------ ------, 22 Feb. 2007
4//========================================
5//INCLUDES & STUFF
6#include <allegro.h>
7//FUNCTION PROTOTYPES
8 
9int main(){
10 //Allegro Stuff
11 int ret;
12 allegro_init();
13 install_keyboard();
14 install_timer();
15 set_color_depth(8);
16 ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
17 if (ret != 0){allegro_message(allegro_error);return 1;}
18 //Actual Game
19 textprintf_ex(screen, font, 1, 1, makecol(255,255,255), -1, "Hello world!");
20 //Load the pics to play with
21 char *filename1 = "h_sprite.bmp";
22 BITMAP *h_sprite;
23 h_sprite = load_bitmap(filename1, NULL);
24 if (!h_sprite){set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
25 allegro_message("Error loading %s", h_sprite);return 1;}
26 while(!key[KEY_Q]);
27 allegro_exit();
28}END_OF_MAIN()

Anyone know why?

Kitty Cat
Quote:

allegro_message("Error loading %s", h_sprite);

h_sprite is the BITMAP* (which has evaluated to NULL/0x00000000 to get there), not the filename.
Tips:
Turn on warnings, and don't ignore them. (sorry, forgot.. Allegro on VC++ can't warn about printf-format errors on Allegro's own functions :P)
Learn proper indentation. It's much easier to spot errors when you can see the stack flow.

Skax459

Yay! Thanks! Well, it doesn't crash, but I still don't know if it works...::)

EDIT: I change my previous opinion. I altered the code to this:

1//========================================
2//Zylin
3//Samuel Milito, 22 Feb. 2007
4//========================================
5//INCLUDES & STUFF
6#include <allegro.h>
7//FUNCTION PROTOTYPES
8 
9int main(){
10 //Allegro Junk
11 int ret;
12 allegro_init();
13 install_keyboard();
14 install_timer();
15 set_color_depth(8);
16 ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
17 if (ret != 0)
18 {
19 allegro_message(allegro_error);
20 return 1;
21 }
22 //Actual Game
23 textprintf_ex(screen, font, 1, 1, makecol(255,255,255), -1, "Hello world!");
24 //Load the pics to play with
25 char *hsprite_name = "h_sprite.bmp";
26 BITMAP *h_sprite;
27 h_sprite = load_bitmap(hsprite_name, NULL);
28 if (!hsprite_name)
29 {
30 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
31 allegro_message("Error loading %s", hsprite_name);return 1;
32 }
33 blit(h_sprite, screen, 0, 0, 0, 0, 15, 15);
34 while(!key[KEY_Q]);
35 allegro_exit();
36}END_OF_MAIN()

And now it returns this:

Unhandled exception at 0x10004d4d in Zylin.exe: 0xC0000005: Access violation reading location 0x00000000.

Its the addition of the line with blit that causes the problem- without that it runs fine.

Its similar to the first time, and I've never done bitmaps and stuff yet...mainly graphics primitives.

Kitty Cat
Quote:

    h_sprite = load_bitmap(hsprite_name, NULL);
    if (!hsprite_name)

I think you goofed a bit there.

And you should probably the filename variable to a const char*, since that's what a string literal is.

Skax459

Um...what did I goof?
And how do I fix it?

Kitty Cat

hsprite_name is the pointer to the filename, which since you explicitly set it to a string literal, will never be NULL. You want to check load_bitmap's return value, h_sprite.

Skax459

Sonova...now it runs, but it says "Error loading h_sprite.bmp"

bamccaig

You are dereferencing NULL (referencing the data pointed to by a pointer that is set to NULL). I just had a long thread about it and got very good explanations of what is going on and why. It might help you out since you seem to be having the same problem:

http://allegro.cc/forums/thread/590183 ???

Pretty much if you see an "access violation" it means your program is trying to access memory that is not allocated to it. If the address is 0x00000000 then that is 0 or NULL and you can assume you are trying to access data pointed to by a pointer somewhere that is set to NULL.

1 
2 /*
3 * Pointers store memory addresses. Here I am setting this one to 0, or
4 * 0x00000000 or NULL.
5 */
6 int *IMAPointer = NULL;
7 
8 /*
9 * Here I am trying to store the value 5 in the address pointed to by
10 * IMAPointer. Since the address is NULL or 0 or 0x00000000 it results in
11 * a memory access violation - my program doesn't have permission from
12 * the operating system to access address 0x00000000.
13 */
14 *IMAPointer = 5; // Crash!!! :'(

It appears that the BITMAP you are trying to blit() is NULL, which means the load failed and likely the path/filename you supplied load_bitmap() is wrong (or something else is wrong - i.e. read permissions).

EDIT

In VC++ 7.1 I have to start relative paths to files from the same directory as my project files. Make sure that h_sprite.bmp is in the same directory as your project files (.sln, .vcproj, etc.).

Kitty Cat
Quote:

Sonova...now it runs, but it says "Error loading h_sprite.bmp"

Make sure you're looking in the right directory. IDEs really like to run programs from odd directories, so something to do is to chdir to where your exe is (using get_executable_name), then do everything relative to there.

Skax459

Should I put the pictures in the same folder as the executable? Because that isn't working.

bamccaig
Quote:

In VC++ 7.1 I have to start relative paths to files from the same directory as my project files. Make sure that h_sprite.bmp is in the same directory as your project files (.sln, .vcproj, etc.).

The image needs to be in the pwd, or present working directory. When running from within VC++ I think the pwd is the project directory (folder). So if you had a project named MyProject in C:\Documents and Settings\%USERNAME%\My Documents\Visual Studio Projects\, you would want the image file located in C:\Documents and Settings\%USERNAME%\My Documents\Visual Studio Projects\MyProject\ when running from VC++ and in whatever directory the executable is located when executing it from Windows.

i.e. "C:\Documents and Settings\%USERNAME%\My Documents\Visual Studio Projects\MyProject\h_sprite.bmp"

Skax459

Yay! Thanks to all who helped! Now all I need is to take this little bit of information and make it into a game.

...:P

EDIT: I actually drew the game map and I'm really excited...it runs. Whooo.

Thread #590219. Printed from Allegro.cc