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>
- const size_t escapeKey = 27; // Escape key
- #ifdef _WIN32 // Windows-specific code
- #include <Windows.h>
- #include <conio.h>
- const size_t upArrow = 72; // Up arrow key (Windows)
- const size_t downArrow = 80; // Down arrow key (Windows)
- const size_t 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 <sys/ioctl.h>
- #include <unistd.h>
- #include <termios.h>
- #include <X11/Xlib.h>
- const size_t upArrow = 65; // Up arrow key (Linux/MacOS)
- const size_t downArrow = 66; // Down arrow key (Linux/MacOS)
- const size_t enterKey = 10; // Enter key (Linux/MacOS)
- void ClearScreen()
- {
- std::system("clear");
- }
- void FontSize(short sizeOfTheFont)
- {
- struct winsize ws;
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
- {
- // Handle error: unable to retrieve terminal size
- // You can print an error message or throw an exception
- return;
- }
- // Increase the font size by modifying the terminal size
- ws.ws_row += sizeOfTheFont; // Increase the number of rows
- ws.ws_col += sizeOfTheFont; // Increase the number of columns
- if (ioctl(STDOUT_FILENO, TIOCSWINSZ, &ws) == -1)
- {
- // Handle error: unable to set terminal size
- // You can print an error message or throw an exception
- return;
- }
- }
- void MaximizeWindow()
- {
- struct winsize ws;
- ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
- std::cout << "\e[8;" << ws.ws_row << ";" << ws.ws_col << "t"; // Set terminal size
- }
- void ShowConsoleCursor(bool showFlag)
- {
- std::cout << (showFlag ? "\033[?25h" : "\033[?25l"); // show/hide cursor
- std::cout.flush();
- }
- short GetConsoleSizes()
- {
- struct winsize ws;
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
- {
- // Handle error: unable to retrieve terminal size
- // You can print an error message or throw an exception
- std::exit(0);
- }
- return ws.ws_col;
- }
- int KeyboardInput()
- {
- struct termios oldSettings, newSettings;
- // Get the current terminal settings
- tcgetattr(STDIN_FILENO, &oldSettings);
- // Copy the current settings to the new settings
- newSettings = oldSettings;
- // Disable canonical mode (line buffering) and input echo
- newSettings.c_lflag &= ~(ICANON | ECHO);
- // Apply the new terminal settings
- tcsetattr(STDIN_FILENO, TCSANOW, &newSettings);
- // Read a single character
- char character = getchar();
- // Restore the original terminal settings
- tcsetattr(STDIN_FILENO, TCSANOW, &oldSettings);
- return character;
- }
- #endif
- 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]);
- if (i < sizeOfTheStringMenu - 1)
- {
- 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)
- {
- 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