Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 30, 2009, 8:42:45 PM (15 years ago)
Author:
rgrieder
Message:

Added comma operator in the GameState hierarchy parser: Use "standalone,client,server" and the whole underlaying tree will be used for all three states. See Main.cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/libraries/core/Game.cc

    r5747 r5843  
    457457    {
    458458        // Split string into pieces of the form whitespacesText
    459         std::vector<std::pair<std::string, unsigned> > stateStrings;
     459        std::vector<std::pair<std::string, int> > stateStrings;
    460460        size_t pos = 0;
    461461        size_t startPos = 0;
    462462        while (pos < str.size())
    463463        {
    464             unsigned indentation = 0;
     464            int indentation = 0;
    465465            while(pos < str.size() && str[pos] == ' ')
    466466                ++indentation, ++pos;
     
    470470            stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation));
    471471        }
    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_)
     472        if (stateStrings.empty())
     473            ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating.");
     474        // Add element with large identation to detect the last with just an iterator
     475        stateStrings.push_back(std::make_pair("", -1));
     476
     477        // Parse elements recursively
     478        parseStates(stateStrings.begin(), this->rootStateNode_);
     479    }
     480
     481    /*** Internal ***/
     482
     483    void Game::parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode)
     484    {
     485        SubString tokens(it->first, ",");
     486        std::vector<std::pair<std::string, int> >::const_iterator startIt = it;
     487
     488        for (unsigned int i = 0; i < tokens.size(); ++i)
     489        {
     490            it = startIt; // Reset iterator to the beginning of the sub tree
     491            if (!this->checkState(tokens[i]))
     492                ThrowException(GameState, "GameState with name '" << tokens[i] << "' not found!");
     493            if (tokens[i] == this->rootStateNode_->name_)
    481494                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 ***/
     495            shared_ptr<GameStateTreeNode> node(new GameStateTreeNode());
     496            node->name_ = tokens[i];
     497            node->parent_ = currentNode;
     498            currentNode->children_.push_back(node);
     499
     500            int currentLevel = it->second;
     501            ++it;
     502            while (it->second != -1)
     503            {
     504                if (it->second <= currentLevel)
     505                    break;
     506                else if (it->second == currentLevel + 1)
     507                    parseStates(it, node);
     508                else
     509                    ThrowException(GameState, "Indentation error while parsing the hierarchy.");
     510            }
     511        }
     512    }
    505513
    506514    void Game::loadGraphics()
Note: See TracChangeset for help on using the changeset viewer.