Changeset 3363 for code/branches/resource/src/core/Game.cc
- Timestamp:
- Jul 29, 2009, 5:24:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/resource/src/core/Game.cc
r3359 r3363 40 40 #include "util/Debug.h" 41 41 #include "util/Exception.h" 42 #include "util/ScopeGuard.h" 42 43 #include "util/Sleep.h" 43 44 #include "util/SubString.h" … … 133 134 134 135 // Set up a basic clock to keep time 135 this->gameClock_ = new Clock();136 this->gameClock_.reset(new Clock()); 136 137 137 138 // Create the Core 138 this->core_ = new Core(cmdLine);139 this->core_.reset(new Core(cmdLine)); 139 140 140 141 // After the core has been created, we can safely instantiate the GameStates that don't require graphics … … 153 154 154 155 // Do this after the Core creation! 155 this->configuration_ = new GameConfiguration();156 this->configuration_.reset(new GameConfiguration()); 156 157 } 157 158 158 159 /** 159 160 @brief 161 All destruction code is handled by scoped_ptrs and SimpleScopeGuards. 160 162 */ 161 163 Game::~Game() 162 164 { 163 // Destroy the configuration helper class instance164 delete this->configuration_;165 166 // Destroy the GameStates (note that the nodes still point to them, but doesn't matter)167 for (std::map<std::string, GameState*>::const_iterator it = constructedStates_.begin();168 it != constructedStates_.end(); ++it)169 delete it->second;170 171 // Destroy the Core and with it almost everything172 delete this->core_;173 delete this->gameClock_;174 175 // Take care of the GameStateFactories176 GameStateFactory::destroyFactories();177 178 165 // Don't assign singletonRef_s with NULL! Recreation is not supported 179 166 } … … 283 270 { 284 271 // Note: The first element is the empty root state, which doesn't need ticking 285 for ( std::vector<GameState*>::const_iterator it = this->loadedStates_.begin() + 1;272 for (GameStateVector::const_iterator it = this->loadedStates_.begin() + 1; 286 273 it != this->loadedStates_.end(); ++it) 287 274 { … … 457 444 } 458 445 459 GameState*Game::getState(const std::string& name)460 { 461 std::map<std::string, GameState*>::const_iterator it = constructedStates_.find(name);446 shared_ptr<GameState> Game::getState(const std::string& name) 447 { 448 GameStateMap::const_iterator it = constructedStates_.find(name); 462 449 if (it != constructedStates_.end()) 463 450 return it->second; … … 469 456 else 470 457 COUT(1) << "Error: Could not find GameState '" << name << "'." << std::endl; 471 return 0;458 return shared_ptr<GameState>(); 472 459 } 473 460 } … … 528 515 { 529 516 core_->loadGraphics(); 517 Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics); 530 518 GameMode::bShowsGraphics_s = true; 531 519 … … 536 524 if (it->second.bGraphicsMode) 537 525 { 526 // Game state loading failure is serious --> don't catch 527 shared_ptr<GameState> gameState = GameStateFactory::fabricate(it->second); 538 528 if (!constructedStates_.insert(std::make_pair( 539 it->second.stateName, GameStateFactory::fabricate(it->second))).second)529 it->second.stateName, gameState)).second) 540 530 assert(false); // GameState was already created! 541 531 } 542 532 } 533 graphicsUnloader.Dismiss(); 543 534 } 544 535 } … … 549 540 { 550 541 // Destroy all the GameStates that require graphics 551 for ( std::map<std::string, GameState*>::iterator it = constructedStates_.begin(); it != constructedStates_.end();)542 for (GameStateMap::iterator it = constructedStates_.begin(); it != constructedStates_.end();) 552 543 { 553 544 if (it->second->getInfo().bGraphicsMode) 554 {555 delete it->second;556 545 constructedStates_.erase(it++); 557 }558 546 else 559 547 ++it; … … 577 565 { 578 566 this->bChangingState_ = true; 567 LOKI_ON_BLOCK_EXIT_OBJ(*this, &Game::resetChangingState); 568 579 569 // If state requires graphics, load it 580 if (gameStateDeclarations_s[name].bGraphicsMode) 570 Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics); 571 if (gameStateDeclarations_s[name].bGraphicsMode && !GameMode::showsGraphics()) 581 572 this->loadGraphics(); 582 GameState* state = this->getState(name); 573 else 574 graphicsUnloader.Dismiss(); 575 576 shared_ptr<GameState> state = this->getState(name); 583 577 state->activate(); 584 578 if (!this->loadedStates_.empty()) … … 586 580 this->loadedStates_.push_back(state); 587 581 state->activity_.topState = true; 588 this->bChangingState_ = false; 582 583 graphicsUnloader.Dismiss(); 589 584 } 590 585 591 586 void Game::unloadState(const std::string& name) 592 587 { 593 GameState* state = this->getState(name);594 588 this->bChangingState_ = true; 595 state->activity_.topState = false;596 this->loadedStates_.pop_back();597 if (!this->loadedStates_.empty())598 this->loadedStates_.back()->activity_.topState = true;599 589 try 600 590 { 591 shared_ptr<GameState> state = this->getState(name); 592 state->activity_.topState = false; 593 this->loadedStates_.pop_back(); 594 if (!this->loadedStates_.empty()) 595 this->loadedStates_.back()->activity_.topState = true; 601 596 state->deactivate(); 602 // Check if graphis is still required603 bool graphicsRequired = false;604 for (unsigned i = 0; i < loadedStates_.size(); ++i)605 graphicsRequired |= loadedStates_[i]->getInfo().bGraphicsMode;606 if (!graphicsRequired)607 this->unloadGraphics();608 597 } 609 598 catch (const std::exception& ex) … … 612 601 COUT(2) << " There might be potential resource leaks involved! To avoid this, improve exception-safety." << std::endl; 613 602 } 603 // Check if graphics is still required 604 bool graphicsRequired = false; 605 for (unsigned i = 0; i < loadedStates_.size(); ++i) 606 graphicsRequired |= loadedStates_[i]->getInfo().bGraphicsMode; 607 if (!graphicsRequired) 608 this->unloadGraphics(); 614 609 this->bChangingState_ = false; 615 610 } 616 611 617 std::map<std::string, Game::GameStateFactory*> Game::GameStateFactory::factories_s;618 619 /*static*/ GameState*Game::GameStateFactory::fabricate(const GameStateInfo& info)620 { 621 std::map<std::string, GameStateFactory*>::const_iterator it = factories_s.find(info.className);612 std::map<std::string, shared_ptr<Game::GameStateFactory> > Game::GameStateFactory::factories_s; 613 614 /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info) 615 { 616 std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = factories_s.find(info.className); 622 617 assert(it != factories_s.end()); 623 618 return it->second->fabricateInternal(info); 624 619 } 625 626 /*static*/ void Game::GameStateFactory::destroyFactories()627 {628 for (std::map<std::string, GameStateFactory*>::const_iterator it = factories_s.begin(); it != factories_s.end(); ++it)629 delete it->second;630 factories_s.clear();631 }632 620 }
Note: See TracChangeset
for help on using the changeset viewer.