ZeroApoc

Updated Space Shooter

Apr 30th, 2011
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.29 KB | None | 0 0
  1.  
  2. //I fixed my original code in this paste
  3. /*
  4. I am still learning how to do a few things and game loops themselves. Now that I got this mostly working I will be starting fresh with what I learned with this one. My TODO list will carry over and extend, as well.
  5. */
  6.  
  7. //main.cpp
  8.  
  9. #include "CEvent.h"
  10.  
  11. int main ( int argc, char* argv[] )
  12. {
  13.     CEvent event;
  14.     event.Load();
  15.     event.Init();
  16.     while(event.State())
  17.     {
  18.         event.Input();
  19.         event.Draw();
  20.         event.Update();
  21.     }
  22.     event.Cleanup();
  23.     return 0;
  24. }
  25.  
  26. //TODO: Put pictures on a working dialog                                -DONE
  27. //TODO: Make picture move                                               -DONE
  28. //TODO: Make sprite disappear an reappear on command                    -DONE
  29. //TODO: Make invisible sprite reappear at 0,0 after reappearance        -IN PROGRESS
  30. //TODO: Addendum: Keep sprite in dialog                                 -DONE
  31. //TODO: Eat food                                                        -DONE COUNTLESS TIMES
  32. //TODO: Take asprin                                                     -AVOIDED FOR NOW...BARELY
  33. //TODO: Figure out that most alien of concepts(Game Loop)               -IN PROGRESS
  34. //TODO: Add collision with other objects                                -IN PROGRESS (Considering Asprin)
  35. //TODO: Make fancy lasers that start at one sprite and end at another   -NOT STARTED
  36. //TODO: Make laser hurt a sprite                                        -NOT STARTED
  37. //TODO: Think of more to do                                             -PREFER NOT TO NEED ASPRIN YET
  38.  
  39.  
  40. /*//////////////////////////////////////////////////////////////////////////////////////*/
  41.  
  42. CEvent.h
  43.  
  44. #include "GameData.h"
  45.  
  46. class CEvent
  47. {
  48. public:
  49.     void Load();
  50.     void Init();
  51.     void Input();
  52.     void Draw();
  53.     void Update();
  54.     void Cleanup();
  55.     bool State();
  56.     //I have no clue as to wether or not these next 3 are in the correct location
  57.     void Apply_Surface(int x, int y, SDL_Surface * source, SDL_Surface * destination);
  58.     void Draw_Sprite (int srcx, int srcy, int dstx, int dsty, int width, int height, SDL_Surface *source, SDL_Surface *destination);
  59.     void Collision(int Vertical_Axis_1, int Horizontal_Axis_1, int Vertical_Axis_2, int Horizontal_Axis_2);
  60. private:
  61.     SDL_Surface *screen;
  62.     bool running;
  63.     bool Collided;
  64. };
  65.  
  66. void CEvent::Load()
  67. {
  68.     //It said load...so I loaded sprites: I consider the background to be a sprite
  69.     background = SDL_LoadBMP("starfield.bmp");
  70.     sprite = SDL_LoadBMP("ship1.bmp");
  71.     //SDL_Init(SDL_INIT_EVERYTHING);
  72. }
  73.  
  74. void CEvent::Init()
  75. {
  76.     running = true;
  77.     //It has INIT in it...you guessed it. I put it in Init()
  78.     SDL_Init(SDL_INIT_EVERYTHING);
  79.     //I figured the main window should be an important part of the INIT process
  80.     screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME);
  81.     //Goes with the window, in my confused mind
  82.     SDL_WM_SetCaption("YouTube Tut 01", NULL);
  83.     Player_Exists = true;
  84. }
  85.  
  86. void CEvent::Input()
  87. {
  88.     if(SDL_PollEvent(&event))
  89.     {
  90.         //The scary red x
  91.         if(event.type == SDL_QUIT)
  92.         {
  93.             running = false;
  94.         }
  95.         if(event.type == SDL_KEYDOWN)
  96.         {
  97.             keys[event.key.keysym.sym] = true;
  98.         }
  99.         if(event.type == SDL_KEYUP)
  100.         {
  101.             keys[event.key.keysym.sym] = false;
  102.         }
  103.     }
  104.     //using key to exit rather than the red x. I dislike using the red x
  105.     if(keys[SDLK_q])
  106.     {
  107.         running = false;
  108.     }
  109.     //..........................Fun with keys..........................
  110.     //Put center of sprite in center of screen. I couldn't believe it worked perfectly on my first attempt. Woo! Hoo!
  111.     if(keys[SDLK_1])
  112.     {
  113.         dsty = ((SCREEN_HEIGHT/2) - (PLAYER_HEIGHT/2));
  114.         dstx = ((SCREEN_WIDTH/2) - (PLAYER_WIDTH/2));
  115.     }
  116.     //Trying and failing to remove sprite: Game crashes, unfortunatly
  117.     if(keys[SDLK_5])
  118.     {
  119.         Player_Exists = false;
  120.     }
  121.     if(keys[SDLK_6])
  122.     {
  123.         Player_Exists = true;
  124.     }
  125.     if(keys[SDLK_0])
  126.     {
  127.         dsty = 0;
  128.         dstx = 0;
  129.     }
  130.     if(keys[SDLK_HOME])
  131.     {
  132.         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME);
  133.     }
  134.     if(keys[SDLK_END])
  135.     {
  136.         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME | SDL_FULLSCREEN);
  137.     }
  138.     if(keys[SDLK_UP])
  139.     {
  140.         if(dsty > 1)
  141.         {
  142.             dsty -= 5;
  143.         }
  144.     }
  145.     if(keys[SDLK_DOWN])
  146.     {
  147.         //Still has an empty pixel on the bottom. Is it SDL's fault? Probably not.
  148.         if(dsty < (SCREEN_HEIGHT - (PLAYER_HEIGHT/* + 1*/)))
  149.         {
  150.             dsty += 5;
  151.         }
  152.     }
  153.     if(keys[SDLK_LEFT])
  154.     {
  155.         if(dstx > 0)
  156.         {
  157.             dstx -= 5;
  158.         }
  159.     }
  160.     if(keys[SDLK_RIGHT])
  161.     {
  162.         //Same empty pixel problem as SLDK_DOWN
  163.         if(dstx < (SCREEN_WIDTH - (PLAYER_WIDTH/* + 1*/)))
  164.         {
  165.             dstx += 5;
  166.         }
  167.     }
  168. }
  169.  
  170. void CEvent::Draw()
  171. {
  172.     Apply_Surface(0, 0, background, screen);
  173.     //if(Player_Exists)
  174.     //{
  175.     Draw_Sprite(srcx, srcy, dstx, dsty, PLAYER_WIDTH, PLAYER_HEIGHT, sprite, screen);
  176.     //}
  177.     /*if(!Player_Exists)
  178.     {
  179.  
  180.         SDL_FreeSurface(sprite);
  181.     }*/
  182.  
  183. }
  184.  
  185. void CEvent::Update()
  186. {
  187.     SDL_Flip(screen);
  188. }
  189.  
  190. void CEvent::Cleanup()
  191. {
  192.     SDL_FreeSurface(sprite);
  193.     SDL_FreeSurface(background);
  194.     SDL_Quit();
  195.     exit(0);
  196. }
  197.  
  198. bool CEvent::State()
  199. {
  200.     return running;
  201. }
  202.  
  203. void CEvent::Apply_Surface(int x, int y, SDL_Surface * source, SDL_Surface * destination)
  204. {
  205.     SDL_Rect rect;
  206.     rect.x = x;
  207.     rect.y = y;
  208.  
  209.     SDL_BlitSurface(source, NULL, destination, &rect);
  210. }
  211. void CEvent::Draw_Sprite (int srcx, int srcy, int dstx, int dsty, int width, int height, SDL_Surface *source, SDL_Surface *destination)
  212. {
  213.     if(Player_Exists)
  214.     {
  215.         SDL_SetColorKey(source, SDL_SRCCOLORKEY, SDL_MapRGB(source->format, 255, 255, 255));
  216.  
  217.         SDL_Rect src;
  218.         src.x = srcx;
  219.         src.y = srcy;
  220.         src.w = width;
  221.         src.h = height;
  222.  
  223.         SDL_Rect dst;
  224.         dst.x = dstx;
  225.         dst.y = dsty;
  226.         dst.w = width;
  227.         dst.h = height;
  228.  
  229.         SDL_BlitSurface(source, &src, destination, &dst);
  230.     }
  231. }
  232. //This function is a work in progress - Tread with care
  233. void CEvent::Collision(int Vertical_Axis_1, int Horizontal_Axis_1, int Vertical_Axis_2, int Horizontal_Axis_2)
  234. {
  235.     if(Vertical_Axis_1 >= Vertical_Axis_2 && (Vertical_Axis_1 + PLAYER_WIDTH) <= (Vertical_Axis_2 + PLAYER_WIDTH))
  236.     {
  237.         if(Horizontal_Axis_1 >= Horizontal_Axis_2 && (Horizontal_Axis_1 + PLAYER_HEIGHT) <= (Horizontal_Axis_2 + PLAYER_HEIGHT))
  238.         {
  239.             Collided = true;
  240.         }
  241.     }
  242. }
  243.  
  244. /*////////////////////////////////////////////////////////////////////////////////////////////*/
  245.  
  246. GameData.h
  247.  
  248. #include <SDL.h>
  249.  
  250. //Good enough for now...Like a little control panel
  251. int SCREEN_WIDTH = 800;//For debugging purposes
  252. int SCREEN_HEIGHT = 600;//See above
  253. int BPP = 32;//Seems like a good number
  254. int PLAYER_WIDTH = 20;//Size of sprite in use
  255. int PLAYER_HEIGHT = 20;//See above
  256. int srcx = 0;//12;
  257. int srcy = 0;//12;
  258. int dstx = 0;
  259. int dsty = 0;
  260. bool keys[323] = {false};//Done by a tutorial creator. Yes, I'm clueless
  261. SDL_Event event;//Seems like a popular name
  262. SDL_Surface *background = NULL;//Gotta have some flavor: Placeholder
  263. SDL_Surface *sprite = NULL;//A two minute toothless space fighter: Placeholder
  264. bool Player_Exists;
  265.  
  266. /*///////////////////////////////////////////////////////////////////////////////////////////*/
  267.  
  268. CCollisionDetection.h
  269.  
  270. #ifndef CCOLLISIONDETECTION_H
  271. #define CCOLLISIONDETECTION_H
  272.  
  273. //Not started yet
  274. class CCollisionDetection
  275. {
  276.     public:
  277.         CCollisionDetection();
  278.         virtual ~CCollisionDetection();
  279.     protected:
  280.     private:
  281. };
  282.  
  283. #endif // CCOLLISIONDETECTION_H
  284.  
  285. /*///////////////////////////////////////////////////////////////////////////////////////////*/
  286.  
  287. CCollisionDetection.cpp
  288.  
  289. #include "CCollisionDetection.h"
  290.  
  291. //Not Started yet
  292. CCollisionDetection::CCollisionDetection()
  293. {
  294.     //ctor
  295. }
  296.  
  297. CCollisionDetection::~CCollisionDetection()
  298. {
  299.     //dtor
  300. }
Advertisement
Add Comment
Please, Sign In to add comment