From: Stephen Snape Newsgroups: comp.os.msdos.djgpp Subject: Re: colide Date: Sun, 22 Nov 1998 23:12:05 +0000 Organization: Virgin Net Usenet Service Lines: 211 Message-ID: <36589A45.EB236D9E@virgin.net> References: <000f01be1552$66e15ea0$30f4ffc2 AT default> NNTP-Posting-Host: 194.168.63.89 Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Mailer: Mozilla 4.04 [en] (Win95; I) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Lazer ---¤--- wrote: > Hi > > Im writing a game using djgpp,allegro and > Bounding-Box collision detection(http://www.geocities.com/SiliconValley/Park/1077) > > Could any-one take alot at my source (its a mess,but..)? > > The error is in line 138,139 & 140, but i dont know what wrong!! (it compiles,but won't run) > > Thanks > > Lazer > > Hi, I looked at your code, and fixed it. The problem was that you were trying to pass BITMAPs to the functions when you only have to pass integers. These integers are related to your sprite by using the other function, mk_spr_bb when you do pass the BITMAP and a number that you want to use. I compiled it and it worked, see code below. Hope its alright now. Steve. #include #include "allegro.h" #define MAX_SPRITES 10 struct spr_bb{ int bb_height; int bb_width; } spr_bb[MAX_SPRITES]; int x1=20, y1=20; int x2=150, y2=150; int sp1=1, sp2=2; void init_sp_bb() { int l; for (l=0; l<(MAX_SPRITES+1); l++) { spr_bb[l].bb_height=0; spr_bb[l].bb_width=0; } } void mk_spr_bb(BITMAP *s3, int spr_no) { int bb_height=0; int bb_width=0; int x1, y1; int p; for (y1=0; y1<128; y1++) { for (x1=0; x1<128; x1++) { p=getpixel(s3,x1,y1); if ((p!=0) && (p!=-1)) { if (y1>bb_height) bb_height=y1; if (x1>bb_width) bb_width=x1; } } } spr_bb[spr_no].bb_height=bb_height; spr_bb[spr_no].bb_width=bb_width; } int check_collision(int spr1, int spr1x, int spr1y, int spr2, int spr2x, int spr2y) { if ((spr1x>spr2x+spr_bb[spr2].bb_width) || (spr2x > spr1x+spr_bb[spr1].bb_width) || (spr1y>spr2y+spr_bb[spr2].bb_height) || (spr2y> spr1y+spr_bb[spr1].bb_height)) { return 0; } else { return 1; } } int main() { int qi = 3; int s = 0; int ways = 1; int b = 0; int w = 1; int a = 100; int i = 0; int q; BITMAP *the_ball; BITMAP *the_image; PALLETE the_pallete; the_image = load_bitmap("pix1.bmp", the_pallete); allegro_init(); install_keyboard(); install_timer(); set_pallete(desktop_pallete); set_gfx_mode (GFX_AUTODETECT, 320,200, 4, 0); set_pallete(the_pallete); clear(the_image); the_ball = create_bitmap(8, 8); clear(the_ball); circle(the_ball, 2, 2, 2, 2); the_image = load_bitmap("gra1.bmp", the_pallete); draw_sprite(screen, the_image, (SCREEN_W-the_image->w)/2, 200-the_image->h); //2 NEW LINES HERE mk_spr_bb(the_ball,1); mk_spr_bb(the_image,2); q = (SCREEN_W-the_image->w)/2; w = 170; b=3; s=SCREEN_W/2; ways=1; while(!key[KEY_ESC]) { if(b<2){ w=w-1; if(ways<2){ s=s-1; } if(s<2){ ways=3; } if(ways>2){ s=s+1; } if(s>318){ ways=1; } if(w<2){ b=3; } } if(b>2){ w=w+1; if(ways>2){ s=s+1; } if(s>318){ ways=1; } if(ways<2){ s=s-1; } if(s<2){ ways=3; } if(w>(200-the_image->h)-2){ b=1; //CHANGE OF LINE if (check_collision(1,s,w,2,q,200-the_image->h)){ rest(1000); } } } clear (screen); draw_sprite(screen, the_image, q, 200-the_image->h); circle(screen, s, w, 2, 2); rest(10); if(key[KEY_LEFT]) { clear(screen); q=q-10; if (q<1) q=1; draw_sprite(screen, the_image, q, 200-the_image->h); circle(screen, s, w, 2, 2); clear_keybuf(); } if(key[KEY_RIGHT]) { clear(screen); q=q+10; if (q>241) q=241; draw_sprite(screen, the_image, q, 200-the_image->h); circle(screen, s, w, 2, 2); clear_keybuf(); } } fade_out(5); }