Using a struct containing a BITMAP
howieson2003

Hi, I'm new here and am not great with programming in C but I understand the basics anyway. I'm doing the following to create a struct called SPRITE containing a BITMAP and four integers that are pretty self-explanatory:

typedef struct SPRITE
{
BITMAP *picture;
int x1;
int y1;
int x2;
int y2;
} SPRITE;

That's stored in a header file along with a function that initialises my programme:

void main_init()
{
allegro_init();
install_keyboard();
install_timer();

LOCK_VARIABLE(speed_counter);
LOCK_FUNCTION(increment_speed_counter);
install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 1024, 768, 0, 0);
}
END_OF_FUNCTION(main_init);

That function is called as the first command in main(). After setting up the bitmaps for the background and double-buffer, I create a new sprite by doing this:

SPRITE char_one = {load_bitmap("char1.bmp", NULL), 0, 0, (char_one.x1 + char_one.picture->w), (char_one.y1 + char_one.picture->h)};

I know the rest of the program works, this is my latest addition to it. When I compile the program, it compiles without any errors or warnings however when I run it, the creen initialises and then the program aborts with a segmentation fault. I've read through the forums here and all I can think of is that the struct is being defined before allegro and the gfx_mode is initialised. I know this would create a seg fault because a gfx function is being used before the gfx_mode is initialised, am I rightt? When I comment out the "SPRITE char_one = {..." then the program executes perfectly.

So, is there anyway to make the struct be defined after allegro is initialised? Any help would be well appreciated because I'm totally pulling my hair out ???

Onewing
Quote:

SPRITE char_one = {load_bitmap("char1.bmp", NULL), 0, 0, (char_one.x1 + char_one.picture->w), (char_one.y1 + char_one.picture->h)};

It seems to me that you are trying to use char_one before it is completed declared.

Paul whoknows

Can you try in this way?

SPRITE char_one;
char_one.picture = load_bitmap("char1.bmp", NULL);
char_one.x1 = char_one.y1 = 0;
char_one.x2 = char_one.x1 + char_one.picture->w;
char_one.y2 = char_one.y1 + char_one.picture->h;

SonShadowCat

Nevermind.

howieson2003

@ Onewing:

Do you mean that when I try to create the x2 and y2 values, it causes a seg fault because I'm looking for information from the struct and it hasn't been completed yet?

@ Paul whoknows:

Thanks, it works perfectly that way! :D But why? Is it like I said above? Or some other reason that I don't understand?

Thanks anyway peeps!

Onewing
Quote:

it causes a seg fault because I'm looking for information from the struct and it hasn't been completed yet?

Yeah, it's kind of like that. It's like you're trying to turn on the car while it's still on the assembly line getting built.

howieson2003

Nice one! It's like the world all makes sense now! ;D Cheers

gnolam
Quote:

That's stored in a header file along with a function that initialises my programme:

Please don't put code in headers. It makes baby Jesus cry.

Jeff Bernard

What's the point of the second x and y coordinates? Being derived from other attributes of the struct make it unneeded.

Removing those coordinates would make your declaration work, assuming that's valid code to declare a struct, I dunno because I always declare mine the way Paul showed you.

And gnolam is saying, which is something I agree with, only declarations (functions, classes, etc) should go in header (.h, .hpp) files and all of the code should go into source files (.c, .cpp). This way, you don't have to recompile all of the code every time you make a change.

Thread #591772. Printed from Allegro.cc