Hello everyone.
I'm using a custom structure, DEST_BITMAP, to work with destructible 2D bitmaps.
This is the current code I'm using:
| 1 | typedef 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 | |
| 11 | void 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 | |
| 23 | int 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 | } |
| 35 | END_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.
Code tags are in lowercase ([code][/code]), not uppercase ([CODE][/CODE]). Edit your post accordingly.
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.
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 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.