Advertisement
thecplusplusguy

SDL tutorial 16

Jun 22nd, 2012
2,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //you need background.bmp (2000 x 480), tree_key.bmp
  4. #include <SDL/SDL.h>
  5. #include <iostream>
  6.  
  7. bool collision(SDL_Rect* rect1,SDL_Rect* rect2)
  8. {
  9.     if(rect1->y >= rect2->y + rect2->h)
  10.         return 0;
  11.     if(rect1->x >= rect2->x + rect2->w)
  12.         return 0;
  13.     if(rect1->y + rect1->h <= rect2->y)
  14.         return 0;
  15.     if(rect1->x + rect1->w <= rect2->x)
  16.         return 0;
  17.     return 1;
  18. }
  19.  
  20. int width = 640;
  21. int height = 480;
  22.  
  23. int main(int argc, char** argv)
  24. {
  25.     SDL_Init(SDL_INIT_EVERYTHING);
  26.     SDL_Surface *screen, *background, *image;
  27.     screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
  28. //  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
  29.     bool running = true;
  30.     const int FPS = 30;
  31.     const int speed = 5;
  32.     Uint32 start;
  33.     int x,y;
  34.     x = 0;
  35.     y = 0;
  36.     background = SDL_LoadBMP("background.bmp");
  37.     image = SDL_LoadBMP("tree_key.bmp");
  38.     SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0xff, 0xff));
  39.     SDL_Rect imgloc = {350,170,100,100};
  40.     SDL_Rect camera;
  41.     camera.x = 0;
  42.     camera.y = 0;
  43.     camera.w = 640;
  44.     camera.h = 480;
  45.     bool b[2] = {0,0};
  46.     while(running) {
  47.         start = SDL_GetTicks();
  48.         SDL_Event event;
  49.         while(SDL_PollEvent(&event)) {
  50.             switch(event.type) {
  51.                 case SDL_QUIT:
  52.                     running = false;
  53.                     break;
  54.                 case SDL_KEYDOWN:
  55.                     switch(event.key.keysym.sym)
  56.                     {
  57.                         case SDLK_RIGHT:
  58.                             b[0] = 1;
  59.                             break;
  60.                         case SDLK_LEFT:
  61.                             b[1] = 1;
  62.                             break;
  63.                         case SDLK_ESCAPE:
  64.                             running = false;
  65.                             break;
  66.                     }
  67.                     break;
  68.                 case SDL_KEYUP:
  69.                     switch(event.key.keysym.sym)
  70.                     {
  71.                         case SDLK_RIGHT:
  72.                             b[0] = 0;
  73.                             break;
  74.                         case SDLK_LEFT:
  75.                             b[1] = 0;
  76.                             break;
  77.                     }
  78.                     break;
  79.             }
  80.         }
  81.  
  82.         //logic
  83.         if(b[0])
  84.         {
  85.             x += speed;
  86.             camera.x += speed;
  87.             if(camera.x >= 2000-640)
  88.                 camera.x = 0;
  89.         }
  90.         else if(b[1])
  91.         {
  92.             x -= speed;
  93.             camera.x -= speed;
  94.             if(camera.x <= 0)
  95.                 camera.x = 2000-640;
  96.         }
  97.  
  98.         SDL_Rect location = {x,y,640,480};
  99.         SDL_Rect relcoord = {imgloc.x-x, imgloc.y-y};
  100.  
  101.         //render
  102.         SDL_BlitSurface(background, &camera, screen, NULL);
  103.         if(collision(&location, &imgloc))
  104.             SDL_BlitSurface(image, NULL, screen, &relcoord);
  105.  
  106.         SDL_Flip(screen);
  107.         if(1000/FPS > SDL_GetTicks()-start) {
  108.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  109.         }
  110.     }
  111.     SDL_FreeSurface(background);
  112.     SDL_FreeSurface(image);
  113.     SDL_Quit();
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement