Advertisement
Guest User

i have issues

a guest
Sep 1st, 2014
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.06 KB | None | 0 0
  1. /*  Frozelar's program thingy
  2.     don't steal plz             */
  3.  
  4. // libraries and external files that will be used
  5. #include <SDL.h>
  6. #include <SDL_image.h>
  7. #include <SDL_mixer.h>
  8. #include <SDL_ttf.h>
  9. #include <iostream>
  10. #include <string>
  11. #include <fstream>
  12. using namespace std;
  13.  
  14. // default dimensions
  15. const int WINDOW_W = 640;
  16. const int WINDOW_H = 480;
  17. const int LEVEL_W = 1280;
  18. const int LEVEL_H = 720;
  19.  
  20. // default properties
  21. const int DEFAULT_H = 64;
  22. const int DEFAULT_W = 64;
  23. const int DEFAULT_FRAMES = 4;
  24. const int DEFAULT_X = 0;
  25. const int DEFAULT_Y = 0;
  26. const int SPRITE_SHEET_DEFAULT = 4;
  27. const int DEFAULT_PLAYER_POS = 11;
  28.  
  29. void clearScreen(void);
  30. void updateScreen(void);
  31. int touchesThing(SDL_Rect, int**);
  32.  
  33. // possible types of events
  34. enum EventTypes{
  35.     PLAYER_EVENT, MENU_EVENT, WINDOW_EVENT
  36. };
  37.  
  38. // blocks
  39. const int TOTAL_BLOCKS = 64;
  40.  
  41. // contains the sprite sheet
  42. SDL_Rect *spriteSheet[SPRITE_SHEET_DEFAULT];
  43.  
  44. // contains possible sprite types
  45. enum block_types
  46. {
  47.     BLANK_BLOCK, SOLID_BLOCK, ENEMY_BLOCK, PLAYER_BLOCK, TOTAL_BLOCK_TYPES
  48. };
  49.  
  50. // enemies
  51. const int TOTAL_ENEMIES = 4;
  52.  
  53. class Camera
  54. {
  55. public:
  56.     // initialize everything
  57.     Camera(int x, int y, int w, int h);
  58.    
  59.     // free and unload everything
  60.     ~Camera();
  61.  
  62.     // SDL_Rect that represents the camera
  63.     SDL_Rect cmRect;
  64. };
  65.  
  66. class Texture
  67. {
  68. public:
  69.     // initialize texture & properties
  70.     Texture(int x, int y, int w, int h);
  71.  
  72.     // destroy texture, deallocate memory, etc
  73.     ~Texture();
  74.    
  75.     // load from file
  76.     bool loadF(string path);
  77.  
  78.     // render to screen
  79.     void render(SDL_Rect *clip, SDL_Point *center, double rotate, SDL_RendererFlip flip);
  80.  
  81.     // free memory and whatnot
  82.     void free();
  83.  
  84.     // set color using red, green, blue
  85.     void color(Uint8 r, Uint8 g, Uint8 b);
  86.  
  87.     // set transparency (aka alpha)
  88.     void alpha(Uint8 a);
  89.  
  90.     // set blend mode
  91.     void blend(SDL_BlendMode b);
  92.  
  93.     // if sdl_ttf, load from rendered text
  94. #ifdef _SDL_TTF_H
  95.     bool loadT(string text, SDL_Color color, TTF_Font* font);
  96. #endif
  97.  
  98.     // rect that represents the texture
  99.     SDL_Rect txRect;
  100.  
  101.     // points to the texture itself
  102.     SDL_Texture* txTexture;
  103. };
  104.  
  105. class Sprite
  106. {
  107. public:
  108.     // initialize everything
  109.     Sprite(int x, int y, int w, int h);
  110.  
  111.     // unload & deinitialize everything
  112.     virtual ~Sprite();
  113.  
  114.     // moves Sprite and checks collision (variable determines whether we're moving a player or enemy)
  115.     void move(int a);
  116.  
  117.     // kills the Sprite in-game
  118.     void die();
  119.  
  120.     // places Sprite in level
  121.     void set();
  122.  
  123.     // box that represents the Sprite
  124.     SDL_Rect spRect;
  125.  
  126.     // number of frames
  127.     int spFrames;
  128.  
  129.     // maximum velocity
  130.     const int spMaxVel = 10;
  131.  
  132.     // current velocity
  133.     int spXvel;
  134.     int spYvel;
  135.  
  136.     // the texture that is displayed
  137.     Texture *spTexture;
  138. };
  139.  
  140. class Player:public Sprite
  141. {
  142. public:
  143.     // initialize everything associated with the player
  144.     Player(int x, int y, int w, int h);
  145.  
  146.     // destroy and deallocate everything associated with the player
  147.     ~Player();
  148.  
  149.     // handles event(s), makes player jump, move, or die
  150.     void handleEvent(SDL_Event &e);
  151.  
  152.     // center camera over player
  153.     void centerCamera();
  154.  
  155.     // type
  156.     const Uint8 type = 3;
  157. };
  158.  
  159. class Enemy:public Sprite
  160. {
  161. public:
  162.     // initialize everything
  163.     Enemy(int x, int y, int w, int h, int type);
  164.  
  165.     // unload everything
  166.     ~Enemy();
  167.  
  168.     // type
  169.     const Uint8 type = 2;
  170.  
  171.     // type of enemy
  172.     int enType;
  173. };
  174.  
  175. class Background
  176. {
  177. public:
  178.     // initialize the background
  179.     Background(int x, int y, int w, int h);
  180.  
  181.     // unload the background
  182.     ~Background();
  183.  
  184.     // set up the background
  185.     void set();
  186.  
  187.     // unloads everything
  188.     void freebg();
  189.  
  190.     // rect that represents the background
  191.     SDL_Rect bgRect;
  192.  
  193.     // number of frames
  194.     int bgFrames;
  195.  
  196.     // points to the background image(s)
  197.     SDL_Rect *bgImages[DEFAULT_FRAMES];
  198.  
  199.     // background texture
  200.     Texture *bgTexture;
  201. };
  202.  
  203. class Block
  204. {
  205. public:
  206.     // initialize everything
  207.     Block(int x, int y, int w, int h, int type);
  208.  
  209.     // unload everything
  210.     ~Block();
  211.  
  212.     // type
  213.     const Uint8 type = 1;
  214.  
  215.     // rect that represents the block
  216.     SDL_Rect blRect;
  217.  
  218.     // the type of block
  219.     int blType;
  220.    
  221.     /*  CODE FOR BLOCK FRAMES
  222.     // the box that represents the block's frame(s)
  223.     SDL_Rect *cBox[DEFAULT_FRAMES];
  224.     */
  225.  
  226.     // number of frames
  227.     int blFrames;
  228.  
  229.     // the texture
  230.     Texture *blTexture;
  231. };
  232.  
  233. class Item
  234. {
  235.  
  236. };
  237.  
  238. class Menu
  239. {
  240.  
  241. };
  242.  
  243. // contains what should be placed where
  244. Block *blocks[TOTAL_BLOCKS];
  245. Enemy *enemies[TOTAL_BLOCKS];
  246. Player *playerPos[TOTAL_BLOCKS];
  247.  
  248. // window and renderer that draws to the window
  249. SDL_Window *gWindow = NULL;
  250. SDL_Renderer *gRenderer = NULL;
  251.  
  252. // player, camera, and background
  253. Player gPlayer(0, 0, 64, 64);
  254. Camera gCamera(0, 0, 64, 64);
  255. Background gBackground(0, 0, 64, 64);
  256.  
  257. Camera::Camera(int passedX = (WINDOW_W / 2 + LEVEL_W / 2), int passedY = (WINDOW_H + LEVEL_H / 2), int passedW = WINDOW_W, int passedH = WINDOW_H)
  258. {
  259.     // initialize variables
  260.     cmRect.x = passedX;
  261.     cmRect.y = passedY;
  262.     cmRect.w = passedW;
  263.     cmRect.h = passedH;
  264. }
  265.  
  266. Camera::~Camera()
  267. {
  268.     // UNNECESSARY???
  269. }
  270.  
  271. Texture::Texture(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
  272. {
  273.     // initialize texture pointer & variables
  274.     txTexture = NULL;
  275.     txRect.x = passedX;
  276.     txRect.y = passedY;
  277.     txRect.w = passedW;
  278.     txRect.h = passedH;
  279. }
  280.  
  281. Texture::~Texture()
  282. {
  283.     // free everything
  284.     free();
  285. }
  286.  
  287. Sprite::Sprite(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
  288. {
  289.     // initialize variables
  290.     spRect.x = passedX;
  291.     spRect.y = passedY;
  292.     spRect.w = passedW;
  293.     spRect.h = passedH;
  294.    
  295.     spXvel = 0;
  296.     spYvel = 0;
  297. }
  298.  
  299. Sprite::~Sprite()
  300. {
  301.     // free current texture that spriteTexture points to
  302.     spTexture->free();
  303.  
  304.     // unload texture
  305.     spTexture = NULL;
  306. }
  307.  
  308. Player::Player(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
  309. {
  310.     // initialize variables
  311.     spRect.x = passedX;
  312.     spRect.y = passedY;
  313.     spRect.w = passedW;
  314.     spRect.h = passedH;
  315.     spFrames = 4;
  316.  
  317.     // load the player's texture
  318.     spTexture->loadF("resources/cubeman.png");
  319.    
  320.     // center the camera around the player
  321.     centerCamera();
  322. }
  323.  
  324. Player::~Player()
  325. {
  326.     // UNNECESSARY??
  327. }
  328.  
  329. Enemy::Enemy(int x = DEFAULT_X, int y = DEFAULT_Y, int w = DEFAULT_W, int h = DEFAULT_H, int pType = NULL)
  330. {
  331.     // initialize variables
  332.     spRect.x = x;
  333.     spRect.y = y;
  334.     spRect.w = w;
  335.     spRect.h = h;
  336.     enType = pType;
  337.  
  338.     // load enemy's texture
  339.     spTexture->loadF("resources/enemy.png");
  340. }
  341.  
  342. Enemy::~Enemy()
  343. {
  344.     // unload the texture
  345.     spTexture = NULL;
  346. }
  347.  
  348. Block::Block(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H, int type = NULL)
  349. {
  350.     // initialize variables
  351.     blRect.x = passedX;
  352.     blRect.y = passedY;
  353.     blRect.w = passedW;
  354.     blRect.h = passedH;
  355.  
  356.     // load texture for block
  357.     blTexture->loadF("resources/block.png");
  358. }
  359.  
  360. Block::~Block()
  361. {
  362.     // UNNECESSARY???
  363. }
  364.  
  365. Background::Background(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = LEVEL_W, int passedH = LEVEL_H)
  366. {
  367.     // initialize pointer & variables
  368.     bgTexture = NULL;
  369.     bgFrames = 1;
  370.     bgRect.x = passedX;
  371.     bgRect.y = passedY;
  372.     bgRect.w = passedW;
  373.     bgRect.h = passedH;
  374.  
  375.     // load background
  376.     bgTexture->loadF("resources/bg.png");
  377. }
  378.  
  379. Background::~Background()
  380. {
  381.     // free up the background
  382.     freebg();
  383. }
  384.  
  385. void Background::freebg()
  386. {
  387.     // free up background texture
  388.     bgTexture->free();
  389.  
  390.     // make texture point to NULL
  391.     bgTexture = NULL;
  392. }
  393.  
  394. #ifdef _SDL_TTF_H
  395. bool Texture::loadT(string text, SDL_Color color, TTF_Font* font)
  396. {
  397.     // discard existing texture if it exists
  398.     free();
  399.  
  400.     // stores any failures
  401.     bool success = true;
  402.  
  403.     // turn text into a surface
  404.     SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), color);
  405.  
  406.     // if surface loaded correctly
  407.     if (textSurface != NULL)
  408.     {
  409.         // create texture from surface
  410.         txTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
  411.  
  412.         // if texture did not load correctly
  413.         if (txTexture == NULL)
  414.         {
  415.             // output an error
  416.             printf("Error creating texture from text surface.  SDL_GetError: %s\n", SDL_GetError());
  417.             success = false;
  418.         }
  419.         else
  420.         {
  421.             // store properties
  422.             txRect.w = textSurface->w;
  423.             txRect.h = textSurface->h;
  424.         }
  425.     }
  426.     else
  427.     {
  428.         // output an error
  429.         printf("Error loading text surface.  SDL_GetError: %s\n", SDL_GetError());
  430.         success = false;
  431.     }
  432.  
  433.     // return success status
  434.     return success;
  435. }
  436. #endif
  437.  
  438. void Texture::render(SDL_Rect *clip = NULL, SDL_Point *center = NULL,
  439.     double rotation = 0.0, SDL_RendererFlip flip = SDL_FLIP_NONE)
  440. {
  441.     // rect to render to
  442.     SDL_Rect renderRect{ txRect.x, txRect.y, txRect.w, txRect.h };
  443.  
  444.     // if clip is given, set properties
  445.     if (clip != NULL)
  446.     {
  447.         renderRect.w = clip->w;
  448.         renderRect.h = clip->h;
  449.     }
  450.  
  451.     // render to the screen
  452.     SDL_RenderCopyEx(gRenderer, txTexture, clip, &renderRect, rotation, center, flip);
  453. }
  454.  
  455. bool Texture::loadF(string path)
  456. {
  457.     // keeps track of whether a failure occurred
  458.     bool success = true;
  459.  
  460.     // free the current texture (if it exists)
  461.     free();
  462.  
  463.     // the texture that we will (hopefully) end up with
  464.     SDL_Texture* createdTexture = NULL;
  465.  
  466.     // the surface that the texture's image will be loaded onto
  467.     SDL_Surface* imageSurface = IMG_Load(path.c_str());
  468.  
  469.     if (imageSurface == NULL)
  470.     {
  471.         // output a message if the surface failed to load
  472.         printf("Error loading image at the specified path: %s\n", path);
  473.         printf("IMG_GetError: %s\n", IMG_GetError());
  474.         success = false;
  475.     }
  476.     else
  477.     {
  478.         // set the color key
  479.         SDL_SetColorKey(imageSurface, SDL_TRUE, SDL_MapRGB(imageSurface->format, 0, 255, 255));
  480.  
  481.         // create the texture itself from the surface
  482.         createdTexture = SDL_CreateTextureFromSurface(gRenderer, imageSurface);
  483.  
  484.         if (createdTexture == NULL)
  485.         {
  486.             // output an error
  487.             printf("Error creating texture from surface.  SDL_GetError: %s\n", SDL_GetError());
  488.         }
  489.         else
  490.         {
  491.             txRect.w = imageSurface->w;
  492.             txRect.h = imageSurface->h;
  493.         }
  494.     }
  495.  
  496.     // the surface isn't needed anymore
  497.     SDL_FreeSurface(imageSurface);
  498.  
  499.     // make the texture member point to the newly created texture
  500.     txTexture = createdTexture;
  501.  
  502.     // return whether everything ran successfully or not
  503.     return success;
  504. }
  505.  
  506. void Texture::free()
  507. {
  508.     // if a texture exists, get rid of it and reinitialize everything
  509.     if (txTexture != NULL)
  510.     {
  511.         SDL_DestroyTexture(txTexture);
  512.         txTexture = NULL;
  513.         txRect.w = DEFAULT_W;
  514.         txRect.h = DEFAULT_H;
  515.         txRect.x = DEFAULT_X;
  516.         txRect.y = DEFAULT_Y;
  517.     }
  518. }
  519.  
  520. void Texture::color(Uint8 r, Uint8 g, Uint8 b)
  521. {
  522.     // set colors to the texture
  523.     SDL_SetTextureColorMod(txTexture, r, g, b);
  524. }
  525.  
  526. void Texture::blend(SDL_BlendMode b)
  527. {
  528.     // set the blend mode
  529.     SDL_SetTextureBlendMode(txTexture, b);
  530. }
  531.  
  532. void Texture::alpha(Uint8 a)
  533. {
  534.     // set alpha to the texture
  535.     SDL_SetTextureAlphaMod(txTexture, a);
  536. }
  537.  
  538. void Sprite::die()
  539. {
  540.     // CURRENTLY THIS IS MOSTLY A PLACEHOLDER SO I KNOW I PROGRAMMED EVERYTHING CORRECTLY.  ACTUAL DEATH ANIMATIONS AND STUFF COME LATER
  541.  
  542.     Texture deathTexture;
  543.  
  544.     spTexture->free();
  545.     deathTexture.loadF("resources/die.png");
  546.     deathTexture.render();
  547.     clearScreen();
  548.     updateScreen();
  549.    
  550. }
  551.  
  552. void Player::handleEvent(SDL_Event &e)
  553. {
  554.     // if the user pressed a movement key, change the sprite's velocity accordingly
  555.     if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
  556.     {
  557.         switch (e.key.keysym.sym)
  558.         {
  559.         case SDLK_UP: spYvel -= spMaxVel; break;
  560.         case SDLK_DOWN: spYvel += spMaxVel; break;
  561.         case SDLK_LEFT: spXvel -= spMaxVel; break;
  562.         case SDLK_RIGHT: spXvel += spMaxVel; break;
  563.         }
  564.     }
  565.     // if the user released a movement key, change velocity accordingly
  566.     else if (e.type == SDL_KEYUP && e.key.repeat == 0)
  567.     {
  568.         switch (e.key.keysym.sym)
  569.         {
  570.         case SDLK_UP: spYvel += spMaxVel; break;
  571.         case SDLK_DOWN: spYvel -= spMaxVel; break;
  572.         case SDLK_LEFT: spXvel += spMaxVel; break;
  573.         case SDLK_RIGHT: spXvel -= spMaxVel; break;
  574.         }
  575.     }
  576.  
  577.     // move the player and center the camera around the player
  578.     move(2);
  579.     centerCamera();
  580. }
  581.  
  582. void Sprite::move(int a)
  583. {
  584.     // rectangle that will represent where the Sprite is about to move
  585.     SDL_Rect moddedRect;
  586.  
  587.     // set rect's properties to the sprite that is being checked
  588.     moddedRect.x = spRect.x;
  589.     moddedRect.y = spRect.y;
  590.     moddedRect.w = spRect.w;
  591.     moddedRect.h = spRect.h;
  592.  
  593.     // represents what specifically was touched
  594.     int *thingsTouched[3];
  595.  
  596.     // change properties to where the sprite is about to move
  597.     moddedRect.x += spXvel;
  598.     moddedRect.y += spYvel;
  599.  
  600.     // if where the player is about to move is a block
  601.     if (touchesThing(moddedRect, &*thingsTouched))
  602.     {
  603.         while (true)
  604.         {
  605.             // if the player touches a block in one pixel, move player one pixel and break
  606.             moddedRect.x = spRect.x + 1;
  607.             moddedRect.y = spRect.y + 1;
  608.             if (touchesThing(moddedRect, &*thingsTouched))
  609.             {
  610.                 spRect.x = moddedRect.x;
  611.                 spRect.y = moddedRect.y;
  612.                 break;
  613.             }
  614.             else
  615.             {
  616.                 // set sprite's position to where it should move
  617.                 spRect.x = moddedRect.x;
  618.                 spRect.y = moddedRect.y;
  619.             }
  620.         }
  621.  
  622.         // if we're working with an enemy and it touched the player, kill the player
  623.         if (thingsTouched[1] > 0 && a == 2)
  624.         {
  625.             gPlayer.die();
  626.         }
  627.         // if we're working with an enemy and it touched another enemy,
  628.         else if (thingsTouched[1] > 0 && a == 1)
  629.         {
  630.             // PUT CODE HERE LATER
  631.         }
  632.         else
  633.         {
  634.             // MIGHT PUT CODE FOR BLOCKS HERE LATER
  635.         }
  636.  
  637.         // if we're working with the player and the player touched an enemy, kill the player
  638.         if (thingsTouched[2] > 0 && a == 1)
  639.         {
  640.             gPlayer.die();
  641.         }
  642.         else if (thingsTouched[2] > 0 && a == 2)
  643.         {
  644.             // MIGHT PUT CODE FOR MULTIPLE PLAYERS HERE LATER
  645.         }
  646.         else
  647.         {
  648.             // MIGHT PUT CODE HERE LATER??  OR MIGHT NOT BE NECESSARY??
  649.         }
  650.     }
  651.     else
  652.     {
  653.         // move the player
  654.         spRect.x += spXvel;
  655.         spRect.y += spYvel;
  656.     }
  657. }
  658.  
  659. int touchesThing(SDL_Rect a, int **thingsTouched)
  660. {
  661.     // variables to represent the dimensions of the rect whose collision is being checked
  662.     int topLeftA = NULL, topRightA = NULL;
  663.     int bottomLeftA = NULL, bottomRightA = NULL;
  664.     int topLeftB = NULL, topRightB = NULL;
  665.     int bottomLeftB = NULL, bottomRightB = NULL;
  666.  
  667.     // represent what specifically was touched
  668.     int blocksTouched = NULL, enemiesTouched = NULL, playerTouched = NULL;
  669.  
  670.     // set the variables accordingly
  671.     topLeftA = a.x;
  672.     topRightA = a.x + a.w;
  673.     bottomLeftA = a.x + a.h;
  674.     bottomRightA = a.x + a.h + a.w;
  675.  
  676.     // check everything in the level
  677.     for (int i = 0; i < TOTAL_BLOCKS; i++)
  678.     {
  679.         // if there is a block in that level space by default
  680.         if (blocks[i] != NULL)
  681.         {
  682.             // check its current position to see if a collision occurred
  683.             if ((blocks[i]->blRect.x) <= (a.x) ||
  684.                 (blocks[i]->blRect.x + blocks[i]->blRect.w) >= (a.x + a.w) ||
  685.                 (blocks[i]->blRect.x + blocks[i]->blRect.h) <= (a.x + a.h) ||
  686.                 (blocks[i]->blRect.x + blocks[i]->blRect.h + blocks[i]->blRect.w) >= (a.x + a.h + a.w))
  687.             {
  688.                 // if the thing is not colliding with the rect, continue checking
  689.                 continue;
  690.             }
  691.             else
  692.             {
  693.                 // if the block and rect collided, increase the counter and continue checking
  694.                 blocksTouched++;
  695.             }
  696.         }
  697.         // if there is an enemy in that level space by default
  698.         else if (enemies[i] != NULL)
  699.         {
  700.             // check its current position (may not be the initial position) to see if a collision occurred
  701.             if ((enemies[i]->spRect.x) <= (a.x) ||
  702.                 (enemies[i]->spRect.x + enemies[i]->spRect.w) >= (a.x + a.w) ||
  703.                 (enemies[i]->spRect.x + enemies[i]->spRect.h) <= (a.x + a.h) ||
  704.                 (enemies[i]->spRect.x + enemies[i]->spRect.h + enemies[i]->spRect.w) >= (a.x + a.h + a.w))
  705.             {
  706.                 // continue if no collision
  707.                 continue;
  708.             }
  709.             else
  710.             {
  711.                 // increase counter & continue if a collision happened
  712.                 enemiesTouched++;
  713.             }
  714.         }
  715.         // if the player starts there by default
  716.         else if (playerPos[i] != NULL)
  717.         {
  718.             // check the player's CURRENT dimensions
  719.             if ((playerPos[i]->spRect.x) <= (a.x) ||
  720.                 (playerPos[i]->spRect.x + playerPos[i]->spRect.w) >= (a.x + a.w) ||
  721.                 (playerPos[i]->spRect.x + playerPos[i]->spRect.h) <= (a.x + a.h) ||
  722.                 (playerPos[i]->spRect.x + playerPos[i]->spRect.h + playerPos[i]->spRect.w) >= (a.x + a.h + a.w))
  723.             {
  724.                 // continue if no collision
  725.                 continue;
  726.             }
  727.             else
  728.             {
  729.                 // increase counter & continue if collision
  730.                 playerTouched++;
  731.             }
  732.         }
  733.     }
  734.  
  735.     // if one or more blocks were touched
  736.     if (blocksTouched > 0)
  737.     {
  738.         // return true and insert amount of blocks in proper array slot
  739.         thingsTouched[0] = &blocksTouched;
  740.         return true;
  741.     }
  742.  
  743.     // if 1+ enemies were touched
  744.     if (enemiesTouched > 0)
  745.     {
  746.         // return true & insert number of enemies in related slot
  747.         thingsTouched[1] = &enemiesTouched;
  748.         return true;
  749.     }
  750.  
  751.     // if player was touched
  752.     if (playerTouched > 0)
  753.     {
  754.         // return true & insert that a player was touched
  755.         thingsTouched[2] = &playerTouched;
  756.         return true;
  757.     }
  758.  
  759.     // return false (no collision occurred)
  760.     return false;
  761. }
  762.  
  763. /*  UNUSED FUNCTION, POSSIBLY STILL USEFUL THOUGH
  764.  
  765. bool touchesSpecificThing(SDL_Rect a, SDL_Rect b, char type)
  766. {
  767.     int topLeftA, topRightA;
  768.     int bottomLeftA, bottomRightA;
  769.     int topLeftB, topRightB;
  770.     int bottomLeftB, bottomRightB;
  771.  
  772.     topLeftA = a.x;
  773.     topRightA = a.x + a.w;
  774.     bottomLeftA = a.x + a.h;
  775.     bottomRightA = a.x + a.h + a.w;
  776.  
  777.     topLeftB = b.x;
  778.     topRightB = b.x + b.w;
  779.     bottomLeftB = b.x + b.h;
  780.     bottomRightB = b.x + b.h + b.w;
  781.  
  782.     if (topLeftA <= topLeftB || topRightA >= topRightB ||
  783.         bottomLeftA <= bottomLeftB || bottomRightA >= bottomRightB)
  784.         return false;
  785.     else
  786.     {
  787.         switch (type)
  788.         {
  789.         case 'b':
  790.         {
  791.                     // if the player touched a block, return true
  792.                     return true;
  793.         }
  794.         case 'e':
  795.         {
  796.                     // if the player touched an enemy, kill the player and return true
  797.                     gPlayer.die();
  798.                     return true;
  799.         }
  800.         }
  801.     }
  802. }
  803.  
  804. */
  805.  
  806. void Player::centerCamera()
  807. {
  808.     // variable to represent the camera
  809.     SDL_Rect* camera = &gCamera.cmRect;
  810.     camera->w = WINDOW_W;
  811.     camera->h = WINDOW_H;
  812.  
  813.     // determines whether the camera is at the level's edge or not
  814.     bool edge = false;
  815.  
  816.     // represents the camera when it moves
  817.     SDL_Rect moddedRect;
  818.  
  819.     // set moddedRect properties
  820.     moddedRect.x = camera->x;
  821.     moddedRect.y = camera->y;
  822.     moddedRect.w = camera->w;
  823.     moddedRect.h = camera->h;
  824.  
  825.     // set moddedRect to camera's next move
  826.     moddedRect.x++;
  827.     moddedRect.y++;
  828.  
  829.     // if camera is about to go off the left of the screen, stop its movement
  830.     if (moddedRect.x < camera->w / 2)
  831.     {
  832.         camera->x = 0;
  833.         edge = true;
  834.     }
  835.  
  836.     // if it is about to go off the right, stop it
  837.     if (moddedRect.x > LEVEL_W - camera->w / 2)
  838.     {
  839.         camera->x = LEVEL_W - camera->w;
  840.         edge = true;
  841.     }
  842.  
  843.     // if it is about to go off the top, stop it
  844.     if (moddedRect.y > camera->h / 2)
  845.     {
  846.         camera->y = 0;
  847.         edge = true;
  848.     }
  849.  
  850.     // if it is about to go off the bottom, stop it
  851.     if (moddedRect.y > LEVEL_H - camera->h / 2)
  852.     {
  853.         camera->y = LEVEL_H - camera->h;
  854.         edge = true;
  855.     }
  856.  
  857.     // if not at the edge of the level, keep camera centered around player
  858.     if (!edge)
  859.     {
  860.         camera->x = spRect.x - (camera->w / 2);
  861.         camera->y = spRect.y - (camera->h / 2);
  862.     }
  863. }
  864.  
  865. bool setBlocks()
  866. {
  867.     // open block file for reading
  868.     ifstream blockSet("resources/blocks.map");
  869.  
  870.     // identify type of block from file
  871.     int blockType;
  872.  
  873.     // current position where a block is being placed
  874.     int x = 0, y = 0;
  875.  
  876.     // indicates whether a success occurred or not
  877.     bool success = true;
  878.    
  879.     for (int i = 0; i <= TOTAL_BLOCKS; i++)
  880.     {
  881.         // make each level space equal to NULL so error checking and whatnot is easier
  882.         blocks[i] = NULL;
  883.         enemies[i] = NULL;
  884.         playerPos[i] = NULL;
  885.     }
  886.  
  887.     /* CODE FOR FRAMES
  888.     int i = 0;
  889.     for (i = 0; i <= DEFAULT_FRAMES; i++)
  890.     {
  891.         cBox[i]->x = x;
  892.         cBox[i]->y = y;
  893.         cBox[i]->w = w;
  894.         cBox[i]->h = h;
  895.     }
  896.     */
  897.  
  898.     /*  I thought this was supposed to work but I get an error so I dunno
  899.  
  900.     if (blockSet == NULL)
  901.     {
  902.         // if loading the level failed, output an error
  903.         printf("Error loading level file!\n");
  904.         success = false;
  905.     }
  906.     else
  907.     {
  908.  
  909.     */
  910.  
  911.     for (int i = 0; i <= TOTAL_BLOCKS; i++)
  912.     {
  913.         // initialize value to -1
  914.         blockType = -1;
  915.  
  916.         // insert the type of block into the variable (0-3)
  917.         blockSet >> blockType;
  918.  
  919.         if (blockSet.fail())
  920.         {
  921.             // if reading the file failed, output an error
  922.             printf("Error reading level file!\n");
  923.             success = false;
  924.         }
  925.         else
  926.         {
  927.             if (blockType == 1)
  928.             {
  929.                 // place a block in this level space
  930.                 blocks[i] = new Block(x, y);
  931.             }
  932.             else if (blockType == 0)
  933.             {
  934.                 // place nothing in this level space
  935.                 continue;
  936.             }
  937.             else if (blockType == 2)
  938.             {
  939.                 // place an enemy in this level space
  940.                 enemies[i] = new Enemy(x, y);
  941.             }
  942.             else if (blockType == 3)
  943.             {
  944.                 // place the player's start in this level space
  945.                 playerPos[i] = new Player(x, y);
  946.             }
  947.             else
  948.             {
  949.                 // the block type is not valid
  950.                 printf("ERROR: Invalid block type: %d\n", blockType);
  951.                 return false;
  952.             }
  953.  
  954.             if (x + DEFAULT_W >= LEVEL_W)
  955.             {
  956.                 // if at level edge, go back to the left and move down one space
  957.                 x = 0;
  958.                 y += DEFAULT_H;
  959.             }
  960.             else
  961.             {
  962.                 // move to the right one space
  963.                 x += DEFAULT_W;
  964.             }
  965.         }
  966.     }
  967.        
  968.         // load sprite sheet if loaded correctly
  969.         if (success)
  970.         {
  971.             // set the spritesheet array equal to the sprites' positions on the actual spritesheet
  972.             spriteSheet[0]->x = DEFAULT_X;
  973.             spriteSheet[0]->y = DEFAULT_Y;
  974.             spriteSheet[0]->w = DEFAULT_W;
  975.             spriteSheet[0]->h = DEFAULT_H;
  976.  
  977.             spriteSheet[1]->x = DEFAULT_X + DEFAULT_W;
  978.             spriteSheet[1]->y = DEFAULT_Y;
  979.             spriteSheet[1]->w = DEFAULT_W;
  980.             spriteSheet[1]->h = DEFAULT_H;
  981.  
  982.             spriteSheet[2]->x = DEFAULT_X + (DEFAULT_W * 2);
  983.             spriteSheet[2]->y = DEFAULT_Y;
  984.             spriteSheet[2]->w = DEFAULT_W;
  985.             spriteSheet[2]->h = DEFAULT_H;
  986.  
  987.             spriteSheet[3]->x = DEFAULT_X + (DEFAULT_W * 3);
  988.             spriteSheet[3]->y = DEFAULT_Y;
  989.             spriteSheet[3]->w = DEFAULT_W;
  990.             spriteSheet[3]->h = DEFAULT_H;
  991.         }
  992.    
  993.     /*
  994.    
  995.     }
  996.  
  997.     */
  998.  
  999.     // close the file
  1000.     blockSet.close();
  1001.  
  1002.     // return whether loading the level was a success or not
  1003.     return success;
  1004. }
  1005.  
  1006. bool init()
  1007. {
  1008.     // keeps track of whether or not an error occurred
  1009.     bool success;
  1010.  
  1011.     // initialize video; if failed, output an error & set success to false
  1012.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  1013.     {
  1014.         printf("SDL initialization error: %s\n", SDL_GetError());
  1015.         success = false;
  1016.     }
  1017.     else
  1018.     {
  1019.         // make texture filtering linear; if failed, output a notification
  1020.         if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
  1021.         {
  1022.             printf("NOTE: Hint failed to be set!");
  1023.         }
  1024.  
  1025.         // create the window and everything
  1026.         gWindow = SDL_CreateWindow("Oh man I made a program", SDL_WINDOWPOS_CENTERED,
  1027.             SDL_WINDOWPOS_CENTERED, WINDOW_W, WINDOW_H, SDL_WINDOW_SHOWN);
  1028.        
  1029.         // if window creation failed, output an error
  1030.         if (gWindow == NULL)
  1031.         {
  1032.             printf("SDL window creation error: %s\n", SDL_GetError());
  1033.             success = false;
  1034.         }
  1035.         // if no error occurred
  1036.         else
  1037.         {
  1038.             // create a renderer for use later
  1039.             gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  1040.            
  1041.             // if the renderer failed to be created, output an error & set success to false
  1042.             if (gRenderer == NULL)
  1043.             {
  1044.                 printf("SDL renderer creation error: %s\n", SDL_GetError());
  1045.                 success = false;
  1046.             }
  1047.             // if no error occurred
  1048.             else
  1049.             {
  1050.                 // initialize image loading; if failed, output an error & set success to false
  1051.                 if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
  1052.                 {
  1053.                     printf("SDL_image initialization error: %s\n", IMG_GetError());
  1054.                     success = false;
  1055.                 }
  1056.             }
  1057.         }
  1058.     }
  1059.  
  1060.     // return the success status
  1061.     return success;
  1062. }
  1063.  
  1064. void clearScreen()
  1065. {
  1066.     // sets screen color
  1067.     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  1068.  
  1069.     // set screen up for updating
  1070.     SDL_RenderClear(gRenderer);
  1071. }
  1072.  
  1073. void updateScreen()
  1074. {
  1075.     // update the screen
  1076.     SDL_RenderPresent(gRenderer);
  1077. }
  1078.  
  1079. void close()
  1080. {
  1081.     // delete each block, enemy, and player from the level
  1082.     for (int i = 0; i <= TOTAL_BLOCKS; i++)
  1083.     {
  1084.         if (blocks[i] != NULL)
  1085.             delete blocks[i];
  1086.         else if (enemies[i] != NULL)
  1087.             delete enemies[i];
  1088.         else if (playerPos[i] != NULL)
  1089.             delete playerPos[i];
  1090.     }
  1091. }
  1092.  
  1093. int main(int argc, char *argv[])
  1094. {
  1095.     // texture pointer to make things easier later on
  1096.     Texture* pTexture;
  1097.  
  1098.     // keeps track of if the user requests to quit
  1099.     bool quit = false;
  1100.  
  1101.     // keeps track of what the user inputs
  1102.     SDL_Event e;
  1103.  
  1104.     // output an error if initialization was not successful
  1105.     if (!init())
  1106.     {
  1107.         printf("ERROR: SDL failed to initialize.");
  1108.     }
  1109.     else
  1110.     {
  1111.         // output an error if setting up the level blocks failed
  1112.         if (!setBlocks())
  1113.         {
  1114.             printf("ERROR: Could not load level.");
  1115.         }
  1116.  
  1117.         // BY THIS POINT, ALL BLOCKS/ENEMIES/PLAYER SHOULD BE SET
  1118.  
  1119.         else
  1120.         {
  1121.             // while the user does not want to quit
  1122.             while (!quit)
  1123.             {
  1124.                 // while the user requests something
  1125.                 while (SDL_PollEvent(&e) != NULL)
  1126.                 {
  1127.                     // if they requested to quit, set the quit boolean to true
  1128.                     if (e.key.keysym.sym == SDL_QUIT)
  1129.                     {
  1130.                         quit = true;
  1131.                     }
  1132.                     // handle the event if it wasn't a request to quit
  1133.                     gPlayer.handleEvent(e);
  1134.                 }
  1135.             }
  1136.             // re-render the player texture
  1137.             pTexture = gPlayer.spTexture;
  1138.             pTexture->render();
  1139.         }
  1140.     }
  1141.     return 0;
  1142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement