Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I need a bitmap reference in my sprite structure

This thread is locked; no one can reply to it. rss feed Print
I need a bitmap reference in my sprite structure
xmltorrent
Member #7,673
August 2006

How would I go about adding a reference to a bitmap in my sprite data structure? See I need it so I can finish my pixel-perfect collision detection routine for a bullet-to-obstacle. I need the bitmap reference so I can access the bitmap itself to get the pixels. Here is my sprite structure:

typedef struct SPRITE{
int x;
int y;
int dir;
int alive;
int width;
int height;
int xspeed;
int yspeed;
int xdelay;
int ydelay;
int xcount;
int ycount;
int framecount;
int framedelay;
int curframe;
int maxframe;
int animdir;

*collision detection variables*
int col_width;
int col_height;
int col_x_offset;
int col_y_offset;
}SPRITE;

My first thought was to add standard statement of the BITMAP type like:

BITMAP *reference;

I tried that but when I went to access it like this:

obstacle->reference;

The compiler gave me an error. Unless I'm trying to access it the wrong way...

Any help would be appreciated. Thanks in advance.

Tomasz Grajewski
Member #4,284
February 2004

1typedef struct SPRITE
2{
3 ...
4 BITMAP reference;
5 ...
6};
7 
8// and access the bitmap:
9obstacle.reference
10 
11 
12// EDIT: You can also do this:
13typedef struct SPRITE
14{
15 ...
16 BITMAP *reference;
17 ...
18};
19 
20// and access the bitmap:
21obstacle->reference

Is this what You meant? And what was the error returned from the compiler?

___________________________________________________________________________________________
mBlocks - a Tetris clone written in JavaScript | Merix Studio - website design & development | Zeitgeist

LennyLen
Member #5,313
December 2004
avatar

Use: obstacle.reference, not obstacle->reference.

edit: Tomasz, neither of the methods you describe work.

xmltorrent
Member #7,673
August 2006

Sorta but not the ticket.

Like for testing purposes, I need the structure to hold the bitmap that represents the sprite. Then I want to draw the bitmap to the back buffer to make sure that the reference is holding the bitmap.

I did what you had there in the code and it compiled but when I went to run the program, it crashed at run-time.

EDIT:
I fixed it. Thanks for your help.

I added this to my sprite structure:

BITMAP *reference;

Then just set it equal to the bitmap in the animation array:

player->reference = player_images[0];

Then drew it to the buffer:

draw_sprite(buffer, player->reference, 300, 400);

LennyLen
Member #5,313
December 2004
avatar

The following compiles and executes for me flawlessly:

1#include <allegro.h>
2 
3typedef struct {
4 
5 BITMAP *pic;
6 int x;
7 int y;
8} sprite;
9 
10int main()
11{
12 allegro_init();
13 install_keyboard();
14 
15 set_color_depth(32);
16 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0) != 0)
17 {
18 allegro_message("Error with gfx mode!");
19 exit(EXIT_FAILURE);
20 }
21 
22 clear_to_color(screen, makecol(0, 0, 0));
23 
24 sprite box;
25 box.pic = create_bitmap(200, 200);
26 box.x = 100;
27 box.y = 100;
28 
29 rect(box.pic, 0, 0, 199, 199, makecol(255, 255, 255));
30 blit(box.pic, screen, 0, 0, box.x, box.y, box.pic->w, box.pic->h);
31 
32 while(!key[KEY_ESC]) {}
33 
34 return 0;
35}
36END_OF_MAIN()

Rick
Member #3,572
June 2003
avatar

You probably didn't load it correctly. Most likely your bitmap is NULL, so when you try to use it it gives an error.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Go to: