Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 26th, 2012  |  syntax: C++  |  size: 10.82 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //The headers
  2. #include "SDL/SDL.h"
  3. #include "SDL/SDL_image.h"
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7. #include <math.h>
  8.  
  9. //Screen attributes
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 400;
  12. const int SCREEN_BPP = 32;
  13.  
  14. //The frame rate
  15. const int FRAMES_PER_SECOND = 20;
  16.  
  17. //The dot attributes
  18. const int DOT_WIDTH = 20;
  19. const int DOT_HEIGHT = 20;
  20.  
  21. //The surfaces
  22. SDL_Surface *dot = NULL;
  23. SDL_Surface *screen = NULL;
  24.  
  25. //The event structure
  26. SDL_Event event;
  27.  
  28. //The dot
  29. class Dot
  30. {
  31.     private:
  32.     //The offsets of the dot
  33.     int x, y;
  34.    
  35.     //Has the dot been shot yet?
  36.     bool shoot;
  37.    
  38.     //The collision boxes of the dot
  39.     std::vector<SDL_Rect> box;
  40.  
  41.     //The velocity of the dot
  42.     int xVel, yVel;
  43.  
  44.     //Moves the collision boxes relative to the dot's offset
  45.     void shift_boxes();
  46.  
  47.     public:
  48.     //Initializes the variables
  49.     Dot( int X, int Y );
  50.  
  51.     //Takes key presses and adjusts the dot's velocity
  52.     void handle_input();
  53.     void handle_AI();
  54.  
  55.     //Moves the dot
  56.     void move();
  57.  
  58.     //Shows the dot on the screen
  59.     void show();
  60.  
  61.     //Gets the collision boxes
  62.     std::vector<SDL_Rect> &get_rects();
  63. };
  64.  
  65. //The timer
  66. class Timer
  67. {
  68.     private:
  69.     //The clock time when the timer started
  70.     int startTicks;
  71.  
  72.     //The ticks stored when the timer was paused
  73.     int pausedTicks;
  74.  
  75.     //The timer status
  76.     bool paused;
  77.     bool started;
  78.  
  79.     public:
  80.     //Initializes variables
  81.     Timer();
  82.  
  83.     //The various clock actions
  84.     void start();
  85.     void stop();
  86.     void pause();
  87.     void unpause();
  88.  
  89.     //Gets the timer's time
  90.     int get_ticks();
  91.  
  92.     //Checks the status of the timer
  93.     bool is_started();
  94.     bool is_paused();
  95. };
  96.  
  97. SDL_Surface *load_image( std::string filename )
  98. {
  99.     //The image that's loaded
  100.     SDL_Surface* loadedImage = NULL;
  101.  
  102.     //The optimized surface that will be used
  103.     SDL_Surface* optimizedImage = NULL;
  104.  
  105.     //Load the image
  106.     loadedImage = IMG_Load( filename.c_str() );
  107.  
  108.     //If the image loaded
  109.     if( loadedImage != NULL )
  110.     {
  111.         //Create an optimized surface
  112.         optimizedImage = SDL_DisplayFormat( loadedImage );
  113.  
  114.         //Free the old surface
  115.         SDL_FreeSurface( loadedImage );
  116.  
  117.         //If the surface was optimized
  118.         if( optimizedImage != NULL )
  119.         {
  120.             //Color key surface
  121.             SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
  122.         }
  123.     }
  124.  
  125.     //Return the optimized surface
  126.     return optimizedImage;
  127. }
  128.  
  129. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  130. {
  131.     //Holds offsets
  132.     SDL_Rect offset;
  133.  
  134.     //Get offsets
  135.     offset.x = x;
  136.     offset.y = y;
  137.  
  138.     //Blit
  139.     SDL_BlitSurface( source, clip, destination, &offset );
  140. }
  141.  
  142. bool check_collision( std::vector<SDL_Rect> &A, std::vector<SDL_Rect> &B )
  143. {
  144.     //The sides of the rectangles
  145.     int leftA, leftB;
  146.     int rightA, rightB;
  147.     int topA, topB;
  148.     int bottomA, bottomB;
  149.  
  150.     //Go through the A boxes
  151.     for( int Abox = 0; Abox < A.size(); Abox++ )
  152.     {
  153.         //Calculate the sides of rect A
  154.         leftA = A[ Abox ].x;
  155.         rightA = A[ Abox ].x + A[ Abox ].w;
  156.         topA = A[ Abox ].y;
  157.         bottomA = A[ Abox ].y + A[ Abox ].h;
  158.  
  159.         //Go through the B boxes
  160.         for( int Bbox = 0; Bbox < B.size(); Bbox++ )
  161.         {
  162.             //Calculate the sides of rect B
  163.             leftB = B[ Bbox ].x;
  164.             rightB = B[ Bbox ].x + B[ Bbox ].w;
  165.             topB = B[ Bbox ].y;
  166.             bottomB = B[ Bbox ].y + B[ Bbox ].h;
  167.  
  168.             //If no sides from A are outside of B
  169.             if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false )
  170.             {
  171.                 //A collision is detected
  172.                 return true;
  173.             }
  174.         }
  175.     }
  176.  
  177.     //If neither set of collision boxes touched
  178.     return false;
  179. }
  180.  
  181. bool init()
  182. {
  183.     //Initialize all SDL subsystems
  184.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  185.     {
  186.         return false;
  187.     }
  188.  
  189.     //Set up the screen
  190.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  191.  
  192.     //If there was an error in setting up the screen
  193.     if( screen == NULL )
  194.     {
  195.         return false;
  196.     }
  197.  
  198.     //Set the window caption
  199.     SDL_WM_SetCaption( "Move the Dot", NULL );
  200.  
  201.     //If everything initialized fine
  202.     return true;
  203. }
  204.  
  205. bool load_files()
  206. {
  207.     //Load the dot image
  208.     dot = load_image( "dot.bmp" );
  209.  
  210.     //If there was a problem in loading the dot
  211.     if( dot == NULL )
  212.     {
  213.         return false;
  214.     }
  215.  
  216.     //If everything loaded fine
  217.     return true;
  218. }
  219.  
  220. void clean_up()
  221. {
  222.     //Free the surface
  223.     SDL_FreeSurface( dot );
  224.  
  225.     //Quit SDL
  226.     SDL_Quit();
  227. }
  228.  
  229. Dot::Dot( int X, int Y )
  230. {
  231.     //Initialize the offsets
  232.     x = X;
  233.     y = Y;
  234.    
  235.     shoot = false;
  236.    
  237.     //Initialize the velocity
  238.     xVel = 0;
  239.     yVel = 0;
  240.  
  241.     //Create the necessary SDL_Rects
  242.     box.resize( 11 );
  243.  
  244.     //Initialize the collision boxes' width and height
  245.     box[ 0 ].w = 6;
  246.     box[ 0 ].h = 1;
  247.  
  248.     box[ 1 ].w = 10;
  249.     box[ 1 ].h = 1;
  250.  
  251.     box[ 2 ].w = 14;
  252.     box[ 2 ].h = 1;
  253.  
  254.     box[ 3 ].w = 16;
  255.     box[ 3 ].h = 2;
  256.  
  257.     box[ 4 ].w = 18;
  258.     box[ 4 ].h = 2;
  259.  
  260.     box[ 5 ].w = 20;
  261.     box[ 5 ].h = 6;
  262.  
  263.     box[ 6 ].w = 18;
  264.     box[ 6 ].h = 2;
  265.  
  266.     box[ 7 ].w = 16;
  267.     box[ 7 ].h = 2;
  268.  
  269.     box[ 8 ].w = 14;
  270.     box[ 8 ].h = 1;
  271.  
  272.     box[ 9 ].w = 10;
  273.     box[ 9 ].h = 1;
  274.  
  275.     box[ 10 ].w = 6;
  276.     box[ 10 ].h = 1;
  277.  
  278.     //Move the collision boxes to their proper spot
  279.     shift_boxes();
  280. }
  281.  
  282. void Dot::shift_boxes()
  283. {
  284.     //The row offset
  285.     int r = 0;
  286.  
  287.     //Go through the dot's collision boxes
  288.     for( int set = 0; set < box.size(); set++ )
  289.     {
  290.         //Center the collision box
  291.         box[ set ].x = x + ( DOT_WIDTH - box[ set ].w ) / 2;
  292.  
  293.         //Set the collision box at its row offset
  294.         box[ set ].y = y + r;
  295.  
  296.         //Move the row offset down the height of the collision box
  297.         r += box[ set ].h;
  298.     }
  299. }
  300.  
  301. void Dot::handle_input()
  302. {
  303.     if(shoot == false)
  304.     {
  305.         if (event.type == SDL_MOUSEBUTTONDOWN)
  306.         {
  307.             if (event.button.button == SDL_BUTTON_LEFT)
  308.             {
  309.                 int tempX,tempY;
  310.                 int Gradient;
  311.                 tempX = event.button.x;
  312.                 tempY = event.button.y;  //x,y
  313.                 Gradient = (ceil(tempY - y) / (tempX - x));
  314.                 xVel = 50;
  315.                 yVel = 50*Gradient;
  316.                 shoot = true;
  317.                
  318.             }
  319.         }
  320.     }
  321. }
  322.  
  323. void Dot::handle_AI()
  324. {
  325.         //Gravity
  326.     yVel = yVel + 1;
  327.     if((y >= SCREEN_HEIGHT) && (yVel >= 0))
  328.     {
  329.         yVel = 0;
  330.     }
  331. }
  332.  
  333. void Dot::move()
  334. {
  335.    
  336.     //Move the dot left or right
  337.     x += ceil((xVel/10));
  338.  
  339.     //Move the collision boxes
  340.     shift_boxes();
  341.  
  342.     //If the dot went too far to the left or right or has collided with the other dot
  343.     if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
  344.     {
  345.         //Move back
  346.         x -= ceil((xVel/10));
  347.         shift_boxes();
  348.     }
  349.  
  350.     //Move the dot up or down
  351.     y += ceil((yVel/10));
  352.  
  353.     //Move the collision boxes
  354.     shift_boxes();
  355.  
  356.     //If the dot went too far up or down or has collided with the other dot
  357.     if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
  358.     {
  359.         //Move back
  360.         y -= ceil((yVel/10));
  361.         shift_boxes();
  362.     }
  363. }
  364.  
  365. void Dot::show()
  366. {
  367.     //Show the dot
  368.     apply_surface( x, y, dot, screen );
  369. }
  370.  
  371. std::vector<SDL_Rect> &Dot::get_rects()
  372. {
  373.     //Retrieve the collision boxes
  374.     return box;
  375. }
  376.  
  377. Timer::Timer()
  378. {
  379.     //Initialize the variables
  380.     startTicks = 0;
  381.     pausedTicks = 0;
  382.     paused = false;
  383.     started = false;
  384. }
  385.  
  386. void Timer::start()
  387. {
  388.     //Start the timer
  389.     started = true;
  390.  
  391.     //Unpause the timer
  392.     paused = false;
  393.  
  394.     //Get the current clock time
  395.     startTicks = SDL_GetTicks();
  396. }
  397.  
  398. void Timer::stop()
  399. {
  400.     //Stop the timer
  401.     started = false;
  402.  
  403.     //Unpause the timer
  404.     paused = false;
  405. }
  406.  
  407. void Timer::pause()
  408. {
  409.     //If the timer is running and isn't already paused
  410.     if( ( started == true ) && ( paused == false ) )
  411.     {
  412.         //Pause the timer
  413.         paused = true;
  414.  
  415.         //Calculate the paused ticks
  416.         pausedTicks = SDL_GetTicks() - startTicks;
  417.     }
  418. }
  419.  
  420. void Timer::unpause()
  421. {
  422.     //If the timer is paused
  423.     if( paused == true )
  424.     {
  425.         //Unpause the timer
  426.         paused = false;
  427.  
  428.         //Reset the starting ticks
  429.         startTicks = SDL_GetTicks() - pausedTicks;
  430.  
  431.         //Reset the paused ticks
  432.         pausedTicks = 0;
  433.     }
  434. }
  435.  
  436. int Timer::get_ticks()
  437. {
  438.     //If the timer is running
  439.     if( started == true )
  440.     {
  441.         //If the timer is paused
  442.         if( paused == true )
  443.         {
  444.             //Return the number of ticks when the timer was paused
  445.             return pausedTicks;
  446.         }
  447.         else
  448.         {
  449.             //Return the current time minus the start time
  450.             return SDL_GetTicks() - startTicks;
  451.         }
  452.     }
  453.  
  454.     //If the timer isn't running
  455.     return 0;
  456. }
  457.  
  458. bool Timer::is_started()
  459. {
  460.     return started;
  461. }
  462.  
  463. bool Timer::is_paused()
  464. {
  465.     return paused;
  466. }
  467.  
  468. int main( int argc, char* args[] )
  469. {
  470.     //Quit flag
  471.     bool quit = false;
  472.     //Make the dots
  473.     Dot myDot( 0, 380 );
  474.  
  475.     //The frame rate regulator
  476.     Timer fps;
  477.  
  478.     //Initialize
  479.     if( init() == false )
  480.     {
  481.         return 1;
  482.     }
  483.  
  484.     //Load the files
  485.     if( load_files() == false )
  486.     {
  487.         return 1;
  488.     }
  489.  
  490.     //While the user hasn't quit
  491.     while( quit == false )
  492.     {
  493.         //Start the frame timer
  494.         fps.start();
  495.  
  496.         //While there's events to handle
  497.         while( SDL_PollEvent( &event ) )
  498.         {
  499.             //Handle events for the dot
  500.             myDot.handle_input();
  501.  
  502.             //If the user has Xed out the window
  503.             if( event.type == SDL_QUIT )
  504.             {
  505.                 //Quit the program
  506.                 quit = true;
  507.             }
  508.         }
  509.        
  510.         myDot.handle_AI();
  511.         myDot.move();
  512.  
  513.         //Fill the screen white
  514.         SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
  515.  
  516.         //Show the dots on the screen
  517.         myDot.show();
  518.  
  519.         //Update the screen
  520.         if( SDL_Flip( screen ) == -1 )
  521.         {
  522.             return 1;
  523.         }
  524.        
  525.         //Cap the frame rate
  526.         if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  527.         {
  528.             SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  529.         }
  530.     }
  531.  
  532.     //Clean up
  533.     clean_up();
  534.  
  535.     return 0;
  536. }
clone this paste RAW Paste Data