Advertisement
Guest User

menu.cpp

a guest
Jan 26th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.63 KB | None | 0 0
  1. //menu.cpp
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <allegro5/allegro.h>
  6. #include <allegro5/allegro_image.h>
  7. #include <allegro5/allegro_font.h>
  8. #include <allegro5/allegro_ttf.h>
  9. #include <allegro5/events.h>
  10. #include "menu.h"
  11.  
  12. using namespace std;
  13. #define WHITE al_map_rgb(255, 255, 255)
  14. #define BLACK al_map_rgb(0, 0, 0)
  15. int menu() {
  16. //ALLEGRO_COLOR back_color = al_map_rgb(106, 90, 205);
  17.  
  18. #define back_color al_map_rgb(230, 230, 25)
  19.  
  20. ALLEGRO_DISPLAY* display = NULL;
  21. if (!al_init()) return -1;
  22. al_init_image_addon();
  23. display = al_create_display(1600, 900);
  24. if (!display)return -1;
  25. // background = al_load_bitmap(BACKGROUND_FILE);
  26. // if (!background)return -1;
  27.  
  28. al_clear_to_color(al_map_rgb(0, 0, 0));
  29. al_install_keyboard();
  30. al_install_mouse();
  31. al_init_font_addon();
  32. al_init_ttf_addon();
  33.  
  34.  
  35. //wybór motywu
  36. int chosen_theme = choose_theme();
  37. //załadowanie background
  38. refresh(chosen_theme);
  39. //wybranie ilości graczy
  40. int n_players = choose_players(chosen_theme);
  41. if (n_players == 2) {
  42. Player player1, player2;
  43. Player* pl1 = &player1; Player* pl2 = &player2;
  44. chose_avatar(chosen_theme, pl1); write_nick(chosen_theme, pl1);
  45. chose_avatar(chosen_theme, pl2); write_nick(chosen_theme, pl2);
  46. }
  47. else if (n_players == 3) {
  48. Player player1, player2, player3;
  49. Player* pl1 = &player1; Player* pl2 = &player2; Player* pl3 = &player3;
  50. chose_avatar(chosen_theme, pl1); write_nick(chosen_theme, pl1);
  51. chose_avatar(chosen_theme, pl2); write_nick(chosen_theme, pl2);
  52. chose_avatar(chosen_theme, pl3); write_nick(chosen_theme, pl3);
  53. }
  54. else {
  55. Player player1, player2, player3, player4;
  56. Player* pl1 = &player1; Player* pl2 = &player2;
  57. Player* pl3 = &player3; Player* pl4 = &player4;
  58. chose_avatar(chosen_theme, pl1); write_nick(chosen_theme, pl1);
  59. chose_avatar(chosen_theme, pl2); write_nick(chosen_theme, pl2);
  60. chose_avatar(chosen_theme, pl3); write_nick(chosen_theme, pl3);
  61. chose_avatar(chosen_theme, pl4); write_nick(chosen_theme, pl4);
  62.  
  63. }
  64.  
  65. refresh(chosen_theme);
  66. al_rest(0.2);
  67.  
  68. return n_players;
  69. }
  70.  
  71.  
  72. int chose_avatar(int chosen_theme, Player* obj) {
  73. refresh(chosen_theme);
  74. ALLEGRO_FONT* font_menu = al_load_ttf_font("TYPEWR__.ttf", 50, 0);
  75. int av1_position[2] = { 50, 400 };
  76. int av2_postion[2] = { 450, 400 };
  77. int av3_position[2] = { 850, 400 };
  78. int av4_position[2] = { 1250, 400 };
  79. int x = 300;
  80. int y = 400;
  81. ALLEGRO_BITMAP* av1 = NULL;
  82. ALLEGRO_BITMAP* av2 = NULL;
  83. ALLEGRO_BITMAP* av3 = NULL;
  84. ALLEGRO_BITMAP* av4 = NULL;
  85.  
  86. if (chosen_theme == 1) {
  87. av1 = al_load_bitmap("Rick.jpg");
  88. av2 = al_load_bitmap("Morty.jpg");
  89. av3 = al_load_bitmap("Summer.jpg");
  90. av4 = al_load_bitmap("Pan_Kupkazpupki.jpg");
  91.  
  92. char wybierz[50] = "WYBIERZ AVATARA";
  93. al_draw_text(font_menu, WHITE, 800, 300, ALLEGRO_ALIGN_CENTRE, wybierz);
  94. al_draw_bitmap(av1, av1_position[0], av1_position[1], 0);
  95. al_draw_bitmap(av2, av2_postion[0], av2_postion[1], 0);
  96. al_draw_bitmap(av3, av3_position[0], av3_position[1], 0);
  97. al_draw_bitmap(av4, av4_position[0], av4_position[1], 0);
  98.  
  99. al_flip_display();
  100. al_rest(0.5);
  101. }
  102. else if (chosen_theme == 2) {
  103. av1 = al_load_bitmap("Geralt.jpeg");
  104. av2 = al_load_bitmap("Yen.jpeg");
  105. av3 = al_load_bitmap("Jaskier.jpeg");
  106. av4 = al_load_bitmap("Ciri.jpeg");
  107.  
  108. char wybierz[50] = "WYBIERZ AVATARA";
  109. al_draw_text(font_menu, BLACK, 800, 75, ALLEGRO_ALIGN_CENTRE, wybierz);
  110. al_draw_bitmap(av1, av1_position[0], av1_position[1], 0);
  111. al_draw_bitmap(av2, av2_postion[0], av2_postion[1], 0);
  112. al_draw_bitmap(av3, av3_position[0], av3_position[1], 0);
  113. al_draw_bitmap(av4, av4_position[0], av4_position[1], 0);
  114.  
  115. al_flip_display();
  116. al_rest(0.5);
  117. }
  118. else if (chosen_theme == 3) {
  119. av1 = al_load_bitmap("Frodo.jpeg");
  120. av2 = al_load_bitmap("Legolas.jpeg");
  121. av3 = al_load_bitmap("Gollum.jpeg");
  122. av4 = al_load_bitmap("Gandalf.jpeg");
  123.  
  124. char wybierz[50] = "WYBIERZ AVATARA";
  125. al_draw_text(font_menu, BLACK, 800, 75, ALLEGRO_ALIGN_CENTRE, wybierz);
  126. al_draw_bitmap(av1, av1_position[0], av1_position[1], 0);
  127. al_draw_bitmap(av2, av2_postion[0], av2_postion[1], 0);
  128. al_draw_bitmap(av3, av3_position[0], av3_position[1], 0);
  129. al_draw_bitmap(av4, av4_position[0], av4_position[1], 0);
  130.  
  131. al_flip_display();
  132. al_rest(0.5);
  133. }
  134.  
  135. ALLEGRO_MOUSE_STATE state;
  136. bool done = false;
  137. while (!done) {
  138. al_get_mouse_state(&state);
  139. if (state.buttons & 1) {
  140. printf("Mouse position: (%d, %d)\n", state.x, state.y);
  141. if ((state.x >= av1_position[0] && state.x <= av1_position[0] + 300) &&
  142. (state.y >= av1_position[1] && state.y <= av1_position[1] + 400)) {
  143. obj->avatar = av1;
  144. al_destroy_bitmap(av2);
  145. al_destroy_bitmap(av3);
  146. al_destroy_bitmap(av4);
  147. return 1;
  148.  
  149. }
  150. else if ((state.x >= av2_postion[0] && state.x <= av2_postion[0] + 300) &&
  151. (state.y >= av2_postion[1] && state.y <= av2_postion[1] + 400)) {
  152. obj->avatar = av2;
  153. al_destroy_bitmap(av1);
  154. al_destroy_bitmap(av3);
  155. al_destroy_bitmap(av4);
  156. return 1;
  157. }
  158. else if ((state.x >= av3_position[0] && state.x <= av3_position[0] + 300) &&
  159. (state.y >= av3_position[1] && state.y <= av3_position[1] + 400)) {
  160. obj->avatar = av3;
  161. al_destroy_bitmap(av1);
  162. al_destroy_bitmap(av2);
  163. al_destroy_bitmap(av4);
  164. return 1;
  165. }
  166. else if ((state.x >= av4_position[0] && state.x <= av4_position[0] + 300) &&
  167. (state.y >= av4_position[1] && state.y <= av4_position[1] + 400)) {
  168. obj->avatar = av4;
  169. al_destroy_bitmap(av1);
  170. al_destroy_bitmap(av2);
  171. al_destroy_bitmap(av3);
  172. return 1;
  173. }
  174. al_rest(0.1);
  175. }
  176.  
  177. }
  178. return 0;
  179. }
  180.  
  181. int write_nick(int chosen_theme, Player* obj) {
  182. refresh(chosen_theme);
  183. ALLEGRO_FONT* font_avatar = al_load_ttf_font("TYPEWR__.ttf", 24, 0);
  184. ALLEGRO_FONT* font_menu = al_load_ttf_font("TYPEWR__.ttf", 50, 0);
  185. char napis[50] = "WPISZ SWOJ NICK";
  186. if (chosen_theme == 1) {
  187. al_draw_text(font_menu, WHITE, 800, 75, ALLEGRO_ALIGN_CENTRE, napis);
  188. }else al_draw_text(font_menu, BLACK, 800, 75, ALLEGRO_ALIGN_CENTRE, napis);
  189. ALLEGRO_BITMAP* av = obj->avatar;
  190. al_draw_bitmap(av, 650, 200, 0);
  191. ALLEGRO_BITMAP* white = al_load_bitmap("white_rectangular.jpg");
  192. al_draw_bitmap(white, 600, 650, 0);
  193. al_flip_display();
  194.  
  195. ALLEGRO_KEYBOARD_STATE keystate;
  196. ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
  197. al_register_event_source(event_queue, al_get_keyboard_event_source());
  198.  
  199. bool done = false;
  200. char nick[20];
  201. int i = 0;
  202.  
  203. while (!done && i<19) {
  204. ALLEGRO_EVENT event;
  205. al_wait_for_event(event_queue, &event);
  206. if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
  207. switch (event.keyboard.keycode)
  208. {
  209. case ALLEGRO_KEY_A:
  210. nick[i] = 'a'; i++;
  211. break;
  212. case ALLEGRO_KEY_B:
  213. nick[i] = 'b'; i++;
  214. break;
  215. case ALLEGRO_KEY_C:
  216. nick[i] = 'c'; i++;
  217. break;
  218. case ALLEGRO_KEY_D:
  219. nick[i] = 'd'; i++;
  220. break;
  221. case ALLEGRO_KEY_E:
  222. nick[i] = 'e'; i++;
  223. break;
  224. case ALLEGRO_KEY_F:
  225. nick[i] = 'f'; i++;
  226. break;
  227. case ALLEGRO_KEY_G:
  228. nick[i] = 'g'; i++;
  229. break;
  230. case ALLEGRO_KEY_H:
  231. nick[i] = 'h'; i++;
  232. break;
  233. case ALLEGRO_KEY_I:
  234. nick[i] = 'i'; i++;
  235. break;
  236. case ALLEGRO_KEY_J:
  237. nick[i] = 'j'; i++;
  238. break;
  239. case ALLEGRO_KEY_K:
  240. nick[i] = 'k'; i++;
  241. break;
  242. case ALLEGRO_KEY_L:
  243. nick[i] = 'l'; i++;
  244. break;
  245. case ALLEGRO_KEY_M:
  246. nick[i] = 'm'; i++;
  247. break;
  248. case ALLEGRO_KEY_N:
  249. nick[i] = 'n'; i++;
  250. break;
  251. case ALLEGRO_KEY_O:
  252. nick[i] = 'o'; i++;
  253. break;
  254. case ALLEGRO_KEY_P:
  255. nick[i] = 'p'; i++;
  256. break;
  257. case ALLEGRO_KEY_Q:
  258. nick[i] = 'q'; i++;
  259. break;
  260. case ALLEGRO_KEY_R:
  261. nick[i] = 'r'; i++;
  262. break;
  263. case ALLEGRO_KEY_S:
  264. nick[i] = 's'; i++;
  265. break;
  266. case ALLEGRO_KEY_T:
  267. nick[i] = 't'; i++;
  268. break;
  269. case ALLEGRO_KEY_U:
  270. nick[i] = 'u'; i++;
  271. break;
  272. case ALLEGRO_KEY_V:
  273. nick[i] = 'v'; i++;
  274. break;
  275. case ALLEGRO_KEY_W:
  276. nick[i] = 'w'; i++;
  277. break;
  278. case ALLEGRO_KEY_X:
  279. nick[i] = 'x'; i++;
  280. break;
  281. case ALLEGRO_KEY_Y:
  282. nick[i] = 'y'; i++;
  283. break;
  284. case ALLEGRO_KEY_Z:
  285. nick[i] = 'z'; i++;
  286. break;
  287. case ALLEGRO_KEY_BACKSPACE:
  288. i--; nick[i] = NULL;
  289. break;
  290. case ALLEGRO_KEY_ESCAPE:
  291. done = true;
  292. break;
  293. case ALLEGRO_KEY_ENTER:
  294. done = true;
  295. break;
  296. }
  297.  
  298. al_draw_bitmap(white, 600, 650, 0);
  299. al_draw_text(font_avatar, BLACK, 620, 660, ALLEGRO_ALIGN_LEFT, nick);
  300. al_flip_display();
  301. al_rest(0.2);
  302. }
  303. }
  304.  
  305. al_flip_display();
  306. al_destroy_event_queue(event_queue);
  307.  
  308. obj->nick = nick;
  309. al_rest(0.2);
  310. return 0;
  311. }
  312. void refresh(int chosen_theme) {
  313. #define rick_morty_b "rick_back2.jpg"
  314. #define witcher_b "witcher_back2.jpg"
  315. #define lord_rings_b "lord_back2.jpg"
  316.  
  317. ALLEGRO_BITMAP* rick_b = NULL;
  318. ALLEGRO_BITMAP* witch_b = NULL;
  319. ALLEGRO_BITMAP* lord_b = NULL;
  320.  
  321. rick_b = al_load_bitmap(rick_morty_b);
  322. witch_b = al_load_bitmap(witcher_b);
  323. lord_b = al_load_bitmap(lord_rings_b);
  324. switch (chosen_theme) {
  325. case 1:
  326. al_draw_bitmap(rick_b, 0, 0, 0);
  327. break;
  328. case 2:
  329. al_draw_bitmap(witch_b, 0, 0, 0);
  330. break;
  331. case 3:
  332. al_draw_bitmap(lord_b, 0, 0, 0);
  333. break;
  334. default: break;
  335. }
  336. al_flip_display();
  337. }
  338. int choose_players(int chosen_theme) {
  339.  
  340. #define B2 "button_2.png"
  341. #define B3 "button_3.png"
  342. #define B4 "button_4.png"
  343.  
  344. ALLEGRO_BITMAP* b2 = NULL;
  345. ALLEGRO_BITMAP* b3 = NULL;
  346. ALLEGRO_BITMAP* b4 = NULL;
  347. ALLEGRO_FONT* font_menu = al_load_ttf_font("TYPEWR__.ttf", 50, 0);
  348.  
  349. b2 = al_load_bitmap(B2);
  350. b3 = al_load_bitmap(B3);
  351. b4 = al_load_bitmap(B4);
  352.  
  353. int by = 500;
  354. int b2x = 350;
  355. int b3x = 750;
  356. int b4x = 1150;
  357. char napis[50] = "WYBIERZ ILOSC GRACZY";
  358. switch (chosen_theme) {
  359. case 1:
  360. al_draw_text(font_menu, WHITE, 800, 700, ALLEGRO_ALIGN_CENTRE, napis);
  361. break;
  362. case 2:
  363. al_draw_text(font_menu, BLACK, 800, 75, ALLEGRO_ALIGN_CENTRE, napis);
  364. break;
  365. case 3:
  366. al_draw_text(font_menu, BLACK, 800, 75, ALLEGRO_ALIGN_CENTRE, napis);
  367. break;
  368. default: break;
  369. }
  370.  
  371. al_draw_bitmap(b2, b2x, by, 0);
  372. al_draw_bitmap(b3, b3x, by, 0);
  373. al_draw_bitmap(b4, b4x, by, 0);
  374.  
  375. // szerokość 800, wysokość 600
  376.  
  377. al_flip_display();
  378.  
  379.  
  380. ALLEGRO_MOUSE_STATE state;
  381. bool done = false;
  382. while (!done) {
  383. al_get_mouse_state(&state);
  384. if (state.buttons & 1) {
  385. printf("Mouse position: (%d, %d)\n", state.x, state.y);
  386. if ((state.x >= b2x && state.x <= b2x + 100) &&
  387. (state.y >= by && state.y <= by + 100)) {
  388. return 2;
  389. }
  390. else if ((state.x >= b3x && state.x <= b3x + 100) &&
  391. (state.y >= by && state.y <= by + 100)) {
  392. return 3;
  393. }
  394. else if ((state.x >= b4x && state.x <= b4x + 100) &&
  395. (state.y >= by && state.y <= by + 100)) {
  396. return 4;
  397. }
  398.  
  399. }
  400. al_rest(0.1);
  401. }
  402. return 0;
  403. }
  404. int choose_theme() {
  405. #define rick_morty "rick_and_morty.jpg"
  406. #define witcher "witcher.jpg"
  407. #define lord_rings "lord_rings.jpg"
  408.  
  409. ALLEGRO_BITMAP* rick = NULL;
  410. ALLEGRO_BITMAP* witch = NULL;
  411. ALLEGRO_BITMAP* lord = NULL;
  412. ALLEGRO_FONT* font_menu = al_load_ttf_font("TYPEWR__.ttf", 50, 0);
  413.  
  414. rick = al_load_bitmap(rick_morty);
  415. witch = al_load_bitmap(witcher);
  416. lord = al_load_bitmap(lord_rings);
  417.  
  418. int rick_position[2] = { 250, 500 };
  419. int witcher_postion[2] = { 1050, 500 };
  420. int lord_position[2] = { 650, 500 };
  421. char wybierz[50] = "WYBIERZ MOTYW GRY";
  422. al_draw_text(font_menu, WHITE, 800, 300, ALLEGRO_ALIGN_CENTRE, wybierz);
  423. al_draw_bitmap(rick, rick_position[0], rick_position[1], 0);
  424. al_draw_bitmap(witch, witcher_postion[0], witcher_postion[1], 0);
  425. al_draw_bitmap(lord, lord_position[0], lord_position[1], 0);
  426.  
  427. // szerokość 800, wysokość 600
  428.  
  429. al_flip_display();
  430. al_destroy_bitmap(rick);
  431. al_destroy_bitmap(witch);
  432. al_destroy_bitmap(lord);
  433.  
  434.  
  435. ALLEGRO_MOUSE_STATE state;
  436. bool done = false;
  437. while (!done) {
  438. al_get_mouse_state(&state);
  439. if (state.buttons & 1) {
  440. printf("Mouse position: (%d, %d)\n", state.x, state.y);
  441. if ((state.x >= rick_position[0] && state.x <= rick_position[0] + 300) &&
  442. (state.y >= rick_position[1] && state.y <= rick_position[1] + 113)) {
  443. cout << "Wybrano rick and morty";
  444. return 1;
  445. }
  446. else if ((state.x >= witcher_postion[0] && state.x <= witcher_postion[0] + 299) &&
  447. (state.y >= witcher_postion[1] && state.y <= witcher_postion[1] + 113)) {
  448. cout << "Wybrano wiedzmin";
  449. return 2;
  450. }
  451. else if ((state.x >= lord_position[0] && state.x <= lord_position[0] + 300) &&
  452. (state.y >= lord_position[1] && state.y <= lord_position[1] + 92)) {
  453. cout << "Wybrano lord of the rings";
  454. return 3;
  455. }
  456.  
  457. }
  458. al_rest(0.1);
  459. }
  460.  
  461. return 0;
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement