10
« on: May 14, 2015, 08:36:53 pm »
// Within Game.cpp
void Game::ShowOptionMenu()
{
OptionMenu menu;
while (true)
{
_mainWindow.clear(sf::Color::Transparent);
//Drawing filter game-background + scoreboard (left and right of menutext)
static sf::RectangleShape filter(sf::Vector2f(SCREEN_WIDTH, SCREEN_HEIGHT));
filter.setFillColor(sf::Color(40, 40, 40, 70));
_gameBackground.Draw(_mainWindow);
_gameObjectManager.DrawAll(_mainWindow);
_mainWindow.draw(filter);
_scoreBoard.draw(_mainWindow);
//Showing menu
unsigned int result = menu.show(_mainWindow);
_mainWindow.display();
//Handling menuEvent
switch (result)
{
case OptionMenu::CHANGE_COLOUR:
_gameState = Game::Playing;
return;
case OptionMenu::BACK_TO_MENU:
_gameState = Game::Paused;
return;
default:
break;
}
}
}
// The header
#pragma once
#include "Menu.h"
#include "SFML/Graphics.hpp"
class OptionMenu : public Menu
{
public:
enum MenuResult { Nothing, CHANGE_COLOUR, BACK_TO_MENU };
OptionMenu();
~OptionMenu();
private:
unsigned int handleEvents(sf::RenderWindow &window); //override
};
// The .cpp File
#include "stdafx.h"
#include "OptionMenu.h"
#include "Game.h"
#include "ServiceLocator.h"
OptionMenu::OptionMenu()
{
sf::Font font;
font.loadFromFile("images/FuturaLTBold.ttf");
MenuItem ChangeColour;
ChangeColour.action = CHANGE_COLOUR;
ChangeColour.menuText = "Change Player Paddle";
ChangeColour.isSelected = true;
MenuItem backToMainMenus;
backToMainMenus.action = BACK_TO_MENU;
backToMainMenus.menuText = "Back to Ingame Menu";
backToMainMenus.isSelected = false;
_menuItems.push_back(ChangeColour);
_menuItems.push_back(backToMainMenus);
}
OptionMenu::~OptionMenu()
{
}
unsigned int OptionMenu::handleEvents(sf::RenderWindow &renderWindow)
{
sf::Event menuEvent;
while (renderWindow.pollEvent(menuEvent))
{
if (menuEvent.type == sf::Event::KeyPressed)
{
switch (menuEvent.key.code)
{
case sf::Keyboard::Return:
return selectedMenuItemResult();
case sf::Keyboard::Down:
selectNextMenuItem();
return Nothing;
case sf::Keyboard::Up:
selectPreviousMenuItem();
return Nothing;
default:
return Nothing;
}
}
if (menuEvent.type == sf::Event::Closed)
{
renderWindow.close();
}
}
return Nothing;
}
// Within the Game.cpp
void Game::GameLoop()
{
sf::Event currentEvent;
switch (_gameState)
{
case Game::ShowingMainMenu:
ShowMainMenu();
break;
case Game::ShowingOption:
ShowOptionz();
break;
case Game::Paused:
ShowInGameMenu();
break;
case Game::ShowingOptionMenu:
ShowOptionMenu();
case Game::ShowingSplash:
ShowSplashScreen();
break;
case Game::ShowingGameBackground:
ShowGameBackground();
break;
case Game::Playing:
_mainWindow.clear(sf::Color(0, 0, 0));
_gameBackground.Draw(_mainWindow);
_scoreBoard.draw(_mainWindow);
_gameObjectManager.UpdateAll();
_gameObjectManager.DrawAll(_mainWindow);
_mainWindow.display();
while (_mainWindow.pollEvent(currentEvent))
{
if (currentEvent.type == sf::Event::Closed)
{
_gameState = Game::Exiting;
}
if (currentEvent.type == sf::Event::KeyPressed)
{
if (currentEvent.key.code == sf::Keyboard::Escape)
{
ServiceLocator::GetAudio()->PlaySound("audio/menuEnter.wav");
_gameState = Paused;
}
}
}
break;
default:
break;
}
}
// Within Game.h
#include "PlayerPaddle.h"
#include "GameObjectManager.h"
#include "scores.h"
#include "GameBackground.h"
class Game
{
public:
static void Start();
const static GameObjectManager& GetGameObjectManager();
static ScoreBoard& GetScoreBoard();
static GameBackground& GetGameBackground();
const static int SCREEN_WIDTH = 1024;
const static int SCREEN_HEIGHT = 768;
private:
static bool isExiting();
static void resetGame();
static void setupGameObjects();
static void GameLoop();
static void ShowGameBackground();
static void ShowSplashScreen();
static void ShowMainMenu();
static void ShowInGameMenu();
static void ShowOptionMenu();
static void ShowOptionz();
enum GameState { Uninitialised, ShowingSplash, ShowingGameBackground, Paused, Exiting, ShowingOption, ShowingOptionMenu, ShowingMainMenu, ShowingInGameMenu, Playing };
static GameState _gameState;
static sf::RenderWindow _mainWindow;
static GameObjectManager _gameObjectManager;
static ScoreBoard _scoreBoard;
static GameBackground _gameBackground;
static bool _multiplayer;
};