Forum: SDL hjälp (igen)

Forum huvudsida -> Programmering -> SDL hjälp (igen)

Sidor: 1

Till botten

supermario89 13:04 - 19:e Mars 2006 | Post #1
Medlem
Inlägg: 12


Skicka PM
Jag har precis börjat med SDL i C++ och det mesta går bra utom en sak, jag håller på med ett pong spel och allt funkar som det ska, men problemet är att programmet tar jättemycket RAM-minne. Först tar det lite men allt eftersom man har programet är igång så tar det mer och mer..

Här kommer källkoden, om nån vet vad som är fel, var snäll och berätta Smiley
Tack på förhand.

  1.  
  2. #include <SDL/SDL.h>
  3. #include <iostream>
  4. #include <conio.h>
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int newgame();
  11. int end();
  12. int win();
  13. int computer();
  14. int checkscore();
  15. void score();
  16. void DrawALL();
  17. void Moveball();
  18. void collision();
  19.  
  20. int player1score = 0;
  21. int player2score = 0;
  22. int maxscore = 10;
  23.  
  24. int ballx = 394;
  25. int bally = 294;
  26. int player1x = 20;
  27. int player1y = 280;
  28. int player2x = 764;
  29. int player2y = 280;
  30. int ballspeedx = 3;
  31. int ballspeedy =1;
  32.  
  33.  
  34. SDL_Event poolevent;
  35. SDL_Event event;
  36. SDL_Surface * screen;
  37. SDL_Surface * score1;
  38. SDL_Surface * score2;
  39. SDL_Surface * back3=SDL_LoadBMP("back3.BMP");
  40. SDL_Surface * back2=SDL_LoadBMP("back2.BMP");
  41. SDL_Surface * back=SDL_LoadBMP("back.BMP");
  42. SDL_Surface * player1=SDL_LoadBMP("player1.BMP");
  43. SDL_Surface * player2=SDL_LoadBMP("player1.BMP");
  44. SDL_Surface * ball=SDL_LoadBMP("ball.BMP");
  45. SDL_Surface * player1win;
  46. SDL_Surface * player2win;
  47.  
  48.  
  49.  
  50. void DrawIMG(SDL_Surface *img, int x, int y)
  51. {
  52. SDL_Rect dest;
  53. dest.x = x;
  54. dest.y = y;
  55. SDL_BlitSurface(img, NULL, screen, &dest);
  56. }
  57.  
  58.  
  59.  
  60. int main( int argc, char *arcv[])
  61. {
  62. if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0 )
  63. {
  64. std::cout<<"ERROR INITILASING SDL!" <<std::endl<<"Reason: "<<SDL_GetError();
  65. exit(1);
  66. }
  67. atexit(SDL_Quit);
  68. screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_HWPALETTE);
  69. SDL_WM_SetCaption( "Pong clone", "Pong clone" );
  70. SDL_ShowCursor( SDL_DISABLE );
  71. if(screen == NULL)
  72. {
  73. std::cout<<"ERROR SETTING 800x600x32 VIDEO!"<<std::endl<<"Reason: "<<SDL_GetError();
  74. exit(1);
  75. }
  76. while(1)
  77. {
  78. DrawALL();
  79. Moveball();
  80. collision();
  81. score();
  82. if (player1score == maxscore || player2score == maxscore)
  83. {
  84. win();
  85. }
  86. SDL_Event event;
  87. Uint8* keys;
  88. while( SDL_PollEvent(&event) )
  89. {
  90. if(event.type == SDL_QUIT) { return 0; }
  91. if(event.type == SDL_KEYDOWN)
  92. {
  93. if(event.key.keysym.sym==SDLK_ESCAPE) { return 0; }
  94. }
  95. }
  96. keys = SDL_GetKeyState(NULL);
  97.  
  98. if ( keys[SDLK_w] && player1y >= 0 )
  99. {
  100. player1y -= 2;
  101. }
  102. if ( keys[SDLK_s] && player1y <= 540 )
  103. {
  104. player1y += 2;
  105. }
  106. if ( keys[SDLK_UP] && player2y >=0)
  107. {
  108. player2y -=2;
  109. }
  110. if ( keys[SDLK_DOWN] && player2y <=540 )
  111. {
  112. player2y +=2;
  113. }
  114. }
  115. }
  116.  
  117. void DrawALL()
  118. {
  119. DrawIMG(player1win, 270, 250);
  120. DrawIMG(player2win, 270, 250);
  121. DrawIMG(back2, ballx-2, bally-2);
  122. DrawIMG(back, player1x-5, player1y-5);
  123. DrawIMG(back, player2x-5, player2y-5);
  124. DrawIMG(player1, player1x, player1y);
  125. DrawIMG(player2, player2x, player2y);
  126. DrawIMG(back3, 300 ,0);
  127. DrawIMG(back3, 450, 0);
  128. DrawIMG(score1, 300, 0);
  129. DrawIMG(score2, 450, 0);
  130. if (player1score < 10 && player2score < 10)
  131. {
  132. DrawIMG(ball, ballx, bally);
  133. }
  134. SDL_Flip(screen);
  135. }
  136. void Moveball()
  137. {
  138. ballx = ballx - ballspeedx;
  139. bally = bally - ballspeedy;
  140. DrawALL();
  141. }
  142. void score()
  143. {
  144. if (player1score == 0)
  145. {
  146. score1=SDL_LoadBMP("0.BMP");
  147. }
  148. if (player1score == 1)
  149. {
  150. score1=SDL_LoadBMP("1.BMP");
  151. }
  152. if (player1score == 2)
  153. {
  154. score1=SDL_LoadBMP("2.BMP");
  155. }
  156. if (player1score == 3)
  157. {
  158. score1=SDL_LoadBMP("3.BMP");
  159. }
  160. if (player1score == 4)
  161. {
  162. score1=SDL_LoadBMP("4.BMP");
  163. }
  164. if (player1score == 5)
  165. {
  166. score1=SDL_LoadBMP("5.BMP");
  167. }
  168. if (player1score == 6)
  169. {
  170. score1=SDL_LoadBMP("6.BMP");
  171. }
  172. if (player1score == 7)
  173. {
  174. score1=SDL_LoadBMP("7.BMP");
  175. }
  176. if (player1score == 8)
  177. {
  178. score1=SDL_LoadBMP("8.BMP");
  179. }
  180. if (player1score == 9)
  181. {
  182. score1=SDL_LoadBMP("9.BMP");
  183. }
  184. if (player1score == 10)
  185. {
  186. score1=SDL_LoadBMP("10.BMP");
  187. }
  188. if (player2score == 0)
  189. {
  190. score2=SDL_LoadBMP("0.BMP");
  191. }
  192. if (player2score == 1)
  193. {
  194. score2=SDL_LoadBMP("1.BMP");
  195. }
  196. if (player2score == 2)
  197. {
  198. score2=SDL_LoadBMP("2.BMP");
  199. }
  200. if (player2score == 3)
  201. {
  202. score2=SDL_LoadBMP("3.BMP");
  203. }
  204. if (player2score == 4)
  205. {
  206. score2=SDL_LoadBMP("4.BMP");
  207. }
  208. if (player2score == 5)
  209. {
  210. score2=SDL_LoadBMP("5.BMP");
  211. }
  212. if (player2score == 6)
  213. {
  214. score2=SDL_LoadBMP("6.BMP");
  215. }
  216. if (player2score == 7)
  217. {
  218. score2=SDL_LoadBMP("7.BMP");
  219. }
  220. if (player2score == 8)
  221. {
  222. score2=SDL_LoadBMP("8.BMP");
  223. }
  224. if (player2score == 9)
  225. {
  226. score2=SDL_LoadBMP("9.BMP");
  227. }
  228. if (player2score == 10)
  229. {
  230. score1=SDL_LoadBMP("10.BMP");
  231. }
  232. }
  233.  
  234. int win()
  235. {
  236. SDL_Event event;
  237. Uint8* keys;
  238. while( SDL_PollEvent(&event) )
  239. {
  240. if(event.type == SDL_QUIT) { return 0; }
  241. if(event.type == SDL_KEYDOWN)
  242. {
  243. if(event.key.keysym.sym==SDLK_ESCAPE) { return 0; }
  244. }
  245. }
  246. keys = SDL_GetKeyState(NULL);
  247. if (player1score == maxscore)
  248. {
  249. player1win=SDL_LoadBMP("player1win.BMP");
  250. DrawALL();
  251. ballspeedx = 0;
  252. ballspeedy = 0;
  253. if ( keys[SDLK_w])
  254. {
  255. newgame();
  256. }
  257. }
  258. if (player2score == maxscore)
  259. {
  260. player2win=SDL_LoadBMP("player2win.BMP");
  261. DrawALL();
  262. ballspeedx = 0;
  263. ballspeedy = 0;
  264. if ( keys[SDLK_w])
  265. {
  266. newgame();
  267. }
  268. }
  269. }
  270.  
  271. int end()
  272. {
  273. }
  274.  
  275. int newgame()
  276. {
  277. player1win = NULL;
  278. player2win = NULL;
  279. DrawIMG(player1win, 270, 250);
  280. DrawIMG(player2win, 270, 250);
  281. DrawIMG(back, player1x-5, player1y-5);
  282. DrawIMG(back, player2x-5, player2y-5);
  283.  
  284. player1score = 0;
  285. player2score = 0;
  286. maxscore = 10;
  287.  
  288. ballx = 394;
  289. bally = 294;
  290. player1x = 20;
  291. player1y = 280;
  292. player2x = 764;
  293. player2y = 280;
  294. ballspeedx = 3;
  295. ballspeedy =1;
  296. SDL_Delay(2000);
  297. }
  298.  
  299. void collision()
  300. {
  301. if (bally < 0)
  302. {
  303. ballspeedy = -ballspeedy;
  304. }
  305. if (bally > 590)
  306. {
  307. ballspeedy = -ballspeedy;
  308. }
  309. if (ballx < -15)
  310. {
  311. DrawALL();
  312. player2score = player2score+1;
  313. ballx = 394;
  314. bally = 294;
  315. SDL_Delay(100);
  316. }
  317. if (ballx > 805)
  318. {
  319. DrawALL();
  320. player1score = player1score+1;
  321. ballx = 394;
  322. bally = 294;
  323. SDL_Delay(100);
  324. }
  325. if (ballx > player1x && ballx < player1x+16 && bally > player1y && bally < player1y +60)
  326. {
  327. ballspeedx = -ballspeedx;
  328. ballspeedy = ballspeedy;
  329. }
  330. if (ballx > player2x-16 && ballx < player2x+32 && bally > player2y && bally < player2y+60)
  331. {
  332. ballspeedx = -ballspeedx;
  333. ballspeedy = ballspeedy;
  334. }
  335. }


