Guest User

Untitled

a guest
Jul 11th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.12 KB | None | 0 0
  1. #include "Game.h"
  2.  
  3.  
  4.  
  5.  
  6.  
  7. void Game::setGameDiff(const difficulty newDiff)
  8. {
  9.     if ( gameState != INGAME ) // to avoid changing game difficulty in-game
  10.         gameDiff = newDiff;
  11. }
  12. void Game::setGameState(const state newState)
  13. {
  14.     gameState = newState;
  15. }
  16.  
  17. Game::difficulty Game::getGameDiff()
  18. {
  19.     return gameDiff;
  20. }
  21. Game::state Game::getGameState()
  22. {
  23.     return gameState;
  24. }
  25. Game::state Game::gameState = INMENU;
  26. Game::difficulty Game::gameDiff = NONEDIFF;
  27.  
  28. Game::Game()
  29.     : gameWindow(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT),WINDOW_TITLE), gameMenu(),
  30.             gameWorm(sf::Vector2f(WINDOW_HEIGHT/GAMEOBJECT_HEIGHT,WINDOW_WIDTH/GAMEOBJECT_WIDTH)),
  31.                  gameFood(gameWorm.getSnakePosition()), resume("resume",GAME_FONT(),DEFAULT_FONT_SIZE),// this is where the problems comes in
  32.                      pause("pause",GAME_FONT(),DEFAULT_FONT_SIZE) // this is where the problems comes in
  33. {
  34.    gameWindow.setFramerateLimit(FPS);
  35. }
  36.  
  37. Game::~Game()
  38. {
  39.  
  40. }
  41.  
  42. void Game::run()
  43. {
  44.     while(gameWindow.isOpen())
  45.     {
  46.  
  47.         sf::Event e;
  48.         while(gameWindow.pollEvent(e))
  49.         {
  50.             eventHandler(e);
  51.         }
  52.         update();
  53.         render();
  54.     }
  55. }
  56.  
  57. void Game::init()
  58. {
  59.  
  60. }
  61.  
  62. void Game::eventHandler(sf::Event e)
  63. {
  64.     if ( sf::Event::Closed == e.type)
  65.     {
  66.         gameWindow.close();
  67.     }
  68.     if ( gameState == INMENU )
  69.     {
  70.         if ( sf::Event::MouseMoved == e.type )
  71.         {
  72.             gameMenu.changeColorWhenHovered(sf::Mouse::getPosition(gameWindow));
  73.         }
  74.         if ( sf::Event::MouseButtonPressed == e.type )
  75.         {
  76.             if ( e.mouseButton.button == sf::Mouse::Left )
  77.             {
  78.                 gameMenu.changeStateWhenClicked(gameWindow);
  79.             }
  80.         }
  81.         if ( sf::Event::KeyPressed == e.type && Menu::getMenuState() == Menu::START )
  82.         {
  83.             switch(e.key.code)
  84.             {
  85.             case sf::Keyboard::Escape:
  86.                 gameState = INMENU;
  87.                 Menu::setMenuState(Menu::DEFAULT);
  88.                 break;
  89.             default:
  90.                 gameState = INGAME;
  91.                 break;
  92.             }
  93.         }
  94.  
  95.     }
  96.     else if ( gameState == INGAME )
  97.     {
  98.         if ( sf::Event::KeyPressed == e.type )
  99.         {
  100.             switch(e.key.code)
  101.             {
  102.             case sf::Keyboard::Left:
  103.                 gameWorm.setNewDir(Worm::LEFT);
  104.                 break;
  105.             case sf::Keyboard::Right:
  106.                 gameWorm.setNewDir(Worm::RIGHT);
  107.                 break;
  108.             case sf::Keyboard::Up:
  109.                 gameWorm.setNewDir(Worm::UP);
  110.                 break;
  111.             case sf::Keyboard::Down:
  112.                 gameWorm.setNewDir(Worm::DOWN);
  113.                 break;
  114.             case sf::Keyboard::P:
  115.                 gameState = PAUSEDINGAME;
  116.                 break;
  117.             default:
  118.                 break;
  119.             }
  120.         }
  121.     }
  122.     else if ( gameState == PAUSEDINGAME )
  123.     {
  124.  
  125.     }
  126. }
  127.  
  128. void Game::update()
  129. {
  130.     if ( gameState == INGAME )
  131.     {
  132.         gameWorm.update();
  133.     }
  134.     else
  135.     {
  136.         if ( Menu::getMenuState() == Menu::NONESTATE )
  137.         {
  138.             gameState = INGAME;
  139.         }
  140.     }
  141. }
  142.  
  143. void Game::render()
  144. {
  145.  
  146.     gameWindow.clear();
  147.     if ( gameState == INGAME )
  148.     {
  149.         gameWorm.draw(gameWindow); // when i changed the gameState as INGAME the gameworm and gamefood renders fine.
  150.         gameFood.draw(gameWindow);
  151.     }
  152.     else if ( gameState == INMENU )
  153.     {
  154.         gameMenu.draw(gameWindow); // when i changed the gameState as INMENU this one doesnt work, i think it has to do with the font.
  155.     }
  156.     gameWindow.display();
  157. }
  158.  
  159. //////////////MENU.CPP
  160. #include "Menu.h"
  161. #include <string>
  162. #include <iostream>
  163. #include "Game.h"
  164.  
  165.  
  166. void Menu::setMenuState(const state newState)
  167. {
  168.     if ( Game::getGameState() == Game::INMENU )
  169.     {
  170.         menuState = newState;
  171.     }
  172. }
  173.  
  174. Menu::state Menu::getMenuState()
  175. {
  176.     return menuState;
  177. }
  178.  
  179. Menu::state Menu::menuState = DEFAULT;
  180.  
  181. bool Menu::isHovered(sf::Text& txt)
  182. {
  183.     sf::Vector2f txtPos = txt.getPosition();
  184.     return ( mousePosition.x > txtPos.x && mousePosition.y > txtPos.y && mousePosition.x < txtPos.x + txt.getGlobalBounds().width && mousePosition.y < txtPos.y + txt.getGlobalBounds().height );
  185. }
  186.  
  187.  
  188. bool Menu::processColorOfTextHovered(sf::Text& txt)
  189. {
  190.     if ( isHovered(txt) )
  191.     {
  192.         txt.setColor(sf::Color::Red);
  193.         return true;
  194.     }
  195.     else
  196.     {
  197.         txt.setColor(sf::Color::White);
  198.         return false;
  199.     }
  200. }
  201.  
  202. sf::Vector2i Menu::mousePosition = sf::Vector2i(0,0);
  203.  
  204. Menu::Menu()
  205.     : start("Start",GAME_FONT(),DEFAULT_FONT_SIZE),
  206.         pressanykeytostart("Press any key to start or ESC to back",GAME_FONT(),DEFAULT_FONT_SIZE),
  207.         difficulty("Difficulty",GAME_FONT(),DEFAULT_FONT_SIZE),
  208.             easy("Easy",GAME_FONT(),DEFAULT_FONT_SIZE),
  209.                 hard("Hard",GAME_FONT(),DEFAULT_FONT_SIZE),
  210.                 exit("Exit",GAME_FONT(),DEFAULT_FONT_SIZE),
  211.                     areyousureyouwanttotexit("Are you sure you want to exit?",GAME_FONT(),DEFAULT_FONT_SIZE),
  212.                         yes("Yes",GAME_FONT(),DEFAULT_FONT_SIZE),
  213.                         no("No",GAME_FONT(),DEFAULT_FONT_SIZE)
  214. {
  215.  
  216.  
  217.  
  218.     defaultFont.loadFromFile("arial.ttf");
  219.     const float verticalOffset = 32;
  220.  
  221.     //default
  222.     start.setPosition((WINDOW_WIDTH/2)-(start.getGlobalBounds().width/2),((WINDOW_HEIGHT/2)-((start.getGlobalBounds().height*3)/2)));
  223.     difficulty.setPosition((WINDOW_WIDTH/2)-(difficulty.getGlobalBounds().width/2),start.getPosition().y+verticalOffset);
  224.     exit.setPosition((WINDOW_WIDTH/2)-(exit.getGlobalBounds().width/2),difficulty.getPosition().y+verticalOffset);
  225.  
  226.     //start
  227.     pressanykeytostart.setPosition((WINDOW_WIDTH/2)-(pressanykeytostart.getGlobalBounds().width/2),((WINDOW_HEIGHT/2)/2)-(pressanykeytostart.getGlobalBounds().height/2));
  228.  
  229.     //difficulty
  230.     easy.setPosition((WINDOW_WIDTH/2)-(easy.getGlobalBounds().width/2),(WINDOW_HEIGHT/2)-(easy.getGlobalBounds().height/2));
  231.     hard.setPosition((WINDOW_WIDTH/2)-(hard.getGlobalBounds().width/2),easy.getPosition().y+verticalOffset);
  232.     std::cout << easy.getPosition().y;
  233.  
  234.     //exit
  235.     areyousureyouwanttotexit.setPosition((WINDOW_WIDTH/2)-(areyousureyouwanttotexit.getGlobalBounds().width/2),(WINDOW_HEIGHT/2)-(areyousureyouwanttotexit.getGlobalBounds().height/2));
  236.     yes.setPosition((WINDOW_WIDTH/2)-(yes.getGlobalBounds().width/2),areyousureyouwanttotexit.getPosition().y+verticalOffset);
  237.     no.setPosition((WINDOW_WIDTH/2)-(no.getGlobalBounds().width/2),yes.getPosition().y+verticalOffset);
  238. }
  239.  
  240. Menu::~Menu()
  241. {
  242.  
  243. }
  244. void Menu::draw(sf::RenderWindow& window)
  245. {
  246.     if ( menuState == DEFAULT )
  247.     {
  248.         window.draw(start);
  249.         window.draw(difficulty);
  250.         window.draw(exit);
  251.     }
  252.     else if ( menuState == START )
  253.     {
  254.         window.draw(pressanykeytostart);
  255.     }
  256.     else if ( menuState == DIFFICULTY )
  257.     {
  258.         window.draw(easy);
  259.         window.draw(hard);
  260.     }
  261.     else if ( menuState == EXIT )
  262.     {
  263.         window.draw(areyousureyouwanttotexit);
  264.         window.draw(yes);
  265.         window.draw(no);
  266.     }
  267. }
  268.  
  269. void Menu::changeColorWhenHovered(const sf::Vector2i& mousePos)
  270. {
  271.     mousePosition = mousePos;
  272.     sf::Color red = sf::Color::Red;
  273.     sf::Color white = sf::Color::White;
  274.     if ( menuState == DEFAULT )
  275.     {
  276.        if ( processColorOfTextHovered(start) ) {}
  277.        else if ( processColorOfTextHovered(difficulty) ) {}
  278.        else if ( processColorOfTextHovered(exit) ) {}
  279.     }
  280.     else if ( menuState == START )
  281.     {
  282.         // nothing to put here just press any key to continue
  283.         // just want to include this else if :D
  284.     }
  285.     else if ( menuState == DIFFICULTY )
  286.     {
  287.         if ( processColorOfTextHovered(easy) ) {}
  288.         else if ( processColorOfTextHovered(hard) ) {}
  289.     }
  290.     else if ( menuState == EXIT )
  291.     {
  292.         if ( processColorOfTextHovered(yes) ) {}
  293.         else if ( processColorOfTextHovered(no) ) {}
  294.     }
  295. }
  296.  
  297. void Menu::changeStateWhenClicked(sf::RenderWindow& window)
  298. {
  299.     mousePosition = sf::Mouse::getPosition(window);
  300.     if ( menuState == DEFAULT )
  301.     {
  302.         if ( isHovered(start) )
  303.         {
  304.             menuState = START;
  305.         }
  306.         else if ( isHovered(difficulty) )
  307.         {
  308.             menuState = DIFFICULTY;
  309.         }
  310.         else if ( isHovered(exit) )
  311.         {
  312.             menuState = EXIT;
  313.         }
  314.     }
  315.     else if ( menuState == START )
  316.     {
  317.         menuState = NONESTATE;
  318.     }
  319.     else if ( menuState == DIFFICULTY )
  320.     {
  321.         if ( isHovered(easy) )
  322.         {
  323.             Game::setGameDiff(Game::EASY);
  324.             menuState = DEFAULT;
  325.         }
  326.         else if ( isHovered(hard) )
  327.         {
  328.             Game::setGameDiff(Game::HARD);
  329.             menuState = DEFAULT;
  330.         }
  331.     }
  332.     else if ( menuState == EXIT )
  333.     {
  334.         if ( isHovered(yes) )
  335.         {
  336.             window.close();
  337.         }
  338.         else if ( isHovered(no) )
  339.         {
  340.             menuState = DEFAULT;
  341.         }
  342.     }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment