Changeset 1689 for code/branches/gui/src/core
- Timestamp:
- Aug 31, 2008, 6:07:57 PM (16 years ago)
- Location:
- code/branches/gui/src/core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/CorePrereqs.h
r1688 r1689 162 162 163 163 // game states 164 class GameStateBase; 165 template <class ParentType> 164 166 class GameState; 165 template <class ParentType>166 class GameStateTyped;167 167 class RootGameState; 168 168 -
code/branches/gui/src/core/GameState.cc
r1688 r1689 30 30 @file 31 31 @brief 32 Implementation of GameState class.32 Implementation of GameStateBase class. 33 33 */ 34 34 … … 43 43 Constructor only initialises variables and sets the name permanently. 44 44 */ 45 GameState ::GameState(const std::string& name)45 GameStateBase::GameStateBase(const std::string& name) 46 46 : name_(name) 47 47 //, parent_(0) … … 57 57 Destructor only checks that we don't delete an active state. 58 58 */ 59 GameState ::~GameState()59 GameStateBase::~GameStateBase() 60 60 { 61 61 OrxAssert(!isInSubtree(getCurrentState()), "Deleting an active GameState is a very bad idea.."); … … 69 69 The state to be added. 70 70 */ 71 void GameState ::addChild(GameState* state)71 void GameStateBase::addChild(GameStateBase* state) 72 72 { 73 73 if (!state) 74 74 return; 75 75 // check if the state/tree to be added has states in it that already exist in this tree. 76 for (std::map<std::string, GameState *>::const_iterator it = state->allChildren_.begin();76 for (std::map<std::string, GameStateBase*>::const_iterator it = state->allChildren_.begin(); 77 77 it != state->allChildren_.end(); ++it) 78 78 { … … 96 96 97 97 // merge the child's children into this tree 98 for (std::map<std::string, GameState *>::const_iterator it = state->allChildren_.begin();98 for (std::map<std::string, GameStateBase*>::const_iterator it = state->allChildren_.begin(); 99 99 it != state->allChildren_.end(); ++it) 100 100 this->grandchildAdded(state, it->second); … … 113 113 GameState by instance pointer 114 114 */ 115 void GameState ::removeChild(GameState* state)116 { 117 std::map<GameState *, GameState*>::iterator it = this->grandchildrenToChildren_.find(state);115 void GameStateBase::removeChild(GameStateBase* state) 116 { 117 std::map<GameStateBase*, GameStateBase*>::iterator it = this->grandchildrenToChildren_.find(state); 118 118 if (it != this->grandchildrenToChildren_.end()) 119 119 { … … 127 127 else 128 128 { 129 for (std::map<GameState *, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin();129 for (std::map<GameStateBase*, GameStateBase*>::const_iterator it = state->grandchildrenToChildren_.begin(); 130 130 it != state->grandchildrenToChildren_.end(); ++it) 131 131 this->grandchildRemoved(it->first); … … 150 150 */ 151 151 152 void GameState ::removeChild(const std::string& name)153 { 154 GameState * state = getState(name);152 void GameStateBase::removeChild(const std::string& name) 153 { 154 GameStateBase* state = getState(name); 155 155 if (state) 156 156 { … … 173 173 The child that has been added. 174 174 */ 175 inline void GameState ::grandchildAdded(GameState* child, GameState* grandchild)175 inline void GameStateBase::grandchildAdded(GameStateBase* child, GameStateBase* grandchild) 176 176 { 177 177 // fill the two maps correctly. … … 191 191 The child that has been removed. 192 192 */ 193 inline void GameState ::grandchildRemoved(GameState* grandchild)193 inline void GameStateBase::grandchildRemoved(GameStateBase* grandchild) 194 194 { 195 195 // adjust the two maps correctly. … … 206 206 Remember that the every node has a map with all its child nodes. 207 207 */ 208 GameState * GameState::getState(const std::string& name)208 GameStateBase* GameStateBase::getState(const std::string& name) 209 209 { 210 210 if (this->getParent()) … … 216 216 return this; 217 217 // Search in the map. If there is no entry, we can be sure the state doesn't exist. 218 std::map<std::string, GameState *>::const_iterator it = this->allChildren_.find(name);218 std::map<std::string, GameStateBase*>::const_iterator it = this->allChildren_.find(name); 219 219 return (it!= this->allChildren_.end() ? it->second : 0); 220 220 } … … 225 225 Returns the root node of the tree. 226 226 */ 227 GameState * GameState::getRoot()227 GameStateBase* GameStateBase::getRoot() 228 228 { 229 229 if (this->getParent()) … … 240 240 have active children itself. Many states can be active at once. 241 241 */ 242 GameState * GameState::getCurrentState()242 GameStateBase* GameStateBase::getCurrentState() 243 243 { 244 244 if (this->operation_.active) … … 262 262 Determines whether 'state' is in this subtree, including this node. 263 263 */ 264 bool GameState ::isInSubtree(GameState* state) const264 bool GameStateBase::isInSubtree(GameStateBase* state) const 265 265 { 266 266 return (grandchildrenToChildren_.find(state) != grandchildrenToChildren_.end() … … 275 275 The state to be entered, has to exist in the tree. 276 276 */ 277 void GameState ::requestState(const std::string& name)277 void GameStateBase::requestState(const std::string& name) 278 278 { 279 279 assert(getRoot()); … … 286 286 the method can assume certain things to be granted (like 'this' is always active). 287 287 */ 288 void GameState ::makeTransition(GameState* source, GameState* destination)288 void GameStateBase::makeTransition(GameStateBase* source, GameStateBase* destination) 289 289 { 290 290 if (source == this->getParent()) … … 308 308 309 309 // Check for 'destination' in the children map first 310 std::map<GameState *, GameState*>::const_iterator it310 std::map<GameStateBase*, GameStateBase*>::const_iterator it 311 311 = this->grandchildrenToChildren_.find(destination); 312 312 if (it != this->grandchildrenToChildren_.end()) … … 330 330 Activates the state. Only sets bActive_ to true and notifies the parent. 331 331 */ 332 void GameState ::activate()332 void GameStateBase::activate() 333 333 { 334 334 this->operation_.active = true; … … 341 341 Activates the state. Only sets bActive_ to false and notifies the parent. 342 342 */ 343 void GameState ::deactivate()343 void GameStateBase::deactivate() 344 344 { 345 345 this->operation_.leaving = true; … … 358 358 This method is not virtual! You cannot override it therefore. 359 359 */ 360 void GameState ::tick(const Clock& time)360 void GameStateBase::tick(const Clock& time) 361 361 { 362 362 this->operation_.running = true; -
code/branches/gui/src/core/GameState.h
r1688 r1689 61 61 Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo. 62 62 */ 63 class _CoreExport GameState 63 class _CoreExport GameStateBase 64 64 { 65 65 friend class RootGameState; 66 66 template <class ParentType> 67 friend class GameState Typed;67 friend class GameState; 68 68 69 69 public: … … 82 82 83 83 public: 84 virtual ~GameState ();84 virtual ~GameStateBase(); 85 85 86 86 const std::string& getName() const { return name_; } 87 87 const Operations getOperation() const { return this->operation_; } 88 bool isInSubtree(GameState * state) const;88 bool isInSubtree(GameStateBase* state) const; 89 89 90 GameState * getState(const std::string& name);91 GameState * getRoot();90 GameStateBase* getState(const std::string& name); 91 GameStateBase* getRoot(); 92 92 //! Returns the currently active game state 93 virtual GameState * getCurrentState();93 virtual GameStateBase* getCurrentState(); 94 94 95 95 virtual void requestState(const std::string& name); 96 96 97 void addChild(GameState * state);98 void removeChild(GameState * state);97 void addChild(GameStateBase* state); 98 void removeChild(GameStateBase* state); 99 99 void removeChild(const std::string& name); 100 100 … … 104 104 virtual void ticked(const Clock& time) = 0; 105 105 106 GameState * getActiveChild() { return this->activeChild_; }106 GameStateBase* getActiveChild() { return this->activeChild_; } 107 107 108 108 void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); } 109 109 110 virtual GameState * getParent() const = 0;111 virtual void setParent(GameState * state) = 0;110 virtual GameStateBase* getParent() const = 0; 111 virtual void setParent(GameStateBase* state) = 0; 112 112 113 113 private: 114 114 // Making the constructor private ensures that game states 115 // are always derivates of GameState Typed<T>. Note the friend declaration above.116 GameState (const std::string& name);115 // are always derivates of GameState<T>. Note the friend declaration above. 116 GameStateBase(const std::string& name); 117 117 118 118 //! Performs a transition to 'destination' 119 virtual void makeTransition(GameState * source, GameState* destination);119 virtual void makeTransition(GameStateBase* source, GameStateBase* destination); 120 120 121 void grandchildAdded(GameState * child, GameState* grandchild);122 void grandchildRemoved(GameState * grandchild);121 void grandchildAdded(GameStateBase* child, GameStateBase* grandchild); 122 void grandchildRemoved(GameStateBase* grandchild); 123 123 124 124 void tick(const Clock& time); … … 126 126 void deactivate(); 127 127 128 const std::string name_;129 Operations operation_;130 GameState *activeChild_;131 //bool bPauseParent_;132 std::map<std::string, GameState *>allChildren_;133 std::map<GameState *, GameState*>grandchildrenToChildren_;128 const std::string name_; 129 Operations operation_; 130 GameStateBase* activeChild_; 131 //bool bPauseParent_; 132 std::map<std::string, GameStateBase*> allChildren_; 133 std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_; 134 134 }; 135 135 136 136 137 template <class ParentType> 137 class GameState Typed : public GameState138 class GameState : public GameStateBase 138 139 { 139 140 public: 140 GameState Typed(const std::string& name) : GameState(name) { }141 virtual ~GameState Typed() { }141 GameState(const std::string& name) : GameStateBase(name) { } 142 virtual ~GameState() { } 142 143 143 144 ParentType* getParent() const … … 145 146 146 147 protected: 147 void setParent(GameState * state)148 void setParent(GameStateBase* state) 148 149 { 149 150 assert(dynamic_cast<ParentType*>(state) != 0); -
code/branches/gui/src/core/RootGameState.cc
r1688 r1689 39 39 40 40 RootGameState::RootGameState(const std::string& name) 41 : GameState Typed<GameState>(name)41 : GameState<GameStateBase>(name) 42 42 , stateRequest_("") 43 43 { … … 53 53 the method can assume certain things to be granted (like 'this' is always active). 54 54 */ 55 void RootGameState::makeTransition(GameState * source, GameState* destination)55 void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination) 56 56 { 57 57 if (source != 0) … … 68 68 69 69 // Check for 'destination' in the children map first 70 std::map<GameState *, GameState*>::const_iterator it70 std::map<GameStateBase*, GameStateBase*>::const_iterator it 71 71 = this->grandchildrenToChildren_.find(destination); 72 72 if (it != this->grandchildrenToChildren_.end()) 73 73 { 74 OrxAssert(dynamic_cast<GameState *>(it->second) != 0,74 OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0, 75 75 "There was a mix with RootGameState and GameState, could not cast."); 76 GameState * child = static_cast<GameState*>(it->second);76 GameStateBase* child = static_cast<GameStateBase*>(it->second); 77 77 // child state. Don't use 'state', might be a grandchild! 78 78 this->activeChild_ = child; … … 88 88 void RootGameState::gotoState(const std::string& name) 89 89 { 90 GameState * request = getState(name);90 GameStateBase* request = getState(name); 91 91 if (request) 92 92 { 93 GameState * current = getCurrentState();93 GameStateBase* current = getCurrentState(); 94 94 if (current) 95 95 { … … 127 127 State to start with (usually main menu or specified by command line) 128 128 */ 129 void RootGameState::start( )129 void RootGameState::start(int argc, char** argv) 130 130 { 131 parseCommandLine(argc, argv); 132 131 133 this->activate(); 132 134 … … 149 151 this->deactivate(); 150 152 } 153 154 void RootGameState::parseCommandLine(int argc, char** argv) 155 { 156 std::vector<std::string> args; 157 for (int i = 1; i < argc; ++i) 158 args.push_back(argv[i]); 159 160 try 161 { 162 orxonox::CommandLine::parse(args); 163 } 164 catch (orxonox::ArgumentException& ex) 165 { 166 COUT(1) << ex.what() << std::endl; 167 COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl; 168 } 169 } 151 170 } -
code/branches/gui/src/core/RootGameState.h
r1688 r1689 35 35 namespace orxonox 36 36 { 37 class _CoreExport RootGameState : public GameState Typed<GameState>37 class _CoreExport RootGameState : public GameState<GameStateBase> 38 38 { 39 39 public: … … 42 42 43 43 void requestState(const std::string& name); 44 void start( );44 void start(int argc, char** argv); 45 45 46 46 private: 47 void makeTransition(GameState * source, GameState* destination);47 void makeTransition(GameStateBase* source, GameStateBase* destination); 48 48 void gotoState(const std::string& name); 49 50 void parseCommandLine(int argc, char** argv); 49 51 50 52 std::string stateRequest_;
Note: See TracChangeset
for help on using the changeset viewer.