-------------------------
Ingen signatur!



Nissebosselasse 13:16 - 19:e Mars 2006 | Post #2
Medlem
Inlägg: 490


Skicka PM
Det enda jag kan se är att funktionen score anropas varje gång i loopen, och att du i den laddar bilder med SDL_LoadBMP, utan att frigöra den föregående bilden (SDL_FreeSurface). Kanske finns din RAM-minnesätare där?

-------------------------
http://pushingcows.se
Gula Nallen



FunkyChicken 14:11 - 19:e Mars 2006 | Post #3
Nyhetsredaktör
Inlägg: 800


Skicka PM
Skulle nog också satsa på det som källa till problemet. Du behåller visserligen samma variabel (score1 och score2) men nånting säger mig att sdl fungerar på samma vis som en directx-grej jag undersökte lite närmare en gång, nämligen som så att så länge du använder loadbmp så läggs ny surfaces "på hög" och dina variabler (som egentligen är pekare?) får bara referenser till den senaste surfacen. Bara för att ingenting längre pekar till den gamla surfaces, betyder det inte att den försvinner. Använd som sagt SDL_FreeSurface.

För övrigt skulle du bespara dig ett antal if-satser genom att slå ihop player1score och ".BMP" och ladda den strängen... Smiley




Sidor: 1

Forum huvudsida -> Programmering -> SDL hjälp (igen)
Atom feed

Du får inte posta i den här tråden | Till toppen