Advertisement
Guest User

C++ Fatal errors. :(

a guest
Aug 6th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.22 KB | None | 0 0
  1. #include"functions.h"
  2. using namespace std;
  3.  
  4. class Projectile;
  5. vector<Projectile> AllProjectiles;
  6. static bool quit = false;
  7. SDL_Event event;
  8. SDL_Surface* screen = NULL;
  9. TTF_Font* font = NULL;
  10. SDL_Rect backgroundStats = {0, 0, 800, 475};
  11. int score = 0;
  12.  
  13. enum ProjectileType
  14. {
  15.      Lazer = 2,
  16.      Rocket = 5,
  17. };
  18.  
  19. enum Direction
  20. {
  21.      LEFT = 1,
  22.      RIGHT
  23. };
  24.  
  25. class Projectile
  26. {
  27.       private:
  28.      
  29.       int personalID;
  30.      
  31.       static int ID;
  32.      
  33.       int x, y, xVel, damage, frame, maxFrame;
  34.      
  35.       ProjectileType type;
  36.    
  37.       SDL_Surface* sprite;
  38.      
  39.       SDL_Rect* animLeft;
  40.      
  41.       SDL_Rect* animRight;
  42.      
  43.       void defineAnim();
  44.      
  45.       Direction direction;
  46.      
  47.       public:
  48.              
  49.       Projectile(ProjectileType Type, int X, int Y, int XVel);
  50.      
  51.       Projectile(const Projectile& proj);
  52.      
  53.       ~Projectile();
  54.      
  55.       void Move();
  56.      
  57.       void HandleAnimation();
  58.      
  59.       void Show();
  60.      
  61.       int getID(){ return personalID; }
  62.      
  63.       int getObjCount(){ return ID; }
  64.      
  65.       static void removeObjCount(){ ID--; }
  66.      
  67.       SDL_Rect getRect();
  68.  
  69. };
  70. int Projectile::ID = 0;
  71.  
  72. SDL_Rect Projectile::getRect()
  73. {
  74.          SDL_Rect temp = {x, y, 94, 20};
  75.          return temp;
  76. }
  77.  
  78. Projectile::Projectile(ProjectileType Type, int X, int Y, int XVel):
  79.  
  80. x(X),
  81. y(Y),
  82. xVel(XVel),
  83. type(Type),
  84. damage(Type * 10),
  85. personalID(ID++),
  86. frame(0)
  87.  
  88. {
  89.            
  90.             if(xVel)
  91.             {
  92.                     direction = RIGHT;
  93.             }
  94.            
  95.             else
  96.             {
  97.                 direction = LEFT;
  98.             }
  99.            
  100.                    
  101.             if(Type == Lazer)
  102.             {
  103.                     sprite = loadImage("Lazer.png");
  104.                     maxFrame = 1;
  105.                    
  106.             }
  107.            
  108.             else if(Type == Rocket)
  109.             {
  110.                     sprite = loadImage("Rocket.png");
  111.                     maxFrame = 4;
  112.             }
  113.            
  114.             animLeft = new SDL_Rect[maxFrame];
  115.             animRight = new SDL_Rect[maxFrame];
  116.            
  117.             defineAnim();
  118. }
  119.  
  120. Projectile::Projectile(const Projectile& proj)
  121. {
  122.        this->x = proj.x;
  123.        this->y = proj.y;
  124.        this->xVel = proj.xVel;
  125.        this->damage = proj.damage;
  126.        this->frame = proj.frame;                      
  127.        this->type = proj.type;
  128.        this->direction = proj.direction;
  129.      
  130.        displayTTF("COPY CONSTRUCTPOR!", 255, 255, 255);
  131.        SDL_Flip(screen);
  132.        SDL_Delay(2000);
  133.        
  134.       if(type == Lazer)
  135.       {
  136.               sprite = loadImage("Lazer.png");
  137.               maxFrame = 1;
  138.       }
  139.      
  140.       else if(type == Rocket)
  141.       {
  142.                     sprite = loadImage("Rocket.png");
  143.                     maxFrame = 4;
  144.       }
  145.      
  146.       animLeft = new SDL_Rect[maxFrame];
  147.       animRight = new SDL_Rect[maxFrame];
  148.            
  149.       defineAnim();
  150. }
  151.  
  152. void Projectile::Move()
  153. {
  154.      x += xVel;
  155.      
  156.      if(xVel)
  157.      {
  158.              direction = RIGHT;
  159.      }
  160.      
  161.      else if(xVel < 0)
  162.      {
  163.           direction = LEFT;
  164.      }
  165. }
  166.  
  167. void Projectile::Show()
  168. {
  169.      if(xVel < 0)
  170.      {
  171.              blitSurface(x, y, sprite, &animLeft[frame]);
  172.      }
  173.      
  174.      else if(xVel > 0)
  175.      {
  176.              blitSurface(x, y, sprite, &animRight[frame]);
  177.      }
  178. }
  179.  
  180. void Projectile::HandleAnimation()
  181. {
  182.      frame = (SDL_GetTicks() / 150) % maxFrame;
  183.      
  184.      if(frame > maxFrame)
  185.      {
  186.               frame = 1;
  187.      }
  188. }
  189.  
  190. void Projectile::defineAnim()
  191. {
  192.    if(type == Lazer)
  193.    {  
  194.        if(xVel < 0)
  195.        {
  196.                animLeft[0].x = 0;      animLeft[1].x = 0;      animLeft[2].x = 0;      animLeft[3].x = 0;
  197.                animLeft[0].y = 0;      animLeft[1].y = 0;      animLeft[2].y = 0;      animLeft[3].y = 0;          
  198.                animLeft[0].w = 20;     animLeft[1].w = 20;     animLeft[2].w = 20;     animLeft[3].w = 20;          
  199.                animLeft[0].h = 6;      animLeft[1].h = 6;      animLeft[2].h = 6;      animLeft[3].h = 6;                        
  200.        }
  201.      
  202.        else if(xVel > 0)
  203.        {
  204.                animRight[0].x = 0;      animRight[1].x = 0;      animRight[2].x = 0;      animRight[3].x = 0;
  205.                animRight[0].y = 0;      animRight[1].y = 0;      animRight[2].y = 0;      animRight[3].y = 0;          
  206.                animRight[0].w = 20;     animRight[1].w = 20;     animRight[2].w = 20;     animRight[3].w = 20;          
  207.                animRight[0].h = 6;      animRight[1].h = 6;      animRight[2].h = 6;      animRight[3].h = 6;      
  208.        }
  209.    }
  210.    
  211.    else if(type == Rocket)
  212.    {
  213.        if(xVel < 0)
  214.        {
  215.                animLeft[0].x = 96;     animLeft[1].x = 96;     animLeft[2].x = 96;     animLeft[3].x = 96;
  216.                animLeft[0].y = 1;      animLeft[1].y = 25;     animLeft[2].y = 48;     animLeft[3].y = 71;          
  217.                animLeft[0].w = 94;     animLeft[1].w = 94;     animLeft[2].w = 94;     animLeft[3].w = 94;          
  218.                animLeft[0].h = 20;     animLeft[1].h = 20;     animLeft[2].h = 20;     animLeft[3].h = 20;                      
  219.        }
  220.      
  221.        else if(xVel > 0)
  222.        {
  223.                animRight[0].x = 2;      animRight[1].x = 2;      animRight[2].x = 2;      animRight[3].x = 2;
  224.                animRight[0].y = 1;      animRight[1].y = 25;     animRight[2].y = 48;     animRight[3].y = 71;          
  225.                animRight[0].w = 94;     animRight[1].w = 94;     animRight[2].w = 94;     animRight[3].w = 94;          
  226.                animRight[0].h = 20;     animRight[1].h = 20;     animRight[2].h = 20;     animRight[3].h = 20;    
  227.        }
  228.    }
  229. }
  230.  
  231.  
  232. Projectile::~Projectile()
  233. {
  234.                          SDL_FreeSurface(sprite);
  235.                          
  236.                          delete[] animLeft;
  237.                          delete[] animRight;
  238.                          
  239.                          animLeft = NULL;
  240.                          animRight = NULL;
  241. }
  242.  
  243. void SpawnProjectile(ProjectileType type, int x, int y, int xVel)
  244. {
  245.          Projectile proj(type, x, y, xVel);
  246.          AllProjectiles.push_back(proj);
  247. }
  248.  
  249. class Player
  250. {
  251.       private:
  252.              
  253.       int x, y, w, h, yVel, health;
  254.      
  255.       SDL_Surface* sprite;
  256.      
  257.       ProjectileType type;
  258.      
  259.       public:
  260.                  
  261.       Player(int X, int Y, int W, int H);
  262.      
  263.       ~Player();
  264.      
  265.       void handleEvents();
  266.      
  267.       void move();
  268.      
  269.       void show();
  270.      
  271.       SDL_Rect getRect();
  272. };
  273.  
  274. SDL_Rect Player::getRect()
  275. {
  276.          SDL_Rect temp = {x, y, w, h};
  277.          return temp;
  278. }
  279.  
  280. Player::Player(int X, int Y, int W, int H):
  281.  
  282. x(X),
  283. y(Y),
  284. w(W),
  285. h(H),
  286. yVel(0),
  287. health(100)
  288.  
  289. {
  290.     sprite = loadImage("Superman.png");
  291.     type = Lazer;
  292. }
  293.  
  294. Player::~Player()
  295. {
  296.                  SDL_FreeSurface(sprite);
  297. }
  298.  
  299. void Player::handleEvents()
  300. {
  301.     if(event.type == SDL_KEYDOWN)
  302.     {
  303.         switch(event.key.keysym.sym)
  304.         {
  305.                case(SDLK_w): yVel--; break;  
  306.                case(SDLK_s): yVel++; break;
  307.                case(SDLK_r): if(type == 2){ type = Rocket; } else{ type = Lazer; } break;
  308.         }
  309.     }
  310.    
  311.     else if(event.type == SDL_KEYUP)
  312.     {
  313.         switch(event.key.keysym.sym)
  314.         {
  315.                case(SDLK_w): yVel++; break;  
  316.                case(SDLK_s): yVel--; break;
  317.         }
  318.     }
  319.    
  320.     static int lastClicked = SDL_GetTicks();
  321.    
  322.     if(event.type == SDL_MOUSEBUTTONDOWN)
  323.     {
  324.                   if(event.button.button == SDL_BUTTON_LEFT)
  325.                   {                      
  326.                       if(SDL_GetTicks() - lastClicked >= (type * 1000))
  327.                       {      
  328.                            SpawnProjectile(type, x + 100, y, 1);
  329.                            lastClicked = SDL_GetTicks();
  330.                       }          
  331.                   }
  332.     }
  333. }
  334.  
  335. void Player::move()
  336. {
  337.      y += yVel;
  338.      
  339.      if((y < 0) || (y + h > (SCREEN_HEIGHT - 50)))
  340.      {
  341.            y -= yVel;
  342.      }
  343. }
  344.  
  345. void Player::show()
  346. {
  347.      blitSurface(x, y, sprite);
  348.      
  349.      displayTTF("Weapon: ", 0, SCREEN_HEIGHT - 25);
  350.      
  351.      if(type == Lazer)
  352.      {
  353.              displayTTF("Lazer beams", 90, SCREEN_HEIGHT - 25);
  354.      }
  355.      
  356.      if(type == Rocket)
  357.      {
  358.              displayTTF("Rockets", 90, SCREEN_HEIGHT - 25);
  359.      }
  360.      
  361.      displayTTF("Health:", 230, SCREEN_HEIGHT - 25);
  362.      
  363.      if(health >= 80)
  364.      {
  365.                displayTTF(health, 310, SCREEN_HEIGHT - 25, 0, 255, 0);
  366.      }
  367.      
  368.      if(health < 80 && health > 60)
  369.      {
  370.                 displayTTF(health, 310, SCREEN_HEIGHT - 25, 128, 255, 0);
  371.      }
  372.      
  373.      if(health < 60 && health > 40)
  374.      {
  375.                 displayTTF(health, 310, SCREEN_HEIGHT - 25, 255, 128, 0);
  376.      }
  377.      
  378.      if(health < 40 && health > 20)
  379.      {
  380.             displayTTF(health, 310, SCREEN_HEIGHT - 25, 244, 80, 0);
  381.      }
  382.      
  383.      if(health < 20)
  384.      {
  385.                 displayTTF(health, 310, SCREEN_HEIGHT - 25, 255, 0, 0);
  386.      }
  387. }
  388.  
  389. int main(int argv, char* argc[])
  390. {
  391.     register vector<Projectile>::iterator it;
  392.    
  393.     if(init("Space Game!"))
  394.     {
  395.                    return 1;
  396.     }
  397.    
  398.     SDL_Surface* background = loadImage("Background.jpg");
  399.    
  400.     Player one(15, 50, 156, 47);
  401.    
  402.     while(!quit)
  403.     {
  404.                 while(SDL_PollEvent(&event))
  405.                 {
  406.                      if(event.type == SDL_QUIT)
  407.                      {
  408.                                    quit = true;
  409.                      }
  410.                      
  411.                      one.handleEvents();
  412.                 }
  413.                
  414.                 clearScreen();
  415.  
  416.                 backgroundStats.x--;
  417.                
  418.                 if( backgroundStats.x <= -backgroundStats.w )
  419.                 {
  420.                      backgroundStats.x = 0;
  421.                 }
  422.  
  423.                 blitSurface(backgroundStats.x, backgroundStats.y, background);
  424.                 blitSurface(backgroundStats.x + backgroundStats.w, backgroundStats.y, background);
  425.                
  426.                 for(it = AllProjectiles.begin(); it != AllProjectiles.end(); it++)
  427.                 {
  428.                        it->Move();
  429.                        it->HandleAnimation();
  430.                        it->Show();
  431.                        
  432.                        SDL_Rect one = it->getRect();
  433.                        
  434.                        if(one.x < 0 || (one.x + one.w) > SCREEN_WIDTH)
  435.                        {
  436.                                 it = AllProjectiles.erase(it);
  437.                        }
  438.                        
  439.                        else
  440.                        {
  441.                            ++it;
  442.                        }
  443.                 }
  444.                
  445.                 one.move();
  446.                 one.show();
  447.                
  448.                 displayTTF("_______________________________________________________________________________", 0, SCREEN_HEIGHT - 50);
  449.                 displayTTF(it->getObjCount(), 370, SCREEN_HEIGHT - 25);
  450.                 displayTTF("Projectiles", 410, SCREEN_HEIGHT - 25);
  451.                 displayTTF("Score:", 530, SCREEN_HEIGHT - 25);
  452.                 displayTTF(score, 590, SCREEN_HEIGHT - 25);
  453.                
  454.                 SDL_Flip(screen);
  455.                 SDL_Delay(1);
  456.     }
  457.    
  458.     clearUp();
  459.    
  460.     return 0;
  461. }
  462.  
  463. //FUNCTIONS.H
  464.  
  465. #ifndef _functions_H_
  466. #define _functions_H_
  467.  
  468. #include"SDL/SDL.h"
  469. #include"SDL/SDL_mixer.h"
  470. #include"SDL/SDL_image.h"
  471. #include"SDL/SDL_ttf.h"
  472. #include<string>
  473. #include<vector>
  474. #include<cmath>
  475. #include<sstream>
  476.  
  477. const int SCREEN_WIDTH = 800;
  478. const int SCREEN_HEIGHT = 500;
  479. const int BPP = 32;
  480.  
  481. int init( std::string caption );
  482.  
  483. double calculateDistance( double x1, double x2, double y1, double y2 );
  484.  
  485. void blitSurface(int srcX, int srcY, SDL_Surface* source, SDL_Rect* clip = NULL);
  486.  
  487. void displayTTF( std::string text, int x, int y, int red = 0, int green = 0, int blue = 0);
  488.  
  489. void displayTTF( int num, int x, int y, int red = 0, int green = 0, int blue = 0);
  490.  
  491. int checkCollison( SDL_Rect* rect1, SDL_Rect* rect2 );
  492.  
  493. SDL_Surface* loadImage( std::string path );
  494.  
  495. void clearScreen();
  496.  
  497. void clearUp();
  498.  
  499. #endif
  500.  
  501. //FUNCTIONS.CPP
  502.  
  503.  
  504. #include"functions.h"
  505.  
  506. extern SDL_Event event;
  507. extern SDL_Surface* screen;
  508. extern TTF_Font* font;
  509.  
  510. int init( std::string caption )
  511. {
  512.     if( TTF_Init() == -1 )
  513.     {
  514.         return 1;
  515.     }
  516.    
  517.     font = TTF_OpenFont("arial.ttf", 20);
  518.    
  519.     if( SDL_Init(SDL_INIT_EVERYTHING) == -1 )
  520.     {
  521.         return 1;
  522.     }
  523.  
  524.     if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
  525.     {
  526.         return 1;
  527.     }
  528.    
  529.     if( (screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_SWSURFACE )) == NULL )
  530.     {
  531.         return 1;
  532.     }
  533.    
  534.     SDL_WM_SetCaption( caption.c_str(), NULL );
  535.    
  536.     return 0;
  537. }
  538.  
  539. double calculateDistance( double x1, double x2, double y1, double y2 )
  540. {
  541.     return sqrt( (x1 - x2)*(x1-x2) + (y2-y1)*(y2-y1) );
  542. }
  543.  
  544. void blitSurface(int srcX, int srcY, SDL_Surface* source, SDL_Rect* clip )
  545. {
  546.      SDL_Rect offsets;
  547.      offsets.x = srcX;
  548.      offsets.y = srcY;
  549.      
  550.      SDL_BlitSurface( source, clip, screen, &offsets );
  551. }
  552.  
  553. void displayTTF( std::string text, int x, int y, int red, int green, int blue)
  554. {
  555.      SDL_Color colour = { red, green, blue };
  556.      SDL_Surface* temp = TTF_RenderText_Solid( font, text.c_str(), colour );
  557.      blitSurface( x, y, temp );
  558.      
  559.      SDL_FreeSurface( temp );
  560.      temp = NULL;
  561. }
  562.  
  563. void displayTTF( int num, int x, int y, int red, int green, int blue)
  564. {
  565.       SDL_Color colour = { red, green, blue };
  566.      
  567.       std::stringstream stream;
  568.       stream << num;
  569.      
  570.       SDL_Surface* temp = TTF_RenderText_Solid( font, stream.str().c_str(), colour );
  571.      
  572.       blitSurface( x, y, temp );
  573.      
  574.       SDL_FreeSurface( temp );
  575.       temp = NULL;
  576. }
  577.  
  578. int checkCollison( SDL_Rect* rect1, SDL_Rect* rect2 )
  579. {
  580.      if(rect1->y >= rect2->y + rect2->h)
  581.         return 0;
  582.                
  583.      if(rect1->x >= rect2->x + rect2->w)
  584.         return 0;
  585.                
  586.      if(rect1->y + rect1->h <= rect2->y)
  587.         return 0;
  588.                
  589.      if(rect1->x + rect1->w <= rect2->x)
  590.         return 0;
  591.                
  592.      return 1;                    
  593. }
  594.  
  595. SDL_Surface* loadImage( std::string path )
  596. {                
  597.       SDL_Surface* loadedImage = NULL;
  598.      
  599.       SDL_Surface* optimizedImage = NULL;
  600.      
  601.       loadedImage = IMG_Load( path.c_str() );
  602.      
  603.       if( loadedImage != NULL )
  604.       {
  605.           optimizedImage = SDL_DisplayFormat( loadedImage );
  606.           SDL_FreeSurface( loadedImage );
  607.       }
  608.      
  609.        if( optimizedImage != NULL )
  610.        {
  611.            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0, 255 );
  612.            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
  613.        }
  614.        
  615.        return optimizedImage;
  616. }
  617.  
  618. void clearScreen()
  619. {
  620.       SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );
  621. }
  622.  
  623. void clearUp()
  624. {
  625.      TTF_CloseFont( font );
  626.      
  627.      SDL_Quit();
  628.      TTF_Quit();
  629.      Mix_CloseAudio();
  630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement