Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: GameState  (Read 3072 times)

0 Members and 1 Guest are viewing this topic.

northLynx

  • Newbie
  • *
  • Posts: 9
    • View Profile
GameState
« on: August 05, 2016, 04:36:36 am »
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);
}
« Last Edit: August 05, 2016, 11:18:58 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: GameState
« Reply #1 on: August 05, 2016, 11:28:52 am »
With C++11 there's no need to inherit from sf::NonCopyable since you can simply delete the copy constructor now.
In the same regard you should use smart pointers instead of raw pointers with manual memory management. Your current manual memory management implementation leaks memory, since you never delete the allocated states, with smart pointers you can just leave the code as is and the smart pointers will automatically release the memory.
Does the provided code even compile? Because in your registereState function you return the stack, even though the function has void as return type. Plus I think you can't and wouldn't want to call the stack (constructor) like that.
Please use / in all your paths, especially in include paths. Even on Windows that will work fine and you don't run the risk of \ getting recognized as escape character.
A personal taste that everyone is welcome to disagree on, is the use of *.hpp for C++ headers instead of *.h.

Also next time make sure to use the [code=cpp][/code] tags, so I don't have to insert them for you. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

northLynx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: GameState
« Reply #2 on: August 05, 2016, 05:51:50 pm »
Thanks exploiter. I was looking over the code again and I was wondering if its possible to write all the state management in one header file? Instead of having two separate files.Ill use a unique pointer as a type for my std::stack. I didnt even notice my template function was void  :-[. I should have checked it. Also, how do make headers files appear as a .hpp? In my visual studious all i see when creating a new header file is .h . What do you mean use tags next time so you dont have to insert them.