Changeset 11054 for code/branches/cpp11_v3/src/libraries/core/Game.cc
- Timestamp:
- Jan 10, 2016, 1:54:11 PM (9 years ago)
- Location:
- code/branches/cpp11_v3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v3
- Property svn:mergeinfo changed
-
code/branches/cpp11_v3/src/libraries/core/Game.cc
r10624 r11054 36 36 37 37 #include <exception> 38 #include <boost/weak_ptr.hpp>39 38 #include <loki/ScopeGuard.h> 40 39 … … 66 65 67 66 std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s; 68 Game* Game::singletonPtr_s = 0;67 Game* Game::singletonPtr_s = nullptr; 69 68 70 69 //! Represents one node of the game state tree. … … 72 71 { 73 72 std::string name_; 74 weak_ptr<GameStateTreeNode> parent_;75 std::vector<s hared_ptr<GameStateTreeNode>> children_;73 std::weak_ptr<GameStateTreeNode> parent_; 74 std::vector<std::shared_ptr<GameStateTreeNode>> children_; 76 75 }; 77 76 78 77 Game::Game(const std::string& cmdLine) 79 : gameClock_( NULL)80 , core_( NULL)78 : gameClock_(nullptr) 79 , core_(nullptr) 81 80 , bChangingState_(false) 82 81 , bAbort_(false) 83 , config_( NULL)82 , config_(nullptr) 84 83 , destructionHelper_(this) 85 84 { … … 116 115 117 116 // After the core has been created, we can safely instantiate the GameStates that don't require graphics 118 for (std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.begin(); 119 it != gameStateDeclarations_s.end(); ++it) 120 { 121 if (!it->second.bGraphicsMode) 122 constructedStates_[it->second.stateName] = GameStateFactory::fabricate(it->second); 117 for (const auto& mapEntry : gameStateDeclarations_s) 118 { 119 if (!mapEntry.second.bGraphicsMode) 120 constructedStates_[mapEntry.second.stateName] = GameStateFactory::fabricate(mapEntry.second); 123 121 } 124 122 125 123 // The empty root state is ALWAYS loaded! 126 this->rootStateNode_ = s hared_ptr<GameStateTreeNode>(new GameStateTreeNode());124 this->rootStateNode_ = std::shared_ptr<GameStateTreeNode>(std::make_shared<GameStateTreeNode>()); 127 125 this->rootStateNode_->name_ = "emptyRootGameState"; 128 126 this->loadedTopStateNode_ = this->rootStateNode_; … … 137 135 138 136 assert(loadedStates_.size() <= 1); // Just empty root GameState 139 // Destroy all GameStates (s hared_ptrs take care of actual destruction)137 // Destroy all GameStates (std::shared_ptrs take care of actual destruction) 140 138 constructedStates_.clear(); 141 139 … … 235 233 while (this->requestedStateNodes_.size() > 0) 236 234 { 237 s hared_ptr<GameStateTreeNode> requestedStateNode = this->requestedStateNodes_.front();235 std::shared_ptr<GameStateTreeNode> requestedStateNode = this->requestedStateNodes_.front(); 238 236 assert(this->loadedTopStateNode_); 239 237 if (!this->loadedTopStateNode_->parent_.expired() && requestedStateNode == this->loadedTopStateNode_->parent_.lock()) … … 263 261 { 264 262 // Note: The first element is the empty root state, which doesn't need ticking 265 for (GameStateVector::const_iterator it = this->loadedStates_.begin() + 1; 266 it != this->loadedStates_.end(); ++it) 263 for (const std::shared_ptr<GameState>& state : this->loadedStates_) 267 264 { 268 265 try … … 270 267 // Add tick time for most of the states 271 268 uint64_t timeBeforeTick = 0; 272 if ( (*it)->getInfo().bIgnoreTickTime)269 if (state->getInfo().bIgnoreTickTime) 273 270 timeBeforeTick = this->gameClock_->getRealMicroseconds(); 274 (*it)->update(*this->gameClock_);275 if ( (*it)->getInfo().bIgnoreTickTime)271 state->update(*this->gameClock_); 272 if (state->getInfo().bIgnoreTickTime) 276 273 this->subtractTickTime(static_cast<int32_t>(this->gameClock_->getRealMicroseconds() - timeBeforeTick)); 277 274 } 278 275 catch (...) 279 276 { 280 orxout(user_error) << "An exception occurred while updating '" << (*it)->getName() << "': " << Exception::handleMessage() << endl;277 orxout(user_error) << "An exception occurred while updating '" << state->getName() << "': " << Exception::handleMessage() << endl; 281 278 orxout(user_error) << "This should really never happen!" << endl; 282 279 orxout(user_error) << "Unloading all GameStates depending on the one that crashed." << endl; 283 s hared_ptr<GameStateTreeNode> current = this->loadedTopStateNode_;284 while (current->name_ != (*it)->getName() && current)280 std::shared_ptr<GameStateTreeNode> current = this->loadedTopStateNode_; 281 while (current->name_ != state->getName() && current) 285 282 current = current->parent_.lock(); 286 283 if (current && current->parent_.lock()) … … 372 369 } 373 370 374 s hared_ptr<GameStateTreeNode> lastRequestedNode;371 std::shared_ptr<GameStateTreeNode> lastRequestedNode; 375 372 if (this->requestedStateNodes_.empty()) 376 373 lastRequestedNode = this->loadedTopStateNode_; … … 384 381 385 382 // Check children first 386 std::vector<s hared_ptr<GameStateTreeNode>> requestedNodes;383 std::vector<std::shared_ptr<GameStateTreeNode>> requestedNodes; 387 384 for (unsigned int i = 0; i < lastRequestedNode->children_.size(); ++i) 388 385 { … … 397 394 { 398 395 // Check parent and all its grand parents 399 s hared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;400 while (currentNode != NULL)396 std::shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode; 397 while (currentNode != nullptr) 401 398 { 402 399 if (currentNode->name_ == name) … … 405 402 requestedNodes.push_back(currentNode); 406 403 } 407 if (currentNode == NULL)404 if (currentNode == nullptr) 408 405 requestedNodes.clear(); 409 406 } … … 424 421 void Game::popState() 425 422 { 426 s hared_ptr<GameStateTreeNode> lastRequestedNode;423 std::shared_ptr<GameStateTreeNode> lastRequestedNode; 427 424 if (this->requestedStateNodes_.empty()) 428 425 lastRequestedNode = this->loadedTopStateNode_; … … 435 432 } 436 433 437 s hared_ptr<GameState> Game::getState(const std::string& name)434 std::shared_ptr<GameState> Game::getState(const std::string& name) 438 435 { 439 436 GameStateMap::const_iterator it = constructedStates_.find(name); … … 447 444 else 448 445 orxout(internal_error) << "Could not find GameState '" << name << "'." << endl; 449 return s hared_ptr<GameState>();446 return std::shared_ptr<GameState>(); 450 447 } 451 448 } … … 454 451 { 455 452 // Split string into pieces of the form whitespacesText 456 std::vector<std::pair<std::string, int> 453 std::vector<std::pair<std::string, int>> stateStrings; 457 454 size_t pos = 0; 458 455 size_t startPos = 0; … … 465 462 while (pos < str.size() && str[pos] != ' ') 466 463 ++pos; 467 stateStrings. push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation));464 stateStrings.emplace_back(str.substr(startPos, pos - startPos), indentation); 468 465 } 469 466 if (stateStrings.empty()) 470 467 ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating."); 471 468 // Add element with large identation to detect the last with just an iterator 472 stateStrings. push_back(std::make_pair(std::string(), -1));469 stateStrings.emplace_back(std::string(), -1); 473 470 474 471 // Parse elements recursively 475 std::vector<std::pair<std::string, int> 472 std::vector<std::pair<std::string, int>>::const_iterator begin = stateStrings.begin(); 476 473 parseStates(begin, this->rootStateNode_); 477 474 } … … 479 476 /*** Internal ***/ 480 477 481 void Game::parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it,shared_ptr<GameStateTreeNode> currentNode)478 void Game::parseStates(std::vector<std::pair<std::string, int>>::const_iterator& it, std::shared_ptr<GameStateTreeNode> currentNode) 482 479 { 483 480 SubString tokens(it->first, ","); 484 std::vector<std::pair<std::string, int> 481 std::vector<std::pair<std::string, int>>::const_iterator startIt = it; 485 482 486 483 for (unsigned int i = 0; i < tokens.size(); ++i) … … 491 488 if (tokens[i] == this->rootStateNode_->name_) 492 489 ThrowException(GameState, "You shouldn't use 'emptyRootGameState' in the hierarchy..."); 493 s hared_ptr<GameStateTreeNode> node(new GameStateTreeNode());490 std::shared_ptr<GameStateTreeNode> node(std::make_shared<GameStateTreeNode>()); 494 491 node->name_ = tokens[i]; 495 492 node->parent_ = currentNode; … … 521 518 522 519 // Construct all the GameStates that require graphics 523 for (std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.begin(); 524 it != gameStateDeclarations_s.end(); ++it) 525 { 526 if (it->second.bGraphicsMode) 520 for (const auto& mapEntry : gameStateDeclarations_s) 521 { 522 if (mapEntry.second.bGraphicsMode) 527 523 { 528 524 // Game state loading failure is serious --> don't catch 529 s hared_ptr<GameState> gameState = GameStateFactory::fabricate(it->second);525 std::shared_ptr<GameState> gameState = GameStateFactory::fabricate(mapEntry.second); 530 526 if (!constructedStates_.insert(std::make_pair( 531 it->second.stateName, gameState)).second)527 mapEntry.second.stateName, gameState)).second) 532 528 assert(false); // GameState was already created! 533 529 } … … 582 578 graphicsUnloader.Dismiss(); 583 579 584 s hared_ptr<GameState> state = this->getState(name);580 std::shared_ptr<GameState> state = this->getState(name); 585 581 state->activateInternal(); 586 582 if (!this->loadedStates_.empty()) … … 599 595 try 600 596 { 601 s hared_ptr<GameState> state = this->getState(name);597 std::shared_ptr<GameState> state = this->getState(name); 602 598 state->activity_.topState = false; 603 599 this->loadedStates_.pop_back(); … … 613 609 // Check if graphics is still required 614 610 bool graphicsRequired = false; 615 for ( unsigned i = 0; i < loadedStates_.size(); ++i)616 graphicsRequired |= loadedStates_[i]->getInfo().bGraphicsMode;611 for (const std::shared_ptr<GameState>& state : loadedStates_) 612 graphicsRequired |= state->getInfo().bGraphicsMode; 617 613 if (!graphicsRequired) 618 614 this->unloadGraphics(!this->bAbort_); // if abort is false, that means the game is still running while unloading graphics. in this case we load a graphics manager without renderer (to keep all necessary ogre instances alive) … … 620 616 } 621 617 622 /*static*/ std::map<std::string, s hared_ptr<Game::GameStateFactory>>& Game::GameStateFactory::getFactories()623 { 624 static std::map<std::string, s hared_ptr<GameStateFactory>> factories;618 /*static*/ std::map<std::string, std::shared_ptr<Game::GameStateFactory>>& Game::GameStateFactory::getFactories() 619 { 620 static std::map<std::string, std::shared_ptr<GameStateFactory>> factories; 625 621 return factories; 626 622 } 627 623 628 /*static*/ s hared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info)629 { 630 std::map<std::string, s hared_ptr<Game::GameStateFactory>>::const_iterator it = getFactories().find(info.className);624 /*static*/ std::shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info) 625 { 626 std::map<std::string, std::shared_ptr<Game::GameStateFactory>>::const_iterator it = getFactories().find(info.className); 631 627 assert(it != getFactories().end()); 632 628 return it->second->fabricateInternal(info);
Note: See TracChangeset
for help on using the changeset viewer.