I want to use the WinAPI function IntersectRect() for collision detection in my game so can anyone tell me how to go about using this function.
i have to imaes loaded and two rects set to the exact position and size as the images and i want to be able to tell if they collide and if they do i want to havethe xpos of image1 be reduced by 100.
Code:
#include "cozSprite.h"
#include <windows.h>
cozSprite spaceship; //sets up object of class cozSprite
SDL_Surface *screen;
class image1
{
public:
int width;
int height;
int xpos;
int ypos;
}image1;
class image2
{
public:
int width;
int height;
int xpos;
int ypos;
}image2;
int main(int argc, char *argv[])
{
Uint8* keys;
/*-----------------------------------------------------------------*/
// image1
image1.width = 201;
image1.height = 199;
image1.xpos = 0;
image1.ypos = 0;
// image2
image2.width = 201;
image2.height = 201;
image2.xpos = 600;
image2.ypos = 0;
/*-----------------------------------------------------------------*/
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1); }
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL ) {
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1); }
spaceship.Init(screen,202,201,2); //initializes the sprite
spaceship.Load("space.bmp", 0,0,0); //loads the sprite
spaceship.Load("space.bmp", 1,0,199);
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
RECT rect1;
rect1.left = image1.xpos;
rect1.top = image1.xpos + image1.width;
rect1.right = image1.xpos - image1.width;
rect1.bottom = image1.ypos - image1.height;
RECT rect2;
rect2.left = image2.xpos;
rect2.top = image2.xpos + image2.width;
rect2.right = image2.xpos - image2.width;
rect2.bottom = image2.ypos - image2.height;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { image1.ypos -= 1; }
if ( keys[SDLK_DOWN] ) { image1.ypos += 1; }
if ( keys[SDLK_LEFT] ) { image1.xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { image1.xpos += 1; }
spaceship.Draw(0,image1.xpos,image1.ypos); //draws frame in specified postion on the screen
spaceship.Draw(1,image2.xpos,image2.ypos);
SDL_Flip(screen);
}
return 0;
}
please don't make any comments on the code i jsut threw this tegether but don't know how to use the function o am asking about. Do i need a third RECT?