TwoOfDiamonds

TicTacToe 2.0

Aug 6th, 2012
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.10 KB | None | 0 0
  1.  SimpleTicTacToe.cpp
  2.  
  3. // SimpleTicTacToe.cpp : Defines the entry point for the console application.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include "Game.h"
  8.  
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     Game::Start() ;
  13.  
  14.     return 0;
  15. }
  16.  
  17.  Game.h
  18.  
  19. #pragma once
  20. #include "SFML\Window.hpp"
  21. #include "SFML\Graphics.hpp"
  22.  
  23. #define SIZE 3
  24.  
  25. class Game
  26. {
  27. public:
  28.     static void Start();
  29.  
  30. private:
  31.  
  32.     static int board[3][3] ;
  33.  
  34.     static bool IsExiting();
  35.     static void GameLoop();
  36.  
  37.     static void LoadBackground() ;
  38.     static void LoadX() ;
  39.     static void LoadO() ;
  40.  
  41.     static void ShowSplashScreen();
  42.     static void ShowMenu() ;
  43.     static void ShowCredits() ;
  44.    
  45.     static void Play1P() ;
  46.     static void Play2P() ;
  47.     static bool checkWin() ;
  48.     static void changeTurn() ;
  49.  
  50.     static void DrawGame() ;
  51.     static void SetDraw () ;
  52.     static void HandleClick (int x, int y) ;
  53.     static void DoAI () ;
  54.     static void ChooseTurn() ;
  55.  
  56.     enum GameState { Uninitialized, ShowingSplash, Paused,
  57.                     ShowingMenu, Playing1P, Playing2P, Credits, Exiting };
  58.  
  59.     enum {EMPTY, X, O, DRAW} ;
  60.  
  61.     static GameState _gameState;
  62.     static sf::RenderWindow _mainWindow;
  63.  
  64.     static sf::Sprite _sprite ;
  65.     static sf::Texture _texture ;
  66.     static std::string _filename ;
  67.     static bool _isLoaded ;
  68.  
  69.     static sf::RectangleShape borders[4] ;
  70.     static sf::RectangleShape lastLine ;
  71.     static sf::RectangleShape lastColumn ;
  72.     static sf::RectangleShape sepLine ;
  73.  
  74.     static sf::Text Turn [4] ;
  75.     static sf::Text Reset ;
  76.     static sf::Text goMainMenu ;
  77.  
  78.     static int _win ;
  79.     static int _turn ;
  80.     static int _computer ;
  81.     static int _player ;
  82.  
  83.     static sf::Sprite xSprite ;
  84.     static sf::Sprite oSprite ;
  85.     static sf::Sprite backSprite ;
  86.  
  87.     static sf::Texture xTexture ;
  88.     static sf::Texture oTexture ;
  89.     static sf::Texture backTexture ;
  90. };
  91.  
  92.  Game.cpp
  93.  
  94. #include "stdafx.h"
  95. #include "Game.h"
  96. #include "SplashScreen.h"
  97. #include "MainMenu.h"
  98. #include "Credits.h"
  99.  
  100. void Game::Start(void)
  101. {
  102.   if(_gameState != Uninitialized)
  103.     return;
  104.  
  105.   _mainWindow.create(sf::VideoMode(560,560,32),"Tic-Tac-Toe");
  106.  
  107.   _gameState = Game::ShowingSplash;
  108.  
  109.   SetDraw() ;
  110.   ChooseTurn() ;
  111.  
  112.   std::srand(time(NULL)) ;
  113.  
  114.   while(!IsExiting())
  115.   {
  116.     GameLoop();
  117.   }
  118.  
  119.   _mainWindow.close();
  120. }
  121.  
  122. void Game::ChooseTurn()
  123. {
  124.     _computer = std::rand() % 2 ;
  125.  
  126.     if (_computer == 0) _computer = Game::X ;
  127.     else _computer = Game::O ;
  128.  
  129.     if (_computer == Game::X)
  130.         _player = Game::O ;
  131.     else _player = Game::X ;
  132. }
  133.  
  134. bool Game::IsExiting()
  135. {
  136.   if(_gameState == Game::Exiting)
  137.     return true;
  138.   else
  139.     return false;
  140. }
  141.  
  142. void Game::GameLoop()
  143. {
  144.     switch (_gameState)
  145.     {
  146.     case Game::ShowingMenu:
  147.         {
  148.             ShowMenu() ;
  149.             break ;
  150.         }
  151.     case Game::ShowingSplash:
  152.         {
  153.             ShowSplashScreen() ;
  154.             break ;
  155.         }
  156.     case Game::Playing1P:
  157.         {
  158.             Play1P();
  159.             break ;
  160.         }
  161.     case Game::Playing2P:
  162.         {
  163.             Play2P() ;
  164.             break ;
  165.         }
  166.     case Game::Credits:
  167.         {
  168.             ShowCredits() ;
  169.             break ;
  170.         }
  171.     }
  172. }
  173.  
  174. void Game::ShowSplashScreen()
  175. {
  176.     SplashScreen splashScreen ;
  177.     splashScreen.Show(_mainWindow) ;
  178.     _gameState = Game::ShowingMenu ;
  179. }
  180.  
  181. void Game::ShowMenu()
  182. {
  183.     MainMenu mainMenu ;
  184.     MainMenu::MenuResult result = mainMenu.Show (_mainWindow) ;
  185.     switch (result)
  186.     {
  187.     case MainMenu::Exit:
  188.         _gameState = Game::Exiting ;
  189.         break ;
  190.     case MainMenu::Play1P:
  191.         _gameState = Game::Playing1P ;
  192.         break ;
  193.     case MainMenu::Play2P:
  194.         _gameState = Game::Playing2P ;
  195.         break ;
  196.     case MainMenu::Credits:
  197.         _gameState = Game::Credits ;
  198.         break ;
  199.     }
  200. }
  201.  
  202. void Game::ShowCredits()
  203. {
  204.     Creditz credits ;
  205.     credits.Show(_mainWindow) ;
  206.     _gameState = Game::ShowingMenu ;
  207. }
  208.  
  209. void Game::Play1P()
  210. {
  211.     sf::Event Event ;
  212.  
  213.     if (_turn == _player)
  214.     {
  215.         while(_mainWindow.pollEvent (Event))
  216.         {
  217.             DrawGame() ;
  218.             _mainWindow.display () ;
  219.  
  220.             if (Event.type == sf::Event::Closed ) _gameState = Game::Exiting ;
  221.  
  222.             if (Event.type == sf::Event::KeyPressed)
  223.             {
  224.                 if (Event.key.code == sf::Keyboard::Escape) ShowMenu() ;
  225.             }
  226.  
  227.             if (Event.type == sf::Event::MouseButtonPressed)
  228.             {
  229.                 HandleClick (Event.mouseButton.x, Event.mouseButton.y) ;
  230.             }
  231.         }
  232.     }
  233.    
  234.     else
  235.     {
  236.         DrawGame() ;
  237.         DoAI() ;
  238.         changeTurn() ;
  239.     }
  240. }
  241.  
  242. void Game::DoAI()
  243. {
  244.     for (int i = 0 ;i < 3; i++)
  245.     {
  246.         for (int j = 0; j<3; j++)
  247.         {
  248.             if (board[i][j] == Game::EMPTY)
  249.             {
  250.                 board[i][j] = _computer ;
  251.                 if (checkWin())
  252.                 {
  253.                     return ;
  254.                 }
  255.                 else board[i][j] = Game::EMPTY ;
  256.             }
  257.         }
  258.     }
  259.  
  260.     for (int i = 0; i<3 ; i++)
  261.     {
  262.         for (int j = 0 ; j<3 ;j++)
  263.         {
  264.             if (board[i][j] == Game::EMPTY)
  265.             {
  266.                 board[i][j] = _player ;
  267.                 if (checkWin())
  268.                 {
  269.                     _win = Game::EMPTY ;
  270.                     board[i][j] = _computer ;
  271.                     return ;
  272.                 }
  273.                 else board [i][j] = Game::EMPTY ;
  274.             }
  275.         }
  276.     }
  277.  
  278.     if ( board[1][1] == Game::EMPTY)
  279.     {
  280.         board[1][1] = _computer ;
  281.         return ;
  282.     }
  283.     else
  284.     {
  285.         while (_win == Game::EMPTY)
  286.         {
  287.             int x = rand() % 3 ;
  288.             int y = rand() % 3 ;
  289.             if (board[x][y] == Game::EMPTY)
  290.             {
  291.                 board[x][y] = _computer ;
  292.                 return ;
  293.             }
  294.         }
  295.     }
  296. }
  297.  
  298. void Game::Play2P()
  299. {
  300.     sf::Event Event ;
  301.     while(_mainWindow.pollEvent (Event))
  302.     {
  303.         DrawGame() ;
  304.         _mainWindow.display () ;
  305.  
  306.         if (Event.type == sf::Event::Closed ) _gameState = Game::Exiting ;
  307.  
  308.         if (Event.type == sf::Event::KeyPressed)
  309.         {
  310.             if (Event.key.code == sf::Keyboard::Escape) ShowMenu() ;
  311.         }
  312.  
  313.         if (Event.type == sf::Event::MouseButtonPressed)
  314.         {
  315.             HandleClick (Event.mouseButton.x, Event.mouseButton.y) ;
  316.         }
  317.     }
  318. }
  319.  
  320. void Game::SetDraw()
  321. {
  322.     for (int i = 0; i<SIZE ; i++)
  323.         for (int j = 0; j<SIZE ; j++)
  324.             board[i][j] = Game::EMPTY ; //init board
  325.    
  326.     LoadBackground () ; //load background image
  327.     LoadX() ;
  328.     LoadO() ;
  329.  
  330.     //set border lines
  331.  
  332.     for ( int i = 0 ; i < 4 ; i++)
  333.     {
  334.         if (i<2)
  335.         {
  336.             borders[i].setPosition (0, (i+1) * 140) ;
  337.             borders[i].setSize (sf::Vector2f (420, 3)) ;
  338.         }
  339.         else
  340.         {
  341.             borders [i].setPosition ((i - 1)*140, 0) ;
  342.             borders[i].setSize (sf::Vector2f (3, 420)) ;
  343.         }
  344.  
  345.         borders[i].setFillColor (sf::Color (0, 0 , 0)) ;
  346.     }
  347.  
  348.     //set Column tine of borders
  349.  
  350.     lastColumn.setPosition (420, 0) ;
  351.     lastColumn.setSize (sf::Vector2f (3, 559)) ;
  352.     lastColumn.setFillColor (sf::Color ( 0, 0 , 0)) ;
  353.    
  354.  
  355.     //set last line of borders
  356.  
  357.     lastLine.setPosition ( 0, 420 ) ;
  358.     lastLine.setSize (sf::Vector2f (599, 3)) ;
  359.     lastLine.setFillColor (sf::Color (0, 0 , 0)) ;
  360.    
  361.     //set separator line
  362.     sepLine.setPosition ( 210, 420) ;
  363.     sepLine.setSize (sf::Vector2f ( 3, 180)) ;
  364.     sepLine.setFillColor (sf::Color (0, 0 ,0)) ;
  365.  
  366.     sf::Font tronFont ;
  367.     if (!tronFont.loadFromFile ("fonts/neon2.ttf"))
  368.     {
  369.         std::cout<<"Couldn't load TRON.ttf font\n";
  370.         tronFont.getDefaultFont() ;
  371.     }
  372.  
  373.     for (int i =0 ; i < 4; i++)
  374.     {
  375.         //Turn[i].setFont (tronFont) ;
  376.         Turn[i].setCharacterSize(60) ;
  377.         Turn[i].setColor (sf::Color (255, 255, 255)) ;
  378.         Turn[i].setPosition ( 470 , i*100 + 10) ;
  379.     }
  380.  
  381.     Turn[0].setString ("T") ;
  382.     Turn[1].setString ("U") ;
  383.     Turn[2].setString ("R") ;
  384.     Turn[3].setString ("N") ;
  385.  
  386.    
  387.     Reset.setColor (sf::Color (255, 0 ,255)) ;
  388.     Reset.setCharacterSize (60) ;
  389.     Reset.setPosition ( 10 , 500) ;
  390.     Reset.setRotation (-35.0f) ;
  391.     Reset.setString ("Reset") ;
  392.  
  393.     goMainMenu.setColor (sf::Color (255, 0, 255)) ;
  394.     goMainMenu.setCharacterSize (40) ;
  395.     goMainMenu.setPosition (220, 520) ;
  396.     goMainMenu.setRotation ( - 35.0f) ;
  397.     goMainMenu.setString ("Main Menu") ;
  398. }
  399.  
  400. void Game::DrawGame()
  401. {
  402.     _mainWindow.draw (backSprite) ;
  403.     //Board::Draw(window) ;
  404.  
  405.     for (int i = 0; i < 4 ; i++)
  406.         _mainWindow.draw (borders[i]) ;
  407.  
  408.     _mainWindow.draw (lastLine) ;
  409.     _mainWindow.draw (lastColumn) ;
  410.     _mainWindow.draw (sepLine) ;
  411.  
  412.     for (int i = 0 ; i < 4 ; i++)
  413.         _mainWindow.draw (Turn[i]) ;
  414.  
  415.     _mainWindow.draw (Reset) ;
  416.     _mainWindow.draw (goMainMenu) ;
  417.    
  418.     for (int i = 0 ; i < SIZE ; i++)
  419.         for (int j = 0; j < SIZE ; j++)
  420.         {
  421.             if ( board[i][j] == Game::X)
  422.             {
  423.                 xSprite.setPosition (i*140, j*140) ;
  424.                 _mainWindow.draw (xSprite) ;
  425.                 //Board::_xPiece.SetPosition (i*140, j*140) ;
  426.                 //Board::_xPiece.Draw(window) ;
  427.             }
  428.             else if ( board[i][j] == Game::O)
  429.             {
  430.                 oSprite.setPosition (i*140, j*140) ;
  431.                 _mainWindow.draw (oSprite) ;
  432.                 //Board::_oPiece.SetPosition (i*140, j *140) ;
  433.                 //Board::_oPiece.Draw (window) ;
  434.             }
  435.         }
  436.  
  437.     if (_turn == Game::X)
  438.     {
  439.         xSprite.setPosition (420, 420) ;
  440.         _mainWindow.draw (xSprite) ;
  441.         //Board::_xPiece.SetPosition (420, 420) ;
  442.         //Board::_xPiece.Draw(window) ;
  443.     }
  444.     else
  445.     {
  446.         oSprite.setPosition (420,420) ;
  447.         _mainWindow.draw (oSprite) ;
  448.         //Board::_oPiece.SetPosition (420, 420) ;
  449.         //Board::_oPiece.Draw(window) ;
  450.     }
  451.  
  452.     if (_win == Game::O)
  453.     {
  454.         sf::Text xWin ("X HAS WON") ;
  455.         if (_gameState == Game::Playing1P && _win == _computer) xWin.setString ("O HAS WON") ;
  456.         xWin.setPosition (sf::Vector2f( 20 , 340)) ;
  457.         xWin.setCharacterSize (70) ;
  458.         xWin.setRotation (-45.0f) ;
  459.         for (int i = 0 ;i < 3;i++)
  460.             for (int j = 0 ; j < 3 ; j++)
  461.                 if (board[i][j] == Game::EMPTY)
  462.                     board[i][j] = 9 ;
  463.  
  464.         _mainWindow.draw (xWin) ;
  465.     }
  466.  
  467.     if (_win == Game::X)
  468.     {
  469.         sf::Text oWin ("O HAS WON") ;
  470.         if (_gameState == Game::Playing1P && _win == _computer) oWin.setString ("X HAS WON") ;
  471.         oWin.setPosition (sf::Vector2f( 20 , 340)) ;
  472.         oWin.setCharacterSize (70) ;
  473.         oWin.setRotation (-45.0f) ;
  474.         for (int i = 0 ;i < 3;i++)
  475.             for (int j = 0 ; j < 3 ; j++)
  476.                 if (board[i][j] == Game::EMPTY)
  477.                     board[i][j] = 9 ;
  478.  
  479.         _mainWindow.draw (oWin) ;
  480.     }
  481.  
  482.     if (_win != Game::O && _win!= Game::X)
  483.     {
  484.         _win = Game::DRAW ;
  485.         for (int i = 0; i< 3;i++)
  486.             for (int j = 0; j<3 ;j++)
  487.                 if (board[i][j] == Game::EMPTY)
  488.                     _win = Game::EMPTY ;
  489.     }
  490.  
  491.     if (_win == Game::DRAW)
  492.     {
  493.         sf::Text dWin ("IT'S A DRAW") ;
  494.         dWin.setCharacterSize (70) ;
  495.         dWin.setPosition (sf::Vector2f( 20 , 340)) ;
  496.         dWin.setRotation (-45.0f) ;
  497.         _mainWindow.draw (dWin) ;
  498.     }
  499.  
  500. }
  501.  
  502. void Game::LoadX()
  503. {
  504.     xTexture.loadFromFile ("images/counters.bmp", sf::IntRect (280, 0 , 140 ,140)) ;
  505.     xSprite.setTexture(xTexture) ;
  506.  
  507. }
  508. void Game::LoadO()
  509. {
  510.     oTexture.loadFromFile ("images/counters.bmp", sf::IntRect (140, 140, 140, 140)) ;
  511.     oSprite.setTexture(oTexture) ;
  512. }
  513.  
  514. void Game::LoadBackground()
  515. {
  516.     backTexture.loadFromFile ("images/background.jpg") ;
  517.     backSprite.setTexture(backTexture) ;
  518. }
  519.  
  520. void Game::HandleClick(int x, int y)
  521. {
  522.     if (x > 0 && x < 420 && y > 0 && y < 420 && board[x/140][y/140] == Game::EMPTY)
  523.     {
  524.         board [x/140][y/140] = _turn ;
  525.         changeTurn() ;
  526.         checkWin() ;
  527.  
  528.     }
  529.  
  530.     else if (x > 0 && x < 210 && y > 420 && y < 560)
  531.     {
  532.         for (int i = 0; i< 3 ;i++ )
  533.             for (int j =0 ;j<3 ;j++)
  534.                 board[i][j] = Game::EMPTY ;
  535.         _turn = Game::X ;
  536.         _win = Game::EMPTY ;
  537.         ChooseTurn() ;
  538.         DrawGame() ;
  539.     }
  540.  
  541.     else if (x > 210 && x < 420 && y > 420 && y < 560)
  542.     {
  543.         _gameState = Game::ShowingMenu ;
  544.         for (int i = 0; i< 3 ;i++ )
  545.             for (int j =0 ;j<3 ;j++)
  546.                 board[i][j] = Game::EMPTY ;
  547.         _turn = Game::X ;
  548.         _win = Game::EMPTY ;
  549.         ChooseTurn() ;
  550.         DrawGame() ;
  551.     }
  552. }
  553.  
  554. void Game::changeTurn ()
  555. {
  556.     if (_turn == Game::X)
  557.         _turn = Game::O ;
  558.     else
  559.         _turn = Game::X ;
  560. }
  561.  
  562. bool Game::checkWin()
  563. {
  564.     if ((board[0][0] == board [0][1] && board [0][0] == board[0][2] && board[0][0] != Game::EMPTY)
  565.         || (board[1][0] == board[1][1] && board[1][0] == board [1][2] && board[1][0] != Game::EMPTY)
  566.         || (board[2][0] == board[2][1] && board[2][0] == board [2][2] && board[2][0] != Game::EMPTY)
  567.         || (board[0][0] == board[1][0] && board[0][0] == board [2][0] && board[0][0] != Game::EMPTY)
  568.         || (board[0][1] == board[1][1] && board[0][1] == board [2][1] && board[0][1] != Game::EMPTY)
  569.         || (board[0][2] == board[1][2] && board[0][2] == board [2][2] && board[0][2] != Game::EMPTY)
  570.         || (board[0][0] == board[1][1] && board[0][0] == board [2][2] && board[0][0] != Game::EMPTY)
  571.         || (board[0][2] == board[1][1] && board[0][2] == board [2][0]) && board[0][2] != Game::EMPTY)
  572.     {
  573.         _win = _turn ;
  574.         return true ;
  575.     }
  576.     return false ;
  577. }
  578.  
  579. Game::GameState Game::_gameState = Uninitialized;
  580. sf::RenderWindow Game::_mainWindow;
  581. int Game::board[3][3] ;
  582. sf::Sprite Game::_sprite ;
  583. sf::Texture Game::_texture ;
  584. std::string Game::_filename ;
  585.  
  586. sf::RectangleShape Game::borders[4] ;
  587. sf::RectangleShape Game::lastColumn ;
  588. sf::RectangleShape Game::lastLine ;
  589. sf::RectangleShape Game::sepLine ;
  590.  
  591. sf::Text Game::Turn [4] ;
  592. sf::Text Game::Reset ;
  593. sf::Text Game::goMainMenu ;
  594.  
  595. int Game::_win = Game::EMPTY;
  596. int Game::_turn = Game::X ;
  597. int Game::_computer ;
  598. int Game::_player ;
  599.  
  600. sf::Sprite Game::xSprite ;
  601. sf::Sprite Game::oSprite ;
  602. sf::Sprite Game::backSprite ;
  603.  
  604. sf::Texture Game::xTexture ;
  605. sf::Texture Game::oTexture ;
  606. sf::Texture Game::backTexture ;
  607.  
  608.  MainMenu.h
  609.  
  610. //include guards
  611. #ifndef _MAINMENU_H
  612. #define _MAINMENU_H
  613.  
  614. //include needed headers
  615. #include "SFML/Window.hpp"
  616. #include "SFML/Graphics.hpp"
  617. #include <list>
  618.  
  619. //the MainMenu class
  620. class MainMenu
  621. {
  622. public:
  623.  
  624.     //creating all possible menu outcomes
  625.     enum MenuResult {Nothing, Exit, Play1P, Play2P, Credits} ;
  626.  
  627.     //creating the MenuItem struct
  628.     struct MenuItem
  629.     {
  630.     public:
  631.         sf::IntRect rect;
  632.         MenuResult action ;
  633.     };
  634.  
  635.     MenuResult Show (sf::RenderWindow&) ; //display the menu
  636.  
  637. private:
  638.     MenuResult GetMenuResponse (sf::RenderWindow&) ; //get menu response to user action
  639.     MenuResult HandleClick (int x, int y) ; //handle the clicks
  640.     std::list<MenuItem> _menuItems ; //list containing all menu items
  641. };
  642.  
  643. #endif //_MAINMENU_H
  644.  
  645.  MainMenu.cpp
  646.  
  647. //include needed headers
  648. #include "stdafx.h"
  649. #include "mainmenu.h"
  650.  
  651. MainMenu::MenuResult MainMenu::Show (sf::RenderWindow& window)
  652. {
  653.  
  654.     window.clear (sf::Color (0, 0, 0)) ;
  655.  
  656.     //create menu with primitives
  657.     sf::RectangleShape lines[4] ;
  658.     for (int i = 0; i < 3 ; i++)
  659.     {
  660.         lines[i].setPosition (0, (i+1) * 140 - 1) ;
  661.         lines[i].setSize (sf::Vector2f (559, 3)) ;
  662.         lines[i].setFillColor (sf::Color (255, 255, 255)) ;
  663.         window.draw (lines[i]) ;
  664.     }
  665.  
  666.     sf::Text text[5] ;
  667.  
  668.     text[0].setString ("Play: 1 Player") ;
  669.     text[1].setString ("Play: 2 Players") ;
  670.     text[2].setString ("Credits") ;
  671.     text[3].setString ("Exit") ;
  672.  
  673.     for (int i = 0; i < 4 ; i++)
  674.     {
  675.         text[i].setColor (sf::Color (255, 255 , 255)) ;
  676.         text[i].setCharacterSize  (40) ;
  677.         text[i].setOrigin (text[i].getLocalBounds().width / 2, text[i].getLocalBounds().height / 2) ;
  678.         text[i].setPosition (270.0f, ((140.0f * (i+1)) - 70.0f)) ;
  679.         window.draw(text[i]) ;
  680.     }
  681.  
  682.     //Setup clickable regions
  683.  
  684.     //play1p menu item coordinates
  685.     MenuItem play1PButton ;
  686.     play1PButton.rect.top = 1 ;
  687.     play1PButton.rect.height = 138 ;
  688.     play1PButton.rect.left = 0 ;
  689.     play1PButton.rect.width = 559 ;
  690.     play1PButton.action = Play1P ;
  691.  
  692.     //play2p menu item coordinates
  693.     MenuItem play2PButton ;
  694.     play2PButton.rect.top = 141 ;
  695.     play2PButton.rect.height = 138 ;
  696.     play2PButton.rect.left = 0 ;
  697.     play2PButton.rect.width = 559 ;
  698.     play2PButton.action = Play2P ;
  699.  
  700.     //credits menu item coordinates
  701.     MenuItem creditsButton ;
  702.     creditsButton.rect.top = 281 ;
  703.     creditsButton.rect.height = 138 ;
  704.     creditsButton.rect.left = 0 ;
  705.     creditsButton.rect.width = 559 ;
  706.     creditsButton.action = Credits ;
  707.  
  708.     //exit menu item coordinates
  709.     MenuItem exitButton ;
  710.     exitButton.rect.top = 419 ;
  711.     exitButton.rect.height = 138 ;
  712.     exitButton.rect.left = 0 ;
  713.     exitButton.rect.width = 559 ;
  714.     exitButton.action = Exit ;
  715.  
  716.     _menuItems.push_back (play1PButton) ;
  717.     _menuItems.push_back (play2PButton) ;
  718.     _menuItems.push_back (creditsButton) ;
  719.     _menuItems.push_back (exitButton) ;
  720.  
  721.     window.display() ;
  722.  
  723.     return GetMenuResponse (window) ;
  724. }
  725.  
  726. MainMenu::MenuResult MainMenu::HandleClick(int x, int y)
  727. {
  728.     for (std::list<MenuItem>::iterator it = _menuItems.begin() ; it != _menuItems.end(); it++)
  729.     {
  730.         if (it->rect.contains (x, y))
  731.             return it->action ;
  732.     }
  733.  
  734.     return Nothing ;
  735. }
  736.  
  737. MainMenu::MenuResult MainMenu::GetMenuResponse (sf::RenderWindow& window)
  738. {
  739.     sf::Event menuEvent ;
  740.  
  741.     while (true)
  742.     {
  743.         while (window.pollEvent(menuEvent))
  744.         {
  745.             if(menuEvent.type == sf::Event::MouseButtonPressed)
  746.             {
  747.                 return HandleClick (menuEvent.mouseButton.x, menuEvent.mouseButton.y) ;
  748.             }
  749.             if(menuEvent.type == sf::Event::Closed)
  750.             {
  751.                 return Exit ;
  752.             }
  753.         }
  754.     }
  755. }
  756.  
  757.  Credits.h
  758.  
  759. #pragma once
  760.  
  761. #include "SFML/Window.hpp"
  762. #include "SFML/Graphics.hpp"
  763.  
  764. class Creditz
  765. {
  766. public:
  767.     void Show (sf::RenderWindow& window) ;
  768. };
  769.  
  770.  Credits.cpp
  771.  
  772. #include "stdafx.h"
  773. #include "Credits.h"
  774.  
  775. void Creditz::Show (sf::RenderWindow& renderWindow)
  776. {
  777.     sf::Text Text ;
  778.     Text.setCharacterSize (20) ;
  779.     Text.setString("   Game created by Radu Daniel Alexandru, \n   student at CNILC Ploiesti\n   Thanks to Gamefromscratch.com for all the tutorials") ;
  780.  
  781.     renderWindow.clear (sf::Color (0, 0, 0)) ;
  782.     renderWindow.draw (Text) ;
  783.     renderWindow.display () ;
  784.  
  785.     sf::Event Event ;
  786.     while (true)
  787.     {
  788.         while (renderWindow.pollEvent (Event))
  789.         {
  790.             if (Event.type == sf::Event::EventType::KeyPressed
  791.                 || Event.type == sf::Event::EventType::MouseButtonPressed
  792.                 || Event.type == sf::Event::EventType::Closed )
  793.             {
  794.                 return ;
  795.             }
  796.         }
  797.     }
  798. }
  799.  
  800.  SplashScreen.h
  801.  
  802. #pragma once
  803.  
  804. class SplashScreen
  805. {
  806. public:
  807.     void Show (sf::RenderWindow &window) ;
  808. };
  809.  
  810.  SplashScreen.cpp
  811.  
  812. #include "StdAfx.h"
  813. #include "SplashScreen.h"
  814.  
  815. void SplashScreen::Show (sf::RenderWindow &window)
  816. {
  817.     sf::Texture Texture ;
  818.     if (!Texture.loadFromFile ("images/splashscreen.jpg"))
  819.     {
  820.         return ;
  821.     }
  822.  
  823.     sf::Sprite sprite(Texture) ;
  824.  
  825.     window.draw (sprite) ;
  826.     window.display () ;
  827.  
  828.     sf::Event Event ;
  829.     while (true)
  830.     {
  831.         while (window.pollEvent (Event))
  832.         {
  833.             if (Event.type == sf::Event::EventType::KeyPressed
  834.                 || Event.type == sf::Event::MouseButtonPressed
  835.                 || Event.type == sf::Event::EventType::Closed)
  836.             {
  837.                 return ;
  838.             }
  839.         }
  840.     }
  841. }
  842.  
  843.  stdafx.h
  844.  
  845. // stdafx.h : include file for standard system include files,
  846. // or project specific include files that are used frequently, but
  847. // are changed infrequently
  848. //
  849.  
  850. #pragma once
  851.  
  852. #include "targetver.h"
  853.  
  854. #include <stdio.h>
  855. #include <stdlib.h>
  856. #include <tchar.h>
  857.  
  858.  
  859.  
  860. // TODO: reference additional headers your program requires here
  861.  
  862. #include <SFML/System.hpp>
  863. #include <SFML/Graphics.hpp>
  864. #include <SFML/Window.hpp>
  865. #include <SFML/Audio.hpp>
  866.  
  867. #include <map>
  868. #include <iostream>
  869. #include <cassert>
  870.  
  871.  stdafx.cpp
  872.  
  873. // stdafx.cpp : source file that includes just the standard includes
  874. // SimpleTicTacToe.pch will be the pre-compiled header
  875. // stdafx.obj will contain the pre-compiled type information
  876.  
  877. #include "stdafx.h"
  878.  
  879. // TODO: reference any additional headers you need in STDAFX.H
  880. // and not in this file
  881.  
  882.  targetver.h
  883.  
  884. #pragma once
  885.  
  886. // The following macros define the minimum required platform.  The minimum required platform
  887. // is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
  888. // your application.  The macros work by enabling all features available on platform versions up to and
  889. // including the version specified.
  890.  
  891. // Modify the following defines if you have to target a platform prior to the ones specified below.
  892. // Refer to MSDN for the latest info on corresponding values for different platforms.
  893. #ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
  894. #define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of Windows.
  895. #endif
Advertisement
Add Comment
Please, Sign In to add comment