I'm making a simple maze game with SDL, it's my first ever SDL program and really my first non-console program ever, so far i've been doing good, but now i've begun on the collision detection, and something is not doing what it's supposed to.
This is how my collision detection function looks:
Is there anything wrong with this function? The thing is, i've got it in a loop checking if the player hits a wall (I only have 1 wall at the moment, no maze yet), if that is the case, the game quits. But with the above function, it just quits instantly when i run it.Code:bool CollisionCheck(SDL_Rect A, SDL_Rect B) { //The sides of the rectangles unsigned int LeftA, LeftB; unsigned int RightA, RightB; unsigned int TopA, TopB; unsigned int BottomA, BottomB; //Get the sizes from the SDL_Rect structs - Rect A LeftA = A.x; RightA = A.x + A.w; TopA = A.y; BottomA = A.y + A.h; //Get the sizes from the SDL_Rect structs - Rect B LeftB = B.x; RightB = B.x + B.w; TopB = B.y; BottomB = B.y + B.h; if( (BottomA <= TopB) || (TopA >= BottomB) || (RightA <= LeftB) || (LeftA >= RightB) ) return false; else { return true; } }
This is the entire program:
I'm guessing something is wrong with my logic, if i take out the call to collision check the program runs flawlessly...Code://Neo1 //LabyrinthX 1.0 #include "SDL/SDL.h" #include "SDL/SDL_Image.h" #include <string> #define START_POS_X 180 #define START_POS_Y 140 #define VELOCITY 3 SDL_Surface *LoadImage(std::string Filename); void ApplySurface(SDL_Rect &Rect, SDL_Surface* Source, SDL_Surface* Destination); bool CollisionCheck(SDL_Rect A, SDL_Rect B); //Need CMD-linge arguments for SDL to function properly int main(int argc, char *argv[]) { //Struct for the position of the man and set coordinates to the default values SDL_Rect MyMan; MyMan.x = START_POS_X; MyMan.y = START_POS_Y; //Struct for the background position SDL_Rect MyBackgroundPos; MyBackgroundPos.x = 0; MyBackgroundPos.y = 0; //Wall SDL_Rect MyWall; MyWall.y = 40; MyWall.x = 300; MyWall.w = 40; MyWall.h = 400; //Screen width, height and color depth const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //Pointers to the stick man, the background image, and the screen SDL_Surface *Man = NULL; SDL_Surface *Background = NULL; SDL_Surface *Screen = NULL; //An event struct for holding KB input SDL_Event Event; //Bool for checking if the user want's to exit bool Quit = false; //Pointer for holding the key state array Uint8 *KeyStates; //Initialize SDL and check for errors if(SDL_Init(SDL_INIT_EVERYTHING) == -1) { return 1; } //Set up the display and check for errors Screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); if(Screen == NULL) { return 1; } //Set the window caption SDL_WM_SetCaption("LabyrinthX", NULL); //Load the images into the surfaces Man = LoadImage("Man.bmp"); Background = LoadImage("background.bmp"); //Main program loop while(!Quit) { while(SDL_PollEvent(&Event)) { if(Event.type == SDL_QUIT) { Quit = true; } } //Update the KeyStates array KeyStates = SDL_GetKeyState(NULL); //Move the man according to key presses -Y if(KeyStates[SDLK_UP]) MyMan.y -= VELOCITY; if(KeyStates[SDLK_DOWN]) MyMan.y += VELOCITY; //Move the man according to key presses -X if(KeyStates[SDLK_LEFT]) MyMan.x -= VELOCITY; if(KeyStates[SDLK_RIGHT]) MyMan.x += VELOCITY; //Check that the man isn't on his way out of the screen -Y if(MyMan.y < 0) MyMan.y += VELOCITY; if( (MyMan.y + MyMan.h) > SCREEN_HEIGHT) MyMan.y -= VELOCITY; //Check that the mas isn't on his way out of the screen -X if(MyMan.x < 0) MyMan.x += VELOCITY; if( (MyMan.x + MyMan.w) > SCREEN_WIDTH) MyMan.x -= VELOCITY; //Check if the man has touched the wall -X if(CollisionCheck(MyWall, MyMan)) { Quit = true; } //Draw the background ApplySurface(MyBackgroundPos, Background, Screen); //Draw the man ApplySurface(MyMan, Man, Screen); //Fill the wall SDL_FillRect(Screen, &MyWall, SDL_MapRGB(Screen->format, 0xFF, 0xFF, 0xFF)); //Update the screen and check for errors if(SDL_Flip(Screen) == -1) { return 1; } } //Clean Up SDL_FreeSurface(Man); SDL_FreeSurface(Background); SDL_Quit(); return 0; } //Image loading function SDL_Surface *LoadImage(std::string Filename) { //Surface pointers for the image SDL_Surface *LoadedImage = NULL; SDL_Surface *OptimizedImage = NULL; //Load the image LoadedImage = IMG_Load(Filename.c_str()); //Error check if(LoadedImage == NULL) { return NULL; } //Optimize the image for the display format and release the original image OptimizedImage = SDL_DisplayFormat(LoadedImage); SDL_FreeSurface(LoadedImage); //Error check if(OptimizedImage == NULL) { return NULL; } //Color key is "0, 255, 255" Uint32 ColorKey = SDL_MapRGB(OptimizedImage->format, 0, 0xFF, 0xFF); SDL_SetColorKey(OptimizedImage, SDL_SRCCOLORKEY, ColorKey); return OptimizedImage; } void ApplySurface(SDL_Rect &Rect, SDL_Surface* Source, SDL_Surface* Destination) { SDL_BlitSurface(Source, NULL, Destination, &Rect); return; } bool CollisionCheck(SDL_Rect A, SDL_Rect B) { //The sides of the rectangles unsigned int LeftA, LeftB; unsigned int RightA, RightB; unsigned int TopA, TopB; unsigned int BottomA, BottomB; //Get the sizes from the SDL_Rect structs - Rect A LeftA = A.x; RightA = A.x + A.w; TopA = A.y; BottomA = A.y + A.h; //Get the sizes from the SDL_Rect structs - Rect B LeftB = B.x; RightB = B.x + B.w; TopB = B.y; BottomB = B.y + B.h; if( (BottomA <= TopB) || (TopA >= BottomB) || (RightA <= LeftB) || (LeftA >= RightB) ) { return false; } return true; }



LinkBack URL
About LinkBacks


