Advertisement
Volatile_Pulse

Menu_Example2.cpp

Jun 18th, 2012
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************************
  2.  *                                                                            *
  3.  *                            Volatile Programming                            *
  4.  *                              Menu_Example2.cpp                             *
  5.  *                                                                            *
  6.  ******************************************************************************
  7.  *                                                                            *
  8.  * By: Volatile Pulse                                                         *
  9.  * What: An example to demonstrate the different features of the Menu2 class  *
  10.  * Date: 06.18.2012                                                           *
  11.  *                                                                            *
  12.  ******************************************************************************/
  13.  
  14. #include "\lib\menu2.h"
  15. #include "\lib\header.h"
  16.  
  17. // Define Calc Functions
  18. void AddMe(int x, int y) { std::cout << x << " + " << y; VP_GetCh(); }
  19. void SubtractMe(int x, int y) { std::cout << x << " - " << y; VP_GetCh(); }
  20. void MultiplyMe() { std::cout << "You chose to do some multiplication"; VP_GetCh(); }
  21. void DivideMe() { std::cout << "You chose to do some division"; VP_GetCh(); }
  22.  
  23. // Define Game Functions
  24. void Tetris() { std::cout << "You chose to play Tetris"; VP_GetCh(); }
  25. void TTT() { std::cout << "You chose to play Tic-Tac-Toe"; VP_GetCh(); }
  26. void Battleship() { std::cout << "You chose to play Battleship"; VP_GetCh(); }
  27. void Minesweeper() { std::cout << "You chose to play Minesweeper"; VP_GetCh(); }
  28.  
  29. // Define AboutMe Function
  30. void AboutMe() {
  31.    std::cout << "This is the latest update in the Menu Class\n\n"
  32.              << "New features include passing functions to the menu options\n\n"
  33.              << "Currently Requires some C++11 support, specifically:\n"
  34.              << "\t#include <functional>";
  35.    VP_GetCh();
  36. }
  37.  
  38. int main() {
  39.    // Disable Console Cursor
  40.    VP_SetConsoleCursor(false);
  41.  
  42.    // Define Calculator Menu
  43.    Menu CalcMenu("Calculator", "-> ", " <-", CENTER, CENTER);
  44.    // Using std::bind to pass values to AddMe
  45.    CalcMenu.AddOption("Addition", std::bind(AddMe, 4, 5));
  46.    // Using a lambda function to call Subtract me with values
  47.    CalcMenu.AddOption("Subtraction", [] () { SubtractMe(9, 5); });
  48.    // Standard function call
  49.    CalcMenu.AddOption("Multiply", MultiplyMe);
  50.    CalcMenu.AddOption("Divide", DivideMe);
  51.    CalcMenu.AddOption("Main Menu");
  52.  
  53.    // Define Games Menu
  54.    Menu GameMenu("Games", "-> ", " <-", CENTER, CENTER);
  55.    GameMenu.AddOption("Tetris", Tetris);
  56.    // Using Lambda to call TTT
  57.    GameMenu.AddOption("Tic-Tac-Toe", [] () { TTT(); } );
  58.    GameMenu.AddOption("Battleship", Battleship);
  59.    GameMenu.AddOption("Minesweeper", Minesweeper);
  60.    GameMenu.AddOption("Main Menu");
  61.  
  62.    // Define Main Menu
  63.    Menu MainMenu("Menu v2 Example", "-> ", " <-", CENTER, CENTER);
  64.    // Must use std::bind or lambda to "Play" another menu
  65.    MainMenu.AddOption("Calculator", std::bind(&Menu::Play, std::ref(CalcMenu)));
  66.    // Notice the &: It is required to call class members
  67.    MainMenu.AddOption("Games", [&] () { GameMenu.Play(); } );
  68.    MainMenu.AddOption("About Me", AboutMe);
  69.    MainMenu.AddOption("Exit");
  70.  
  71.    MainMenu.Play();
  72.  
  73.    return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement