1 /////////////////////////////////////////////////////////////////////////////// 2 // Copyright (C) 2004-2010 by The Allacrost Project 3 // All Rights Reserved 4 // 5 // This code is licensed under the GNU GPL version 2. It is free software 6 // and you may modify it and/or redistribute it under the terms of this license. 7 // See http://www.gnu.org/copyleft/gpl.html for details. 8 /////////////////////////////////////////////////////////////////////////////// 9 10 /** *************************************************************************** 11 *** \file mode_manager.h 12 *** \author Tyler Olsen, roots@allacrost.org 13 *** \brief Header file for game mode processingd 14 *** **************************************************************************/ 15 16 #ifndef __MODE_MANAGER_HEADER__ 17 #define __MODE_MANAGER_HEADER__ 18 19 #include "utils.h" 20 #include "defs.h" 21 22 //! All calls to the mode management code are wrapped inside this namespace 23 namespace hoa_mode_manager { 24 25 //! The singleton pointer responsible for maintaining and updating the game mode state. 26 extern ModeEngine* ModeManager; 27 28 //! Determines whether the code in the hoa_mode_manager namespace should print debug statements or not. 29 extern bool MODE_MANAGER_DEBUG; 30 31 //! \name Game States/Modes 32 //@{ 33 //! \brief Different modes of operation that the game can be in. 34 const uint8 MODE_MANAGER_DUMMY_MODE = 0; 35 const uint8 MODE_MANAGER_BOOT_MODE = 1; 36 const uint8 MODE_MANAGER_MAP_MODE = 2; 37 const uint8 MODE_MANAGER_BATTLE_MODE = 3; 38 const uint8 MODE_MANAGER_MENU_MODE = 4; 39 const uint8 MODE_MANAGER_SHOP_MODE = 5; 40 const uint8 MODE_MANAGER_PAUSE_MODE = 6; 41 const uint8 MODE_MANAGER_SCENE_MODE = 7; 42 const uint8 MODE_MANAGER_WORLD_MODE = 8; 43 const uint8 MODE_MANAGER_SAVE_MODE = 9; 44 //@} 45 46 47 /** *************************************************************************** 48 *** \brief An abstract class that all game mode classes inherit from. 49 *** 50 *** The GameMode class is the starting base for developing a new mode of operation 51 *** for the game. The ModeEngine class handles all of the GameMode class 52 *** objects. One should learn to understand the interaction between these two 53 *** classes. 54 *** 55 *** \note THIS IS VERY IMPORTANT. Never, under any circumstances should 56 *** you ever invoke the delete function on a pointer to this object or its related 57 *** subclasses. The reason is that all of the memory reference handling is done 58 *** by the ModeEngine class. If you attempt to ignore this warning you \b will 59 *** generate a segmentation fault. 60 *** **************************************************************************/ 61 class GameMode { 62 friend class ModeEngine; 63 64 protected: 65 //! Indicates what 'mode' this object is in (what type of inherited class). 66 uint8 mode_type; 67 68 private: 69 //! Copy constructor is private, because making a copy of a game mode object is a \b bad idea. 70 GameMode(const GameMode& other); 71 //! Copy assignment operator is private, because making a copy of a game mode object is a \b bad idea. 72 GameMode& operator=(const GameMode& other); 73 // Note: Should I make the delete and delete[] operators private too? 74 public: 75 GameMode(); 76 //! \param mt The mode_type to set the new GameMode object to. 77 GameMode(uint8 mt); 78 //! Destructor is virutal, since the inherited class holds all the important data. 79 virtual ~GameMode(); 80 81 //! Updates the state of the game mode. 82 virtual void Update() = 0; 83 //! Draws the next screen frame for the game mode. 84 virtual void Draw() = 0; 85 /** \brief Resets the state of the class. 86 *** 87 *** This function is called whenever the game mode is made active (ie, it is made the new active game mode 88 *** on the top of the game modestack). This includes when the game mode is first created and pushed onto the 89 *** game stack, so in that manner it can also be viewed as a helper function to the constructor. 90 **/ 91 virtual void Reset() = 0; 92 }; // class GameMode 93 94 95 /** *************************************************************************** 96 *** \brief Manages and maintains all of the living game mode objects. 97 *** 98 *** The ModeEngine class keeps a stack of GameMode objects, where the object 99 *** on the top of the stack is the active GameMode (there can only be one active 100 *** game mode at any time). The Update() and Draw() functions for this class are 101 *** wrapper calls to the GameMode functions of the same name, and act on the 102 *** active game mode. 103 *** 104 *** When a condition is encountered in which a game mode wishes to destroy itself 105 *** and/or push a new mode onto the stack, this does not occur until the next 106 *** call to the ModeEngine#Update() function. The GameModeManager#push_stack 107 *** retains all the game modes we wish to push onto the stack on the next call to 108 *** ModeEngine#Update(), and the GameModeManager#pop_count member retains 109 *** how many modes to delete and pop off the ModeEngine#game_stack. Pop 110 *** operations are \b always performed before push operations. 111 *** 112 *** \note 1) This class is a singleton. 113 *** 114 *** \note 2) You might be wondering why the game stack uses a vector container 115 *** rather than a stack container. There are two reasons: the first being that 116 *** we can't do a debug printout of the game_stack without removing elements *if* 117 *** a stack is used. The second reason is "just in case" we need to access a stack 118 *** element that is not on the top of the stack. 119 *** **************************************************************************/ 120 class ModeEngine : public hoa_utils::Singleton<ModeEngine> { 121 friend class hoa_utils::Singleton<ModeEngine>; 122 123 private: 124 ModeEngine(); 125 126 /** \brief A stack containing all the live game modes. 127 *** \note The back/last element of the vector is the top of the stack. 128 **/ 129 std::vector<GameMode*> _game_stack; 130 131 //! A vector of game modes to push to the stack on the next call to ModeEngine#Update(). 132 std::vector<GameMode*> _push_stack; 133 134 //! True if a state change occured and we need to change the active game mode. 135 bool _state_change; 136 137 //! The number of game modes to pop from the back of the stack on the next call to ModeEngine#Update(). 138 uint32 _pop_count; 139 140 public: 141 ~ModeEngine(); 142 143 bool SingletonInitialize(); 144 145 //! \brief Increments by one the number of game modes to pop off the stack 146 void Pop(); 147 148 /** \brief Removes all game modes from the stack on the next call to ModeEngine#Update(). 149 *** 150 *** This function sets the ModeEngine#pop_count member to the size of GameModeManager#game_stack. 151 *** If there is no game mode in ModeEngine#push_stack before the next call to GameModeManager#Update(), 152 *** The game will encounter a segmentation fault and die. Therefore, be careful with this function. 153 *** 154 *** \note Typically this function is only used when the game exits, or when a programmer is smoking crack. 155 **/ 156 void PopAll(); 157 158 /** \brief Pushes a new GameMode object on top of the stack. 159 *** \param gm The new GameMode object that will go to the top of the stack. 160 *** \note This should be obvious, but once you push a new object on the stack 161 *** top, it will automatically become the new active game state. 162 **/ 163 void Push(GameMode* gm); 164 165 /** \brief Gets the type of the currently active game mode. 166 *** \return The value of the mode_type member of the GameMode object on the top of the stack. 167 **/ 168 uint8 GetGameType(); 169 170 /** \brief Gets the type of a game mode in the stack. 171 *** \return The value of the mode_type member of the GameMode object on the top of the stack. 172 **/ 173 uint8 GetGameType(uint32 index); 174 175 /** \brief Gets a pointer to the top game stack object. 176 *** \return A pointer to the GameMode object on the top of the stack. 177 **/ 178 GameMode* GetTop(); 179 180 /** \brief Gets a pointer to a game stack object. 181 *** \return A pointer to the GameMode object at (index) from the top. 182 **/ 183 GameMode* Get(uint32 index); 184 185 //! \brief Checks if the game stack needs modes pushed or popped, then calls Update on the active game mode. 186 void Update(); 187 188 //! \brief Calls the Draw() function on the active game mode. 189 void Draw(); 190 191 //! \brief Prints the contents of the game_stack member to standard output. 192 void DEBUG_PrintStack(); 193 }; // class ModeEngine : public hoa_utils::Singleton<ModeEngine> 194 195 } // namespace hoa_mode_manager 196 197 #endif 198