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.
]]>Is this what You meant? And what was the error returned from the compiler?
]]>Use: obstacle.reference, not obstacle->reference.
edit: Tomasz, neither of the methods you describe work.
]]>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);
]]>The following compiles and executes for me flawlessly:
1 | #include <allegro.h> |
2 | |
3 | typedef struct { |
4 | |
5 | BITMAP *pic; |
6 | int x; |
7 | int y; |
8 | } sprite; |
9 | |
10 | int 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 | } |
36 | END_OF_MAIN() |
]]>
You probably didn't load it correctly. Most likely your bitmap is NULL, so when you try to use it it gives an error.
]]>