piexil

Start of projectiles

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