Changeset 5929 for code/trunk/src/libraries/core/Game.cc
- Timestamp:
- Oct 12, 2009, 8:20:07 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core5 (added) merged: 5768-5769,5772,5775-5780,5783-5785,5791-5792,5795-5807,5809-5814,5816-5832,5836-5839,5842-5853,5855-5899,5904-5922,5924-5928
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/Game.cc
r5781 r5929 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 46 #include "CommandLine.h" 47 47 #include "ConsoleCommand.h" … … 57 57 { Game::getInstance().stop(); } 58 58 SetConsoleCommandShortcutExternAlias(stop_game, "exit"); 59 static void printFPS() 60 { COUT(0) << Game::getInstance().getAvgFPS() << std::endl; } 61 SetConsoleCommandShortcutExternAlias(printFPS, "printFPS"); 62 static void printTickTime() 63 { COUT(0) << Game::getInstance().getAvgTickTime() << std::endl; } 64 SetConsoleCommandShortcutExternAlias(printTickTime, "printTickTime"); 59 65 60 66 std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s; … … 111 117 Game::Game(const std::string& cmdLine) 112 118 // Destroy factories before the Core! 113 : gsFactoryDestroyer_(Game::GameStateFactory:: factories_s, &std::map<std::string, shared_ptr<GameStateFactory> >::clear)119 : gsFactoryDestroyer_(Game::GameStateFactory::getFactories(), &std::map<std::string, shared_ptr<GameStateFactory> >::clear) 114 120 { 115 121 this->bAbort_ = false; … … 410 416 requestedNodes.push_back(currentNode); 411 417 } 418 if (currentNode == NULL) 419 requestedNodes.clear(); 412 420 } 413 421 … … 457 465 { 458 466 // Split string into pieces of the form whitespacesText 459 std::vector<std::pair<std::string, unsigned> > stateStrings;467 std::vector<std::pair<std::string, int> > stateStrings; 460 468 size_t pos = 0; 461 469 size_t startPos = 0; 462 470 while (pos < str.size()) 463 471 { 464 unsignedindentation = 0;472 int indentation = 0; 465 473 while(pos < str.size() && str[pos] == ' ') 466 474 ++indentation, ++pos; … … 470 478 stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation)); 471 479 } 472 unsigned int currentLevel = 0; 473 shared_ptr<GameStateTreeNode> currentNode = this->rootStateNode_; 474 for (std::vector<std::pair<std::string, unsigned> >::const_iterator it = stateStrings.begin(); it != stateStrings.end(); ++it) 475 { 476 std::string newStateName = it->first; 477 unsigned newLevel = it->second + 1; // empty root is 0 478 if (!this->checkState(newStateName)) 479 ThrowException(GameState, "GameState with name '" << newStateName << "' not found!"); 480 if (newStateName == this->rootStateNode_->name_) 480 if (stateStrings.empty()) 481 ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating."); 482 // Add element with large identation to detect the last with just an iterator 483 stateStrings.push_back(std::make_pair("", -1)); 484 485 // Parse elements recursively 486 std::vector<std::pair<std::string, int> >::const_iterator begin = stateStrings.begin(); 487 parseStates(begin, this->rootStateNode_); 488 } 489 490 /*** Internal ***/ 491 492 void Game::parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode) 493 { 494 SubString tokens(it->first, ","); 495 std::vector<std::pair<std::string, int> >::const_iterator startIt = it; 496 497 for (unsigned int i = 0; i < tokens.size(); ++i) 498 { 499 it = startIt; // Reset iterator to the beginning of the sub tree 500 if (!this->checkState(tokens[i])) 501 ThrowException(GameState, "GameState with name '" << tokens[i] << "' not found!"); 502 if (tokens[i] == this->rootStateNode_->name_) 481 503 ThrowException(GameState, "You shouldn't use 'emptyRootGameState' in the hierarchy..."); 482 shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode); 483 newNode->name_ = newStateName; 484 485 if (newLevel <= currentLevel) 486 { 487 do 488 currentNode = currentNode->parent_.lock(); 489 while (newLevel <= --currentLevel); 490 } 491 if (newLevel == currentLevel + 1) 492 { 493 // Add the child 494 newNode->parent_ = currentNode; 495 currentNode->children_.push_back(newNode); 496 } 497 else 498 ThrowException(GameState, "Indentation error while parsing the hierarchy."); 499 currentNode = newNode; 500 currentLevel = newLevel; 501 } 502 } 503 504 /*** Internal ***/ 504 shared_ptr<GameStateTreeNode> node(new GameStateTreeNode()); 505 node->name_ = tokens[i]; 506 node->parent_ = currentNode; 507 currentNode->children_.push_back(node); 508 509 int currentLevel = it->second; 510 ++it; 511 while (it->second != -1) 512 { 513 if (it->second <= currentLevel) 514 break; 515 else if (it->second == currentLevel + 1) 516 parseStates(it, node); 517 else 518 ThrowException(GameState, "Indentation error while parsing the hierarchy."); 519 } 520 } 521 } 505 522 506 523 void Game::loadGraphics() 507 524 { 508 if (!GameMode:: bShowsGraphics_s)525 if (!GameMode::showsGraphics()) 509 526 { 510 527 core_->loadGraphics(); 511 528 Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics); 512 GameMode::bShowsGraphics_s = true;513 529 514 530 // Construct all the GameStates that require graphics … … 531 547 void Game::unloadGraphics() 532 548 { 533 if (GameMode:: bShowsGraphics_s)549 if (GameMode::showsGraphics()) 534 550 { 535 551 // Destroy all the GameStates that require graphics … … 543 559 544 560 core_->unloadGraphics(); 545 GameMode::bShowsGraphics_s = false;546 561 } 547 562 } … … 607 622 } 608 623 609 std::map<std::string, shared_ptr<Game::GameStateFactory> > Game::GameStateFactory::factories_s; 624 /*static*/ std::map<std::string, shared_ptr<Game::GameStateFactory> >& Game::GameStateFactory::getFactories() 625 { 626 static std::map<std::string, shared_ptr<GameStateFactory> > factories; 627 return factories; 628 } 610 629 611 630 /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info) 612 631 { 613 std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = factories_s.find(info.className);614 assert(it != factories_s.end());632 std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = getFactories().find(info.className); 633 assert(it != getFactories().end()); 615 634 return it->second->fabricateInternal(info); 616 635 }
Note: See TracChangeset
for help on using the changeset viewer.