Advertisement
PIBogdanov

MenuCPPLinux

Jul 1st, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <ncurses.h>
  3. #include <string>
  4. #include <thread>
  5. #include <chrono>
  6.  
  7. const int escapeKey = 27; // '\033' / Escape key
  8.  
  9. const int upArrow = KEY_UP;     // Up arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  10.  
  11. const int downArrow = KEY_DOWN; // Down arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  12.  
  13. const int enterKey = 10;        // '\n' / Enter key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
  14.  
  15. void ClearScreen()
  16. {
  17.     clear();
  18.     refresh();
  19. }
  20.  
  21. void FontSize(short sizeOfTheFont)
  22. {
  23.     resize_term(sizeOfTheFont, sizeOfTheFont);
  24. }
  25.  
  26. void MaximizeWindow()
  27. {
  28.     resize_term(0, 0);
  29. }
  30.  
  31. void ShowConsoleCursor(bool showFlag)
  32. {
  33.     curs_set(showFlag ? 1 : 0); // Show / Hide cursor
  34. }
  35.  
  36. int GetConsoleSizes()
  37. {
  38.     int rows, cols;
  39.     getmaxyx(stdscr, rows, cols);
  40.     return cols;
  41. }
  42.  
  43. int KeyboardInput()
  44. {
  45.     return getch();
  46. }
  47.  
  48. template <typename T>
  49. void MainMenu(T[], size_t, std::string[], size_t, std::string&, int);
  50.  
  51. void DisplayMenu(std::string[], size_t, std::string&, const size_t&);
  52.  
  53. template <typename T>
  54. int MenuMovement(T[], int, size_t, int);
  55.  
  56. void GameStart(int);
  57.  
  58. void Controls(int);
  59.  
  60. void Credits(int);
  61.  
  62. void ExitGame(int);
  63.  
  64. bool IsKeyPressedToGoBackToThePreviousMenu(int);
  65.  
  66. template <typename T>
  67. void DifficultyMenu(T[], size_t, std::string[], size_t, std::string&, int);
  68.  
  69. void CoutCentered(const std::string&);
  70.  
  71. int main()
  72. {
  73.     initscr();  // Initialize ncurses
  74.     start_color();  // Enable color support
  75.     cbreak();  // Disable line buffering
  76.     noecho();  // Disable echoing of input characters
  77.     keypad(stdscr, true);  // Enable keypad mode
  78.     curs_set(0);  // Hide cursor
  79.  
  80.     FontSize(6);
  81.     MaximizeWindow();
  82.  
  83.     typedef void(*menuOptions)(int);
  84.     menuOptions mainMenuOptions[] = { GameStart, Controls, Credits, ExitGame };
  85.     size_t sizeOfTheArray = sizeof(mainMenuOptions) / sizeof(mainMenuOptions[0]);
  86.  
  87.     std::string mainMenu[] = { "Start Game", "Controls", "Credits", "Exit" };
  88.     size_t sizeOfTheStringMenu = std::size(mainMenu);
  89.     std::string text = "Main Menu";
  90.  
  91.     MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, 0);
  92.  
  93.     endwin();  // Clean up ncurses
  94.  
  95.     return 0;
  96. }
  97.  
  98. template <typename T>
  99. void MainMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  100. {
  101.     int key = 0;
  102.     int previousSelectedOption = 0;
  103.  
  104.     while (true)
  105.     {
  106.         DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
  107.  
  108.         previousSelectedOption = selectedOption;
  109.         selectedOption = MenuMovement(mainMenuOptions, selectedOption, sizeOfTheArray, key);
  110.  
  111.         if (selectedOption < 0)
  112.         {
  113.             selectedOption = previousSelectedOption;
  114.         }
  115.         else if (selectedOption >= 0 && selectedOption < static_cast<int>(sizeOfTheArray) && key == enterKey)
  116.         {
  117.             mainMenuOptions[selectedOption](selectedOption);
  118.             ClearScreen();
  119.             text = "Main Menu";
  120.             selectedOption = 0;
  121.         }
  122.         else if (IsKeyPressedToGoBackToThePreviousMenu(key))
  123.         {
  124.             break;
  125.         }
  126.     }
  127. }
  128.  
  129. void DisplayMenu(std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
  130. {
  131.     ClearScreen();
  132.     CoutCentered(text);
  133.     for (size_t i = 0; i < sizeOfTheStringMenu; ++i)
  134.     {
  135.         if (i == selectedOption)
  136.         {
  137.             attron(A_STANDOUT);
  138.         }
  139.         CoutCentered(mainMenu[i]);
  140.         attroff(A_STANDOUT);
  141.     }
  142. }
  143.  
  144. template <typename T>
  145. int MenuMovement(T mainMenuOptions[], int selectedOption, size_t sizeOfTheArray, int key)
  146. {
  147.     key = KeyboardInput();
  148.  
  149.     switch (key)
  150.     {
  151.         case upArrow:
  152.             selectedOption--;
  153.             break;
  154.         case downArrow:
  155.             selectedOption++;
  156.             break;
  157.     }
  158.  
  159.     return selectedOption;
  160. }
  161.  
  162. void GameStart(int)
  163. {
  164.     std::cout << "Game is starting..." << std::endl;
  165.     std::this_thread::sleep_for(std::chrono::seconds(2));
  166. }
  167.  
  168. void Controls(int)
  169. {
  170.     std::cout << "Controls:\n\n"
  171.               << "WASD: Move\n"
  172.               << "Spacebar: Jump\n"
  173.               << "Left Mouse Button: Shoot\n"
  174.               << "Right Mouse Button: Aim\n"
  175.               << "Esc: Pause Menu" << std::endl;
  176.  
  177.     std::this_thread::sleep_for(std::chrono::seconds(5));
  178. }
  179.  
  180. void Credits(int)
  181. {
  182.     std::cout << "Credits:\n\n"
  183.               << "Developed by: Your Name" << std::endl;
  184.  
  185.     std::this_thread::sleep_for(std::chrono::seconds(3));
  186. }
  187.  
  188. void ExitGame(int)
  189. {
  190.     std::cout << "Exiting the game..." << std::endl;
  191.     std::this_thread::sleep_for(std::chrono::seconds(2));
  192.     std::exit(0);
  193. }
  194.  
  195. bool IsKeyPressedToGoBackToThePreviousMenu(int key)
  196. {
  197.     return key == escapeKey;
  198. }
  199.  
  200. template <typename T>
  201. void DifficultyMenu(T difficultyMenuOptions[], size_t sizeOfTheArray, std::string difficultyMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  202. {
  203.     int key = 0;
  204.     int previousSelectedOption = 0;
  205.  
  206.     while (true)
  207.     {
  208.         DisplayMenu(difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
  209.  
  210.         previousSelectedOption = selectedOption;
  211.         selectedOption = MenuMovement(difficultyMenuOptions, selectedOption, sizeOfTheArray, key);
  212.  
  213.         if (selectedOption < 0)
  214.         {
  215.             selectedOption = previousSelectedOption;
  216.         }
  217.         else if (selectedOption >= 0 && selectedOption < static_cast<int>(sizeOfTheArray) && key == enterKey)
  218.         {
  219.             difficultyMenuOptions[selectedOption](selectedOption);
  220.             ClearScreen();
  221.             text = "Main Menu";
  222.             selectedOption = 0;
  223.         }
  224.         else if (IsKeyPressedToGoBackToThePreviousMenu(key))
  225.         {
  226.             break;
  227.         }
  228.     }
  229. }
  230.  
  231. void CoutCentered(const std::string& text)
  232. {
  233.     int cols = GetConsoleSizes();
  234.     std::string::size_type len = text.size();
  235.     int spaces = (cols - len) / 2;
  236.  
  237.     for (int i = 0; i < spaces; ++i)
  238.     {
  239.         std::cout << ' ';
  240.     }
  241.  
  242.     std::cout << text << std::endl;
  243. }
  244.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement