Advertisement
Guest User

Untitled

a guest
Mar 7th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. // Compile with g++ main.cpp -lSDL2 -lSDL2_image -o 'Morshu Game'
  2.  
  3. #include <iostream>
  4. #include <SDL2/SDL.h>
  5. #include <SDL2/SDL_image.h>
  6.  
  7. #define FAILURE 1
  8. #define SUCCESS 0
  9.  
  10. #define CENTER_X 1920/2
  11. #define CENTER_Y 1080/2
  12.  
  13. #define morshuRoom 0
  14. #define mainMenuText 1
  15. #define startGameText 2
  16. #define cityBackground 3
  17. #define whatWillYouDoText 4
  18.  
  19. #define mainMenu 0
  20. #define roadScene 1
  21.  
  22. int images[5];
  23.  
  24. int imageIndex = 0;
  25.  
  26. bool running = true;
  27. bool fullscreen = false;
  28.  
  29. SDL_Window* window = nullptr;
  30. SDL_Renderer* renderer = nullptr;
  31. SDL_Texture* backgroundTexture = nullptr;
  32.  
  33. SDL_Texture* imageSlot1 = nullptr;
  34. SDL_Texture* imageSlot2 = nullptr;
  35. SDL_Texture* imageSlot3 = nullptr;
  36. SDL_Texture* imageSlot4 = nullptr;
  37. SDL_Texture* imageSlot5 = nullptr;
  38.  
  39. SDL_Rect imageRect1;
  40. SDL_Rect imageRect2;
  41. SDL_Rect imageRect3;
  42. SDL_Rect imageRect4;
  43. SDL_Rect imageRect5;
  44.  
  45. SDL_Event event;
  46.  
  47. int scene = mainMenu;
  48.  
  49. int createWindow()
  50. {
  51. if(SDL_Init(SDL_INIT_EVERYTHING) != 0){return FAILURE;}
  52. if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG){return FAILURE;}
  53. window = SDL_CreateWindow("Morshu Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
  54. if(!window){return FAILURE;}
  55. renderer = SDL_CreateRenderer(window, -1, 0);
  56. if(!renderer){return FAILURE;}
  57. return SUCCESS;
  58. }
  59.  
  60. void quit()
  61. {
  62. SDL_DestroyTexture(imageSlot1);
  63. SDL_DestroyTexture(imageSlot2);
  64. SDL_DestroyTexture(imageSlot3);
  65. SDL_DestroyTexture(imageSlot4);
  66. SDL_DestroyTexture(imageSlot5);
  67.  
  68. SDL_DestroyRenderer(renderer);
  69. SDL_DestroyWindow(window);
  70.  
  71. IMG_Quit();
  72. SDL_Quit();
  73.  
  74. running = false;
  75. }
  76.  
  77. void getInput()
  78. {
  79. while(SDL_PollEvent(&event))
  80. {
  81. switch(event.type)
  82. {
  83. case SDL_QUIT:
  84. quit();
  85. break;
  86. case SDL_KEYDOWN:
  87. switch(event.key.keysym.sym)
  88. {
  89. case SDLK_ESCAPE:
  90. quit();
  91. break;
  92. case SDLK_F4:
  93. if(fullscreen == false)
  94. {
  95. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  96. fullscreen = true;
  97. }
  98. else if(fullscreen == true)
  99. {
  100. SDL_SetWindowFullscreen(window, 0);
  101. fullscreen = false;
  102. }
  103. break;
  104. case SDLK_RETURN:
  105. scene = roadScene;
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. }
  112. }
  113.  
  114. void loadImage(int sprite, int posX, int posY, int sizeX, int sizeY)
  115. {
  116. SDL_Surface* image1;
  117. SDL_Surface* image2;
  118. SDL_Surface* image3;
  119. SDL_Surface* image4;
  120. SDL_Surface* image5;
  121.  
  122. switch(imageIndex)
  123. {
  124. case 0:
  125. switch(sprite)
  126. {
  127. case mainMenuText:
  128. imageRect1.w = sizeX;
  129. imageRect1.h = sizeY;
  130. imageRect1.x = posX+sizeX/2;
  131. imageRect1.y = posY-sizeY/2;
  132.  
  133. image1 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  134. images[0] = mainMenuText;
  135. imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
  136.  
  137. SDL_DestroyTexture(imageSlot1);
  138.  
  139. imageIndex++;
  140. break;
  141. case startGameText:
  142. imageRect1.w = sizeX;
  143. imageRect1.h = sizeY;
  144. imageRect1.x = posX+sizeX/2;
  145. imageRect1.y = posY-sizeY/2;
  146.  
  147. image1 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  148. images[0] = mainMenuText;
  149. imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
  150.  
  151. SDL_DestroyTexture(imageSlot1);
  152.  
  153. imageIndex++;
  154. break;
  155. case whatWillYouDoText:
  156. imageRect1.w = sizeX;
  157. imageRect1.h = sizeY;
  158. imageRect1.x = posX+sizeX/2;
  159. imageRect1.y = posY-sizeY/2;
  160.  
  161. image1 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
  162. images[0] = mainMenuText;
  163. imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
  164.  
  165. SDL_DestroyTexture(imageSlot1);
  166.  
  167. imageIndex++;
  168. break;
  169. default:
  170. break;
  171. }
  172. break;
  173. case 1:
  174. switch(sprite)
  175. {
  176. case mainMenuText:
  177. imageRect2.w = sizeX;
  178. imageRect2.h = sizeY;
  179. imageRect2.x = posX+sizeX/2;
  180. imageRect2.y = posY-sizeY/2;
  181.  
  182.  
  183. image2 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  184. images[1] = mainMenuText;
  185. imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
  186.  
  187. SDL_DestroyTexture(imageSlot2);
  188.  
  189. imageIndex++;
  190. break;
  191. case startGameText:
  192. imageRect2.w = sizeX;
  193. imageRect2.h = sizeY;
  194. imageRect2.x = posX+sizeX/2;
  195. imageRect2.y = posY-sizeY/2;
  196.  
  197. image2 = IMG_Load("./Assets/Sprites/startGameText.png");
  198. images[0] = mainMenuText;
  199. imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
  200.  
  201. SDL_DestroyTexture(imageSlot2);
  202.  
  203. imageIndex++;
  204. break;
  205. case whatWillYouDoText:
  206. imageRect2.w = sizeX;
  207. imageRect2.h = sizeY;
  208. imageRect2.x = posX+sizeX/2;
  209. imageRect2.y = posY-sizeY/2;
  210.  
  211. image2 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
  212. images[1] = mainMenuText;
  213. imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
  214.  
  215. SDL_DestroyTexture(imageSlot2);
  216.  
  217. imageIndex++;
  218. break;
  219. default:
  220. break;
  221. }
  222. break;
  223. case 2:
  224. switch(sprite)
  225. {
  226. case mainMenuText:
  227. imageRect3.w = sizeX;
  228. imageRect3.h = sizeY;
  229. imageRect3.x = posX+sizeX/2;
  230. imageRect3.y = posY-sizeY/2;
  231.  
  232. image3 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  233. images[2] = mainMenuText;
  234. imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
  235.  
  236. SDL_DestroyTexture(imageSlot3);
  237.  
  238. imageIndex++;
  239. break;
  240. case startGameText:
  241. imageRect3.w = sizeX;
  242. imageRect3.h = sizeY;
  243. imageRect3.x = posX+sizeX/2;
  244. imageRect3.y = posY-sizeY/2;
  245.  
  246. image3 = IMG_Load("./Assets/Sprites/startGameText.png");
  247. images[2] = mainMenuText;
  248. imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
  249.  
  250. SDL_DestroyTexture(imageSlot3);
  251.  
  252. imageIndex++;
  253. break;
  254. case whatWillYouDoText:
  255. imageRect3.w = sizeX;
  256. imageRect3.h = sizeY;
  257. imageRect3.x = posX+sizeX/2;
  258. imageRect3.y = posY-sizeY/2;
  259.  
  260. image3 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
  261. images[2] = mainMenuText;
  262. imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
  263.  
  264. SDL_DestroyTexture(imageSlot3);
  265.  
  266. imageIndex++;
  267. break;
  268. default:
  269. break;
  270. }
  271. break;
  272. case 3:
  273. switch(sprite)
  274. {
  275. case mainMenuText:
  276. imageRect4.w = sizeX;
  277. imageRect4.h = sizeY;
  278. imageRect4.x = posX+sizeX/2;
  279. imageRect4.y = posY-sizeY/2;
  280.  
  281. image4 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  282. images[3] = mainMenuText;
  283. imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
  284.  
  285. SDL_DestroyTexture(imageSlot4);
  286.  
  287. imageIndex++;
  288. break;
  289. case startGameText:
  290. imageRect4.w = sizeX;
  291. imageRect4.h = sizeY;
  292. imageRect4.x = posX+sizeX/2;
  293. imageRect4.y = posY-sizeY/2;
  294.  
  295. image1 = IMG_Load("./Assets/Sprites/startGameText.png");
  296. images[3] = mainMenuText;
  297. imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
  298.  
  299. SDL_DestroyTexture(imageSlot4);
  300.  
  301. imageIndex++;
  302. break;
  303. case whatWillYouDoText:
  304. imageRect4.w = sizeX;
  305. imageRect4.h = sizeY;
  306. imageRect4.x = posX+sizeX/2;
  307. imageRect4.y = posY-sizeY/2;
  308.  
  309. image4 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
  310. images[3] = mainMenuText;
  311. imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
  312.  
  313. SDL_DestroyTexture(imageSlot4);
  314.  
  315. imageIndex++;
  316. break;
  317. default:
  318. break;
  319. }
  320. break;
  321. case 4:
  322. switch(sprite)
  323. {
  324. case mainMenuText:
  325. imageRect5.w = sizeX;
  326. imageRect5.h = sizeY;
  327. imageRect5.x = posX+sizeX/2;
  328. imageRect5.y = posY-sizeY/2;
  329.  
  330. image5 = IMG_Load("./Assets/Sprites/mainMenuText.png");
  331. images[4] = mainMenuText;
  332. imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
  333.  
  334. SDL_DestroyTexture(imageSlot5);
  335.  
  336. imageIndex++;
  337. break;
  338. case startGameText:
  339. imageRect5.w = sizeX;
  340. imageRect5.h = sizeY;
  341. imageRect5.x = posX+sizeX/2;
  342. imageRect5.y = posY-sizeY/2;
  343.  
  344. image5 = IMG_Load("./Assets/Sprites/startGameText.png");
  345. images[4] = mainMenuText;
  346. imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
  347.  
  348. SDL_DestroyTexture(imageSlot5);
  349.  
  350. imageIndex++;
  351. break;
  352. case whatWillYouDoText:
  353. imageRect5.w = sizeX;
  354. imageRect5.h = sizeY;
  355. imageRect5.x = posX+sizeX/2;
  356. imageRect5.y = posY-sizeY/2;
  357.  
  358. image5 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
  359. images[4] = mainMenuText;
  360. imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
  361.  
  362. SDL_DestroyTexture(imageSlot5);
  363.  
  364. imageIndex++;
  365. break;
  366. default:
  367. break;
  368. }
  369. break;
  370. }
  371. }
  372.  
  373. void renderScreen()
  374. {
  375. SDL_RenderClear(renderer);
  376.  
  377. SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
  378.  
  379. SDL_RenderCopy(renderer, imageSlot1, NULL, &imageRect1);
  380. SDL_RenderCopy(renderer, imageSlot2, NULL, &imageRect2);
  381. SDL_RenderCopy(renderer, imageSlot3, NULL, &imageRect3);
  382. SDL_RenderCopy(renderer, imageSlot4, NULL, &imageRect4);
  383. SDL_RenderCopy(renderer, imageSlot5, NULL, &imageRect5);
  384.  
  385. SDL_RenderPresent(renderer);
  386. }
  387.  
  388. void changeBackground(int sprite)
  389. {
  390. SDL_Surface* image;
  391.  
  392. switch(sprite)
  393. {
  394. case morshuRoom:
  395. image = IMG_Load("./Assets/Backgrounds/morshuRoom.png");
  396. break;
  397. case cityBackground:
  398. image = IMG_Load("./Assets/Backgrounds/cityBackground.png");
  399. break;
  400. default:
  401. break;
  402. }
  403.  
  404. backgroundTexture = SDL_CreateTextureFromSurface(renderer, image);
  405. }
  406.  
  407. void clearScene()
  408. {
  409. SDL_DestroyTexture(imageSlot1);
  410. SDL_DestroyTexture(imageSlot2);
  411. SDL_DestroyTexture(imageSlot3);
  412. SDL_DestroyTexture(imageSlot4);
  413. SDL_DestroyTexture(imageSlot5);
  414.  
  415. imageSlot1 = nullptr;
  416. imageSlot2 = nullptr;
  417. imageSlot3 = nullptr;
  418. imageSlot4 = nullptr;
  419. imageSlot5 = nullptr;
  420.  
  421. backgroundTexture = nullptr;
  422. }
  423.  
  424. void updateScreen()
  425. {
  426. changeBackground(morshuRoom);
  427. loadImage(mainMenuText, 210, 175, 800, 200);
  428. loadImage(startGameText, 300, 700, 700, 175);
  429.  
  430. if(scene == roadScene)
  431. {
  432. clearScene();
  433. changeBackground(cityBackground);
  434. loadImage(whatWillYouDoText, 210, 175, 900, 275);
  435. }
  436. }
  437.  
  438. int main()
  439. {
  440. if(FAILURE == createWindow()){quit();}
  441.  
  442. while(running)
  443. {
  444. getInput();
  445. updateScreen();
  446. renderScreen();
  447. }
  448.  
  449. return SUCCESS;
  450. }
  451.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement