piexil

...

Oct 11th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.36 KB | None | 0 0
  1. /*Tilemap code (C) LazyFoo Productions, modified by piexil.
  2. Aditions to code (C) 2012 Piexil
  3. Liscened under the GNU\GPL
  4. */
  5.  
  6. //The headers
  7. #include "SDL.h"
  8. #include "SDL_image.h"
  9. #include <string>
  10. #include <fstream>
  11. #include <vector>
  12. using namespace std;
  13. int mapload = 0;
  14. //Screen attributes
  15. const int SCREEN_WIDTH = 800;
  16. const int SCREEN_HEIGHT = 600 ;
  17. const int SCREEN_BPP = 32;    
  18. //The frame rate
  19. const int FRAMES_PER_SECOND = 60;
  20.  
  21. //The dot dimensions
  22. const int DOT_WIDTH = 20;
  23. const int DOT_HEIGHT = 20;
  24.  
  25.  
  26. //The dimensions of the level
  27. const int LEVEL_WIDTH = 960; //12 tiles
  28. const int LEVEL_HEIGHT = 5120; //64 tiles
  29.  
  30. //Tile constants
  31. const int TILE_WIDTH = 80;
  32. const int TILE_HEIGHT = 80;
  33. const int TOTAL_TILES = 768;
  34. const int TILE_SPRITES = 48;
  35.  
  36. //The different tile sprites
  37. const int TILE_RED = 0;
  38. const int TILE_DOOR = 1;
  39. const int TILE_ENEMY = 2;
  40. const int TILE_CENTER = 3;
  41. const int TILE_TOP = 4;
  42. const int TILE_TOPRIGHT = 5;
  43. const int TILE_RIGHT = 6;
  44. const int TILE_BOTTOMRIGHT = 7;
  45. const int TILE_BOTTOM = 8;
  46. const int TILE_BOTTOMLEFT = 9;
  47. const int TILE_LEFT = 10;
  48. const int TILE_TOPLEFT = 11;
  49. int box_dotx = 0;
  50. int box_doty = 0;
  51. bool projectile_show = false;
  52. int var=0;
  53.  
  54. //Debug
  55. const int debugmode = 1;
  56. string DR = "O";
  57.  
  58.  
  59.  
  60. //The surfaces
  61. SDL_Surface *Projectile = NULL;
  62. SDL_Surface *dot = NULL;
  63. SDL_Surface *screen = NULL;
  64. SDL_Surface *tileSheet = NULL;
  65.  
  66. //Sprite from the tile sheet
  67. SDL_Rect clips[ TILE_SPRITES ];
  68.  
  69. //The event structure
  70. SDL_Event event;
  71.  
  72. //The camera
  73. SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
  74.  
  75. //The tile
  76. class Tile
  77. {
  78.     private:
  79.     //The attributes of the tile
  80.     SDL_Rect box;
  81.  
  82.     //The tile type
  83.     int type;
  84.  
  85.     public:
  86.     //Initializes the variables
  87.     Tile( int x, int y, int tileType );
  88.  
  89.     //Shows the tile
  90.     void show();
  91.  
  92.     //Get the tile type
  93.     int get_type();
  94.  
  95.     //Get the collision box
  96.     SDL_Rect get_box();
  97. };
  98.  
  99. //The dot
  100. class Dot
  101. {
  102.     private:
  103.     //The dot's collision box
  104.     SDL_Rect box;
  105.        
  106.  
  107.  
  108.  
  109.     //The velocity of the dot
  110.     int xVel, yVel;
  111.  
  112.     public:
  113.     //Initializes the variables
  114.     Dot();
  115.  
  116.     //Takes key presses and adjusts the dot's velocity
  117.     void handle_input();
  118.  
  119.     //Moves the dot
  120.     void move( Tile *tiles[] );
  121.  
  122.     //Shows the dot on the screen
  123.     void show();
  124.  
  125.     //Sets the camera over the dot
  126.     void set_camera();
  127. };
  128. class projectile
  129. {
  130.         private:
  131.                 //Collision
  132.         SDL_Rect box1;
  133.         //Velocity
  134.         int xVel_projectile;
  135.         int yVel_projectile;
  136.         public:
  137.                 //Intitialize
  138.         projectile();
  139.         //SHow
  140.         void show();
  141.         //Handle input
  142.         void handle_input();
  143.         //handle movement
  144.         void move( Tile *tiles[] );
  145. };
  146. //the projectile
  147.  
  148.  
  149.  
  150. //The timer
  151. class Timer
  152. {
  153.     private:
  154.     //The clock time when the timer started
  155.     int startTicks;
  156.  
  157.     //The ticks stored when the timer was paused
  158.     int pausedTicks;
  159.  
  160.     //The timer status
  161.     bool paused;
  162.     bool started;
  163.  
  164.     public:
  165.     //Initializes variables
  166.     Timer();
  167.  
  168.     //The various clock actions
  169.     void start();
  170.     void stop();
  171.     void pause();
  172.     void unpause();
  173.  
  174.     //Gets the timer's time
  175.     int get_ticks();
  176.  
  177.     //Checks the status of the timer
  178.     bool is_started();
  179.     bool is_paused();
  180. };
  181.  
  182. SDL_Surface *load_image( std::string filename )
  183. {
  184.     //The image that's loaded
  185.     SDL_Surface* loadedImage = NULL;
  186.  
  187.     //The optimized surface that will be used
  188.     SDL_Surface* optimizedImage = NULL;
  189.  
  190.     //Load the image
  191.     loadedImage = IMG_Load( filename.c_str() );
  192.  
  193.     //If the image loaded
  194.     if( loadedImage != NULL )
  195.     {
  196.         //Create an optimized surface
  197.         optimizedImage = SDL_DisplayFormat( loadedImage );
  198.  
  199.         //Free the old surface
  200.         SDL_FreeSurface( loadedImage );
  201.  
  202.         //If the surface was optimized
  203.         if( optimizedImage != NULL )
  204.         {
  205.             //Color key surface
  206.             SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
  207.         }
  208.     }
  209.  
  210.     //Return the optimized surface
  211.     return optimizedImage;
  212. }
  213.  
  214. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  215. {
  216.     //Holds offsets
  217.     SDL_Rect offset;
  218.  
  219.     //Get offsets
  220.     offset.x = x;
  221.     offset.y = y;
  222.  
  223.     //Blit
  224.     SDL_BlitSurface( source, clip, destination, &offset );
  225. }
  226.  
  227. bool check_collision( SDL_Rect A, SDL_Rect B )
  228. {
  229.     //The sides of the rectangles
  230.     int leftA, leftB;
  231.     int rightA, rightB;
  232.     int topA, topB;
  233.     int bottomA, bottomB;
  234.  
  235.     //Calculate the sides of rect A
  236.     leftA = A.x;
  237.     rightA = A.x + A.w;
  238.     topA = A.y;
  239.     bottomA = A.y + A.h;
  240.  
  241.     //Calculate the sides of rect B
  242.     leftB = B.x;
  243.     rightB = B.x + B.w;
  244.     topB = B.y;
  245.     bottomB = B.y + B.h;
  246.  
  247.     //If any of the sides from A are outside of B
  248.     if( bottomA <= topB )
  249.     {
  250.         return false;
  251.     }
  252.  
  253.     if( topA >= bottomB )
  254.     {
  255.         return false;
  256.     }
  257.  
  258.     if( rightA <= leftB )
  259.     {
  260.         return false;
  261.     }
  262.  
  263.     if( leftA >= rightB )
  264.     {
  265.         return false;
  266.     }
  267.  
  268.     //If none of the sides from A are outside B
  269.     return true;
  270. }
  271.  
  272. bool init()
  273. {
  274.     //Initialize all SDL subsystems
  275.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  276.     {
  277.         return false;
  278.     }
  279.  
  280.     //Set up the screen
  281.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  282.  
  283.     //If there was an error in setting up the screen
  284.     if( screen == NULL )
  285.     {
  286.         return false;
  287.     }
  288.  
  289.     //Set the window caption
  290.     SDL_WM_SetCaption( "Bullet Hell", NULL );
  291.  
  292.     //If everything initialized fine
  293.     return true;
  294. }
  295.  
  296. bool load_files()
  297. {
  298.     //Load the dot image
  299.     dot = load_image( "dot.png" );
  300.  
  301.     //If there was a problem in loading the dot
  302.     if( dot == NULL )
  303.     {
  304.         return false;
  305.     }
  306.  
  307.         Projectile = load_image ( "Projectile.png" );
  308.         //If there was a problem in loading the projectile
  309.     if( Projectile == NULL )
  310.     {
  311.         return false;
  312.     }
  313.     //Load the tile sheet
  314.        
  315.     tileSheet = load_image( "tiles.png" );
  316.        
  317.         if(debugmode == 1){
  318.                 tileSheet = load_image( "tiles_debug.png");
  319.         }
  320.     //If there was a problem in loading the tiles
  321.     if( tileSheet == NULL )
  322.     {
  323.         return false;
  324.     }
  325.  
  326.     //If everything loaded fine
  327.     return true;
  328. }
  329.  
  330. void clean_up( Tile *tiles[] )
  331. {
  332.     //Free the surfaces
  333.     SDL_FreeSurface( dot );
  334.     SDL_FreeSurface( tileSheet );
  335.         SDL_FreeSurface( Projectile );
  336.  
  337.     //Free the tiles
  338.     for( int t = 0; t < TOTAL_TILES; t++ )
  339.     {
  340.         delete tiles[ t ];
  341.     }
  342.  
  343.     //Quit SDL
  344.     SDL_Quit();
  345. }
  346.  
  347. void clip_tiles()
  348. {
  349.     //Clip the sprite sheet
  350.     clips[ TILE_RED ].x = 0;
  351.     clips[ TILE_RED ].y = 0;
  352.     clips[ TILE_RED ].w = TILE_WIDTH;
  353.     clips[ TILE_RED ].h = TILE_HEIGHT;
  354.  
  355.     clips[ TILE_DOOR ].x = 0;
  356.     clips[ TILE_DOOR ].y = 80;
  357.     clips[ TILE_DOOR ].w = TILE_WIDTH;
  358.     clips[ TILE_DOOR ].h = TILE_HEIGHT;
  359.  
  360.     clips[ TILE_ENEMY ].x = 0;
  361.     clips[ TILE_ENEMY ].y = 160;
  362.     clips[ TILE_ENEMY ].w = TILE_WIDTH;
  363.     clips[ TILE_ENEMY ].h = TILE_HEIGHT;
  364.  
  365.     clips[ TILE_TOPLEFT ].x = 80;
  366.     clips[ TILE_TOPLEFT ].y = 0;
  367.     clips[ TILE_TOPLEFT ].w = TILE_WIDTH;
  368.     clips[ TILE_TOPLEFT ].h = TILE_HEIGHT;
  369.  
  370.     clips[ TILE_LEFT ].x = 80;
  371.     clips[ TILE_LEFT ].y = 80;
  372.     clips[ TILE_LEFT ].w = TILE_WIDTH;
  373.     clips[ TILE_LEFT ].h = TILE_HEIGHT;
  374.  
  375.     clips[ TILE_BOTTOMLEFT ].x = 80;
  376.     clips[ TILE_BOTTOMLEFT ].y = 160;
  377.     clips[ TILE_BOTTOMLEFT ].w = TILE_WIDTH;
  378.     clips[ TILE_BOTTOMLEFT ].h = TILE_HEIGHT;
  379.  
  380.     clips[ TILE_TOP ].x = 160;
  381.     clips[ TILE_TOP ].y = 0;
  382.     clips[ TILE_TOP ].w = TILE_WIDTH;
  383.     clips[ TILE_TOP ].h = TILE_HEIGHT;
  384.  
  385.     clips[ TILE_CENTER ].x = 160;
  386.     clips[ TILE_CENTER ].y = 80;
  387.     clips[ TILE_CENTER ].w = TILE_WIDTH;
  388.     clips[ TILE_CENTER ].h = TILE_HEIGHT;
  389.  
  390.     clips[ TILE_BOTTOM ].x = 160;
  391.     clips[ TILE_BOTTOM ].y = 160;
  392.     clips[ TILE_BOTTOM ].w = TILE_WIDTH;
  393.     clips[ TILE_BOTTOM ].h = TILE_HEIGHT;
  394.  
  395.     clips[ TILE_TOPRIGHT ].x = 240;
  396.     clips[ TILE_TOPRIGHT ].y = 0;
  397.     clips[ TILE_TOPRIGHT ].w = TILE_WIDTH;
  398.     clips[ TILE_TOPRIGHT ].h = TILE_HEIGHT;
  399.  
  400.     clips[ TILE_RIGHT ].x = 240;
  401.     clips[ TILE_RIGHT ].y = 80;
  402.     clips[ TILE_RIGHT ].w = TILE_WIDTH;
  403.     clips[ TILE_RIGHT ].h = TILE_HEIGHT;
  404.  
  405.     clips[ TILE_BOTTOMRIGHT ].x = 240;
  406.     clips[ TILE_BOTTOMRIGHT ].y = 160;
  407.     clips[ TILE_BOTTOMRIGHT ].w = TILE_WIDTH;
  408.     clips[ TILE_BOTTOMRIGHT ].h = TILE_HEIGHT;
  409.  
  410.        
  411. }
  412.  
  413.  
  414.  
  415. bool set_tiles( Tile *tiles[] )
  416.  
  417. {
  418.        
  419.     //The tile offsets
  420.     int x = 0, y = 0;
  421.  
  422.         //0 is the defualt for DR
  423.         fstream map;
  424.         if(DR=="O"){
  425.                 map.open("lazy.map");
  426.                        
  427.         }
  428.         if (DR=="D1R1"){
  429.                 map.open("lazy.map");
  430.         }
  431.        
  432.  
  433.        
  434.     //Initialize the tiles
  435.     for( int t = 0; t < TOTAL_TILES; t++ )
  436.     {
  437.         //Determines what kind of tile will be made
  438.         int tileType = -1;
  439.  
  440.         //Read tile from map file
  441.         map >> tileType;
  442.  
  443.         //If the was a problem in reading the map
  444.         if( map.fail() == true )
  445.         {
  446.             //Stop loading map
  447.             map.close();
  448.             return false;
  449.         }
  450.  
  451.         //If the number is a valid tile number
  452.         if( ( tileType >= 0 ) && ( tileType < TILE_SPRITES ) )
  453.         {
  454.             tiles[ t ] = new Tile( x, y, tileType );
  455.         }
  456.         //If we don't recognize the tile type
  457.         else
  458.         {
  459.             //Stop loading map
  460.             map.close();
  461.             return false;
  462.         }
  463.  
  464.         //Move to next tile spot
  465.         x += TILE_WIDTH;
  466.  
  467.         //If we've gone too far
  468.         if( x >= LEVEL_WIDTH )
  469.         {
  470.             //Move back
  471.             x = 0;
  472.  
  473.             //Move to the next row
  474.             y += TILE_HEIGHT;
  475.         }
  476.     }
  477.  
  478.     //Close the file
  479.     map.close();
  480.  
  481.     //If the map was loaded fine
  482.     return true;
  483. }
  484.  
  485.  
  486.  
  487.  
  488. bool touches_wall( SDL_Rect box, Tile *tiles[] )
  489. {
  490.     //Go through the tiles
  491.     for( int t = 0; t < TOTAL_TILES; t++ )
  492.     {
  493.         //If the tile is a wall type tile
  494.         if( ( tiles[ t ]->get_type() >= TILE_CENTER ) && ( tiles[ t ]->get_type() <= TILE_TOPLEFT ) )
  495.         {
  496.             //If the collision box touches the wall tile
  497.             if( check_collision( box, tiles[ t ]->get_box() ) == true )
  498.             {
  499.                 return true;
  500.             }
  501.         }
  502.     }
  503.  
  504.     //If no wall tiles were touched
  505.     return false;
  506. }
  507. bool touches_door( SDL_Rect box, Tile *tiles[] )
  508. {
  509.         for( int t = 0; t < TOTAL_TILES; t++ )
  510.     {
  511.                 //If the tile is door type
  512.                 if  (tiles [ t ] ->get_type() == TILE_DOOR)
  513.                 {
  514.                          //If the collision box touches the door tile
  515.             if( check_collision( box, tiles[ t ]->get_box() ) == true )
  516.             {
  517.                 return true;
  518.             }
  519.                 }
  520.         }
  521.  
  522. return false;
  523. }
  524.                        
  525.  
  526. Tile::Tile( int x, int y, int tileType )
  527. {
  528.     //Get the offsets
  529.     box.x = x;
  530.     box.y = y;
  531.  
  532.     //Set the collision box
  533.     box.w = TILE_WIDTH;
  534.     box.h = TILE_HEIGHT;
  535.  
  536.     //Get the tile type
  537.     type = tileType;
  538. }
  539.  
  540. void Tile::show()
  541. {
  542.     //If the tile is on screen
  543.     if( check_collision( camera, box ) == true )
  544.     {
  545.         //Show the tile
  546.         apply_surface( box.x - camera.x, box.y - camera.y, tileSheet, screen, &clips[ type ] );
  547.                
  548.  
  549.     }
  550. }
  551.  
  552. int Tile::get_type()
  553. {
  554.     return type;
  555. }
  556.  
  557. SDL_Rect Tile::get_box()
  558. {
  559.     return box;
  560. }
  561.  
  562. Dot::Dot()
  563. {
  564.     //Initialize the offsets
  565.     box.x = 480;
  566.     box.y = 5100;
  567.     box.w = DOT_WIDTH;
  568.     box.h = DOT_HEIGHT;
  569.        
  570.  
  571.     //Initialize the velocity
  572.     xVel = 0;
  573.     yVel = -4;
  574. }
  575.  
  576. projectile::projectile()
  577. {
  578.         box1.x = 480;
  579.     box1.y = 5100;
  580.     box1.w = DOT_WIDTH;
  581.     box1.h = DOT_HEIGHT;
  582.         xVel_projectile = 0;
  583.         yVel_projectile = -15;
  584. }
  585. void projectile::handle_input(){
  586.         //If a key was pressed
  587.     if( event.type == SDL_KEYDOWN )
  588.     {
  589.         //fire
  590.         switch( event.key.keysym.sym )
  591.         {
  592.                
  593.                         case SDLK_SPACE:box1.y=box_doty, box1.x=box_dotx, projectile_show = true, var++;
  594.                            
  595.                                
  596.  
  597.                                
  598.                        
  599.                 }
  600.         }
  601.         else if( event.type == SDL_KEYUP )
  602.     {
  603.         //Adjust the velocity
  604.         switch( event.key.keysym.sym )
  605.         {
  606.                 case SDLK_SPACE: box1.y=box_doty, box1.x=box_dotx,projectile_show = true, var++;
  607.         }
  608.     }
  609. }
  610. void projectile::show()
  611. {
  612.     //Show the dot
  613.         apply_surface( box1.x - camera.x, box1.y - camera.y, Projectile, screen );
  614. }
  615.  
  616.  
  617.  
  618. void Dot::handle_input()
  619. {
  620.  
  621.  
  622.     //If a key was pressed
  623.     if( event.type == SDL_KEYDOWN )
  624.     {
  625.         //Adjust the velocity
  626.         switch( event.key.keysym.sym )
  627.         {
  628.             case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
  629.             case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
  630.             case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
  631.             case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
  632.         }
  633.     }
  634.     //If a key was released
  635.     else if( event.type == SDL_KEYUP )
  636.     {
  637.         //Adjust the velocity
  638.         switch( event.key.keysym.sym )
  639.         {
  640.             case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
  641.             case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
  642.             case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
  643.             case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
  644.         }
  645.     }
  646. }
  647.  
  648. void Dot::move( Tile *tiles[] )
  649. {
  650.     //Move the dot left or right
  651.     box.x += xVel;
  652.  
  653.     //If the dot went too far to the left or right or touched a wall
  654.     if( ( box.x < 0 ) || ( box.x + DOT_WIDTH > LEVEL_WIDTH ) || touches_wall( box, tiles ) )
  655.     {
  656.         //move back
  657.         box.x -= xVel;
  658.     }
  659.         if( touches_door( box, tiles)) {
  660.                 DR="O";
  661.                 set_tiles( tiles ) == false;
  662.                 box.x=480;
  663.                 box.y=5100;
  664.         }
  665.  
  666.     //Move the dot up or down
  667.         box.y += yVel;
  668.    
  669.  
  670.     //If the dot went too far up or down or touched a wall
  671.     if( ( box.y < 0 ) || ( box.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) ){
  672.    
  673.         //move back
  674.         box.y -= yVel;
  675.     }
  676. box_dotx = box.x;
  677.         box_doty = box.y;
  678. }
  679. void projectile::move (Tile *tiles[] ){
  680.         box1.y += yVel_projectile;
  681. }
  682.  
  683.  
  684.  
  685. void Dot::show()
  686. {
  687.     //Show the dot
  688.     apply_surface( box.x - camera.x, box.y - camera.y, dot, screen );
  689. }
  690.  
  691. void Dot::set_camera()
  692. {
  693.     //Center the camera over the dot
  694.     camera.x = ( box.x + DOT_WIDTH / 2 ) - SCREEN_WIDTH /2 ;
  695.     camera.y = ( box.y + DOT_HEIGHT / 2 ) - SCREEN_HEIGHT /2;
  696.  
  697.     //Keep the camera in bounds.
  698.     if( camera.x < 0 )
  699.     {
  700.         camera.x = 0;
  701.     }
  702.     if( camera.y < 0 )
  703.     {
  704.         camera.y = 0;
  705.     }
  706.     if( camera.x > LEVEL_WIDTH - camera.w )
  707.     {
  708.         camera.x = LEVEL_WIDTH - camera.w;
  709.     }
  710.     if( camera.y > LEVEL_HEIGHT - camera.h )
  711.     {
  712.         camera.y = LEVEL_HEIGHT - camera.h;
  713.     }
  714. }
  715.  
  716. Timer::Timer()
  717. {
  718.     //Initialize the variables
  719.     startTicks = 0;
  720.     pausedTicks = 0;
  721.     paused = false;
  722.     started = false;
  723. }
  724. //v
  725.  
  726. void Timer::start()
  727. {
  728.     //Start the timer
  729.     started = true;
  730.  
  731.     //Unpause the timer
  732.     paused = false;
  733.  
  734.     //Get the current clock time
  735.     startTicks = SDL_GetTicks();
  736. }
  737.  
  738. void Timer::stop()
  739. {
  740.     //Stop the timer
  741.     started = false;
  742.  
  743.     //Unpause the timer
  744.     paused = false;
  745. }
  746.  
  747. void Timer::pause()
  748. {
  749.     //If the timer is running and isn't already paused
  750.     if( ( started == true ) && ( paused == false ) )
  751.     {
  752.         //Pause the timer
  753.         paused = true;
  754.  
  755.         //Calculate the paused ticks
  756.         pausedTicks = SDL_GetTicks() - startTicks;
  757.     }
  758. }
  759.  
  760. void Timer::unpause()
  761. {
  762.     //If the timer is paused
  763.     if( paused == true )
  764.     {
  765.         //Unpause the timer
  766.         paused = false;
  767.  
  768.         //Reset the starting ticks
  769.         startTicks = SDL_GetTicks() - pausedTicks;
  770.  
  771.         //Reset the paused ticks
  772.         pausedTicks = 0;
  773.     }
  774. }
  775.  
  776. int Timer::get_ticks()
  777. {
  778.     //If the timer is running
  779.     if( started == true )
  780.     {
  781.         //If the timer is paused
  782.         if( paused == true )
  783.         {
  784.             //Return the number of ticks when the timer was paused
  785.             return pausedTicks;
  786.         }
  787.         else
  788.         {
  789.             //Return the current time minus the start time
  790.             return SDL_GetTicks() - startTicks;
  791.         }
  792.     }
  793.  
  794.     //If the timer isn't running
  795.     return 0;
  796. }
  797.  
  798. bool Timer::is_started()
  799. {
  800.     return started;
  801. }
  802.  
  803. bool Timer::is_paused()
  804. {
  805.     return paused;
  806. }
  807.  
  808. int main( int argc, char* args[] )
  809. {
  810.        
  811.     //Quit flag
  812.     bool quit = false;
  813.  
  814.     //The dot
  815.     Dot myDot;
  816.        
  817.     vector<projectile> myProj;
  818.  
  819.     //The tiles that will be used
  820.     Tile *tiles[ TOTAL_TILES ];
  821.  
  822.     //The frame rate regulator
  823.     Timer fps;
  824.  
  825.     //Initialize
  826.     if( init() == false )
  827.     {
  828.         return 1;
  829.     }
  830.  
  831.     //Load the files
  832.     if( load_files() == false )
  833.     {
  834.         return 1;
  835.     }
  836.  
  837.     //Clip the tile sheet
  838.     clip_tiles();
  839.        
  840.        
  841.     //While the user hasn't quit
  842.     while( quit == false )
  843.     {
  844.                
  845.     //Set the tiles
  846.    
  847.         //Start the frame timer
  848.         fps.start();
  849.  
  850.         //While there's events to handle
  851.  
  852.         while( SDL_PollEvent( &event ) )
  853.         {
  854.                          if( set_tiles( tiles ) == false )
  855.     {
  856.         return 1;
  857.     }
  858.  
  859.             //Handle events for the dot
  860.             myDot.handle_input();
  861.                         //handle input for projectile
  862.            
  863.                         myProj.push_back(projectile());
  864.                         myProj[var].handle_input();
  865.             //If the user has Xed out the window
  866.             if( event.type == SDL_QUIT )
  867.             {
  868.                 //Quit the program
  869.                 quit = true;
  870.             }
  871.         }
  872.  
  873.         //Move the dot
  874.         myDot.move( tiles );
  875.                 //Fire projectile
  876.                 myProj[var].move( tiles );
  877.  
  878.         //Set the camera
  879.         myDot.set_camera();
  880.  
  881.         //Show the tiles
  882.         for( int t = 0; t < TOTAL_TILES; t++ )
  883.         {
  884.             tiles[ t ]->show();
  885.         }
  886.  
  887.         //Show the dot on the screen
  888.                
  889.         myDot.show();
  890.                
  891.                 if(projectile_show = true){
  892.                    
  893.                         myProj[var].show();
  894.                        
  895.                        
  896.                 }
  897.                
  898.  
  899.         //Update the screen
  900.         if( SDL_Flip( screen ) == -1 )
  901.         {
  902.             return 1;
  903.         }
  904.  
  905.         //Cap the frame rate
  906.         if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  907.         {
  908.             SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  909.         }
  910.     }
  911.  
  912.     //Clean up
  913.     clean_up( tiles );
  914.  
  915.     return 0;
  916. }
Advertisement
Add Comment
Please, Sign In to add comment