Advertisement
Suby

main.cpp

Apr 27th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "SDL_image.h"
  3. #include "SDL_ttf.h"
  4. #include <string>
  5. #include "constants.h"
  6. #include "classes.h"
  7. #include "functions.h"
  8. #include "globals.h"
  9. #include <ctime>
  10. #include <cmath>
  11. #include <string>
  12. #include <sstream>
  13.  
  14. //10 wide, 18 height. blocks 35 wide. going to be 640 high, 350 wide, then the remainder of it will be the next block
  15.  
  16. int main( int argc, char* args[] )
  17. {
  18. srand( int( time( NULL ) ) );
  19.  
  20. bool quit = false;
  21.  
  22. Timer moveshapedown;
  23. Timer HoldKeyDown;
  24.  
  25. if( init() == false )
  26. {
  27. return 1;
  28. }
  29.  
  30. if( load_files() == false )
  31. {
  32. return 1;
  33. }
  34.  
  35. SDL_Rect playablearea = {0, 0, 350, 640};
  36.  
  37. int Scoreint = 0;
  38. std::stringstream ScoreDisplayText;
  39. ScoreDisplayText << Scoreint;
  40.  
  41. int LinesClearedint = 0;
  42. std::stringstream LinesClearedDisplayText;
  43. LinesClearedDisplayText << LinesClearedint;
  44.  
  45. SDL_Color WhiteTextColor = {255, 255, 255};
  46.  
  47. ScoreNumberText = TTF_RenderText_Solid( font, ScoreDisplayText.str().c_str(), WhiteTextColor );
  48. LinesClearedNumberText = TTF_RenderText_Solid( font, LinesClearedDisplayText.str().c_str(), WhiteTextColor );
  49.  
  50. int LinesClearedTextWidth = 0;
  51. int ScoreTextWidth = 0;
  52. int GameOverTextWidth = 0;
  53. int GameOverTextHeight = 0;
  54. int PlayAgainTextWidth = 0;
  55. int ScoreNumberTextWidth = 0;
  56. int LinesClearedNumberTextWidth = 0;
  57. int PlayAgain2TextWidth = 0;
  58.  
  59. TTF_SizeText(font, "Lines", &LinesClearedTextWidth, 0);
  60. TTF_SizeText(font, "Score", &ScoreTextWidth, 0);
  61. TTF_SizeText(font, "Game Over", &GameOverTextWidth, &GameOverTextHeight);
  62. TTF_SizeText(font, "Press Enter To", &PlayAgainTextWidth, 0);
  63. TTF_SizeText(font, "Play again", &PlayAgain2TextWidth, 0);
  64. TTF_SizeText(font, "0", &ScoreNumberTextWidth, 0);
  65. TTF_SizeText(font, "9", &LinesClearedNumberTextWidth, 0);
  66.  
  67. int LinesClearedTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - LinesClearedTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  68. int LinesClearedTextyLocation = 200;
  69. int LinesClearedNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - LinesClearedNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  70. int LinesClearedNumberTextyLocation = LinesClearedTextyLocation + 50;
  71.  
  72. int ScoreTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  73. int ScoreTextyLocation = 500;
  74. int ScoreTextxLocation2 = ((SCREEN_WIDTH - ScoreTextWidth) / 2);
  75.  
  76.  
  77. int GameOverTextxLocation = ((SCREEN_WIDTH - GameOverTextWidth) / 2);
  78. int GameOverTextyLocation = ((SCREEN_HEIGHT - GameOverTextHeight) / 2) - 50;
  79.  
  80. int PlayAgainTextxLocation = ((SCREEN_WIDTH - PlayAgainTextWidth) / 2);
  81. int PlayAgainTextyLocation = ((SCREEN_HEIGHT - GameOverTextHeight) / 2);
  82. int PlayAgain2xLocation = (SCREEN_WIDTH - PlayAgain2TextWidth) / 2;
  83.  
  84. int ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  85. int ScoreNumberTextyLocation = ScoreTextyLocation + 50;
  86.  
  87.  
  88. //(SCREEN_WIDTH - TextWidth) / 2;
  89. int CenterText = 0;
  90. int ScoreTempHolder = 0;
  91. int LinesClearedTempHolder = 0;
  92. int MaxSize = 200;
  93. int ShapeIndex = 0, BlockIndex = 0, NextBlockShape = 0, tempvalue = 0;
  94. bool GetNewShapeCheck = 0, ClearLineCheck = 0;
  95. int BlockMarker[4] = {0};
  96.  
  97. Block SidebarBlock[4];
  98. Shape SidebarShape;
  99.  
  100. Block* myBlock = new Block[MaxSize];
  101. Shape myShape;
  102.  
  103. NextBlockShape = 1 + (rand() % 7);
  104. myShape.SetShape(NextBlockShape, myBlock, MaxSize);
  105.  
  106. NextBlockShape = 1 + (rand() % 7);
  107. SidebarShape.SpawnNextShapeInSidebar(NextBlockShape, SidebarBlock);
  108.  
  109. moveshapedown.start();
  110.  
  111. while( quit == false )
  112. {
  113. while( SDL_PollEvent( &event ) )
  114. {
  115. myShape.handle_input();
  116. if (myShape.GetIfKeyHeldDown() == 1)
  117. if (HoldKeyDown.is_started() == false)
  118. HoldKeyDown.start();
  119.  
  120. if( event.type == SDL_QUIT )
  121. {
  122. quit = true;
  123. }
  124. }
  125.  
  126. LinesClearedTempHolder = LinesClearedint;
  127. ScoreTempHolder = Scoreint;
  128.  
  129. if (myShape.GetIfKeyHeldDown() == false)
  130. if (HoldKeyDown.is_started() == true)
  131. {
  132. HoldKeyDown.stop();
  133. myShape.SetFast(0);
  134. }
  135.  
  136. if (myShape.GetIfKeyHeldDown() == true)
  137. if (HoldKeyDown.get_ticks() >= 400)
  138. myShape.SetFast(1);
  139.  
  140. if (myShape.GetPause() == false)
  141. {
  142. myShape.Rotate(myBlock, myShape, MaxSize);
  143.  
  144. GetNewShapeCheck = myShape.move(myBlock, myShape, MaxSize);
  145. if (GetNewShapeCheck == true)
  146. {
  147. myShape.SetOrientation(1);
  148. Scoreint += 17;
  149. }
  150. myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
  151.  
  152. if (moveshapedown.get_ticks() >= 600)
  153. {
  154. GetNewShapeCheck = myShape.MoveShapeDown(myBlock, myShape, MaxSize);
  155. if (GetNewShapeCheck == true)
  156. myShape.SetOrientation(1);
  157. moveshapedown.start();
  158. }
  159.  
  160. myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
  161.  
  162. SidebarShape.SpawnNextShapeInSidebar(NextBlockShape, SidebarBlock);
  163. }
  164.  
  165. SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 50, 40, 20 ) );
  166.  
  167. SDL_FillRect( screen, &playablearea, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
  168.  
  169. apply_surface( LinesClearedTextxLocation, 200, LinesClearedText, screen );
  170.  
  171. if (Scoreint != ScoreTempHolder)
  172. {
  173. if (Scoreint > 9 && Scoreint < 99 && CenterText != 1)
  174. {
  175. TTF_SizeText(font, "00", &ScoreNumberTextWidth, 0);
  176. ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  177. CenterText = 1;
  178. }
  179. if (Scoreint > 99 && Scoreint < 999 && CenterText != 2)
  180. {
  181. TTF_SizeText(font, "000", &ScoreNumberTextWidth, 0);
  182. ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  183. CenterText = 2;
  184. }
  185. if (Scoreint > 999 && Scoreint < 9999 && CenterText != 3)
  186. {
  187. TTF_SizeText(font, "0000", &ScoreNumberTextWidth, 0);
  188. ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  189. CenterText = 3;
  190. }
  191. if (Scoreint > 9999 && Scoreint < 99999 && CenterText != 4)
  192. {
  193. TTF_SizeText(font, "00000", &ScoreNumberTextWidth, 0);
  194. ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  195. CenterText = 4;
  196. }
  197. if (Scoreint > 99999 && Scoreint < 999999 && CenterText != 5)
  198. {
  199. TTF_SizeText(font, "000000", &ScoreNumberTextWidth, 0);
  200. ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
  201. CenterText = 5;
  202. }
  203. ScoreDisplayText.str("");
  204. ScoreDisplayText << Scoreint;
  205. ScoreNumberText = TTF_RenderText_Solid( font, ScoreDisplayText.str().c_str(), WhiteTextColor );
  206. ScoreTempHolder = Scoreint;
  207. }
  208.  
  209.  
  210. if (LinesClearedint != LinesClearedTempHolder)
  211. {
  212. LinesClearedDisplayText.str("");
  213. LinesClearedDisplayText << LinesClearedint;
  214. LinesClearedNumberText = TTF_RenderText_Solid( font, LinesClearedDisplayText.str().c_str(), WhiteTextColor );
  215. LinesClearedTempHolder = LinesClearedint;
  216. }
  217. apply_surface( LinesClearedNumberTextxLocation, LinesClearedNumberTextyLocation, LinesClearedNumberText, screen );
  218.  
  219. apply_surface( ScoreTextxLocation, ScoreTextyLocation, ScoreText, screen );
  220.  
  221. apply_surface( ScoreNumberTextxLocation, ScoreNumberTextyLocation, ScoreNumberText, screen );
  222.  
  223. BlockIndex = 0;
  224. while (BlockIndex < MaxSize)
  225. {
  226. myBlock[BlockIndex].DisplayBlocks();
  227. BlockIndex++;
  228. }
  229. BlockIndex = 0;
  230. while (BlockIndex < 4)
  231. {
  232. SidebarBlock[BlockIndex].DisplayBlocks();
  233. BlockIndex++;
  234. }
  235.  
  236. if (myShape.CheckGameOver(myBlock, MaxSize) == true)
  237. {
  238. myShape.SetPause(1);
  239. SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0 ) );
  240. apply_surface( GameOverTextxLocation, GameOverTextyLocation, GameOverText, screen );
  241. apply_surface( PlayAgainTextxLocation, PlayAgainTextyLocation, PlayAgainText, screen );
  242. apply_surface( PlayAgain2xLocation, PlayAgainTextyLocation + 30, PlayAgain2Text, screen );
  243. apply_surface( ScoreNumberTextxLocation, ScoreNumberTextyLocation, ScoreNumberText, screen );
  244. apply_surface( ScoreTextxLocation, ScoreTextyLocation, ScoreText, screen );
  245. if (myShape.GetRestart() == true)
  246. {
  247. myShape.SetPause(0);
  248. BlockIndex = 0;
  249. while (BlockIndex < MaxSize - 1)
  250. {
  251. myBlock[BlockIndex].Sety(-1);
  252. myBlock[BlockIndex].Setx(-1);
  253. myBlock[BlockIndex].SetShowBlock(0);
  254. BlockIndex++;
  255. }
  256. myShape.SetRestart(false);
  257. GetNewShapeCheck = true;
  258. myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
  259. }
  260. }
  261.  
  262. if( SDL_Flip( screen ) == -1 )
  263. {
  264. return 1;
  265. }
  266.  
  267. }
  268. delete [] myBlock;
  269. myBlock = NULL;
  270.  
  271. return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement