Advertisement
Guest User

Untitled

a guest
Apr 14th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 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 -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 fullscreen = false;
  20.  
  21. bool loadedScenes[2] = {false, false};
  22.  
  23. bool buttons = false;
  24.  
  25. bool touchingButton1 = false;
  26. bool touchingButton2 = false;
  27. bool touchingButton3 = false;
  28.  
  29. int buttonX[6];
  30. int buttonY[6];
  31.  
  32. int mouseX;
  33. int mouseY;
  34.  
  35. int buttonIndex = 0;
  36.  
  37. const char* mainMenuBackground = "Assets/Backgrounds/main_menu.png";
  38. const char* playButton = "Assets/Sprites/play_button.png";
  39. const char* wimpButton = "Assets/Sprites/wimp_button.png";
  40.  
  41. const char* mainMenuMusic = "Assets/Music/mainMenuMusic.mp3";
  42.  
  43. void createHDWindow()
  44. {
  45. SDL_Init(SDL_INIT_EVERYTHING);
  46. IMG_Init(IMG_INIT_PNG);
  47. Mix_Init(MIX_INIT_MP3);
  48.  
  49. Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
  50.  
  51. window = SDL_CreateWindow("Butter Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN);
  52. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  53. }
  54.  
  55. int quit()
  56. {
  57. SDL_DestroyTexture(imageSlot1);
  58. SDL_DestroyTexture(imageSlot2);
  59. SDL_DestroyTexture(imageSlot3);
  60. SDL_DestroyTexture(imageSlot4);
  61. SDL_DestroyTexture(imageSlot5);
  62. SDL_DestroyTexture(imageSlot6);
  63. SDL_DestroyTexture(imageSlot7);
  64. SDL_DestroyTexture(imageSlot8);
  65. SDL_DestroyTexture(imageSlot9);
  66. SDL_DestroyTexture(imageSlot10);
  67.  
  68. Mix_CloseAudio();
  69.  
  70. SDL_DestroyWindow(window);
  71. SDL_DestroyRenderer(renderer);
  72.  
  73. return -1;
  74. }
  75.  
  76. void createButton(int oneX, int twoX, int oneY, int twoY)
  77. {
  78. buttonX[buttonIndex] = oneX;
  79. buttonX[buttonIndex++] = twoX;
  80.  
  81. buttonY[buttonIndex] = oneY;
  82. buttonY[buttonIndex++] = twoY;
  83.  
  84. buttonIndex += 2;
  85.  
  86. buttons = true;
  87. }
  88.  
  89. void deleteButtons()
  90. {
  91. buttonX[0] == NULL;
  92. buttonX[1] == NULL;
  93. buttonX[2] == NULL;
  94. buttonX[3] == NULL;
  95. buttonX[4] == NULL;
  96. buttonX[5] == NULL;
  97.  
  98. buttons = false;
  99. }
  100.  
  101. void getInput()
  102. {
  103. SDL_Event event;
  104.  
  105. while(SDL_PollEvent(&event))
  106. {
  107. switch(event.type)
  108. {
  109. case SDL_MOUSEMOTION:
  110. SDL_GetMouseState(&mouseX, &mouseY);
  111.  
  112. if(buttons)
  113. {
  114. if(mouseX > buttonX[0] && mouseX < buttonX[1] && mouseY > buttonY[0] && mouseY < buttonY[1])
  115. {
  116. touchingButton1 = true;
  117. std::cout << "Touching button one!" << std::endl;
  118. }
  119. else
  120. {
  121. touchingButton1 = false;
  122. }
  123.  
  124. if(mouseX > buttonX[2] && mouseX < buttonX[3] && mouseY > buttonY[2] && mouseY < buttonY[3])
  125. {
  126. touchingButton2 = true;
  127. std::cout << "Touching button two!" << std::endl;
  128. }
  129. else
  130. {
  131. touchingButton2 = false;
  132. }
  133.  
  134. if(mouseX > buttonX[4] && mouseX < buttonX[5] && mouseY > buttonY[4] && mouseY < buttonY[5])
  135. {
  136. touchingButton3 = true;
  137. std::cout << "Touching button three!" << std::endl;
  138. }
  139. else
  140. {
  141. touchingButton3 = false;
  142. }
  143. }
  144. break;
  145. case SDL_KEYDOWN:
  146. switch(event.key.keysym.sym)
  147. {
  148. case SDLK_F4:
  149. if(fullscreen)
  150. {
  151. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  152. std::cout << "Not Fullscreen" << std::endl;
  153. fullscreen = false;
  154. }
  155. else if(!fullscreen)
  156. {
  157. SDL_SetWindowFullscreen(window, 0);
  158. std::cout << "Fullscreen" << std::endl;
  159. fullscreen = true;
  160. }
  161.  
  162. break;
  163. }
  164. break;
  165.  
  166. case SDL_QUIT:
  167. quit();
  168. break;
  169. break;
  170. }
  171. }
  172. }
  173.  
  174. void queueVisualEvents()
  175. {
  176. if(loadedScenes[1] != true)
  177. {
  178. playMusic(mainMenuMusic, -1);
  179.  
  180. loadImage(mainMenuBackground, 0, 0, 1920, 1080);
  181. loadImage(playButton, 1400, 270, 340, 170);
  182. loadImage(wimpButton, 1400, 540, 340, 170);
  183.  
  184. createButton(1400, 1740, 270, 440);
  185.  
  186. loadedScenes[1] = true;
  187. }
  188. }
  189.  
  190. void renderScreen()
  191. {
  192. SDL_RenderClear(renderer);
  193.  
  194. SDL_RenderCopy(renderer, imageSlot1, NULL, &imageRect1);
  195. SDL_RenderCopy(renderer, imageSlot2, NULL, &imageRect2);
  196. SDL_RenderCopy(renderer, imageSlot3, NULL, &imageRect3);
  197. SDL_RenderCopy(renderer, imageSlot4, NULL, &imageRect4);
  198. SDL_RenderCopy(renderer, imageSlot5, NULL, &imageRect5);
  199. SDL_RenderCopy(renderer, imageSlot6, NULL, &imageRect6);
  200. SDL_RenderCopy(renderer, imageSlot7, NULL, &imageRect7);
  201. SDL_RenderCopy(renderer, imageSlot8, NULL, &imageRect8);
  202. SDL_RenderCopy(renderer, imageSlot9, NULL, &imageRect9);
  203. SDL_RenderCopy(renderer, imageSlot10, NULL, &imageRect10);
  204.  
  205. SDL_RenderPresent(renderer);
  206. }
  207.  
  208. int main()
  209. {
  210. running = true;
  211.  
  212. createHDWindow();
  213.  
  214. while(running)
  215. {
  216. getInput();
  217. queueVisualEvents();
  218. renderScreen();
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement