Advertisement
Suby

functions.cpp

Apr 27th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "SDL_image.h"
  3. #include <string>
  4. #include "constants.h"
  5. #include "functions.h"
  6. #include "globals.h"
  7. #include "classes.h"
  8.  
  9. SDL_Surface *load_image( std::string filename )
  10. {
  11. SDL_Surface* loadedImage = NULL;
  12.  
  13. SDL_Surface* optimizedImage = NULL;
  14.  
  15. loadedImage = IMG_Load( filename.c_str() );
  16.  
  17. if( loadedImage != NULL )
  18. {
  19. optimizedImage = SDL_DisplayFormat( loadedImage );
  20.  
  21. SDL_FreeSurface( loadedImage );
  22.  
  23. if( optimizedImage != NULL )
  24. {
  25. SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
  26. }
  27. }
  28.  
  29. return optimizedImage;
  30. }
  31.  
  32. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
  33. {
  34. SDL_Rect offset;
  35.  
  36. offset.x = x;
  37. offset.y = y;
  38.  
  39. SDL_BlitSurface( source, clip, destination, &offset );
  40. }
  41.  
  42. bool init()
  43. {
  44. if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  45. {
  46. return false;
  47. }
  48.  
  49. screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  50.  
  51. if( screen == NULL )
  52. {
  53. return false;
  54. }
  55.  
  56. SDL_WM_SetCaption( "Dan's Tetris", NULL );
  57.  
  58. if( TTF_Init() == -1 )
  59. {
  60. return false;
  61. }
  62.  
  63. return true;
  64. }
  65.  
  66. bool load_files()
  67. {
  68. skyrider = load_image( "skyrider.png" );
  69.  
  70. if( skyrider == NULL )
  71. {
  72. return false;
  73. }
  74.  
  75. JShapeBlock = load_image( "JShapeBlock.png" );
  76.  
  77. if( JShapeBlock == NULL )
  78. {
  79. return false;
  80. }
  81.  
  82. LShapeBlock = load_image( "LShapeBlock.png" );
  83.  
  84. if( LShapeBlock == NULL )
  85. {
  86. return false;
  87. }
  88.  
  89. OShapeBlock = load_image( "OShapeBlock.png" );
  90.  
  91. if( OShapeBlock == NULL )
  92. {
  93. return false;
  94. }
  95.  
  96. ZShapeBlock = load_image( "ZShapeBlock.png" );
  97.  
  98. if( ZShapeBlock == NULL )
  99. {
  100. return false;
  101. }
  102.  
  103. TShapeBlock = load_image( "TShapeBlock.png" );
  104.  
  105. if( TShapeBlock == NULL )
  106. {
  107. return false;
  108. }
  109.  
  110. SShapeBlock = load_image( "SShapeBlock.png" );
  111.  
  112. if( SShapeBlock == NULL )
  113. {
  114. return false;
  115. }
  116.  
  117. font = TTF_OpenFont( "pirulen.ttf", 28 );
  118. SDL_Color WhiteTextColor = {255, 255, 255};
  119.  
  120. LinesClearedText = TTF_RenderText_Solid( font, "Lines", WhiteTextColor );
  121. GameOverText = TTF_RenderText_Solid( font, "Game Over", WhiteTextColor );
  122. ScoreText = TTF_RenderText_Solid( font, "Score", WhiteTextColor );
  123. PlayAgainText = TTF_RenderText_Solid( font, "Press Enter To", WhiteTextColor );
  124. PlayAgain2Text = TTF_RenderText_Solid( font, "Play Again", WhiteTextColor );
  125.  
  126. return true;
  127. }
  128.  
  129. void clean_up()
  130. {
  131. SDL_FreeSurface( skyrider );
  132. SDL_FreeSurface( JShapeBlock );
  133. SDL_FreeSurface( LShapeBlock );
  134. SDL_FreeSurface( OShapeBlock );
  135. SDL_FreeSurface( ZShapeBlock );
  136. SDL_FreeSurface( TShapeBlock );
  137. SDL_FreeSurface( SShapeBlock );
  138. SDL_FreeSurface( LinesClearedText );
  139. SDL_FreeSurface( ScoreText );
  140. SDL_FreeSurface( GameOverText );
  141. SDL_FreeSurface( PlayAgainText );
  142. SDL_FreeSurface( PlayAgain2Text );
  143. SDL_FreeSurface( ScoreNumberText );
  144. SDL_FreeSurface( LinesClearedNumberText );
  145.  
  146. TTF_CloseFont( font );
  147.  
  148. TTF_Quit();
  149.  
  150. SDL_Quit();
  151. }
  152.  
  153. void CheckForLine(Block *aBlock, int maxsize, int &linesclearedint, int &scorevalue)
  154. {
  155. int counter = 0, RowCounter = 0;
  156. const int NumberOfRows = 18;
  157. int Row[NumberOfRows] = {0};
  158.  
  159. while (counter < (maxsize - 1))
  160. {
  161. while (RowCounter < (NumberOfRows - 1))
  162. {
  163. if (aBlock[counter].Gety() == 35 * (RowCounter + 1))
  164. Row[RowCounter]++;
  165. RowCounter++;
  166. }
  167.  
  168. RowCounter = 0;
  169. counter++;
  170. }
  171.  
  172. RowCounter = 0;
  173. counter = 0;
  174. while (RowCounter < NumberOfRows)
  175. {
  176. if (Row[RowCounter] == 10)
  177. {
  178. while (counter < maxsize - 1)
  179. {
  180. if (aBlock[counter].Gety() == 35 * (RowCounter + 1))
  181. {
  182. aBlock[counter].Sety(-1);
  183. aBlock[counter].Setx(-1);
  184. aBlock[counter].SetShowBlock(0);
  185. }
  186. counter++;
  187. }
  188. }
  189. counter = 0;
  190. RowCounter++;
  191. }
  192.  
  193. counter = 0;
  194. RowCounter = 0;
  195. while (counter < (maxsize - 1))
  196. {
  197. while (RowCounter < NumberOfRows)
  198. {
  199. if (Row[RowCounter] == 10)
  200. {
  201. if (aBlock[counter].Gety() != -1 && aBlock[counter].Gety() <= 35 * (RowCounter + 1))
  202. aBlock[counter].Sety(aBlock[counter].Gety() + 35);
  203. }
  204. RowCounter++;
  205. }
  206. RowCounter = 0;
  207. counter++;
  208. }
  209.  
  210. RowCounter = 0;
  211. int temp = 0;
  212. bool CheckIfRunAgain = 0;
  213. while (RowCounter < NumberOfRows)
  214. {
  215. if (Row[RowCounter] == 10)
  216. {
  217. CheckIfRunAgain = 1;
  218. linesclearedint++;
  219. temp++;
  220. }
  221. RowCounter++;
  222. }
  223.  
  224. scorevalue = scorevalue + (17 * (temp + 1));
  225.  
  226. if (CheckIfRunAgain == 1)
  227. CheckForLine(aBlock, maxsize, linesclearedint, scorevalue);
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement