Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ncurses.h>
- #include <string>
- #include <thread>
- #include <chrono>
- const int escapeKey = 27; // '\033' / Escape key
- const int upArrow = KEY_UP; // Up arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
- const int downArrow = KEY_DOWN; // Down arrow key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
- const int enterKey = 10; // '\n' / Enter key (Unix/Linux/MacOS, because it uses the global constant value from the <ncurses.h> header)
- void ClearScreen()
- {
- clear();
- refresh();
- }
- void FontSize(short sizeOfTheFont)
- {
- resize_term(sizeOfTheFont, sizeOfTheFont);
- }
- void MaximizeWindow()
- {
- resize_term(0, 0);
- }
- void ShowConsoleCursor(bool showFlag)
- {
- curs_set(showFlag ? 1 : 0); // Show / Hide cursor
- }
- int GetConsoleSizes()
- {
- int rows, cols;
- getmaxyx(stdscr, rows, cols);
- return cols;
- }
- int KeyboardInput()
- {
- return getch();
- }
- template <typename T>
- void MainMenu(T[], size_t, std::string[], size_t, std::string&, int);
- void DisplayMenu(std::string[], size_t, std::string&, const size_t&);
- template <typename T>
- int MenuMovement(T[], int, size_t, int);
- void GameStart(int);
- void Controls(int);
- void Credits(int);
- void ExitGame(int);
- bool IsKeyPressedToGoBackToThePreviousMenu(int);
- template <typename T>
- void DifficultyMenu(T[], size_t, std::string[], size_t, std::string&, int);
- void CoutCentered(const std::string&);
- int main()
- {
- initscr(); // Initialize ncurses
- start_color(); // Enable color support
- cbreak(); // Disable line buffering
- noecho(); // Disable echoing of input characters
- keypad(stdscr, true); // Enable keypad mode
- curs_set(0); // Hide cursor
- FontSize(6);
- MaximizeWindow();
- typedef void(*menuOptions)(int);
- menuOptions mainMenuOptions[] = { GameStart, Controls, Credits, ExitGame };
- size_t sizeOfTheArray = sizeof(mainMenuOptions) / sizeof(mainMenuOptions[0]);
- std::string mainMenu[] = { "Start Game", "Controls", "Credits", "Exit" };
- size_t sizeOfTheStringMenu = std::size(mainMenu);
- std::string text = "Main Menu";
- MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, 0);
- endwin(); // Clean up ncurses
- return 0;
- }
- template <typename T>
- void MainMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
- {
- int key = 0;
- int previousSelectedOption = 0;
- while (true)
- {
- DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
- previousSelectedOption = selectedOption;
- selectedOption = MenuMovement(mainMenuOptions, selectedOption, sizeOfTheArray, key);
- if (selectedOption < 0)
- {
- selectedOption = previousSelectedOption;
- }
- else if (selectedOption >= 0 && selectedOption < static_cast<int>(sizeOfTheArray) && key == enterKey)
- {
- mainMenuOptions[selectedOption](selectedOption);
- ClearScreen();
- text = "Main Menu";
- selectedOption = 0;
- }
- else if (IsKeyPressedToGoBackToThePreviousMenu(key))
- {
- break;
- }
- }
- }
- void DisplayMenu(std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
- {
- ClearScreen();
- CoutCentered(text);
- for (size_t i = 0; i < sizeOfTheStringMenu; ++i)
- {
- if (i == selectedOption)
- {
- attron(A_STANDOUT);
- }
- CoutCentered(mainMenu[i]);
- attroff(A_STANDOUT);
- }
- }
- template <typename T>
- int MenuMovement(T mainMenuOptions[], int selectedOption, size_t sizeOfTheArray, int key)
- {
- key = KeyboardInput();
- switch (key)
- {
- case upArrow:
- selectedOption--;
- break;
- case downArrow:
- selectedOption++;
- break;
- }
- return selectedOption;
- }
- void GameStart(int)
- {
- std::cout << "Game is starting..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
- void Controls(int)
- {
- std::cout << "Controls:\n\n"
- << "WASD: Move\n"
- << "Spacebar: Jump\n"
- << "Left Mouse Button: Shoot\n"
- << "Right Mouse Button: Aim\n"
- << "Esc: Pause Menu" << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(5));
- }
- void Credits(int)
- {
- std::cout << "Credits:\n\n"
- << "Developed by: Your Name" << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(3));
- }
- void ExitGame(int)
- {
- std::cout << "Exiting the game..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- std::exit(0);
- }
- bool IsKeyPressedToGoBackToThePreviousMenu(int key)
- {
- return key == escapeKey;
- }
- template <typename T>
- void DifficultyMenu(T difficultyMenuOptions[], size_t sizeOfTheArray, std::string difficultyMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
- {
- int key = 0;
- int previousSelectedOption = 0;
- while (true)
- {
- DisplayMenu(difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
- previousSelectedOption = selectedOption;
- selectedOption = MenuMovement(difficultyMenuOptions, selectedOption, sizeOfTheArray, key);
- if (selectedOption < 0)
- {
- selectedOption = previousSelectedOption;
- }
- else if (selectedOption >= 0 && selectedOption < static_cast<int>(sizeOfTheArray) && key == enterKey)
- {
- difficultyMenuOptions[selectedOption](selectedOption);
- ClearScreen();
- text = "Main Menu";
- selectedOption = 0;
- }
- else if (IsKeyPressedToGoBackToThePreviousMenu(key))
- {
- break;
- }
- }
- }
- void CoutCentered(const std::string& text)
- {
- int cols = GetConsoleSizes();
- std::string::size_type len = text.size();
- int spaces = (cols - len) / 2;
- for (int i = 0; i < spaces; ++i)
- {
- std::cout << ' ';
- }
- std::cout << text << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement