Looking for criticism on my GameState class

This is a discussion on Looking for criticism on my GameState class within the Game Programming forums, part of the General Programming Boards category; StateMgr.h Code: //---------------------------------------------------------------------------- // File: StateManager.h // Description: Interface for the StateManager class. //---------------------------------------------------------------------------- #ifndef _STATEMANAGER_H #define _STATEMANAGER_H #include <vector> ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Looking for criticism on my GameState class

    StateMgr.h
    Code:
    //----------------------------------------------------------------------------
    // File:         StateManager.h
    // Description:  Interface for the StateManager class.
    //----------------------------------------------------------------------------
    #ifndef _STATEMANAGER_H
    #define _STATEMANAGER_H
    
    #include <vector>
    
    // forward declaration for GameState class
    class GameState;
    
    //----------------------------------------------------------------------------
    // Class:          StateManager
    // Description:  Manages game states for the engine.
    //----------------------------------------------------------------------------
    class StateManager
    {
       public:
          // Constructor
          StateManager();
          // Destructor
          ~StateManager();
    
          // Push a new state onto the stack
          bool pushState(GameState* state);
    
          // Pop the current state off the stack
          GameState* popState();
    
          // Remove all states from the stack
          void popAll();
    
          // Call the current state's update method
          bool update(float delta);
    
          // Call the current state's render method
          void render();
    
          // Return the singleton StateManager instance
          static StateManager* getInstance();
    
       private:
          std::vector<GameState*> _states;
          static StateManager* _instance;
    };
    #endif
    StateMgr.cpp
    Code:
    //----------------------------------------------------------------------------
    // File:         StateManager.cpp
    // Description:  Implementation for the StateManager class.
    //----------------------------------------------------------------------------
    #include "StateMgr.h"
    #include "GameState.h"
    
    //----------------------------------------------------------------------------
    // Static Instance Variables
    //----------------------------------------------------------------------------
    StateManager* StateManager::_instance = 0;
    
    //----------------------------------------------------------------------------
    // Method:   Constructor
    //----------------------------------------------------------------------------
    StateManager::StateManager()
    {
    }
    
    //----------------------------------------------------------------------------
    // Method:   Destructor
    //----------------------------------------------------------------------------
    StateManager::~StateManager()
    {
        // if there's anything left in the _states vector, then we have a
        // memory leak. it's up to the calling application to make sure
        // the states are cleaned up and deleted.
    }
    
    //----------------------------------------------------------------------------
    // Method:   pushState
    // Description:   Push a new state onto the state stack. Calls the state's
    //       initialize method.
    //----------------------------------------------------------------------------
    bool StateManager::pushState(GameState* state)
    {
       bool t = state->initialize();
       _states.push_back(state);
       return t;
    }
    
    //----------------------------------------------------------------------------
    // Method:   popState
    // Description:   Pops the current state off the state stack. Calls the state's
    //       shutdown method.
    // Returns:   The state that was popped. This gives the caller a chance to
    //       delete the state if appropriate.
    //----------------------------------------------------------------------------
    GameState* StateManager::popState()
    {
       GameState* state = 0;
    
       if (_states.size() > 0)
       {
          state = _states[_states.size()-1];
          _states.pop_back();
          state->shutdown();
       }
    
       return state;
    }
    
    //----------------------------------------------------------------------------
    // Method:   popAll
    // Description:   Pops all states from the state stack. Note that the states
    //       are not deleted, so it is up to the caller to take care of
    //       deleting the states. This obviously means that the states
    //       must be kept in a list somewhere in the caller.
    //       One possible solution is to put a "delete this;"
    //       into the shutdown methods of your states.
    //       That would ensure that they get deleted when
    //       they get popped.
    //----------------------------------------------------------------------------
    void StateManager::popAll()
    {
       while (popState())
          ;
    }
    
    //----------------------------------------------------------------------------
    // Method:   update
    // Parameters:   delta - number of seconds since last call
    // Description:   Calls the current state's update method.
    // Returns:   If there is a current state, then it returns the return value
    //       from the current state's update method. Otherwise, it returns
    //       false.
    //----------------------------------------------------------------------------
    bool StateManager::update(float delta)
    {
       if (_states.size() > 0)
       {
          return _states[_states.size()-1]->update(delta);
       }
       else
       {
          return false;
       }
    }
    
    //----------------------------------------------------------------------------
    // Method:   render
    // Description:   Calls the current state's render method.
    //----------------------------------------------------------------------------
    void StateManager::render()
    {
       if (_states.size() > 0)
       {
          _states[_states.size()-1]->render();
       }
    }
    
    //----------------------------------------------------------------------------
    // Method:   getInstance
    // Description:   Gets the singleton StateManager instance.
    // Returns:   A pointer to the StateManager instance.
    //----------------------------------------------------------------------------
    StateManager* StateManager::getInstance()
    {
       if (_instance == 0)
       {
          _instance = new StateManager();
       }
    
       return _instance;
    }
    GameState.h
    Code:
    //----------------------------------------------------------------------------
    // File:         GameState.h
    // Description:  Interface for the GameState abstract class.
    //----------------------------------------------------------------------------
    #ifndef _GAMESTATE_H
    #define _GAMESTATE_H
    
    class GameState
    {
       public:
          // initialize the state
          virtual bool initialize() = 0;
          // shutdown the state
          virtual void shutdown() = 0;
    
          // handle frame update
          virtual bool update(float delta) = 0;
          // handle frame rendering
          virtual bool render() = 0;
    };
    
    #endif
    That is my code for GameState management. I want to know what can be improved, removed, or changed.

    Thank you for any input.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Looks pretty straight forward. I guess if you're looking for criticism, one downside to using a queue is that your game will be linear. Some type of graph representation would let you have multiple options for the next game state.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 05:30 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 03:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21