piexil

Bullet Hell Pre-pre-pre-pre-alpha

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