Advertisement
thecplusplusguy

SDL tile collision

Oct 23rd, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //SDL tile collision
  3. #include <iostream>
  4. #include <vector>
  5. #include <SDL/SDL.h>
  6. #include <cmath>
  7. #include <fstream>
  8.  
  9. const int WIDTH=640;
  10. const int HEIGHT=480;
  11.  
  12. int map[15][20]={
  13. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  14. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  15. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  16. {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
  17. {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
  18. {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
  19. {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
  20. {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
  21. {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
  22. {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
  23. {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
  24. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  25. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  26. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  27. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  28. };
  29.  
  30. bool collision(SDL_Rect* player,int direction)
  31. {
  32.  
  33.         if(direction == 1 && (map[(player->y/32)+1][player->x/32] == 1 || map[(player->y/32)+1][(int)ceil(player->x/32.0)] == 1)) return true;
  34.         if(direction == 2 && (map[(int)ceil(player->y/32.)-1][player->x/32] == 1 || map[(int)ceil(player->y/32.)-1][(int)ceil(player->x/32.)] == 1)) return true;
  35.         if(direction == 3 && (map[player->y/32][(int)ceil(player->x/32.)-1] == 1 || map[(int)ceil(player->y/32.)][(int)ceil(player->x/32.)-1] == 1)) return true;
  36.         if(direction == 4 && (map[player->y/32][(player->x/32)+1] == 1 || map[(int)ceil(player->y/32.)][(player->x/32)+1] == 1)) return true;
  37.         return false;
  38.  
  39.  
  40. }
  41.  
  42. int main(int argc,char** argv)
  43. {
  44.     SDL_Init(SDL_INIT_EVERYTHING);
  45.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  46.     bool running=true;
  47.     SDL_Event event;
  48.     Uint32 start;
  49.     SDL_Rect player={32,32,32,32};
  50.     bool b[4]={0,0,0,0};
  51.     int direction=0;
  52.     while(running)
  53.     {
  54.         start=SDL_GetTicks();
  55.         while(SDL_PollEvent(&event))
  56.         {
  57.             switch(event.type)
  58.             {
  59.                 case SDL_QUIT:
  60.                     running=false;
  61.                     break;
  62.                 case SDL_KEYDOWN:
  63.                     switch(event.key.keysym.sym)
  64.                     {
  65.                         case SDLK_ESCAPE:
  66.                             running=false;
  67.                             break;
  68.                     }
  69.                     break;
  70.                 case SDL_MOUSEMOTION:
  71.                     break;
  72.                    
  73.             }
  74.         }
  75.         for(int i=0;i<15;i++)
  76.             for(int j=0;j<20;j++)
  77.             {
  78.                 SDL_Rect rec={j*32,i*32,32,32};
  79.                 SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,0,0,map[i][j]*255));
  80.             }
  81.         SDL_FillRect(screen,&player,SDL_MapRGB(screen->format,255,0,0));
  82.         direction=0;
  83.         Uint8* keys=SDL_GetKeyState(0);
  84.         if(keys[SDLK_w])
  85.         {
  86.             direction=2;
  87.             if(!collision(&player,direction))
  88.                 player.y-=2;
  89.         }
  90.         if(keys[SDLK_s])
  91.         {
  92.             direction=1;
  93.             if(!collision(&player,direction))
  94.                 player.y+=2;
  95.         }
  96.         if(keys[SDLK_a])
  97.         {
  98.             direction=3;
  99.             if(!collision(&player,direction))
  100.                 player.x-=2;
  101.         }
  102.         if(keys[SDLK_d])
  103.         {
  104.             direction=4;
  105.             if(!collision(&player,direction))
  106.                 player.x+=2;
  107.         }
  108.  
  109.        
  110.         SDL_Flip(screen);
  111.         if(1000.0/30>(SDL_GetTicks()-start))
  112.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  113.     }
  114.     SDL_Quit();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement