ZeroApoc

Learning C++ Space Shooter

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