I have been looking at some State menu code from sfml game development book and other game source code. I was wonder if my game state code will work if implemented in a game? i created a state and gamestate header files with their cpp files. I been trying to practice on my own rather than just copy and past code from other source code so if I did something wrong or wrote code in a bad way please point it out and tell me how to write it better or if i need to remove some functions or add functions to a header file or just write the whole thing over again. Thank you
#pragma once
#include "State.h"
#include <stack>
#include <memory>
#include <SFML\System\NonCopyable.hpp>
namespace sf
{
class RenderWindow;
class Time;
}
class GameState : public sf::NonCopyable
{
public:
GameState();
void push(States::ID);
void pop();
bool isEmpty();
template<typename T>
void registerState(States::ID);
private:
void update(sf::Time& dt);
void render(sf::RenderWindow& wind);
private:
std::stack<States::ID> mStack;
State *activeState;
};
template<typename T>
void registerState(States::ID)
{
return mStack(new T(States::ID));
}
#include "GameState.h"
GameState::GameState()
{
activeState = nullptr;
}
void GameState::push(States::ID mStateID)
{
if (!isEmpty())
mStack.push(mStateID);
}
void GameState::pop()
{
if (!isEmpty())
mStack.pop();
}
void GameState::update(sf::Time& dt)
{
if (activeState)
activeState->update(dt);
}
void GameState::render(sf::RenderWindow& wind)
{
if (activeState)
activeState->render(wind);
}
bool GameState::isEmpty()
{
mStack.empty();
}
#pragma once
#include "GameState.h"
namespace sf
{
class RenderWindow;
class Time;
}
namespace States
{
enum ID
{
Title,
MainMenu,
Settings,
Quit,
};
}
class GameState;
class State
{
public:
virtual void update(sf::Time& dt) = 0;
virtual void render(sf::RenderWindow& wind) = 0;
public:
State(GameState mstate);
virtual ~State();
void addState();
private:
GameState mstack;
};
#include "State.h"
State::State(GameState mstate) : mstack(mstate)
{
}
State::~State()
{
}
void State::addState()
{
mstack.push(States::Title);
}