Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 13, 2009, 5:05:17 PM (15 years ago)
Author:
dafrick
Message:

Hopefully merged trunk successfully into pickup branch.

Location:
code/branches/pickup
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup

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

    r5781 r5935  
    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"
    4646#include "CommandLine.h"
    4747#include "ConsoleCommand.h"
     
    5757        { Game::getInstance().stop(); }
    5858    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");
    5965
    6066    std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s;
     
    111117    Game::Game(const std::string& cmdLine)
    112118        // 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)
    114120    {
    115121        this->bAbort_ = false;
     
    410416                requestedNodes.push_back(currentNode);
    411417            }
     418            if (currentNode == NULL)
     419                requestedNodes.clear();
    412420        }
    413421
     
    457465    {
    458466        // 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;
    460468        size_t pos = 0;
    461469        size_t startPos = 0;
    462470        while (pos < str.size())
    463471        {
    464             unsigned indentation = 0;
     472            int indentation = 0;
    465473            while(pos < str.size() && str[pos] == ' ')
    466474                ++indentation, ++pos;
     
    470478            stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation));
    471479        }
    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_)
    481503                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    }
    505522
    506523    void Game::loadGraphics()
    507524    {
    508         if (!GameMode::bShowsGraphics_s)
     525        if (!GameMode::showsGraphics())
    509526        {
    510527            core_->loadGraphics();
    511528            Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics);
    512             GameMode::bShowsGraphics_s = true;
    513529
    514530            // Construct all the GameStates that require graphics
     
    531547    void Game::unloadGraphics()
    532548    {
    533         if (GameMode::bShowsGraphics_s)
     549        if (GameMode::showsGraphics())
    534550        {
    535551            // Destroy all the GameStates that require graphics
     
    543559
    544560            core_->unloadGraphics();
    545             GameMode::bShowsGraphics_s = false;
    546561        }
    547562    }
     
    607622    }
    608623
    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    }
    610629
    611630    /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info)
    612631    {
    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());
    615634        return it->second->fabricateInternal(info);
    616635    }
Note: See TracChangeset for help on using the changeset viewer.