GAME.CPP
#include "gamestates\include\mainMenuState.h"
#include "gamestates\include\playState.h"
#include "include\game.h"
Game::Game()
{
running = true;
windowName = "Zombie Shooter";
App.setFramerateLimit(60);
App.create(sf::VideoMode(800, 600), windowName, sf::Style::Close);
}
void Game::Run()
{
while (App.isOpen())
{
HandleEvents();
Update();
Render();
Quit();
}
}
void Game::Quit()
{
if (running == false)
App.close();
}
void Game::HandleEvents()
{
while (App.pollEvent(event))
{
if (event.type == sf::Event::Closed)
running = false;
CurrentState->HandleEvents(*this);
}
}
void Game::Update()
{
CurrentState->Update(*this);
}
void Game::Render()
{
App.clear(sf::Color::Green);
CurrentState->Render(App);
App.display();
}
void Game::changeState(gameStates newState)
{
switch (newState)
{
case(gameStates::MAINMENU):
CurrentState = std::make_unique<mainMenuState>();
break;
case(gameStates::PLAY) :
CurrentState = std::make_unique<playState>();
break;
default:
std::cout << "Can not find gamestate!" << std::endl;
}
}
void Game::Stop()
{
running = false;
App.close();
}
Game::~Game()
{
}
GAME.HPP
#pragma once
#include <SFML\Graphics.hpp>
#include <iostream>
#include <memory>
#include "gamestates\include\gameState.h"
class gameState;
class mainMenuState;
class playState;
class Game
{
public:
enum class gameStates{MAINMENU, PLAY};
Game();
~Game();
void Run();
void changeState(gameStates newState);
void Stop();
sf::RenderWindow& operator[](int index)
{
switch (index)
{
case 0:
return App;
break;
}
}
private:
void HandleEvents();
void Update();
void Render();
void Quit();
bool running;
sf::RenderWindow App;
sf::Event event;
std::string windowName;
std::unique_ptr<gameState> CurrentState;
};