Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <string>
  3.  
  4. SDL_Surface* buffer = NULL;
  5. SDL_Surface* background = NULL;
  6. SDL_Surface* dot = NULL;
  7.  
  8. SDL_Event event;
  9.  
  10. SDL_Rect offset;
  11.  
  12. int dx = 0;
  13. int dy = 0;
  14.  
  15. int main(int argc, char* args[])
  16. {
  17.     SDL_Init(SDL_INIT_EVERYTHING);
  18.  
  19.     buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  20.  
  21.     background = SDL_LoadBMP("background.bmp");
  22.     dot = SDL_LoadBMP("dot.bmp");
  23.  
  24.     offset.x = 300;
  25.     offset.y = 200;
  26.  
  27.  
  28.     int x = 0;
  29.     int y = 0;
  30.  
  31.     // This is our target-position variables. Initialize it to the current location
  32.     int toX = offset.x;
  33.     int toY = offset.y;
  34.  
  35.     Uint8 *key = SDL_GetKeyState( NULL );
  36.  
  37.     while (event.type != SDL_QUIT)
  38.     {
  39.         if( event.type == SDL_MOUSEMOTION )
  40.         {
  41.             //Get the mouse offsets
  42.             x = event.motion.x;
  43.             y = event.motion.y;
  44.         }
  45.         if( event.type == SDL_MOUSEBUTTONDOWN )
  46.         {
  47.             // The button has been pressed.
  48.             if( event.button.button == SDL_BUTTON_LEFT )
  49.             {
  50.                 // We need to set our target x and y location as the current mouse location. We need to store this as seperate variables.
  51.                 // Think what would happen if we just used the x and y, and we wanted to check if we had reached the target location (which would then keep moving with the mouse)
  52.                 toX = x;
  53.                 toY = Y;
  54.  
  55.                 dx = toX - offset.x;
  56.                 dy = toY - offset.y;
  57.             }
  58.         }
  59.  
  60.         // If we are not currently at the target location we need to move towards it
  61.         if (offset.x != toX || offset.y != toY)
  62.         {
  63.            
  64.             offset.x += dx / 20;
  65.             offset.y += dy / 20;    
  66.       }
  67.  
  68.         SDL_BlitSurface(background, NULL, buffer, NULL);
  69.         SDL_BlitSurface(dot, NULL, buffer, &offset);
  70.         SDL_PollEvent(&event);
  71.         SDL_Flip(buffer);
  72.     }
  73.  
  74.     SDL_FreeSurface(background);
  75.     SDL_FreeSurface(dot);
  76.     SDL_Quit();
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement