Advertisement
PIBogdanov

MenuCPP

Jul 1st, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <cstddef>
  5. #include <thread>
  6. #include <chrono>
  7.  
  8. #ifdef _WIN32 // Windows-specific code
  9.  
  10. #include <Windows.h>
  11. #include <conio.h>
  12.  
  13. const int upArrow = 72; // Up arrow key (Windows)
  14.  
  15. const int downArrow = 80; // Down arrow key (Windows)
  16.  
  17. const int enterKey = 13; // Enter key (Windows)
  18.  
  19. void ClearScreen()
  20. {
  21.     std::system("cls");
  22. }
  23.  
  24. HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  25.  
  26. void FontSize(SHORT sizeOfTheFont)
  27. {
  28.     CONSOLE_FONT_INFOEX fontInfo{};
  29.  
  30.     fontInfo.cbSize = sizeof(fontInfo);
  31.  
  32.     if (!GetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
  33.     {
  34.         // Handle error: unable to retrieve font info
  35.         // You can print an error message or throw an exception
  36.  
  37.         return;
  38.     }
  39.  
  40.     fontInfo.dwFontSize.X += sizeOfTheFont; // Increase the font width
  41.  
  42.     fontInfo.dwFontSize.Y += sizeOfTheFont; // Increase the font height
  43.  
  44.     if (!SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
  45.     {
  46.         // Handle error: unable to set font info
  47.         // You can print an error message or throw an exception
  48.  
  49.         return;
  50.     }
  51. }
  52.  
  53. HWND consoleWindowHandle = GetConsoleWindow(); // Get the console window handle
  54.  
  55. void MaximizeWindow()
  56. {
  57.     ShowWindow(consoleWindowHandle, SW_MAXIMIZE); // Maximize the window
  58. }
  59.  
  60. void ShowConsoleCursor(bool showFlag)
  61. {
  62.     CONSOLE_CURSOR_INFO cursorInfo;
  63.  
  64.     if (!GetConsoleCursorInfo(consoleHandle, &cursorInfo))
  65.     {
  66.         // Handle error: unable to retrieve cursor info
  67.         // You can print an error message or throw an exception
  68.  
  69.         return;
  70.     }
  71.  
  72.     cursorInfo.bVisible = showFlag; // Sets the cursor visibility
  73.  
  74.     if (!SetConsoleCursorInfo(consoleHandle, &cursorInfo))
  75.     {
  76.         // Handle error: unable to set cursor info
  77.         // You can print an error message or throw an exception
  78.  
  79.         return;
  80.     }
  81. }
  82.  
  83. SHORT GetConsoleSizes()
  84. {
  85.     CONSOLE_SCREEN_BUFFER_INFO screenInfo;
  86.  
  87.     if (!GetConsoleScreenBufferInfo(consoleHandle, &screenInfo))
  88.     {
  89.         // Handle error: unable to retrieve font info
  90.         // You can print an error message or throw an exception
  91.  
  92.         return false;
  93.     }
  94.  
  95.     return screenInfo.dwSize.X;
  96. }
  97.  
  98. int KeyboardInput()
  99. {
  100.     return _getch();;
  101. }
  102.  
  103. #else // Unix/Linux/MacOS-specific code
  104.  
  105. #include <ncurses.h>
  106.  
  107. const int upArrow = KEY_UP; // 65 Up arrow key (Linux/MacOS)
  108.  
  109. const int downArrow = KEY_DOWN; // 66 Down arrow key (Linux/MacOS)
  110.  
  111. const int enterKey = '\n'; // Enter key (Linux/MacOS)
  112.  
  113. void ClearScreen()
  114. {
  115.     clear();
  116.  
  117.     refresh();
  118. }
  119.  
  120. void FontSize(short sizeOfTheFont)
  121. {
  122.     resize_term(sizeOfTheFont, sizeOfTheFont);
  123. }
  124.  
  125. void MaximizeWindow()
  126. {
  127.     resize_term(0, 0);
  128. }
  129.  
  130. void ShowConsoleCursor(bool showFlag)
  131. {
  132.     curs_set(showFlag ? 1 : 0); // show/hide cursor
  133. }
  134.  
  135. short GetConsoleSizes()
  136. {
  137.     int rows, cols;
  138.  
  139.     getmaxyx(stdscr, rows, cols);
  140.  
  141.     return cols;
  142. }
  143.  
  144. int KeyboardInput()
  145. {
  146.     initscr();  // Initialize ncurses
  147.  
  148.     keypad(stdscr, true);  // Enable keypad mode
  149.  
  150.     noecho();  // Disable echoing of input characters
  151.  
  152.     int key = getch();  // Read a key press
  153.  
  154.     endwin();  // Clean up ncurses
  155.  
  156.     return key;
  157. }
  158.  
  159. #endif
  160.  
  161. const int escapeKey = 27; // Escape key
  162.  
  163. template <typename T>
  164.  
  165. void MainMenu(T[], size_t, std::string[], size_t, std::string&, int);
  166.  
  167. void DisplayMenu(std::string[], size_t, std::string&, const size_t&);
  168.  
  169. template <typename T>
  170.  
  171. int MenuMovement(T[], int, size_t, int);
  172.  
  173. void GameStart(int);
  174.  
  175. void Controls(int);
  176.  
  177. void Credits(int);
  178.  
  179. void ExitGame(int);
  180.  
  181. bool IsKeyPressedToGoBackToThePreviousMenu(int);
  182.  
  183. template <typename T>
  184.  
  185. void DifficultyMenu(T[], size_t, std::string[], size_t, std::string&, int);
  186.  
  187. void CoutCentered(const std::string&);
  188.  
  189. int main()
  190. {
  191.     FontSize(6);
  192.  
  193.     MaximizeWindow();
  194.  
  195.     ShowConsoleCursor(false);
  196.  
  197.     typedef void(*menuOptions)(int);
  198.  
  199.     menuOptions mainMenuOptions[] = { GameStart, Controls, Credits, ExitGame };
  200.  
  201.     size_t sizeOfTheArray = sizeof(mainMenuOptions) / sizeof(mainMenuOptions[0]);
  202.  
  203.     std::string mainMenu[] = { "Start Game", "Controls", "Credits", "Exit" };
  204.  
  205.     size_t sizeOfTheStringMenu = sizeof(mainMenu) / sizeof(mainMenu[0]);
  206.  
  207.     std::string text = "Main Menu";
  208.  
  209.     MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, 0);
  210.  
  211.     return 0;
  212. }
  213.  
  214. template <typename T>
  215.  
  216. void MainMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  217. {
  218.     int key = 0;
  219.  
  220.     int previousSelectedOption = 0;
  221.  
  222.     while (true)
  223.     {
  224.         DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
  225.  
  226.         previousSelectedOption = selectedOption;
  227.  
  228.         selectedOption = MenuMovement(mainMenuOptions, selectedOption, sizeOfTheArray, key);
  229.  
  230.         if (selectedOption < 0)
  231.         {
  232.             selectedOption = previousSelectedOption;
  233.         }
  234.  
  235.         std::this_thread::sleep_for(std::chrono::milliseconds(12));
  236.     }
  237. }
  238.  
  239. void DisplayMenu(std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
  240. {
  241.     ClearScreen();
  242.  
  243.     std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
  244.  
  245.     CoutCentered(text);
  246.  
  247.     std::cout << "\n" << "\n" << "\n";
  248.  
  249.     for (size_t i = 0; i < sizeOfTheStringMenu; i++)
  250.     {
  251.         selectedOption == i ? CoutCentered("--> " + mainMenu[i]) : CoutCentered("" + mainMenu[i]);
  252.  
  253.         std::cout << "\n";
  254.     }
  255. }
  256.  
  257. /*
  258. This function is "int" and not "size_t", because the "size_t" data type doesn't return negative numbers,
  259. it returns only numbers from 0 to 18 446 744 073 709 551 615 or 18_446_744_073_709_551_615 (the same number),
  260. in other words, only positive, including 0
  261. */
  262.  
  263. template <typename T>
  264.  
  265. int MenuMovement(T menu[], int selectedOption, size_t sizeOfTheArray, int key)
  266. {
  267.     key = KeyboardInput();
  268.  
  269.     if (key == upArrow)
  270.     {
  271.         selectedOption--;
  272.  
  273.         if (selectedOption < 0)
  274.         {
  275.             selectedOption = sizeOfTheArray - 1;
  276.         }
  277.     }
  278.  
  279.     else if (key == downArrow)
  280.     {
  281.         selectedOption++;
  282.  
  283.         if (selectedOption >= sizeOfTheArray)
  284.         {
  285.             selectedOption = 0;
  286.         }
  287.     }
  288.  
  289.     else if (key == enterKey)
  290.     {
  291.         menu[selectedOption](selectedOption);
  292.     }
  293.  
  294.     else if (key == escapeKey)
  295.     {
  296.         selectedOption = - 6;
  297.     }
  298.  
  299.     return selectedOption;
  300. }
  301.  
  302. void GameStart(int selectedOption)
  303. {
  304.     ClearScreen();
  305.  
  306.     std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
  307.  
  308.     typedef void(*difficultyOptions)(int);
  309.  
  310.     difficultyOptions difficultyMenuOptions[] = { GameStart, Credits, ExitGame };
  311.  
  312.     size_t sizeOfTheArray = sizeof(difficultyMenuOptions) / sizeof(difficultyMenuOptions[0]);
  313.  
  314.     std::string difficultyMenu[] = { "Start", "Cdits", "Eit" };
  315.  
  316.     size_t sizeOfTheStringMenu = sizeof(difficultyMenu) / sizeof(difficultyMenu[0]);
  317.  
  318.     std::string text = "Difficulty Menu";
  319.  
  320.     DifficultyMenu(difficultyMenuOptions, sizeOfTheArray, difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
  321. }
  322.  
  323. void Controls(int selectedOption)
  324. {
  325.     ClearScreen();
  326.  
  327.     std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
  328.  
  329.     CoutCentered("The controls are showed!");
  330.  
  331.     while (!IsKeyPressedToGoBackToThePreviousMenu(KeyboardInput()));
  332. }
  333.  
  334. void Credits(int selectedOption)
  335. {
  336.     ClearScreen();
  337.  
  338.     std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
  339.  
  340.     CoutCentered("The credits are showed!");
  341.  
  342.     while (!IsKeyPressedToGoBackToThePreviousMenu(KeyboardInput()));
  343. }
  344.  
  345. void ExitGame(int selectedOption)
  346. {
  347.     ClearScreen();
  348.  
  349.     std::exit(0);
  350. }
  351.  
  352. bool IsKeyPressedToGoBackToThePreviousMenu(int key)
  353. {
  354.     if (key == escapeKey)
  355.     {
  356.         return true;
  357.     }
  358.  
  359.     return false;
  360. }
  361.  
  362. template <typename T>
  363.  
  364. void DifficultyMenu(T difficultyMenuOptions[], size_t sizeOfTheArray, std::string difficultyMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  365. {
  366.     int key = 0;
  367.  
  368.     while (selectedOption != -6)
  369.     {
  370.         DisplayMenu(difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
  371.  
  372.         selectedOption = MenuMovement(difficultyMenuOptions, selectedOption, sizeOfTheArray, key);
  373.  
  374.         std::this_thread::sleep_for(std::chrono::milliseconds(12));
  375.     }
  376. }
  377.  
  378. void CoutCentered(const std::string& text)
  379. {
  380.     size_t textWidth = text.length();
  381.  
  382.     short consoleWidth = GetConsoleSizes();
  383.  
  384.     size_t leftMargin = (consoleWidth - textWidth) / 2;
  385.  
  386.     std::cout.width(leftMargin + textWidth);
  387.  
  388.     std::cout << text;
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement