Advertisement
Guest User

Untitled

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