Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 5, 2009, 9:22:22 PM (15 years ago)
Author:
rgrieder
Message:

Synchronised sandbox with current code trunk. There should be a few bug fixes.

Location:
sandbox
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sandbox

  • sandbox/src/libraries/core/Game.cc

    r5782 r6038  
    3838#include <boost/weak_ptr.hpp>
    3939
     40#include "util/Clock.h"
    4041#include "util/Debug.h"
    4142#include "util/Exception.h"
     
    4344#include "util/Sleep.h"
    4445#include "util/SubString.h"
    45 #include "Clock.h"
    46 #include "CommandLine.h"
     46#include "CommandLineParser.h"
    4747#include "Core.h"
    4848#include "CoreIncludes.h"
     
    105105    Game::Game(const std::string& cmdLine)
    106106        // 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)
    108108    {
    109109        this->bAbort_ = false;
     
    404404                requestedNodes.push_back(currentNode);
    405405            }
     406            if (currentNode == NULL)
     407                requestedNodes.clear();
    406408        }
    407409
     
    451453    {
    452454        // 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;
    454456        size_t pos = 0;
    455457        size_t startPos = 0;
    456458        while (pos < str.size())
    457459        {
    458             unsigned indentation = 0;
     460            int indentation = 0;
    459461            while(pos < str.size() && str[pos] == ' ')
    460462                ++indentation, ++pos;
     
    464466            stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation));
    465467        }
    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_)
    475491                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);
    491505            else
    492506                ThrowException(GameState, "Indentation error while parsing the hierarchy.");
    493             currentNode = newNode;
    494             currentLevel = newLevel;
    495         }
    496     }
    497 
    498     /*** Internal ***/
     507        }
     508    }
     509    }
    499510
    500511    void Game::loadGraphics()
     
    566577    }
    567578
    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    }
    569584
    570585    /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info)
    571586    {
    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());
    574589        return it->second->fabricateInternal(info);
    575590    }
Note: See TracChangeset for help on using the changeset viewer.