Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 10, 2016, 1:54:11 PM (9 years ago)
Author:
landauf
Message:

merged branch cpp11_v2 into cpp11_v3

Location:
code/branches/cpp11_v3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v3

  • code/branches/cpp11_v3/src/libraries/core/Game.cc

    r10624 r11054  
    3636
    3737#include <exception>
    38 #include <boost/weak_ptr.hpp>
    3938#include <loki/ScopeGuard.h>
    4039
     
    6665
    6766    std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s;
    68     Game* Game::singletonPtr_s = 0;
     67    Game* Game::singletonPtr_s = nullptr;
    6968
    7069    //! Represents one node of the game state tree.
     
    7271    {
    7372        std::string name_;
    74         weak_ptr<GameStateTreeNode> parent_;
    75         std::vector<shared_ptr<GameStateTreeNode> > children_;
     73        std::weak_ptr<GameStateTreeNode> parent_;
     74        std::vector<std::shared_ptr<GameStateTreeNode>> children_;
    7675    };
    7776
    7877    Game::Game(const std::string& cmdLine)
    79         : gameClock_(NULL)
    80         , core_(NULL)
     78        : gameClock_(nullptr)
     79        , core_(nullptr)
    8180        , bChangingState_(false)
    8281        , bAbort_(false)
    83         , config_(NULL)
     82        , config_(nullptr)
    8483        , destructionHelper_(this)
    8584    {
     
    116115
    117116        // 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);
    123121        }
    124122
    125123        // The empty root state is ALWAYS loaded!
    126         this->rootStateNode_ = shared_ptr<GameStateTreeNode>(new GameStateTreeNode());
     124        this->rootStateNode_ = std::shared_ptr<GameStateTreeNode>(std::make_shared<GameStateTreeNode>());
    127125        this->rootStateNode_->name_ = "emptyRootGameState";
    128126        this->loadedTopStateNode_ = this->rootStateNode_;
     
    137135
    138136        assert(loadedStates_.size() <= 1); // Just empty root GameState
    139         // Destroy all GameStates (shared_ptrs take care of actual destruction)
     137        // Destroy all GameStates (std::shared_ptrs take care of actual destruction)
    140138        constructedStates_.clear();
    141139
     
    235233        while (this->requestedStateNodes_.size() > 0)
    236234        {
    237             shared_ptr<GameStateTreeNode> requestedStateNode = this->requestedStateNodes_.front();
     235            std::shared_ptr<GameStateTreeNode> requestedStateNode = this->requestedStateNodes_.front();
    238236            assert(this->loadedTopStateNode_);
    239237            if (!this->loadedTopStateNode_->parent_.expired() && requestedStateNode == this->loadedTopStateNode_->parent_.lock())
     
    263261    {
    264262        // 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_)
    267264        {
    268265            try
     
    270267                // Add tick time for most of the states
    271268                uint64_t timeBeforeTick = 0;
    272                 if ((*it)->getInfo().bIgnoreTickTime)
     269                if (state->getInfo().bIgnoreTickTime)
    273270                    timeBeforeTick = this->gameClock_->getRealMicroseconds();
    274                 (*it)->update(*this->gameClock_);
    275                 if ((*it)->getInfo().bIgnoreTickTime)
     271                state->update(*this->gameClock_);
     272                if (state->getInfo().bIgnoreTickTime)
    276273                    this->subtractTickTime(static_cast<int32_t>(this->gameClock_->getRealMicroseconds() - timeBeforeTick));
    277274            }
    278275            catch (...)
    279276            {
    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;
    281278                orxout(user_error) << "This should really never happen!" << endl;
    282279                orxout(user_error) << "Unloading all GameStates depending on the one that crashed." << endl;
    283                 shared_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)
    285282                    current = current->parent_.lock();
    286283                if (current && current->parent_.lock())
     
    372369        }
    373370
    374         shared_ptr<GameStateTreeNode> lastRequestedNode;
     371        std::shared_ptr<GameStateTreeNode> lastRequestedNode;
    375372        if (this->requestedStateNodes_.empty())
    376373            lastRequestedNode = this->loadedTopStateNode_;
     
    384381
    385382        // Check children first
    386         std::vector<shared_ptr<GameStateTreeNode> > requestedNodes;
     383        std::vector<std::shared_ptr<GameStateTreeNode>> requestedNodes;
    387384        for (unsigned int i = 0; i < lastRequestedNode->children_.size(); ++i)
    388385        {
     
    397394        {
    398395            // Check parent and all its grand parents
    399             shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;
    400             while (currentNode != NULL)
     396            std::shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;
     397            while (currentNode != nullptr)
    401398            {
    402399                if (currentNode->name_ == name)
     
    405402                requestedNodes.push_back(currentNode);
    406403            }
    407             if (currentNode == NULL)
     404            if (currentNode == nullptr)
    408405                requestedNodes.clear();
    409406        }
     
    424421    void Game::popState()
    425422    {
    426         shared_ptr<GameStateTreeNode> lastRequestedNode;
     423        std::shared_ptr<GameStateTreeNode> lastRequestedNode;
    427424        if (this->requestedStateNodes_.empty())
    428425            lastRequestedNode = this->loadedTopStateNode_;
     
    435432    }
    436433
    437     shared_ptr<GameState> Game::getState(const std::string& name)
     434    std::shared_ptr<GameState> Game::getState(const std::string& name)
    438435    {
    439436        GameStateMap::const_iterator it = constructedStates_.find(name);
     
    447444            else
    448445                orxout(internal_error) << "Could not find GameState '" << name << "'." << endl;
    449             return shared_ptr<GameState>();
     446            return std::shared_ptr<GameState>();
    450447        }
    451448    }
     
    454451    {
    455452        // Split string into pieces of the form whitespacesText
    456         std::vector<std::pair<std::string, int> > stateStrings;
     453        std::vector<std::pair<std::string, int>> stateStrings;
    457454        size_t pos = 0;
    458455        size_t startPos = 0;
     
    465462            while (pos < str.size() && str[pos] != ' ')
    466463                ++pos;
    467             stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation));
     464            stateStrings.emplace_back(str.substr(startPos, pos - startPos), indentation);
    468465        }
    469466        if (stateStrings.empty())
    470467            ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating.");
    471468        // 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);
    473470
    474471        // Parse elements recursively
    475         std::vector<std::pair<std::string, int> >::const_iterator begin = stateStrings.begin();
     472        std::vector<std::pair<std::string, int>>::const_iterator begin = stateStrings.begin();
    476473        parseStates(begin, this->rootStateNode_);
    477474    }
     
    479476    /*** Internal ***/
    480477
    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)
    482479    {
    483480        SubString tokens(it->first, ",");
    484         std::vector<std::pair<std::string, int> >::const_iterator startIt = it;
     481        std::vector<std::pair<std::string, int>>::const_iterator startIt = it;
    485482
    486483        for (unsigned int i = 0; i < tokens.size(); ++i)
     
    491488            if (tokens[i] == this->rootStateNode_->name_)
    492489                ThrowException(GameState, "You shouldn't use 'emptyRootGameState' in the hierarchy...");
    493             shared_ptr<GameStateTreeNode> node(new GameStateTreeNode());
     490            std::shared_ptr<GameStateTreeNode> node(std::make_shared<GameStateTreeNode>());
    494491            node->name_ = tokens[i];
    495492            node->parent_ = currentNode;
     
    521518
    522519            // 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)
    527523                {
    528524                    // Game state loading failure is serious --> don't catch
    529                     shared_ptr<GameState> gameState = GameStateFactory::fabricate(it->second);
     525                    std::shared_ptr<GameState> gameState = GameStateFactory::fabricate(mapEntry.second);
    530526                    if (!constructedStates_.insert(std::make_pair(
    531                         it->second.stateName, gameState)).second)
     527                        mapEntry.second.stateName, gameState)).second)
    532528                        assert(false); // GameState was already created!
    533529                }
     
    582578            graphicsUnloader.Dismiss();
    583579
    584         shared_ptr<GameState> state = this->getState(name);
     580        std::shared_ptr<GameState> state = this->getState(name);
    585581        state->activateInternal();
    586582        if (!this->loadedStates_.empty())
     
    599595        try
    600596        {
    601             shared_ptr<GameState> state = this->getState(name);
     597            std::shared_ptr<GameState> state = this->getState(name);
    602598            state->activity_.topState = false;
    603599            this->loadedStates_.pop_back();
     
    613609        // Check if graphics is still required
    614610        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;
    617613        if (!graphicsRequired)
    618614            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)
     
    620616    }
    621617
    622     /*static*/ std::map<std::string, shared_ptr<Game::GameStateFactory> >& Game::GameStateFactory::getFactories()
    623     {
    624         static std::map<std::string, shared_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;
    625621        return factories;
    626622    }
    627623
    628     /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info)
    629     {
    630         std::map<std::string, shared_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);
    631627        assert(it != getFactories().end());
    632628        return it->second->fabricateInternal(info);
Note: See TracChangeset for help on using the changeset viewer.