Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string>
- #include <cstddef>
- #include <thread>
- #include <chrono>
- #ifdef _WIN32 // Windows-specific code
- #include <Windows.h>
- #include <conio.h>
- const int upArrow = 72; // Up arrow key (Windows)
- const int downArrow = 80; // Down arrow key (Windows)
- const int enterKey = 13; // Enter key (Windows)
- void ClearScreen()
- {
- std::system("cls");
- }
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- void FontSize(SHORT sizeOfTheFont)
- {
- CONSOLE_FONT_INFOEX fontInfo{};
- fontInfo.cbSize = sizeof(fontInfo);
- if (!GetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
- {
- // Handle error: unable to retrieve font info
- // You can print an error message or throw an exception
- return;
- }
- fontInfo.dwFontSize.X += sizeOfTheFont; // Increase the font width
- fontInfo.dwFontSize.Y += sizeOfTheFont; // Increase the font height
- if (!SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo))
- {
- // Handle error: unable to set font info
- // You can print an error message or throw an exception
- return;
- }
- }
- HWND consoleWindowHandle = GetConsoleWindow(); // Get the console window handle
- void MaximizeWindow()
- {
- ShowWindow(consoleWindowHandle, SW_MAXIMIZE); // Maximize the window
- }
- void ShowConsoleCursor(bool showFlag)
- {
- CONSOLE_CURSOR_INFO cursorInfo;
- if (!GetConsoleCursorInfo(consoleHandle, &cursorInfo))
- {
- // Handle error: unable to retrieve cursor info
- // You can print an error message or throw an exception
- return;
- }
- cursorInfo.bVisible = showFlag; // Sets the cursor visibility
- if (!SetConsoleCursorInfo(consoleHandle, &cursorInfo))
- {
- // Handle error: unable to set cursor info
- // You can print an error message or throw an exception
- return;
- }
- }
- SHORT GetConsoleSizes()
- {
- CONSOLE_SCREEN_BUFFER_INFO screenInfo;
- if (!GetConsoleScreenBufferInfo(consoleHandle, &screenInfo))
- {
- // Handle error: unable to retrieve font info
- // You can print an error message or throw an exception
- return false;
- }
- return screenInfo.dwSize.X;
- }
- int KeyboardInput()
- {
- return _getch();;
- }
- #else // Unix/Linux/MacOS-specific code
- #include <ncurses.h>
- const int upArrow = KEY_UP; // 65 Up arrow key (Linux/MacOS)
- const int downArrow = KEY_DOWN; // 66 Down arrow key (Linux/MacOS)
- const int enterKey = '\n'; // Enter key (Linux/MacOS)
- 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
- }
- short GetConsoleSizes()
- {
- int rows, cols;
- getmaxyx(stdscr, rows, cols);
- return cols;
- }
- int KeyboardInput()
- {
- initscr(); // Initialize ncurses
- keypad(stdscr, true); // Enable keypad mode
- noecho(); // Disable echoing of input characters
- int key = getch(); // Read a key press
- endwin(); // Clean up ncurses
- return key;
- }
- #endif
- const int escapeKey = 27; // Escape key
- 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()
- {
- FontSize(6);
- MaximizeWindow();
- ShowConsoleCursor(false);
- 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);
- 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;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(12));
- }
- }
- void DisplayMenu(std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
- {
- ClearScreen();
- std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered(text);
- std::cout << "\n" << "\n" << "\n";
- for (size_t i = 0; i < sizeOfTheStringMenu; i++)
- {
- selectedOption == i ? CoutCentered("--> " + mainMenu[i]) : CoutCentered("" + mainMenu[i]);
- std::cout << "\n";
- }
- }
- /*
- This function is "int" and not "size_t", because the "size_t" data type doesn't return negative numbers,
- 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),
- in other words, only positive, including 0
- */
- template <typename T>
- int MenuMovement(T menu[], int selectedOption, size_t sizeOfTheArray, int key)
- {
- key = KeyboardInput();
- if (key == upArrow)
- {
- selectedOption--;
- if (selectedOption < 0)
- {
- selectedOption = sizeOfTheArray - 1;
- }
- }
- else if (key == downArrow)
- {
- selectedOption++;
- if (selectedOption >= sizeOfTheArray)
- {
- selectedOption = 0;
- }
- }
- else if (key == enterKey)
- {
- menu[selectedOption](selectedOption);
- }
- else if (key == escapeKey)
- {
- selectedOption = - 6;
- }
- return selectedOption;
- }
- void GameStart(int selectedOption)
- {
- ClearScreen();
- std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- typedef void(*difficultyOptions)(int);
- difficultyOptions difficultyMenuOptions[] = { GameStart, Credits, ExitGame };
- size_t sizeOfTheArray = sizeof(difficultyMenuOptions) / sizeof(difficultyMenuOptions[0]);
- std::string difficultyMenu[] = { "Start", "Cdits", "Eit" };
- size_t sizeOfTheStringMenu = std::size(difficultyMenu);
- std::string text = "Difficulty Menu";
- DifficultyMenu(difficultyMenuOptions, sizeOfTheArray, difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
- }
- void Controls(int selectedOption)
- {
- ClearScreen();
- std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The controls are showed!");
- while (!IsKeyPressedToGoBackToThePreviousMenu(KeyboardInput()));
- }
- void Credits(int selectedOption)
- {
- ClearScreen();
- std::cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The credits are showed!");
- while (!IsKeyPressedToGoBackToThePreviousMenu(KeyboardInput()));
- }
- void ExitGame(int selectedOption)
- {
- ClearScreen();
- std::exit(0);
- }
- bool IsKeyPressedToGoBackToThePreviousMenu(int key)
- {
- if (key == escapeKey)
- {
- return true;
- }
- return false;
- }
- template <typename T>
- void DifficultyMenu(T difficultyMenuOptions[], size_t sizeOfTheArray, std::string difficultyMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
- {
- int key = 0;
- while (selectedOption != -6)
- {
- DisplayMenu(difficultyMenu, sizeOfTheStringMenu, text, selectedOption);
- selectedOption = MenuMovement(difficultyMenuOptions, selectedOption, sizeOfTheArray, key);
- std::this_thread::sleep_for(std::chrono::milliseconds(12));
- }
- }
- void CoutCentered(const std::string& text)
- {
- size_t textWidth = text.length();
- short consoleWidth = GetConsoleSizes();
- size_t leftMargin = (consoleWidth - textWidth) / 2;
- std::cout.width(leftMargin + textWidth);
- std::cout << text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement