Advertisement
thecplusplusguy

SDL camera moving collision

Nov 2nd, 2012
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //you need a chessimage.bmp 1920*1920 image
  3. #include <SDL/SDL.h>
  4. #include <iostream>
  5.  
  6. bool collision(SDL_Rect* rect1,SDL_Rect* rect2)
  7. {
  8.     if(rect1->y >= rect2->y + rect2->h)
  9.         return 0;
  10.     if(rect1->x >= rect2->x + rect2->w)
  11.         return 0;
  12.     if(rect1->y + rect1->h <= rect2->y)
  13.         return 0;
  14.     if(rect1->x + rect1->w <= rect2->x)
  15.         return 0;
  16.     return 1;
  17. }
  18.  
  19.  
  20. int main(int argc,char** argv)
  21. {
  22.     SDL_Init(SDL_INIT_EVERYTHING);
  23.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  24.     bool running=true;
  25.     Uint32 start;
  26.     SDL_Event event;
  27.     bool b[4]={0,0,0,0};
  28.     SDL_Surface* chessImage=SDL_LoadBMP("chessimage.bmp");
  29.     if(!chessImage)
  30.         std::cout << "You don't ave chessimage.bmp" << std::endl;
  31.     SDL_Rect camera={0,0,640,480};
  32.     SDL_Rect absCoord={0,0,640,480};
  33.     SDL_Rect playerpos={320,240,20,20};
  34.     SDL_Rect rec={100,100,20,20};
  35.     while(running)
  36.     {
  37.         start=SDL_GetTicks();
  38.         while(SDL_PollEvent(&event))
  39.         {
  40.             switch(event.type)
  41.             {
  42.                 case SDL_QUIT:
  43.                     running=false;
  44.                     break;
  45.                 case SDL_KEYDOWN:
  46.                     switch(event.key.keysym.sym)
  47.                     {
  48.                         case SDLK_ESCAPE:
  49.                             running=false;
  50.                             break;
  51.                         case SDLK_UP:
  52.                             b[0]=true;
  53.                             break;
  54.                         case SDLK_RIGHT:
  55.                             b[1]=true;
  56.                             break;
  57.                         case SDLK_DOWN:
  58.                             b[2]=true;
  59.                             break;
  60.                         case SDLK_LEFT:
  61.                             b[3]=true;
  62.                             break;
  63.                     }
  64.                     break;
  65.                 case SDL_KEYUP:
  66.                     switch(event.key.keysym.sym)
  67.                     {
  68.                         case SDLK_ESCAPE:
  69.                             running=false;
  70.                             break;
  71.                         case SDLK_UP:
  72.                             b[0]=false;
  73.                             break;
  74.                         case SDLK_RIGHT:
  75.                             b[1]=false;
  76.                             break;
  77.                         case SDLK_DOWN:
  78.                             b[2]=false;
  79.                             break;
  80.                         case SDLK_LEFT:
  81.                             b[3]=false;
  82.                             break;
  83.                     }
  84.                     break;
  85.             }
  86.         }
  87.         if(b[0])
  88.         {
  89.             if(playerpos.y>=30)
  90.                 playerpos.y-=3;
  91.             else
  92.             {
  93.                 absCoord.y-=3;
  94.                 camera.y-=3;
  95.             }
  96.         }
  97.         if(b[1])
  98.         {
  99.             if(playerpos.x<=610)
  100.                 playerpos.x+=3;
  101.             else
  102.             {
  103.                 absCoord.x+=3;
  104.                 camera.x+=3;
  105.             }
  106.         }
  107.         if(b[2])
  108.         {
  109.             if(playerpos.y<=450)
  110.                 playerpos.y+=3;
  111.             else
  112.             {
  113.                 absCoord.y+=3;
  114.                 camera.y+=3;
  115.             }
  116.         }
  117.         if(b[3])
  118.         {
  119.             if(playerpos.x>=30)
  120.                 playerpos.x-=3;
  121.             else
  122.             {
  123.                 absCoord.x-=3;
  124.                 camera.x-=3;
  125.             }
  126.         }
  127.        
  128.         if(camera.x>1920-640)   //image width: 1920, screen-width: 640
  129.             camera.x=0;
  130.         if(camera.y>1920-480)
  131.             camera.y=0;
  132.         if(camera.x<0)
  133.             camera.x=1920-640;
  134.         if(camera.y<0)
  135.             camera.y=1920-640;
  136.            
  137.            
  138.         SDL_Rect tmpRec={rec.x-absCoord.x,rec.y-absCoord.y,rec.w,rec.h};
  139.            
  140.         if(collision(&tmpRec,&playerpos))
  141.             std::cout << "collision" << std::endl;
  142.        
  143.         SDL_BlitSurface(chessImage,&camera,screen,NULL);
  144.         SDL_FillRect(screen,&playerpos,SDL_MapRGB(screen->format,255,0,0));
  145.         SDL_FillRect(screen,&tmpRec,600);
  146.         SDL_Flip(screen);
  147.         if(1000/30>(SDL_GetTicks()-start))
  148.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  149.     }
  150.     SDL_Quit();
  151.     return 0;  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement