Guest User

Untitled

a guest
Apr 15th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1. // Snooping through the files now are we?
  2.  
  3. // Compile With: g++ main.cpp loadImage.h playSound.h -lSDL2 -lSDL2_image -lSDL2_mixer -Wall -o "Butter Game"
  4.  
  5. #include <SDL2/SDL.h>
  6. #include <SDL2/SDL_image.h>
  7.  
  8. #include <iostream>
  9.  
  10. SDL_Texture* imageSlot1;
  11. SDL_Texture* imageSlot2;
  12. SDL_Texture* imageSlot3;
  13. SDL_Texture* imageSlot4;
  14. SDL_Texture* imageSlot5;
  15. SDL_Texture* imageSlot6;
  16. SDL_Texture* imageSlot7;
  17. SDL_Texture* imageSlot8;
  18. SDL_Texture* imageSlot9;
  19. SDL_Texture* imageSlot10;
  20.  
  21. SDL_Rect imageRect1;
  22. SDL_Rect imageRect2;
  23. SDL_Rect imageRect3;
  24. SDL_Rect imageRect4;
  25. SDL_Rect imageRect5;
  26. SDL_Rect imageRect6;
  27. SDL_Rect imageRect7;
  28. SDL_Rect imageRect8;
  29. SDL_Rect imageRect9;
  30. SDL_Rect imageRect10;
  31.  
  32. SDL_Rect hitbox1;
  33. SDL_Rect hitbox2;
  34. SDL_Rect hitbox3;
  35. SDL_Rect hitbox4;
  36. SDL_Rect hitbox5;
  37. SDL_Rect hitbox6;
  38. SDL_Rect hitbox7;
  39. SDL_Rect hitbox8;
  40. SDL_Rect hitbox9;
  41. SDL_Rect hitbox10;
  42. SDL_Rect hitbox11;
  43. SDL_Rect hitbox12;
  44. SDL_Rect hitbox13;
  45. SDL_Rect hitbox14;
  46. SDL_Rect hitbox15;
  47. SDL_Rect hitbox16;
  48. SDL_Rect hitbox17;
  49. SDL_Rect hitbox18;
  50. SDL_Rect hitbox19;
  51. SDL_Rect hitbox20;
  52.  
  53. int hitboxIndex = 1;
  54.  
  55. int imageIndex = 1;
  56.  
  57. extern SDL_Renderer* renderer;
  58. extern void deleteButtons();
  59.  
  60. int loadImage(const char* path, int posX, int posY, int sizeX, int sizeY)
  61. {
  62. if(imageIndex > 10)
  63. {
  64. imageIndex = 1;
  65. }
  66.  
  67. switch(imageIndex)
  68. {
  69. case 1:
  70. imageRect1.x = posX;
  71. imageRect1.y = posY;
  72. imageRect1.w = sizeX;
  73. imageRect1.h = sizeY;
  74.  
  75. SDL_Surface* surface1;
  76.  
  77. surface1 = IMG_Load(path);
  78.  
  79. imageSlot1 = SDL_CreateTextureFromSurface(renderer, surface1);
  80.  
  81. SDL_FreeSurface(surface1);
  82.  
  83. imageIndex++;
  84.  
  85. std::cout << "imageIndex 1 loaded." << std::endl;
  86.  
  87. break;
  88. case 2:
  89. imageRect2.x = posX;
  90. imageRect2.y = posY;
  91. imageRect2.w = sizeX;
  92. imageRect2.h = sizeY;
  93.  
  94. SDL_Surface* surface2;
  95.  
  96. surface2 = IMG_Load(path);
  97.  
  98. imageSlot2 = SDL_CreateTextureFromSurface(renderer, surface2);
  99.  
  100. SDL_FreeSurface(surface2);
  101.  
  102. imageIndex++;
  103.  
  104. std::cout << "imageIndex 2 loaded." << std::endl;
  105.  
  106. break;
  107. case 3:
  108. imageRect3.x = posX;
  109. imageRect3.y = posY;
  110. imageRect3.w = sizeX;
  111. imageRect3.h = sizeY;
  112.  
  113. SDL_Surface* surface3;
  114.  
  115. surface3 = IMG_Load(path);
  116.  
  117. imageSlot3 = SDL_CreateTextureFromSurface(renderer, surface3);
  118.  
  119. SDL_FreeSurface(surface3);
  120.  
  121. imageIndex++;
  122.  
  123. std::cout << "imageIndex 3 loaded." << std::endl;
  124.  
  125. break;
  126. case 4:
  127. imageRect4.x = posX;
  128. imageRect4.y = posY;
  129. imageRect4.w = sizeX;
  130. imageRect4.h = sizeY;
  131.  
  132. SDL_Surface* surface4;
  133.  
  134. surface4 = IMG_Load(path);
  135.  
  136. imageSlot4 = SDL_CreateTextureFromSurface(renderer, surface4);
  137.  
  138. SDL_FreeSurface(surface4);
  139.  
  140. imageIndex++;
  141.  
  142. std::cout << "imageIndex 4 loaded." << std::endl;
  143.  
  144. break;
  145. case 5:
  146. imageRect5.x = posX;
  147. imageRect5.y = posY;
  148. imageRect5.w = sizeX;
  149. imageRect5.h = sizeY;
  150.  
  151. SDL_Surface* surface5;
  152.  
  153. surface5 = IMG_Load(path);
  154.  
  155. imageSlot5 = SDL_CreateTextureFromSurface(renderer, surface5);
  156.  
  157. SDL_FreeSurface(surface5);
  158.  
  159. imageIndex++;
  160.  
  161. std::cout << "imageIndex 5 loaded." << std::endl;
  162.  
  163. break;
  164. case 6:
  165. imageRect6.x = posX;
  166. imageRect6.y = posY;
  167. imageRect6.w = sizeX;
  168. imageRect6.h = sizeY;
  169.  
  170. SDL_Surface* surface6;
  171.  
  172. surface6 = IMG_Load(path);
  173.  
  174. imageSlot6 = SDL_CreateTextureFromSurface(renderer, surface6);
  175.  
  176. SDL_FreeSurface(surface6);
  177.  
  178. imageIndex++;
  179.  
  180. std::cout << "imageIndex 6 loaded." << std::endl;
  181.  
  182. break;
  183. case 7:
  184. imageRect7.x = posX;
  185. imageRect7.y = posY;
  186. imageRect7.w = sizeX;
  187. imageRect7.h = sizeY;
  188.  
  189. SDL_Surface* surface7;
  190.  
  191. surface7 = IMG_Load(path);
  192.  
  193. imageSlot7 = SDL_CreateTextureFromSurface(renderer, surface7);
  194.  
  195. SDL_FreeSurface(surface7);
  196.  
  197. imageIndex++;
  198.  
  199. std::cout << "imageIndex 7 loaded." << std::endl;
  200.  
  201. break;
  202. case 8:
  203. imageRect8.x = posX;
  204. imageRect8.y = posY;
  205. imageRect8.w = sizeX;
  206. imageRect8.h = sizeY;
  207.  
  208. SDL_Surface* surface8;
  209.  
  210. surface8 = IMG_Load(path);
  211.  
  212. imageSlot8 = SDL_CreateTextureFromSurface(renderer, surface8);
  213.  
  214. SDL_FreeSurface(surface8);
  215.  
  216. imageIndex++;
  217.  
  218. std::cout << "imageIndex 8 loaded." << std::endl;
  219.  
  220. break;
  221. case 9:
  222. imageRect9.x = posX;
  223. imageRect9.y = posY;
  224. imageRect9.w = sizeX;
  225. imageRect9.h = sizeY;
  226.  
  227. SDL_Surface* surface9;
  228.  
  229. surface9 = IMG_Load(path);
  230.  
  231. imageSlot9 = SDL_CreateTextureFromSurface(renderer, surface9);
  232.  
  233. SDL_FreeSurface(surface9);
  234.  
  235. imageIndex++;
  236.  
  237. std::cout << "imageIndex 9 loaded." << std::endl;
  238.  
  239. break;
  240. case 10:
  241. imageRect10.x = posX;
  242. imageRect10.y = posY;
  243. imageRect10.w = sizeX;
  244. imageRect10.h = sizeY;
  245.  
  246. SDL_Surface* surface10;
  247.  
  248. surface10 = IMG_Load(path);
  249.  
  250. imageSlot10 = SDL_CreateTextureFromSurface(renderer, surface10);
  251.  
  252. SDL_FreeSurface(surface10);
  253.  
  254. imageIndex++;
  255.  
  256. std::cout << "imageIndex 10 loaded." << std::endl;
  257.  
  258. break;
  259. }
  260.  
  261. return imageIndex;
  262. }
  263.  
  264. void loadSpecificImageSlot(const char* path, int posX, int posY, int sizeX, int sizeY, int imageSlot)
  265. {
  266. switch(imageSlot)
  267. {
  268. case 1:
  269. imageRect1.x = posX;
  270. imageRect1.y = posY;
  271. imageRect1.w = sizeX;
  272. imageRect1.h = sizeY;
  273.  
  274. SDL_Surface* surface1;
  275.  
  276. surface1 = IMG_Load(path);
  277.  
  278. imageSlot1 = SDL_CreateTextureFromSurface(renderer, surface1);
  279.  
  280. SDL_FreeSurface(surface1);
  281.  
  282. imageIndex++;
  283.  
  284. break;
  285. case 2:
  286. imageRect2.x = posX;
  287. imageRect2.y = posY;
  288. imageRect2.w = sizeX;
  289. imageRect2.h = sizeY;
  290.  
  291. SDL_Surface* surface2;
  292.  
  293. surface2 = IMG_Load(path);
  294.  
  295. imageSlot2 = SDL_CreateTextureFromSurface(renderer, surface2);
  296.  
  297. SDL_FreeSurface(surface2);
  298.  
  299. imageIndex++;
  300.  
  301. break;
  302. case 3:
  303. imageRect3.x = posX;
  304. imageRect3.y = posY;
  305. imageRect3.w = sizeX;
  306. imageRect3.h = sizeY;
  307.  
  308. SDL_Surface* surface3;
  309.  
  310. surface3 = IMG_Load(path);
  311.  
  312. imageSlot3 = SDL_CreateTextureFromSurface(renderer, surface3);
  313.  
  314. SDL_FreeSurface(surface3);
  315.  
  316. imageIndex++;
  317.  
  318. break;
  319. case 4:
  320. imageRect4.x = posX;
  321. imageRect4.y = posY;
  322. imageRect4.w = sizeX;
  323. imageRect4.h = sizeY;
  324.  
  325. SDL_Surface* surface4;
  326.  
  327. surface4 = IMG_Load(path);
  328.  
  329. imageSlot4 = SDL_CreateTextureFromSurface(renderer, surface4);
  330.  
  331. SDL_FreeSurface(surface4);
  332.  
  333. imageIndex++;
  334.  
  335. break;
  336. case 5:
  337. imageRect5.x = posX;
  338. imageRect5.y = posY;
  339. imageRect5.w = sizeX;
  340. imageRect5.h = sizeY;
  341.  
  342. SDL_Surface* surface5;
  343.  
  344. surface5 = IMG_Load(path);
  345.  
  346. imageSlot5 = SDL_CreateTextureFromSurface(renderer, surface5);
  347.  
  348. SDL_FreeSurface(surface5);
  349.  
  350. imageIndex++;
  351.  
  352. break;
  353. case 6:
  354. imageRect6.x = posX;
  355. imageRect6.y = posY;
  356. imageRect6.w = sizeX;
  357. imageRect6.h = sizeY;
  358.  
  359. SDL_Surface* surface6;
  360.  
  361. surface6 = IMG_Load(path);
  362.  
  363. imageSlot6 = SDL_CreateTextureFromSurface(renderer, surface6);
  364.  
  365. SDL_FreeSurface(surface6);
  366.  
  367. imageIndex++;
  368.  
  369. break;
  370. case 7:
  371. imageRect7.x = posX;
  372. imageRect7.y = posY;
  373. imageRect7.w = sizeX;
  374. imageRect7.h = sizeY;
  375.  
  376. SDL_Surface* surface7;
  377.  
  378. surface7 = IMG_Load(path);
  379.  
  380. imageSlot7 = SDL_CreateTextureFromSurface(renderer, surface7);
  381.  
  382. SDL_FreeSurface(surface7);
  383.  
  384. imageIndex++;
  385.  
  386. break;
  387. case 8:
  388. imageRect8.x = posX;
  389. imageRect8.y = posY;
  390. imageRect8.w = sizeX;
  391. imageRect8.h = sizeY;
  392.  
  393. SDL_Surface* surface8;
  394.  
  395. surface8 = IMG_Load(path);
  396.  
  397. imageSlot8 = SDL_CreateTextureFromSurface(renderer, surface8);
  398.  
  399. SDL_FreeSurface(surface8);
  400.  
  401. imageIndex++;
  402.  
  403. break;
  404. case 9:
  405. imageRect9.x = posX;
  406. imageRect9.y = posY;
  407. imageRect9.w = sizeX;
  408. imageRect9.h = sizeY;
  409.  
  410. SDL_Surface* surface9;
  411.  
  412. surface9 = IMG_Load(path);
  413.  
  414. imageSlot9 = SDL_CreateTextureFromSurface(renderer, surface9);
  415.  
  416. SDL_FreeSurface(surface9);
  417.  
  418. imageIndex++;
  419.  
  420. break;
  421. case 10:
  422. imageRect10.x = posX;
  423. imageRect10.y = posY;
  424. imageRect10.w = sizeX;
  425. imageRect10.h = sizeY;
  426.  
  427. SDL_Surface* surface10;
  428.  
  429. surface10 = IMG_Load(path);
  430.  
  431. imageSlot10 = SDL_CreateTextureFromSurface(renderer, surface10);
  432.  
  433. SDL_FreeSurface(surface10);
  434.  
  435. imageIndex++;
  436.  
  437. break;
  438. }
  439. }
  440.  
  441. void freeImageSlot(int imageSlot)
  442. {
  443. switch(imageSlot)
  444. {
  445. case 1:
  446. SDL_DestroyTexture(imageSlot1);
  447. break;
  448. case 2:
  449. SDL_DestroyTexture(imageSlot2);
  450. break;
  451. case 3:
  452. SDL_DestroyTexture(imageSlot3);
  453. break;
  454. case 4:
  455. SDL_DestroyTexture(imageSlot4);
  456. break;
  457. case 5:
  458. SDL_DestroyTexture(imageSlot5);
  459. break;
  460. case 6:
  461. SDL_DestroyTexture(imageSlot6);
  462. break;
  463. case 7:
  464. SDL_DestroyTexture(imageSlot7);
  465. break;
  466. case 8:
  467. SDL_DestroyTexture(imageSlot8);
  468. break;
  469. case 9:
  470. SDL_DestroyTexture(imageSlot9);
  471. break;
  472. case 10:
  473. SDL_DestroyTexture(imageSlot10);
  474. break;
  475. }
  476. }
  477.  
  478. void createHitbox(int posX, int posY, int width, int height)
  479. {
  480. switch(hitboxIndex)
  481. {
  482. case 1:
  483. hitbox1.x = posX;
  484. hitbox1.y = posY;
  485. hitbox1.w = width;
  486. hitbox1.h = height;
  487.  
  488. break;
  489. case 2:
  490. hitbox2.x = posX;
  491. hitbox2.y = posY;
  492. hitbox2.w = width;
  493. hitbox2.h = height;
  494.  
  495. break;
  496. case 3:
  497. hitbox3.x = posX;
  498. hitbox3.y = posY;
  499. hitbox3.w = width;
  500. hitbox1.h = height;
  501.  
  502. break;
  503. case 4:
  504. hitbox4.x = posX;
  505. hitbox4.y = posY;
  506. hitbox4.w = width;
  507. hitbox4.h = height;
  508.  
  509. break;
  510. case 5:
  511. hitbox5.x = posX;
  512. hitbox5.y = posY;
  513. hitbox5.w = width;
  514. hitbox5.h = height;
  515.  
  516. break;
  517. case 6:
  518. hitbox6.x = posX;
  519. hitbox6.y = posY;
  520. hitbox6.w = width;
  521. hitbox6.h = height;
  522.  
  523. break;
  524. case 7:
  525. hitbox7.x = posX;
  526. hitbox7.y = posY;
  527. hitbox7.w = width;
  528. hitbox7.h = height;
  529.  
  530. break;
  531. case 8:
  532. hitbox8.x = posX;
  533. hitbox8.y = posY;
  534. hitbox8.w = width;
  535. hitbox8.h = height;
  536.  
  537. break;
  538. case 9:
  539. hitbox9.x = posX;
  540. hitbox9.y = posY;
  541. hitbox9.w = width;
  542. hitbox9.h = height;
  543.  
  544. break;
  545. case 10:
  546. hitbox10.x = posX;
  547. hitbox10.y = posY;
  548. hitbox10.w = width;
  549. hitbox10.h = height;
  550.  
  551. break;
  552. case 11:
  553. hitbox11.x = posX;
  554. hitbox11.y = posY;
  555. hitbox11.w = width;
  556. hitbox11.h = height;
  557.  
  558. break;
  559. case 12:
  560. hitbox12.x = posX;
  561. hitbox12.y = posY;
  562. hitbox12.w = width;
  563. hitbox12.h = height;
  564.  
  565. break;
  566. case 13:
  567. hitbox13.x = posX;
  568. hitbox13.y = posY;
  569. hitbox13.w = width;
  570. hitbox13.h = height;
  571.  
  572. break;
  573. case 14:
  574. hitbox14.x = posX;
  575. hitbox14.y = posY;
  576. hitbox14.w = width;
  577. hitbox14.h = height;
  578.  
  579. break;
  580. case 15:
  581. hitbox15.x = posX;
  582. hitbox15.y = posY;
  583. hitbox15.w = width;
  584. hitbox15.h = height;
  585.  
  586. break;
  587. case 16:
  588. hitbox16.x = posX;
  589. hitbox16.y = posY;
  590. hitbox16.w = width;
  591. hitbox16.h = height;
  592.  
  593. break;
  594. case 17:
  595. hitbox17.x = posX;
  596. hitbox17.y = posY;
  597. hitbox17.w = width;
  598. hitbox17.h = height;
  599.  
  600. break;
  601. case 18:
  602. hitbox18.x = posX;
  603. hitbox18.y = posY;
  604. hitbox18.w = width;
  605. hitbox18.h = height;
  606.  
  607. break;
  608. case 19:
  609. hitbox19.x = posX;
  610. hitbox19.y = posY;
  611. hitbox19.w = width;
  612. hitbox19.h = height;
  613.  
  614. break;
  615. case 20:
  616. hitbox20.x = posX;
  617. hitbox20.y = posY;
  618. hitbox20.w = width;
  619. hitbox20.h = height;
  620.  
  621. break;
  622. }
  623. }
  624.  
  625. void clearScene()
  626. {
  627. imageIndex = 1;
  628.  
  629. freeImageSlot(1);
  630. freeImageSlot(2);
  631. freeImageSlot(3);
  632. freeImageSlot(4);
  633. freeImageSlot(5);
  634. freeImageSlot(6);
  635. freeImageSlot(7);
  636. freeImageSlot(8);
  637. freeImageSlot(9);
  638. freeImageSlot(10);
  639.  
  640. deleteButtons();
  641. }
Add Comment
Please, Sign In to add comment