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

Space shooter game - Norbo11 - SDL

By: a guest on Jan 1st, 2012  |  syntax: C++  |  size: 7.82 KB  |  views: 111  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "SDL/SDL.h"
  2. #include "SDL/SDL_image.h"
  3. #include "SDL/SDL_ttf.h"
  4. #include "SDL/SDL_mixer.h"
  5. #include <string>
  6. #include <sstream>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. //constants
  12. const int screenWidth = 1280;
  13. const int screenHeight = 720;
  14. const int screenBPP = 32;
  15.  
  16. const int playerSpeed = 3;
  17. const int enemySpeed = 3;
  18. const int laserSpeed = 6;
  19.  
  20. SDL_Event event;
  21.  
  22. //surfaces
  23. SDL_Surface *spaceship = NULL;
  24. SDL_Surface *enemyBig = NULL;
  25. SDL_Surface *enemySmall = NULL;
  26. SDL_Surface *laserBlue = NULL;
  27. SDL_Surface *laserRed = NULL;
  28. SDL_Surface *menuBG = NULL;
  29. SDL_Surface *gameBG = NULL;
  30.  
  31. SDL_Surface *screen = NULL;
  32.  
  33. //music
  34. Mix_Music *gameMusic = NULL;
  35. Mix_Music *menuMusic = NULL;
  36. Mix_Music *deathMusic = NULL;
  37. Mix_Chunk *laser1 = NULL;
  38. Mix_Chunk *laser2 = NULL;
  39.  
  40. //fonts
  41. TTF_Font *font = NULL;
  42. SDL_Color textColor = { 255, 255, 255 };
  43.  
  44. //variables
  45. bool quit;
  46. int state = 1; // 0 = MENU, 1 = GAME, 2= DEATH
  47. std::ofstream logger( "log.txt" );
  48.  
  49. //====================================================================
  50. //CLASSES
  51. //====================================================================
  52.  
  53. class Laser
  54. {
  55.     private:
  56.     int x;
  57.     int y;
  58.     int xVel;
  59.     int yVel;
  60.     int width;
  61.     int height;
  62.     int countX;
  63.     int countY;
  64.  
  65.     public:
  66.     bool visible;
  67.     Laser();
  68.     void handleLogic(int playerX, int playerY, int playerWidth);
  69.     void render();
  70. };
  71.  
  72. class Player
  73. {
  74.     private:
  75.     int xVel;
  76.     int yVel;
  77.     int height;
  78.  
  79.     public:
  80.     int x;
  81.     int y;
  82.     int width;
  83.     Player();
  84.     void handleInput(Laser laser);
  85.     void handleLogic();
  86.     void render();
  87. };
  88.  
  89. //====================================================================
  90. //FUNCTIONS
  91. //====================================================================
  92.  
  93. void log( std::string message )
  94. {
  95.     //Write message to file
  96.     logger << message << std::endl;
  97.     //Flush the buffer
  98.     logger.flush();
  99. }
  100.  
  101. SDL_Surface *loadImage(std::string filename)
  102. {
  103.     SDL_Surface* loadedImage = NULL;
  104.     SDL_Surface* optimizedImage = NULL;
  105.  
  106.     loadedImage = IMG_Load(filename.c_str());
  107.     if(loadedImage != NULL)
  108.     {
  109.         optimizedImage = SDL_DisplayFormatAlpha(loadedImage);
  110.         SDL_FreeSurface(loadedImage);
  111.     }
  112.     log("Loaded image "+filename);
  113.     return optimizedImage;
  114. }
  115.  
  116. void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
  117. {
  118.     SDL_Rect offset;
  119.     offset.x = x;
  120.     offset.y = y;
  121.     SDL_BlitSurface(source, NULL, destination, &offset);
  122. }
  123.  
  124. bool loadResources()
  125. {
  126.     log("Loading Resources...");
  127.     spaceship = loadImage("art/spaceship.png");
  128.     enemyBig = loadImage("art/enemy_big.png");
  129.     enemySmall = loadImage("art/enemy_small.png");
  130.     laserRed = loadImage("art/laserRed.png");
  131.     laserBlue = loadImage("art/laserBlue.png");
  132.     menuBG = loadImage("art/menu_bg.png");
  133.     gameBG = loadImage("art/space_bg.png");
  134.  
  135.     font = TTF_OpenFont("art/Fh_Space.ttf",25);
  136.     if (font == NULL)
  137.     {
  138.         return false;
  139.     }
  140.  
  141.     gameMusic = Mix_LoadMUS("sound/music_game.mp3");
  142.     menuMusic = Mix_LoadMUS("sound/music_menu.ogg");
  143.     deathMusic = Mix_LoadMUS("sound/music_death.mp3");
  144.     laser1 = Mix_LoadWAV("sound/laser1.wav");
  145.     laser2 = Mix_LoadWAV("sound/laser1.wav");
  146.     log("Loaded Resources.");
  147.     return true;
  148. }
  149.  
  150. bool startUp()
  151. {
  152.     log("Initializing everything...");
  153.      if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
  154.      {
  155.         return false;
  156.      }
  157.      if (TTF_Init() == -1)
  158.      {
  159.         return false;
  160.      }
  161.      if (IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF) == -1)
  162.      {
  163.          return false;
  164.      }
  165.     screen = SDL_SetVideoMode(screenWidth,screenHeight,screenBPP,SDL_SWSURFACE);
  166.     if (screen == NULL)
  167.     {
  168.         return false;
  169.     }
  170.     if (Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096 ) == -1)
  171.     {
  172.         return false;
  173.     }
  174.     SDL_WM_SetCaption("Space Norbos",NULL);
  175.     if (loadResources() == false)
  176.     {
  177.         return false;
  178.     }
  179.     quit = false;
  180.     log("Done initializing.");
  181.     return true;
  182. }
  183.  
  184. void cleanUp()
  185. {
  186.     log("Cleaning up...");
  187.     SDL_FreeSurface(gameBG);
  188.     SDL_FreeSurface(menuBG);
  189.     SDL_FreeSurface(spaceship);
  190.     SDL_FreeSurface(enemyBig);
  191.     SDL_FreeSurface(enemySmall);
  192.     SDL_FreeSurface(laserBlue);
  193.     SDL_FreeSurface(laserRed);
  194.     TTF_CloseFont(font);
  195.     Mix_FreeMusic(gameMusic);
  196.     Mix_FreeMusic(menuMusic);
  197.     Mix_FreeMusic(deathMusic);
  198.     Mix_FreeChunk(laser1);
  199.     Mix_FreeChunk(laser2);
  200.     TTF_Quit();
  201.     IMG_Quit();
  202.     Mix_Quit();
  203.     SDL_Quit();
  204.     log("Cleaned up.");
  205. }
  206.  
  207. void shoot(Laser laser)
  208. {
  209.     log("Shooting!");
  210.     laser.visible = true;
  211. }
  212.  
  213. //====================================================================
  214. //CLASS FUNCTIONS
  215. //====================================================================
  216.  
  217. //PLAYER======================================
  218.  
  219. Player::Player()
  220. {
  221.     x = (screenWidth/2)-(width/2);
  222.     y = 656;
  223.     xVel = 0;
  224.     yVel = 0;
  225.     width = 56;
  226.     height = 54;
  227. }
  228.  
  229. void Player::handleInput(Laser laser)
  230. {
  231.     if (event.type == SDL_KEYDOWN)
  232.     {
  233.         switch (event.key.keysym.sym)
  234.         {
  235.             case SDLK_LEFT: xVel -= playerSpeed; break;
  236.             case SDLK_RIGHT: xVel += playerSpeed; break;
  237.             case SDLK_SPACE: laser.visible = true;
  238.         }
  239.     }
  240.     else if( event.type == SDL_KEYUP )
  241.     {
  242.         //Adjust the velocity
  243.         switch( event.key.keysym.sym )
  244.         {
  245.             case SDLK_LEFT: xVel += playerSpeed; break;
  246.             case SDLK_RIGHT: xVel -= playerSpeed; break;
  247.         }
  248.     }
  249. }
  250.  
  251. void Player::handleLogic()
  252. {
  253.     x = x + xVel;
  254.     if (x <= 0)
  255.     {
  256.         x = 0;
  257.     }
  258.     if (x + width > screenWidth)
  259.     {
  260.         x = screenWidth - width;
  261.     }
  262.     y = y + yVel;
  263.     if (y <= 0)
  264.     {
  265.         y = 0;
  266.     }
  267.     if (y + height > screenHeight)
  268.     {
  269.         y = screenHeight - height;
  270.     }
  271. }
  272.  
  273. void Player::render()
  274. {
  275.     applySurface(x,y,spaceship,screen);
  276. }
  277.  
  278. //LASER======================================
  279.  
  280. Laser::Laser()
  281. {
  282.     x = 0;
  283.     y = 0;
  284.     xVel = 0;
  285.     yVel = (laserSpeed);
  286.     width = 10;
  287.     height = 50;
  288.     countX = 0;
  289.     countY = 0;
  290.     visible = false;
  291. }
  292.  
  293. void Laser::handleLogic(int playerX, int playerY, int playerWidth)
  294. {
  295.     if (visible == true)
  296.     {
  297.         if (countY == 0)
  298.         {
  299.             y = playerY - height - 2;
  300.             countY += 1;
  301.         }
  302.         if (countX == 0)
  303.         {
  304.             x = playerX + (playerWidth/2)- (width/2);
  305.             countX += 1;
  306.         }
  307.         y -= laserSpeed;
  308.         if (y = 0)
  309.         {
  310.             visible = false;
  311.         }
  312.     }
  313. }
  314.  
  315. void Laser::render()
  316. {
  317.     if (visible == true)
  318.     {
  319.         applySurface(x,y,laserBlue,screen);
  320.     }
  321. }
  322.  
  323. //====================================================================
  324. //MAIN
  325. //====================================================================
  326.  
  327. int main(int argc, char* args[])
  328. {
  329.     if (startUp() == false)
  330.     {
  331.         return 1;
  332.     }
  333.     Player player;
  334.     Laser laser;
  335.     while (quit == false)
  336.     {
  337.         SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
  338.         if (state == 1)
  339.         {
  340.             applySurface(0,0,gameBG,screen);
  341.             while (SDL_PollEvent(&event))
  342.             {
  343.                 player.handleInput(laser);
  344.                 switch (event.type)
  345.                 {
  346.                     case SDL_QUIT: quit = true; break;
  347.                 }
  348.             }
  349.             player.handleLogic();
  350.             player.render();
  351.             laser.handleLogic(player.x,player.x,player.width);
  352.             laser.render();
  353.             if (SDL_Flip(screen) == -1)
  354.             {
  355.                 return 1;
  356.             }
  357.         }
  358.     }
  359.     cleanUp();
  360.     return 0;
  361. }