Advertisement
PIBogdanov

MenuCPP

Jul 1st, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.47 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. const int escapeKey = 27; // '\033' / Escape key
  9.  
  10. #ifdef _WIN32 // Windows-specific code
  11.  
  12. #include <Windows.h>
  13. #include <conio.h>
  14.  
  15. const int upArrow = 72;   // Up arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
  16.  
  17. const int downArrow = 80; // Down arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
  18.  
  19. const int enterKey = 13;  // '\r' / Enter key (Windows, because it uses the _getch() function from the <conio.h> header)
  20.  
  21. void ClearScreen()
  22. {
  23.     std::system("cls");
  24. }
  25.  
  26. HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  27.  
  28. void FontSize(SHORT sizeOfTheFont)
  29. {
  30.     CONSOLE_FONT_INFOEX fontInfo{};
  31.  
  32.     fontInfo.cbSize = sizeof(fontInfo);
  33.  
  34.     if (!GetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
  35.     {
  36.         // Handle error: unable to retrieve font info
  37.         // You can print an error message or throw an exception
  38.  
  39.         return;
  40.     }
  41.  
  42.     fontInfo.dwFontSize.X += sizeOfTheFont; // Increase the font width
  43.  
  44.     fontInfo.dwFontSize.Y += sizeOfTheFont; // Increase the font height
  45.  
  46.     if (!SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
  47.     {
  48.         // Handle error: unable to set font info
  49.         // You can print an error message or throw an exception
  50.  
  51.         return;
  52.     }
  53. }
  54.  
  55. HWND consoleWindowHandle = GetConsoleWindow(); // Get the console window handle
  56.  
  57. void MaximizeWindow()
  58. {
  59.     ShowWindow(consoleWindowHandle, SW_MAXIMIZE); // Maximize the window
  60. }
  61.  
  62. void ShowConsoleCursor(bool showFlag)
  63. {
  64.     CONSOLE_CURSOR_INFO cursorInfo;
  65.  
  66.     if (!GetConsoleCursorInfo(consoleHandle, &cursorInfo))
  67.     {
  68.         // Handle error: unable to retrieve cursor info
  69.         // You can print an error message or throw an exception
  70.  
  71.         return;
  72.     }
  73.  
  74.     cursorInfo.bVisible = showFlag; // Sets the cursor visibility
  75.  
  76.     if (!SetConsoleCursorInfo(consoleHandle, &cursorInfo))
  77.     {
  78.         // Handle error: unable to set cursor info
  79.         // You can print an error message or throw an exception
  80.  
  81.         return;
  82.     }
  83. }
  84.  
  85. SHORT GetConsoleSizes()
  86. {
  87.     CONSOLE_SCREEN_BUFFER_INFO screenInfo;
  88.  
  89.     if (!GetConsoleScreenBufferInfo(consoleHandle, &screenInfo))
  90.     {
  91.         // Handle error: unable to retrieve font info
  92.         // You can print an error message or throw an exception
  93.  
  94.         return false;
  95.     }
  96.  
  97.     return screenInfo.dwSize.X;
  98. }
  99.  
  100. int KeyboardInput()
  101. {
  102.     return _getch();;
  103. }
  104.  
  105. #else // Unix/Linux/MacOS-specific code
  106.  
  107. #include <ncurses.h>
  108.  
  109. const int upArrow = KEY_UP;     // Up arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  110.  
  111. const int downArrow = KEY_DOWN; // 66 Down arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  112.  
  113. const int enterKey = 10;        // '\n' / Enter key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  114.  
  115. void ClearScreen()
  116. {
  117.     clear();
  118.  
  119.     refresh();
  120. }
  121.  
  122. void FontSize(short sizeOfTheFont)
  123. {
  124.     resize_term(sizeOfTheFont, sizeOfTheFont);
  125. }
  126.  
  127. void MaximizeWindow()
  128. {
  129.     resize_term(0, 0);
  130. }
  131.  
  132. void ShowConsoleCursor(bool showFlag)
  133. {
  134.     curs_set(showFlag ? 1 : 0); // Show / Hide cursor
  135. }
  136.  
  137. short GetConsoleSizes()
  138. {
  139.     int rows, cols;
  140.  
  141.     getmaxyx(stdscr, rows, cols);
  142.  
  143.     return cols;
  144. }
  145.  
  146. int KeyboardInput()
  147. {
  148.     initscr();  // Initialize ncurses
  149.  
  150.     keypad(stdscr, true);  // Enable keypad mode
  151.  
  152.     noecho();  // Disable echoing of input characters
  153.  
  154.     int key = getch();  // Read a key press
  155.  
  156.     endwin();  // Clean up ncurses
  157.  
  158.     return key;
  159. }
  160.  
  161. #endif
  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";
  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