Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualJTippetts

Posted 02 January 2013 - 12:08 AM

A pattern that I frequently use is to have my state->update() method return a shared_ptr to the state the game should switch to. If update returns an empty pointer, no state change occurs. Something like this (very quick, incomplete and un-tested, but it shows the gist of it)
 
enum EStates
{
	STATE_NULL,
	STATE_MENU,
	STATE_PLAYING,
	STATE_EXIT
};

class GameState
{
	public:
	GameState(){}
	virtual ~GameState(){}

	virtual std::shared_ptr<GameState> update()=0;
	virtual void event()=0;
	virtual void render()=0;

	virtual unsigned int getState()=0;
};


class MainMenuState : public GameState
{
	public:
	MainMenuState() : GameState(), nextState(STATE_NULL){}
	~MainMenuState(){}

	unsigned int getState()
	{
		return STATE_MENU;
	}

	std::shared_ptr<GameState> update()
	{
		switch(nextState)
		{
			case STATE_NULL: return std::shared_ptr<GameState>(); break;  // Return empty ptr
			case STATE_PLAYING: return std::shared_ptr<GameState>(std::make_shared<PlayingState>()); break;
			case STATE_EXIT: return std::shared_ptr<GameState>(std::make_shared<ExitState>()); break;
			default: return std::shared_ptr<GameState>(); break;
		}
	}

	void event()
	{
		cin >> nextState;
	}

	void render()
	{
		cout << "MAIN MENU" << endl;
		cout << "Press '3' to play"  << endl;
		cout << "Press '0' to continue in Main Menu"  << endl;
	}

	private:
	unsigned int nextState;
};

int main()
{
	std::shared_ptr<GameState> state=std::make_shared<MainMenuState>();

	while(state->getState()!=STATE_EXIT)
	{
	    state->render();
	    state->event();
		std::shared_ptr<GameState> newstate=state->update();
		if(newstate)
		{
			state=newstate;
			newstate.reset();
		}
	}
}

Update performs a switch on nextState as in your code, and returns either an empty pointer if no state change should occur, or it creates a new state object (std::make_shared news an object and assigns it to a shared_ptr in one call, safely) and returns a shared_ptr to the new state object. In the main loop, the return result of update is checked, and if it is a non-empty pointer, then state is assigned to the new pointer. This essentially overwrites the old state that was assigned previously, which will then drop out and be automatically and safely deleted.

Any time you find yourself doing what amounts to delete this you are almost certainly making an error, regardless of whether or not it seems to work. That sort of thing is just not wise to do unless you are experienced enough to know what is really going on.

#1JTippetts

Posted 02 January 2013 - 12:07 AM

A pattern that I frequently use is to have my state->update() method return a shared_ptr to the state the game should switch to. If update returns an empty pointer, no state change occurs. Something like this (very quick, incomplete and un-tested, but it shows the gist of it)
 
enum EStates
{
	STATE_NULL,
	STATE_MENU,
	STATE_PLAYING,
	STATE_EXIT
};

class GameState
{
	public:
	GameState(){}
	virtual ~GameState(){}

	virtual std::shared_ptr<GameState> update()=0;
	virtual void event()=0;
	virtual void render()=0;

	virtual unsigned int getState()=0;
};


class MainMenuState : public GameState
{
	public:
	MainMenuState() : GameState(), nextState(STATE_NULL){}
	~MainMenuState(){}

	unsigned int getState()
	{
		return STATE_MENU;
	}

	std::shared_ptr<GameState> update()
	{
		switch(nextState)
		{
			case STATE_NULL: return std::shared_ptr<GameState>(); break;  // Return empty ptr
			case STATE_PLAYING: return std::shared_ptr<GameState>(std::make_shared<PlayingState>()); break;
			case STATE_EXIT: return std::shared_ptr<GameState>(std::make_shared<ExitState>()); break;
			default: return std::shared_ptr<GameState>(); break;
		}
	}

	void event()
	{
		cin >> nextState;
	}

	void render()
	{
		cout << "MAIN MENU" << endl;
		cout << "Press '3' to play"  << endl;
		cout << "Press '0' to continue in Main Menu"  << endl;
	}

	private:
	unsigned int nextState;
};

std::shared_ptr<GameState> ExitState::update()
    {
        switch(nextState)
        {
            case STATE_NULL: return std::shared_ptr<GameState>(); break;  // Return empty ptr
			case STATE_PLAYING: return std::shared_ptr<GameState>(std::make_shared<PlayingState>()); break;
			case STATE_EXIT: return std::shared_ptr<GameState>(std::make_shared<ExitState>()); break;
			default: return std::shared_ptr<GameState>(); break;
		}
	}

std::shared_ptr<GameState> PlayingState::update()
    {
        switch(nextState)
        {
            case STATE_NULL: return std::shared_ptr<GameState>(); break;  // Return empty ptr
			case STATE_PLAYING: return std::shared_ptr<GameState>(std::make_shared<PlayingState>()); break;
			case STATE_EXIT: return std::shared_ptr<GameState>(std::make_shared<ExitState>()); break;
			default: return std::shared_ptr<GameState>(); break;
		}
	}

int main()
{
	std::shared_ptr<GameState> state=std::make_shared<MainMenuState>();

	while(state->getState()!=STATE_EXIT)
	{
	    state->render();
	    state->event();
		std::shared_ptr<GameState> newstate=state->update();
		if(newstate)
		{
			state=newstate;
			newstate.reset();
		}
	}
}

Update performs a switch on nextState as in your code, and returns either an empty pointer if no state change should occur, or it creates a new state object (std::make_shared news an object and assigns it to a shared_ptr in one call, safely) and returns a shared_ptr to the new state object. In the main loop, the return result of update is checked, and if it is a non-empty pointer, then state is assigned to the new pointer. This essentially overwrites the old state that was assigned previously, which will then drop out and be automatically and safely deleted.

Any time you find yourself doing what amounts to delete this you are almost certainly making an error, regardless of whether or not it seems to work. That sort of thing is just not wise to do unless you are experienced enough to know what is really going on.

PARTNERS