Changeset 6038 for sandbox/src/libraries/core/Game.cc
- Timestamp:
- Nov 5, 2009, 9:22:22 PM (15 years ago)
- Location:
- sandbox
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox
- Property svn:mergeinfo changed
-
sandbox/src/libraries/core/Game.cc
r5782 r6038 38 38 #include <boost/weak_ptr.hpp> 39 39 40 #include "util/Clock.h" 40 41 #include "util/Debug.h" 41 42 #include "util/Exception.h" … … 43 44 #include "util/Sleep.h" 44 45 #include "util/SubString.h" 45 #include "Clock.h" 46 #include "CommandLine.h" 46 #include "CommandLineParser.h" 47 47 #include "Core.h" 48 48 #include "CoreIncludes.h" … … 105 105 Game::Game(const std::string& cmdLine) 106 106 // Destroy factories before the Core! 107 : gsFactoryDestroyer_(Game::GameStateFactory:: factories_s, &std::map<std::string, shared_ptr<GameStateFactory> >::clear)107 : gsFactoryDestroyer_(Game::GameStateFactory::getFactories(), &std::map<std::string, shared_ptr<GameStateFactory> >::clear) 108 108 { 109 109 this->bAbort_ = false; … … 404 404 requestedNodes.push_back(currentNode); 405 405 } 406 if (currentNode == NULL) 407 requestedNodes.clear(); 406 408 } 407 409 … … 451 453 { 452 454 // Split string into pieces of the form whitespacesText 453 std::vector<std::pair<std::string, unsigned> > stateStrings;455 std::vector<std::pair<std::string, int> > stateStrings; 454 456 size_t pos = 0; 455 457 size_t startPos = 0; 456 458 while (pos < str.size()) 457 459 { 458 unsignedindentation = 0;460 int indentation = 0; 459 461 while(pos < str.size() && str[pos] == ' ') 460 462 ++indentation, ++pos; … … 464 466 stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation)); 465 467 } 466 unsigned int currentLevel = 0; 467 shared_ptr<GameStateTreeNode> currentNode = this->rootStateNode_; 468 for (std::vector<std::pair<std::string, unsigned> >::const_iterator it = stateStrings.begin(); it != stateStrings.end(); ++it) 469 { 470 std::string newStateName = it->first; 471 unsigned newLevel = it->second + 1; // empty root is 0 472 if (!this->checkState(newStateName)) 473 ThrowException(GameState, "GameState with name '" << newStateName << "' not found!"); 474 if (newStateName == this->rootStateNode_->name_) 468 if (stateStrings.empty()) 469 ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating."); 470 // Add element with large identation to detect the last with just an iterator 471 stateStrings.push_back(std::make_pair("", -1)); 472 473 // Parse elements recursively 474 std::vector<std::pair<std::string, int> >::const_iterator begin = stateStrings.begin(); 475 parseStates(begin, this->rootStateNode_); 476 } 477 478 /*** Internal ***/ 479 480 void Game::parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode) 481 { 482 SubString tokens(it->first, ","); 483 std::vector<std::pair<std::string, int> >::const_iterator startIt = it; 484 485 for (unsigned int i = 0; i < tokens.size(); ++i) 486 { 487 it = startIt; // Reset iterator to the beginning of the sub tree 488 if (!this->checkState(tokens[i])) 489 ThrowException(GameState, "GameState with name '" << tokens[i] << "' not found!"); 490 if (tokens[i] == this->rootStateNode_->name_) 475 491 ThrowException(GameState, "You shouldn't use 'emptyRootGameState' in the hierarchy..."); 476 shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode); 477 newNode->name_ = newStateName; 478 479 if (newLevel <= currentLevel) 480 { 481 do 482 currentNode = currentNode->parent_.lock(); 483 while (newLevel <= --currentLevel); 484 } 485 if (newLevel == currentLevel + 1) 486 { 487 // Add the child 488 newNode->parent_ = currentNode; 489 currentNode->children_.push_back(newNode); 490 } 492 shared_ptr<GameStateTreeNode> node(new GameStateTreeNode()); 493 node->name_ = tokens[i]; 494 node->parent_ = currentNode; 495 currentNode->children_.push_back(node); 496 497 int currentLevel = it->second; 498 ++it; 499 while (it->second != -1) 500 { 501 if (it->second <= currentLevel) 502 break; 503 else if (it->second == currentLevel + 1) 504 parseStates(it, node); 491 505 else 492 506 ThrowException(GameState, "Indentation error while parsing the hierarchy."); 493 currentNode = newNode; 494 currentLevel = newLevel; 495 } 496 } 497 498 /*** Internal ***/ 507 } 508 } 509 } 499 510 500 511 void Game::loadGraphics() … … 566 577 } 567 578 568 std::map<std::string, shared_ptr<Game::GameStateFactory> > Game::GameStateFactory::factories_s; 579 /*static*/ std::map<std::string, shared_ptr<Game::GameStateFactory> >& Game::GameStateFactory::getFactories() 580 { 581 static std::map<std::string, shared_ptr<GameStateFactory> > factories; 582 return factories; 583 } 569 584 570 585 /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info) 571 586 { 572 std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = factories_s.find(info.className);573 assert(it != factories_s.end());587 std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = getFactories().find(info.className); 588 assert(it != getFactories().end()); 574 589 return it->second->fabricateInternal(info); 575 590 }
Note: See TracChangeset
for help on using the changeset viewer.