When the block contacts a corner it goes through the side (not the top or bottom) but everywhere else it works just fine, any ideas?
here is my code
thank you.Code:#include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <string> using namespace std; SDL_Surface *screen = NULL; SDL_Surface *block = NULL; SDL_Surface *wall = NULL; bool quit = false; SDL_Event event; //block coordinates int x = 0; int y = 0; int w = x + 32; int h = y +32; int right; int left; int up; int down; int collision(){ SDL_Rect blockColl; blockColl.x = x; blockColl.y = y; blockColl.w = w; blockColl.h = h; if(blockColl.y == 0){ return 1; } if(blockColl.h == 480){ return 2; } if(blockColl.w == 320){ return 3; } if(blockColl.x == 0){ return 4; } return 0; } void handle_events(){ if(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT){ quit = true; } if(event.type == SDL_KEYDOWN){ switch(event.key.keysym.sym){ case SDLK_UP: // up = -1; break; case SDLK_DOWN:// down = 1; break; case SDLK_RIGHT:// right = 1; break; case SDLK_LEFT:// left = -1; break; case SDLK_ESCAPE: quit = true; break; default: break; } } if(event.type == SDL_KEYUP){ switch(event.key.keysym.sym){ case SDLK_UP: up = 0; break; case SDLK_DOWN: down = 0; break; case SDLK_RIGHT: right = 0; break; case SDLK_LEFT: left = 0; break; default: break; } } } } void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface *destination){ SDL_Rect pos; pos.x = X; pos.y = Y; SDL_BlitSurface(source, NULL, destination, &pos); } SDL_Surface *load_image(string filename){ SDL_Surface *image; image = IMG_Load(filename.c_str()); return image; } bool init(){ if(SDL_Init(SDL_INIT_EVERYTHING) == -1){ return false; } screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); if(screen == NULL){ return false; } SDL_WM_SetCaption("Test", NULL); return true; } int main( int argc, char* args[] ) { if(init() == false){ return 1; } block = load_image("block.png"); wall = load_image("wall.png"); if(block == NULL){ return 1; } if(wall == NULL){ return 1; } while(quit == false){ handle_events(); w = x + 32; h = y + 32; if(collision() != 1){ y += up; } if(collision() != 2){ y += down; } if(collision() != 3){ x += right; } if(collision() != 4){ x += left; } //make screen color SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x12, 0x24, 0x48)); apply_surface(x, y, block, screen); apply_surface(320, 0, wall, screen); SDL_Delay(1000/100); SDL_Flip(screen); } //Quit SDL SDL_Quit(); return 0; }



LinkBack URL
About LinkBacks


