Advertisement
Guest User

Untitled

a guest
Apr 17th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. // Snooping through the files 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 <iostream>
  6. #include <SDL2/SDL.h>
  7. #include <SDL2/SDL_image.h>
  8.  
  9. #include "loadImage.h"
  10. #include "playSound.h"
  11.  
  12. #define CENTER 0
  13.  
  14. SDL_Window* window;
  15. SDL_Renderer* renderer;
  16.  
  17. bool running;
  18.  
  19. bool buttons = false;
  20.  
  21. SDL_Point mousePosition;
  22.  
  23. SDL_Rect button1;
  24. SDL_Rect button2;
  25. SDL_Rect button3;
  26.  
  27. bool loadedScenes[2] = {false, false};
  28.  
  29. bool mouseInsideButton1 = false;
  30. bool mouseInsideButton2 = false;
  31. bool mouseInsideButton3 = false;
  32.  
  33. bool playGrown = false;
  34. bool playShrunk = true;
  35.  
  36. bool wimpGrown = false;
  37. bool wimpShrunk = true;
  38.  
  39. bool leftMouseDown = false;
  40.  
  41. int buttonIndex = 1;
  42.  
  43. int scene = 1;
  44.  
  45. int playerX = 300;
  46. int playerY = 653;
  47.  
  48. int gravity = 1;
  49.  
  50. bool gravityOn;
  51.  
  52. int imageIndexHolder;
  53.  
  54. const char* mainMenuBackground = "Assets/Backgrounds/mainMenuBackground.png";
  55. const char* bedroomBackground = "Assets/Backgrounds/bedroomBackground.png";
  56.  
  57. const char* playButton = "Assets/Sprites/playButton.png";
  58. const char* wimpButton = "Assets/Sprites/wimpButton.png";
  59. const char* playButtonBig = "Assets/Sprites/playButtonBig.png";
  60. const char* wimpButtonBig = "Assets/Sprites/wimpButtonBig.png";
  61. const char* woodFloor = "Assets/Sprites/woodFloor.png";
  62. const char* bed = "Assets/Sprites/bed.png";
  63. const char* arthur = "Assets/Sprites/arthur.png";
  64.  
  65. const char* mainMenuMusic = "Assets/Music/mainMenuMusic.mp3";
  66.  
  67. int positiveToNegative(int number)
  68. {
  69. number -= number*2;
  70.  
  71. return number;
  72. }
  73.  
  74. void createHDWindow()
  75. {
  76. SDL_Init(SDL_INIT_EVERYTHING);
  77. IMG_Init(IMG_INIT_PNG);
  78. Mix_Init(MIX_INIT_MP3);
  79.  
  80. Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
  81.  
  82. Mix_VolumeMusic(400);
  83.  
  84. window = SDL_CreateWindow("Butter Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_FULLSCREEN);
  85. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  86. }
  87.  
  88. int quit()
  89. {
  90. SDL_DestroyTexture(imageSlot1);
  91. SDL_DestroyTexture(imageSlot2);
  92. SDL_DestroyTexture(imageSlot3);
  93. SDL_DestroyTexture(imageSlot4);
  94. SDL_DestroyTexture(imageSlot5);
  95. SDL_DestroyTexture(imageSlot6);
  96. SDL_DestroyTexture(imageSlot7);
  97. SDL_DestroyTexture(imageSlot8);
  98. SDL_DestroyTexture(imageSlot9);
  99. SDL_DestroyTexture(imageSlot10);
  100.  
  101. Mix_CloseAudio();
  102.  
  103. SDL_DestroyWindow(window);
  104. SDL_DestroyRenderer(renderer);
  105.  
  106. exit(1);
  107. }
  108.  
  109. void createButton(int width, int height, int posX, int posY)
  110. {
  111. if(buttonIndex == 1)
  112. {
  113. button1.x = posX;
  114. button1.y = posY;
  115. button1.w = width;
  116. button1.h = height;
  117. }
  118. else if(buttonIndex == 2)
  119. {
  120. button2.x = posX;
  121. button2.y = posY;
  122. button2.w = width;
  123. button2.h = height;
  124. }
  125. else if(buttonIndex == 3)
  126. {
  127. button3.x = posX;
  128. button3.y = posY;
  129. button3.w = width;
  130. button3.h = height;
  131. }
  132.  
  133. buttons = true;
  134.  
  135. buttonIndex++;
  136. }
  137.  
  138. void deleteButtons()
  139. {
  140. buttonIndex = 1;
  141.  
  142. button1 = {0, 0, 0, 0};
  143. button2 = {0, 0, 0, 0};
  144. button3 = {0, 0, 0, 0};
  145. }
  146.  
  147. void getInput()
  148. {
  149. SDL_Event event;
  150.  
  151. SDL_GetMouseState(&mousePosition.x, &mousePosition.y);
  152.  
  153. mouseInsideButton1 = SDL_PointInRect(&mousePosition, &button1);
  154. mouseInsideButton2 = SDL_PointInRect(&mousePosition, &button2);
  155. mouseInsideButton3 = SDL_PointInRect(&mousePosition, &button3);
  156.  
  157. while(SDL_PollEvent(&event))
  158. {
  159. if(event.button.button != SDL_BUTTON_LEFT)
  160. {
  161. leftMouseDown = false;
  162. }
  163.  
  164. switch(event.type)
  165. {
  166. case SDL_MOUSEBUTTONDOWN:
  167. if(event.button.button == SDL_BUTTON_LEFT)
  168. {
  169. leftMouseDown = true;
  170. }
  171. break;
  172. case SDL_KEYDOWN:
  173. switch(event.key.keysym.sym)
  174. {
  175.  
  176. }
  177. break;
  178.  
  179. case SDL_QUIT:
  180. quit();
  181. break;
  182. break;
  183. }
  184. }
  185. }
  186.  
  187. void queueVisualEvents()
  188. {
  189. if(scene == 1 && loadedScenes[0] != true)
  190. {
  191. playMusic(mainMenuMusic, -1);
  192.  
  193. loadImage(mainMenuBackground, 0, 0, 1920, 1080);
  194. loadImage(playButton, 1400, 270, 340, 170);
  195. loadImage(wimpButton, 1400, 540, 340, 170);
  196.  
  197. createButton(340, 170, 1400, 270);
  198. createButton(340, 170, 1400, 540);
  199.  
  200. loadedScenes[0] = true;
  201. }
  202.  
  203. if(scene == 1)
  204. {
  205. if(mouseInsideButton1)
  206. {
  207. if(!playGrown)
  208. {
  209. freeImageSlot(2);
  210.  
  211. loadSpecificImageSlot(playButtonBig, 1374, 244, 391, 196, 2);
  212.  
  213. playGrown = true;
  214. playShrunk = false;
  215. }
  216.  
  217. if(leftMouseDown)
  218. {
  219. clearScene();
  220.  
  221. scene = 2;
  222.  
  223. SDL_Delay(700);
  224. }
  225. }
  226.  
  227. if(!mouseInsideButton1)
  228. {
  229. if(!playShrunk)
  230. {
  231. freeImageSlot(2);
  232.  
  233. loadSpecificImageSlot(playButton, 1400, 270, 340, 170, 2);
  234.  
  235. playShrunk = true;
  236. playGrown = false;
  237. }
  238. }
  239.  
  240. if(mouseInsideButton2)
  241. {
  242. if(!wimpGrown)
  243. {
  244. freeImageSlot(3);
  245.  
  246. loadSpecificImageSlot(wimpButtonBig, 1374, 514, 391, 196, 3);
  247.  
  248. wimpGrown = true;
  249. wimpShrunk = false;
  250. }
  251.  
  252. if(leftMouseDown)
  253. {
  254. SDL_Delay(700);
  255.  
  256. quit();
  257.  
  258. SDL_Delay(700);
  259. }
  260. }
  261.  
  262. if(!mouseInsideButton2)
  263. {
  264. if(!wimpShrunk)
  265. {
  266. freeImageSlot(3);
  267.  
  268. loadSpecificImageSlot(wimpButton, 1400, 540, 340, 170, 3);
  269.  
  270. wimpShrunk = true;
  271. wimpGrown = false;
  272. }
  273. }
  274. }
  275.  
  276. if(scene == 2)
  277. {
  278. if(loadedScenes[1] != true)
  279. {
  280. Mix_HaltMusic();
  281.  
  282. loadImage(bedroomBackground, CENTER, CENTER, 1920, 1080);
  283. loadImage(woodFloor, CENTER, 1029, 1920, 51);
  284. loadImage(bed, 50, 546, 555, 503);
  285.  
  286. createHitbox(50, 546, 555, 206); // BED
  287. createHitbox(0, 1029, 1920, 51); // FLOOR
  288. createHitbox(playerX, playerY, 118, 174); // ARTHUR
  289.  
  290. loadedScenes[1] = true;
  291. }
  292.  
  293. imageIndexHolder = loadImage(arthur, playerX, playerY, 118, 174);
  294.  
  295. imageIndexHolder--;
  296.  
  297. playerY += gravity;
  298.  
  299. moveHitbox(hitbox3, 0, positiveToNegative(gravity));
  300.  
  301. gravity++;
  302.  
  303. imageIndex--;
  304. }
  305.  
  306.  
  307. }
  308.  
  309. void renderScreen()
  310. {
  311. SDL_RenderClear(renderer);
  312.  
  313. SDL_RenderCopy(renderer, imageSlot1, NULL, &imageRect1);
  314. SDL_RenderCopy(renderer, imageSlot2, NULL, &imageRect2);
  315. SDL_RenderCopy(renderer, imageSlot3, NULL, &imageRect3);
  316. SDL_RenderCopy(renderer, imageSlot4, NULL, &imageRect4);
  317. SDL_RenderCopy(renderer, imageSlot5, NULL, &imageRect5);
  318. SDL_RenderCopy(renderer, imageSlot6, NULL, &imageRect6);
  319. SDL_RenderCopy(renderer, imageSlot7, NULL, &imageRect7);
  320. SDL_RenderCopy(renderer, imageSlot8, NULL, &imageRect8);
  321. SDL_RenderCopy(renderer, imageSlot9, NULL, &imageRect9);
  322. SDL_RenderCopy(renderer, imageSlot10, NULL, &imageRect10);
  323.  
  324. SDL_RenderPresent(renderer);
  325. }
  326.  
  327. int main()
  328. {
  329. running = true;
  330.  
  331. createHDWindow();
  332.  
  333. while(running)
  334. {
  335. std::cout << proccessHitboxes() << std::endl;
  336.  
  337. getInput();
  338. queueVisualEvents();
  339. renderScreen();
  340.  
  341. if(scene == 2)
  342. {
  343. freeImageSlot(imageIndexHolder);
  344. }
  345. }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement