Advertisement
thecplusplusguy

SDL scrolling

Aug 5th, 2012
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 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. int main(int argc,char** argv)
  7. {
  8.     SDL_Init(SDL_INIT_EVERYTHING);
  9.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  10.     bool running=true;
  11.     Uint32 start;
  12.     SDL_Event event;
  13.     bool b[4]={0,0,0,0};
  14.     SDL_Surface* chessImage=SDL_LoadBMP("chessimage.bmp");
  15.     if(!chessImage)
  16.         std::cout << "You don't ave chessimage.bmp" << std::endl;
  17.     SDL_Rect camera={0,0,640,480};
  18.     SDL_Rect playerpos={320,240,20,20};
  19.     while(running)
  20.     {
  21.         start=SDL_GetTicks();
  22.         while(SDL_PollEvent(&event))
  23.         {
  24.             switch(event.type)
  25.             {
  26.                 case SDL_QUIT:
  27.                     running=false;
  28.                     break;
  29.                 case SDL_KEYDOWN:
  30.                     switch(event.key.keysym.sym)
  31.                     {
  32.                         case SDLK_ESCAPE:
  33.                             running=false;
  34.                             break;
  35.                         case SDLK_UP:
  36.                             b[0]=true;
  37.                             break;
  38.                         case SDLK_RIGHT:
  39.                             b[1]=true;
  40.                             break;
  41.                         case SDLK_DOWN:
  42.                             b[2]=true;
  43.                             break;
  44.                         case SDLK_LEFT:
  45.                             b[3]=true;
  46.                             break;
  47.                     }
  48.                     break;
  49.                 case SDL_KEYUP:
  50.                     switch(event.key.keysym.sym)
  51.                     {
  52.                         case SDLK_ESCAPE:
  53.                             running=false;
  54.                             break;
  55.                         case SDLK_UP:
  56.                             b[0]=false;
  57.                             break;
  58.                         case SDLK_RIGHT:
  59.                             b[1]=false;
  60.                             break;
  61.                         case SDLK_DOWN:
  62.                             b[2]=false;
  63.                             break;
  64.                         case SDLK_LEFT:
  65.                             b[3]=false;
  66.                             break;
  67.                     }
  68.                     break;
  69.             }
  70.         }
  71.         if(b[0])
  72.         {
  73.             if(playerpos.y>=30)
  74.                 playerpos.y-=3;
  75.             else
  76.                 camera.y-=3;
  77.         }
  78.         if(b[1])
  79.         {
  80.             if(playerpos.x<=610)
  81.                 playerpos.x+=3;
  82.             else
  83.                 camera.x+=3;
  84.         }
  85.         if(b[2])
  86.         {
  87.             if(playerpos.x<=450)
  88.                 playerpos.y+=3;
  89.             else
  90.                 camera.y+=3;
  91.         }
  92.         if(b[3])
  93.         {
  94.             if(playerpos.x>=30)
  95.                 playerpos.x-=3;
  96.             else
  97.                 camera.x-=3;
  98.         }
  99.         if(camera.x>1920-640)   //image width: 1920, screen-width: 640
  100.             camera.x=0;
  101.         if(camera.y>1920-480)
  102.             camera.y=0;
  103.         if(camera.x<0)
  104.             camera.x=1920-640;
  105.         if(camera.y<0)
  106.             camera.y=1920-640;
  107.        
  108.         SDL_BlitSurface(chessImage,&camera,screen,NULL);
  109.         SDL_FillRect(screen,&playerpos,SDL_MapRGB(screen->format,255,0,0));
  110.         SDL_Flip(screen);
  111.         if(1000/30>(SDL_GetTicks()-start))
  112.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  113.     }
  114.     SDL_Quit();
  115.     return 0;  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement