Pong.cpp :
#include "Game.h"
int main()
{
Game Pong;
Pong.ChangeState(Game::State::MAINMENU);
while (Pong.IsRunning())
{
Pong.Run();
}
return 0;
}
GameState.h :
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP
class Game;
class GameState{
public:
virtual void HandleEvents(Game& game) = 0;
virtual void Update(Game& game) = 0;
virtual void Draw(Game& game) = 0;
virtual ~GameState() {};
};
#endif
Game.h :
#ifndef GAME_HPP
#define GAME_HPP
#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include <memory>
class Game{
public:
enum class State{ MAINMENU, PLAY };
sf::RenderWindow window;
Game();
void Run();
void ChangeState(State);
bool IsRunning();
void setRunning(bool);
~Game();
private:
std::unique_ptr<GameState> CurrentState;
bool isRunning;
};
#endif
Game.cpp :
#include"Game.h"
Game::Game()
{
window.create(sf::VideoMode(1280, 720), "Pong");
isRunning = true;
//std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
}
void Game::Run()
{
while (window.isOpen())
{
CurrentState->HandleEvents(*this);
window.clear(sf::Color(0, 134, 194));
CurrentState->Update(*this);
CurrentState->Draw(*this);
window.display();
}
}
void Game::ChangeState(State state)
{
switch (state)
{
case State::MAINMENU: CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState)); break;
case State::PLAY: CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState)); break;
}
}
bool Game::IsRunning()
{
return isRunning;
}
void Game::setRunning(bool running)
{
isRunning = running;
}
Game::~Game()
{
}
MainMenuState.h :
#ifndef MAINMENUSTATE_HPP
#define MAINMENUSTATE_HPP
#include "GameState.h"
#include "SFML\Graphics.hpp"
class MainMenuState : public GameState{
public:
sf::Text StartGame;
sf::Text QuitGame;
sf::Font font;
MainMenuState();
void HandleEvents(Game &game);
void Update(Game &game);
void Draw(Game &game);
~MainMenuState();
bool bStartGameSelected = false;
bool bQuitGameSelected = false;
};
#endif
MainMenuState.cpp :
#include "MainMenuState.h"
#include "Game.h"
MainMenuState::MainMenuState()
{
if(!font.loadFromFile("assets\\fonts\\HAMMERHEAD.ttf"))
{
printf("Unable to load font");
}
StartGame.setFont(font);
QuitGame.setFont(font);
StartGame.setString("Start Game");
QuitGame.setString("Quit Game");
StartGame.setCharacterSize(50);
QuitGame.setCharacterSize(50);
StartGame.setPosition(440.f,300.f);
QuitGame.setPosition(440.f,350.f);
}
void MainMenuState::HandleEvents(Game &game)
{
sf::Event event;
while (game.window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
game.window.close();
game.setRunning(false);
}
if (event.type == sf::Event::MouseButtonReleased)
{
if (event.mouseButton.button == 0)
{
if (bStartGameSelected)
{
game.ChangeState(Game::State::PLAY);
}
else if (bQuitGameSelected)
{
game.window.close();
game.setRunning(false);
}
}
}
}
}
void MainMenuState::Update(Game &game)
{
sf::Vector2i pixelPos = sf::Mouse::getPosition(game.window);
sf::Vector2f worldPos = game.window.mapPixelToCoords(pixelPos);
if (StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() != sf::Color::Green)
{
StartGame.setColor(sf::Color::Green);
bStartGameSelected = true;
}
else if (!StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() == sf::Color::Green)
{
StartGame.setColor(sf::Color::White);
bStartGameSelected = false;
}
if (QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() != sf::Color::Green)
{
QuitGame.setColor(sf::Color::Green);
bQuitGameSelected = true;
}
else if (!QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() == sf::Color::Green)
{
QuitGame.setColor(sf::Color::White);
bQuitGameSelected = false;
}
}
void MainMenuState::Draw(Game &game)
{
game.window.draw(StartGame);
game.window.draw(QuitGame);
}
MainMenuState::~MainMenuState()
{
printf("MainMenu Destructor called\n");
}
PlayState.h :
#ifndef PLAYSTATE_HPP
#define PLAYSTATE_HPP
#include "GameState.h"
class PlayState : public GameState{
public:
PlayState();
void HandleEvents(Game &game);
void Update(Game &game);
void Draw(Game &game);
~PlayState();
};
#endif
PlayState.cpp :
#include "PlayState.h"
#include "Game.h"
PlayState::PlayState()
{
printf("PlayState-Constructor called\n");
}
void PlayState::HandleEvents(Game &game)
{
sf::Event event;
while (game.window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed)
{
if (event.type == sf::Keyboard::Escape)
{
game.ChangeState(Game::State::MAINMENU);
}
}
}
}
void PlayState::Update(Game &game)
{
}
void PlayState::Draw(Game &game)
{
}
PlayState::~PlayState()
{
}
Thats my code.....