SHARE
TWEET

Untitled

a guest Aug 25th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LEARN SUBCLASSES. WOULD HAVE SAVED A GOOD PORTION OF TIME AND WOULD HAVE RESULTED IN A LOT LESS LINES   
  2. // ALSO LEARN ERROR HANDLING AND EXCEPTIONS
  3.  
  4. /*
  5.  
  6. TODO:
  7. - Allow user to make pawn turn into any other piece should the pawn reach the opposite side of the board.
  8. - Put 2 rooks on each side of the board and allow for them to kill/be killed.
  9. - pieces are moving with being clicked...? the piece is still selected even if another piece is selected afterwards..
  10.  
  11. */
  12.  
  13. #include <SDL.h>
  14. #include <SDL_image.h>
  15. #include <iostream>
  16. #include <string>
  17. #include "pawn.h"
  18. #include "rook.h"
  19.  
  20. using namespace std;
  21.  
  22. const int WINDOW_WIDTH = 800;
  23. const int WINDOW_HEIGHT = 800;
  24. SDL_Window* gWindow = nullptr;
  25. SDL_Renderer* gRenderer = nullptr;
  26.  
  27. bool init()
  28. {
  29.     bool success = true;
  30.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  31.     {
  32.         cout << "SDL failed to initialize! SDL error: " << SDL_GetError() << endl;
  33.         success = false;
  34.     }
  35.     else
  36.     {
  37.         gWindow = SDL_CreateWindow("Chess", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
  38.         if (gWindow == nullptr)
  39.         {
  40.             cout << "SDL failed to create the window! SDL error: " << SDL_GetError() << endl;
  41.             success = false;
  42.         }
  43.         gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  44.         if (gRenderer == nullptr)
  45.         {
  46.             success = false;
  47.             cout << "SDL failed to create the renderer! SDL error: " << SDL_GetError() << endl;
  48.         }
  49.         if (IMG_Init(IMG_INIT_PNG) < 0)
  50.         {
  51.             cout << "IMG failed to initialize PNG! IMG error: " << IMG_GetError() << endl;
  52.             success = false;
  53.         }
  54.     }
  55.  
  56.     return success;
  57. }
  58.  
  59. SDL_Texture* loadTexture(string path)
  60. {
  61.     SDL_Surface* loadedSurface = IMG_Load(path.c_str());
  62.     if (loadedSurface == nullptr)
  63.         cout << "Error loading surface! IMG error: " << IMG_GetError() << endl;
  64.     SDL_Texture* finalSurface = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
  65.     return finalSurface;
  66. }
  67.  
  68. SDL_Texture* spritesheet;
  69. // 8x8 are the dimensions of a chess board
  70. SDL_Rect tileRects[8 * 8];
  71. // should've studied subclasses prior to this, as it would have made this so much more line efficient
  72. int whitePieces = 10;
  73. int blackPieces = 10;
  74.  
  75. Pawn whitePawns[8];
  76. Pawn blackPawns[8];
  77. Rook whiteRooks[2];
  78. Rook blackRooks[2];
  79.  
  80. // currently 16 pieces. each piece has 2 positions (x, y).
  81. int poses[16][2];
  82. bool loadMedia()
  83. {
  84.     bool success = true;
  85.     spritesheet = loadTexture("spritesheet.png");
  86.  
  87.     int counter = 0;
  88.     int counter2 = 0;
  89.     for (int i = 0; i < sizeof(tileRects) / sizeof(tileRects[0]); i++)
  90.     {
  91.         tileRects[i].w = 100;
  92.         tileRects[i].h = 100;
  93.  
  94.         if (counter2 >= 8)
  95.             counter2 = 0;
  96.         if (i % 8 == 0 && i != 0)
  97.         {
  98.             counter2++;
  99.             counter++;
  100.         }
  101.         tileRects[i].x = 100 * counter2;
  102.  
  103.         if (counter > 0)
  104.             tileRects[i].y = 100 * counter;
  105.  
  106.         counter2++;
  107.     }
  108.  
  109.     for (int i = 0; i < sizeof(whitePawns) / sizeof(whitePawns[0]); i++)
  110.         whitePawns[i] = Pawn(i, "white");
  111.  
  112.     for (int i = 0; i < sizeof(blackPawns) / sizeof(blackPawns[0]); i++)
  113.         blackPawns[i] = Pawn(i, "black");
  114.    
  115.     for (int i = 0; i < sizeof(whiteRooks) / sizeof(whiteRooks[0]); i++)
  116.         whiteRooks[i] = Rook(i * 7, "white");
  117.    
  118.     for (int i = 0; i < sizeof(blackRooks) / sizeof(blackRooks[0]); i++)
  119.         blackRooks[i] = Rook(i * 7, "black");
  120.  
  121.     return success;
  122. }
  123.  
  124. int gMouseX = 0;
  125. int gMouseY = 0;
  126.  
  127. bool clicked = false;
  128. int main(int argc, char* argv[])
  129. {
  130.     init();
  131.     loadMedia();
  132.  
  133.     string turn = "white";
  134.  
  135.     SDL_Event events;
  136.     bool running = true;
  137.     while (running)
  138.     {
  139.         int posesCounter = 0;
  140.         while (SDL_PollEvent(&events) != 0)
  141.         {
  142.             if (events.type == SDL_QUIT)
  143.                 running = false;
  144.  
  145.             bool leftClick = false;
  146.             if (events.type == SDL_MOUSEBUTTONDOWN)
  147.                 leftClick = true;
  148.             else
  149.                 leftClick = false;
  150.  
  151.             if (turn == "white")
  152.             {
  153.                 for (int i = 0; i < sizeof(whitePawns) / sizeof(whitePawns[0]); i++)
  154.                 {
  155.                     // this makes it so that you have to click the piece for it to be selected, and click again for it to go somewhere.
  156.                     if (whitePawns[i].mSelected && leftClick)
  157.                         whitePawns[i].canMove = true;
  158.                     else
  159.                         whitePawns[i].canMove = false;
  160.                     whitePawns[i].handleEvents(events, leftClick);
  161.  
  162.                     // this ensures that only when piece is selected at once.
  163.                     if (whitePawns[i].mSelected)
  164.                     {
  165.                         for (int j = 0; j < sizeof(whitePawns) / sizeof(whitePawns[0]); j++)
  166.                             if (j != i)
  167.                                 whitePawns[j].mSelected = false;
  168.                         for (int j = 0; j < sizeof(whiteRooks) / sizeof(whiteRooks[0]); j++)
  169.                             whiteRooks[j].mSelected = false;
  170.                     }
  171.  
  172.                 }
  173.                
  174.                 for (int i = 0; i < sizeof(whiteRooks) / sizeof(whiteRooks[0]); i++)
  175.                 {
  176.                     if (whiteRooks[i].mSelected && leftClick)
  177.                         whiteRooks[i].canMove = true;
  178.                     else
  179.                         whiteRooks[i].canMove = false;
  180.                     whiteRooks[i].handleEvents(events, leftClick);
  181.  
  182.                     if (whiteRooks[i].mSelected)
  183.                     {
  184.                         for (int j = 0; j < sizeof(whitePawns) / sizeof(whitePawns[0]); j++)
  185.                             whitePawns[j].mSelected = false;
  186.                         if (i == 1)
  187.                             whitePawns[0].mSelected = false;
  188.                         else
  189.                             whitePawns[1].mSelected = false;
  190.                     }
  191.  
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 for (int i = 0; i < sizeof(blackPawns) / sizeof(blackPawns[0]); i++)
  197.                 {
  198.                     if (blackPawns[i].mSelected && leftClick)
  199.                         blackPawns[i].canMove = true;
  200.                     else
  201.                         blackPawns[i].canMove = false;
  202.                     blackPawns[i].handleEvents(events, leftClick);
  203.  
  204.                     if (blackPawns[i].mSelected)
  205.                     {
  206.                         for (int j = 0; j < sizeof(blackPawns) / sizeof(blackPawns[0]); j++)
  207.                             if (j != i)
  208.                                 blackPawns[j].mSelected = false;
  209.                         for (int j = 0; j < sizeof(blackRooks) / sizeof(blackRooks[0]); j++)
  210.                             blackRooks[j].mSelected = false;
  211.                     }
  212.                 }
  213.                 for (int i = 0; i < sizeof(blackRooks) / sizeof(blackRooks[0]); i++)
  214.                 {
  215.                     if (blackRooks[i].mSelected && leftClick)
  216.                         blackRooks[i].canMove = true;
  217.                     else
  218.                         blackRooks[i].canMove = false;
  219.                     blackRooks[i].handleEvents(events, leftClick);
  220.  
  221.                     if (blackRooks[i].mSelected)
  222.                     {
  223.                         for (int j = 0; j < sizeof(blackPawns) / sizeof(blackPawns[0]); j++)
  224.                             blackPawns[j].mSelected = false;
  225.                         if (i == 1)
  226.                             blackPawns[0].mSelected = false;
  227.                         else
  228.                             blackPawns[1].mSelected = false;
  229.                     }
  230.  
  231.                 }
  232.             }
  233.  
  234.         }
  235.        
  236.         SDL_GetMouseState(&gMouseX, &gMouseY);
  237.         gMouseX /= 100;
  238.         gMouseY /= 100;
  239.  
  240.         SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);
  241.         SDL_RenderClear(gRenderer);
  242.  
  243.         for (int i = 0; i < sizeof(tileRects) / sizeof(tileRects[0]); i++)
  244.         {
  245.             if (i % 2 == 0)
  246.             {
  247.                 SDL_SetRenderDrawColor(gRenderer, 84, 168, 9, 255);
  248.                 SDL_RenderFillRect(gRenderer, &tileRects[i]);
  249.             }
  250.             else
  251.             {
  252.                 SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
  253.                 SDL_RenderFillRect(gRenderer, &tileRects[i]);
  254.             }
  255.         }
  256.  
  257.         for (int i = 0; i < whitePieces; i++)
  258.         {
  259.             if (i <= 7)
  260.             {
  261.                 poses[i][0] = whitePawns[i].getX();
  262.                 poses[i][1] = whitePawns[i].getY();
  263.                 whitePawns[i].move(gMouseX, gMouseY, clicked, turn, poses);
  264.                 SDL_SetRenderDrawColor(gRenderer, 255, 0, 0, 255);
  265.                 SDL_RenderFillRect(gRenderer, &whitePawns[i].getRect());
  266.  
  267.                 if (whitePawns[i].killedPiece)
  268.                 {
  269.                     for (int j = 0; j < sizeof(blackPawns) / sizeof(blackPawns[0]); j++)
  270.                     {
  271.                         if (blackPawns[j].getX() == whitePawns[i].getX() && blackPawns[j].getY() == whitePawns[i].getY())
  272.                         {
  273.                             blackPawns[j].killed = true;
  274.                             turn = "black";
  275.                         }
  276.                     }
  277.                 }
  278.                 posesCounter++;
  279.             }
  280.             else if (i > 7 && i <= 9)
  281.             {
  282.                 poses[i][0] = whiteRooks[i - posesCounter].getX();
  283.                 poses[i][1] = whiteRooks[i - posesCounter].getY();
  284.                 whiteRooks[i - posesCounter].move(gMouseX, gMouseY, clicked, turn, poses);
  285.                 SDL_SetRenderDrawColor(gRenderer, 255, 255, 0, 255);
  286.                 SDL_RenderFillRect(gRenderer, &whiteRooks[i - posesCounter].getRect());
  287.             }
  288.             //posesCounter++;
  289.         }
  290.  
  291.         // OI FUTURE ME. YOU'RE GOING TO HAVE TO MAKE THE POSESCOUNTER INCREMENT IN THIS FOR LOOP OR ELSE IT WON'T WORK!!
  292.         for (int i = 0; i < sizeof(blackPawns) / sizeof(blackPawns[0]); i++)
  293.         {
  294.             poses[i + posesCounter + 2][0] = blackPawns[i].getX();
  295.             poses[i + posesCounter + 2][1] = blackPawns[i].getY();
  296.             blackPawns[i].move(gMouseX, gMouseY, clicked, turn, poses);
  297.             SDL_SetRenderDrawColor(gRenderer, 0, 0, 255, 255);
  298.             SDL_RenderFillRect(gRenderer, &blackPawns[i].getRect());
  299.  
  300.             if (blackPawns[i].killedPiece)
  301.             {
  302.                 for (int j = 0; j < sizeof(whitePawns) / sizeof(whitePawns[0]); j++)
  303.                 {
  304.                     if (blackPawns[i].getX() == whitePawns[j].getX() && blackPawns[i].getY() == whitePawns[j].getY())
  305.                     {
  306.                         whitePawns[j].killed = true;
  307.                         turn = "white";
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.  
  313.         SDL_RenderPresent(gRenderer);
  314.     }
  315.  
  316.     return 0;
  317. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top