Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Why won't this work?

This thread is locked; no one can reply to it. rss feed Print
Why won't this work?
biubid_boy
Member #8,502
April 2007

Hello everyone.

I'm using a custom structure, DEST_BITMAP, to work with destructible 2D bitmaps.
This is the current code I'm using:

1typedef struct
2{
3 BITMAP *image;
4 BITMAP *dest_map;
5 BITMAP *curr_image;
6 BITMAP *curr_dest;
7 int open_col,
8 dest_col;
9} DEST_BITMAP;
10 
11void load_dest_bitmap(DEST_BITMAP dest, char image_path[], char dest_map_path[])
12{
13 dest.image = load_bitmap(image_path, NULL);
14 dest.dest_map = load_bitmap(dest_map_path, NULL);
15 dest.curr_image = create_bitmap(dest.image->w, dest.image->h);
16 dest.curr_dest = dest.dest_map;
17 dest.open_col = makecol(255, 255, 255);
18 dest.dest_col = makecol(0, 0, 0);
19}
20 
21...
22 
23int main() {
24 init();
25 
26 DEST_BITMAP dest;
27
28 char image_path[] = "test-image.bmp";
29 char dest_map_path[] = "test-dest_map.bmp";
30
31 load_dest_bitmap(dest, image_path, dest_map_path);
32 ...
33 return 0;
34}
35END_OF_MAIN()

But when I called load_dest_bitmap in my main function, it crashes my program. The funny thing is, when the exact same code is used straight in my main, the program runs fine. The program crashes every time I access the bitmap data, so there is something wrong with the loading - I just can't see it.

Any help would be appreciated,
Biubid_boy.

gnolam
Member #2,030
March 2002
avatar

Code tags are in lowercase ([code][/code]), not uppercase ([CODE][/CODE]). Edit your post accordingly.

Quote:

The program crashes every time I access the bitmap data, so there is something wrong with the loading - I just can't see it.

You can't change the value of a function argument, unless you use a pointer.

Consider this:

int xsquared(int x)
{
 x = x*x;

 return x;
}

int main(void)
{
 int n;
 n = 2;
 //The value of n is now 2. 

 xsquared(n);
 //What's the value of n here?

The correct answer is "still 2".

So you have to either have your load_dest_bitmap() return a DEST_BITMAP structure, or make the DEST_BITMAP argument a pointer.

Oh, and check the return value of load_bitmap(), and make sure you destroy any loaded or created bitmaps.

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

someone972
Member #7,719
August 2006
avatar

Make sure that the bitmaps your programs are accessing exist in the spot you tell it to look. You could also perform error checking on load_bitmap() to see if it returns NULL(indicating that it couldn't find the bitmap).

As for changing the value passed to the load_dest_bitmap() function you could do as gnolam said or you could pass by reference by using the '&' operator:

void load_dest_bitmap(DEST_BITMAP& dest, char image_path[],
char dest_map_path[])

______________________________________
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
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Kibiz0r
Member #6,203
September 2005
avatar

Quote:

As for changing the value passed to the load_dest_bitmap() function you could do as gnolam said or you could pass by reference by using the '&' operator:

No such thing exists in C. :-/

Go to: