Guest User

Untitled

a guest
Jun 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. // SDL_App.cpp : Defines the entry point for our Project
  2. //
  3. //#include "stdafx.h"
  4. #include <Windows.h>
  5. #include "GL_Functions.h"
  6. #include "pctimer.h"
  7. #include <string>
  8. #include <cmath>
  9. #include <iostream>
  10.  
  11. #include <crtdbg.h>
  12. #include "Game.h"
  13. #include "Ball.h"
  14. //#include "AIECollision.h"
  15. //#include "DLLInclude.h"
  16. //#include "Vector2.h"
  17.  
  18. using namespace std;
  19.  
  20. //Prototype / Lets main see gameloop as defined below
  21. void GameLoop();
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25.  
  26. //Lets open the window and initialise opengl
  27. //Using NOT magic numbers to define the size....
  28. InitGL(SCREEN_WIDTH,SCREEN_HEIGHT);
  29.  
  30. do
  31. {
  32. //Clear the screen, so previous frames don't build up
  33. ClearScreen();
  34.  
  35. //run gameloop until i say otherwise!
  36. GameLoop();
  37.  
  38. break;
  39.  
  40. //Stop it from running too fast! Sleep ZZzzz
  41. //Sleep(5);
  42.  
  43. } while (FrameworkUpdate()); //Do some secret stuff,
  44.  
  45.  
  46.  
  47. //Close down
  48. CloseDown();
  49.  
  50. _CrtDumpMemoryLeaks();
  51.  
  52. //Quit!
  53. return 0;
  54. }
  55.  
  56. void GameLoop()
  57. {
  58. //Define Declarations
  59.  
  60. float fScoreX = 64;
  61. float fScoreY = 64;
  62. const float fSpeed = 0.3f;
  63.  
  64. bool bWin = false;
  65. bool bGameOver = false;
  66.  
  67. int bgtex = LoadTexture("./images/Bg.png");
  68. int menutex = LoadTexture("./images/Menu.png");
  69. int balltex = LoadTexture("./images/BrainBall.png");
  70. int cursortex = LoadTexture("./images/Cursor.png");
  71. int paddletex = LoadTexture("./images/RightPaddle.png");
  72. int howtoplaytex = LoadTexture("./images/HowtoPlay.png");
  73. int player1win = LoadTexture("./images/Player1Win.png");
  74. int player2win = LoadTexture("./images/Player2Win.png");
  75.  
  76. //numbers for scores
  77. int iScore0 = LoadTexture("./images/Score0.png");
  78. int iScore1 = LoadTexture("./images/Score1.png");
  79. int iScore2 = LoadTexture("./images/Score2.png");
  80. int iScore3 = LoadTexture("./images/Score3.png");
  81. int iScore4 = LoadTexture("./images/Score4.png");
  82. int iScore5 = LoadTexture("./images/Score5.png");
  83. int iScore6 = LoadTexture("./images/Score6.png");
  84.  
  85. //score array
  86. //int iScore = 0;
  87. int iScoreL = 0;
  88. int iScoreR = 0;
  89.  
  90. int iScoreLeft[] = {iScore0, iScore1, iScore2, iScore3, iScore4, iScore5, iScore6};
  91. int iScoreRight[] = {iScore0, iScore1, iScore2, iScore3, iScore4, iScore5, iScore6};
  92.  
  93.  
  94.  
  95. //defines the structs for use in gameloop
  96. sScreen screen = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH , SCREEN_HEIGHT, bgtex};
  97. sScore score1 = {SCREEN_WIDTH / 3, SCREEN_HEIGHT / 7, fScoreX, fScoreY, iScoreLeft[iScoreL]};
  98. sScore score2 = {(SCREEN_WIDTH / 3 * 2), SCREEN_HEIGHT / 7, fScoreX, fScoreY, iScoreRight[iScoreR]};
  99. sBall ball = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 75, 0.5, 0.5, 16, fSpeed, balltex};
  100. sPaddle paddle1 = {100, SCREEN_HEIGHT / 2 + 75, 96, 24, fSpeed, 'w', 's', paddletex};
  101. sPaddle paddle2 = {924, SCREEN_HEIGHT / 2 + 75, 96, 24, fSpeed, KEY_UP, KEY_DOWN, paddletex};
  102.  
  103. //sScreen screen = {0, 0, SCREEN_WIDTH , SCREEN_HEIGHT, LoadTexture("./images/Bg.png")};
  104.  
  105. //End Define
  106. do
  107. {
  108. //Clear the screen, so previous frames don't build up
  109. //ClearScreen();
  110.  
  111. //Logic
  112.  
  113. //the escape key ends the gameloop
  114. if (IsKeyDown(KEY_ESCAPE))
  115. {
  116. break;
  117. }
  118.  
  119.  
  120. //function for paddle behaviour and ball movement/collision
  121. PaddleMove(paddle1);
  122. PaddleMove(paddle2);
  123. BallCollide(paddle1, paddle2, ball);
  124. BallAct(ball);
  125.  
  126. //int iScoreL = 0;
  127. //int iScoreR = 0;
  128.  
  129. if ( bWin == false )
  130. {
  131.  
  132. if ( iScoreL < 6 && iScoreR < 6)
  133. {
  134.  
  135. //Score left on screen exit
  136. if ( ball.fPosX <= BORDER_SIDE)
  137. {
  138. score2.iSprite = iScoreLeft[++iScoreL];
  139. ball.fPosX = (SCREEN_WIDTH / 2);
  140. ball.fPosY = (SCREEN_HEIGHT / 2 + 75);
  141. ball.fDirX *= -1;
  142. Sleep(500);
  143. }
  144.  
  145. //Score right on screen exit
  146. if ( ball.fPosX >= SCREEN_WIDTH - BORDER_SIDE)
  147. {
  148. score1.iSprite = iScoreRight[++iScoreR];
  149. ball.fPosX = (SCREEN_WIDTH / 2);
  150. ball.fPosY = (SCREEN_HEIGHT / 2 + 75);
  151. ball.fDirX *= -1;
  152. Sleep(500);
  153. }
  154.  
  155.  
  156.  
  157. //break;
  158. }
  159.  
  160. if ( iScoreL >= 6 || iScoreR >= 6 )
  161. {
  162. bWin = true;
  163. }
  164. }
  165.  
  166.  
  167.  
  168.  
  169. //End Logic
  170.  
  171. //Draw on screen
  172.  
  173. //draws the screen, ball and paddles
  174. DrawSpriteRotated(screen.iSprite, screen.x, screen.y, screen.fWidth, screen.fHeight, 0);
  175. DrawSpriteRotated(score1.iSprite, score1.x, score1.y, score1.fWidth, score1.fHeight, 0);
  176. DrawSpriteRotated(score2.iSprite, score2.x, score2.y, score2.fWidth, score2.fHeight, 0);
  177. DrawSpriteRotated(ball.iSprite, ball.fPosX, ball.fPosY, ball.fDiameter, ball.fDiameter, 0);
  178. DrawSpriteRotated(paddle1.iSprite, paddle1.fPosX, paddle1.fPosY, paddle1.fWidth, paddle1.fHeight, 0);
  179. DrawSpriteRotated(paddle2.iSprite, paddle2.fPosX, paddle2.fPosY, paddle2.fWidth, paddle2.fHeight, 0);
  180.  
  181. if (bWin == true && bGameOver == false)
  182. {
  183. if ( iScoreL == 6 )
  184. {
  185. //Clear the screen, to show win
  186. //ClearScreen();
  187. DrawSpriteRotated(player2win, screen.x, screen.y, screen.fWidth, screen.fHeight, 0);
  188. FrameworkUpdate();
  189. Sleep(5000);
  190. bGameOver = true;
  191. }
  192.  
  193. if ( iScoreR == 6 )
  194. {
  195. //Clear the screen, to show win
  196. //ClearScreen();
  197. DrawSpriteRotated(player1win, screen.x, screen.y, screen.fWidth, screen.fHeight, 0);
  198. FrameworkUpdate();
  199. Sleep(5000);
  200. bGameOver = true;
  201. }
  202.  
  203.  
  204. //system ("Pause");
  205. //break;
  206. }
  207.  
  208. if ( bGameOver == true )
  209. {
  210. ClearScreen();
  211. int iScoreL = 0;
  212. int iScoreR = 0;
  213. bGameOver = false;
  214. GameLoop();
  215. //break;
  216.  
  217. }
  218. //End Draw on screen
  219.  
  220.  
  221. //Stop it from running too fast! Sleep ZZzzz
  222. //Sleep(25);
  223.  
  224. } while (FrameworkUpdate()); //Do some secret stuff,
  225.  
  226. //free textures from memory
  227. FreeTexture(bgtex);
  228. FreeTexture(menutex);
  229. FreeTexture(balltex);
  230. FreeTexture(paddletex);
  231. FreeTexture(iScore0);
  232. FreeTexture(iScore1);
  233. FreeTexture(iScore2);
  234. FreeTexture(iScore3);
  235. FreeTexture(iScore4);
  236. FreeTexture(iScore5);
  237. FreeTexture(iScore6);
  238. FreeTexture(player1win);
  239. FreeTexture(player2win);
  240. FreeTexture(cursortex);
  241. FreeTexture(howtoplaytex);
  242.  
  243. }
Add Comment
Please, Sign In to add comment