Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/core/Game.cc @ 4036

Last change on this file since 4036 was 3299, checked in by rgrieder, 15 years ago

Sometimes I honestly ask myself why such things could run on Linux, magic I suppose

  • Property svn:eol-style set to native
File size: 14.8 KB
RevLine 
[2805]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@brief
32    Implementation of the Game class.
33*/
34
35#include "Game.h"
36
37#include <exception>
[3196]38#include <boost/weak_ptr.hpp>
[2805]39
40#include "util/Debug.h"
41#include "util/Exception.h"
[3289]42#include "util/Sleep.h"
[2850]43#include "util/SubString.h"
[2844]44#include "Clock.h"
45#include "CommandLine.h"
46#include "ConsoleCommand.h"
47#include "Core.h"
48#include "CoreIncludes.h"
49#include "ConfigValueIncludes.h"
50#include "GameState.h"
[2805]51
52namespace orxonox
53{
[3196]54    using boost::shared_ptr;
55    using boost::weak_ptr;
56
[2845]57    static void stop_game()
58        { Game::getInstance().stop(); }
59    SetConsoleCommandShortcutExternAlias(stop_game, "exit");
[2805]60
[2844]61    struct _CoreExport GameStateTreeNode
62    {
[3196]63        GameState* state_;
64        weak_ptr<GameStateTreeNode> parent_;
65        std::vector<shared_ptr<GameStateTreeNode> > children_;
[2844]66    };
67
68    std::map<std::string, GameState*> Game::allStates_s;
[2805]69    Game* Game::singletonRef_s = 0;
70
71    /**
72    @brief
73        Non-initialising constructor.
74    */
75    Game::Game(int argc, char** argv)
76    {
77        assert(singletonRef_s == 0);
78        singletonRef_s = this;
79
80        this->abort_ = false;
81
[2817]82        // reset statistics
83        this->statisticsStartTime_ = 0;
84        this->statisticsTickTimes_.clear();
85        this->periodTickTime_ = 0;
86        this->periodTime_ = 0;
87        this->avgFPS_ = 0.0f;
88        this->avgTickTime_ = 0.0f;
89
[2846]90
91        // Set up a basic clock to keep time
92        this->gameClock_ = new Clock();
93
[2807]94        this->core_ = new orxonox::Core();
[2846]95        this->core_->initialise(argc, argv);
[2817]96
97        RegisterRootObject(Game);
98        this->setConfigValues();
[2805]99    }
100
101    /**
102    @brief
103    */
104    Game::~Game()
105    {
106        // Destroy pretty much everyhting left
107        delete this->core_;
108
[2846]109        delete this->gameClock_;
110
[2805]111        assert(singletonRef_s);
112        singletonRef_s = 0;
113    }
114
[2817]115    void Game::setConfigValues()
116    {
117        SetConfigValue(statisticsRefreshCycle_, 250000)
118            .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
119        SetConfigValue(statisticsAvgLength_, 1000000)
120            .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
[3036]121        SetConfigValue(levelName_, "presentation_dm.oxw")
122            .description("Sets the preselection of the level in the main menu.");
[3289]123      SetConfigValue(FPSLimit_, 50)
124            .description("Sets the desired framerate (0 for no limit).");
[2817]125    }
126
[3036]127    void Game::setLevel(std::string levelName)
128    {
129        ModifyConfigValue(levelName_, set, levelName);
130    }
131
[3037]132    std::string Game::getLevel()
[3036]133    {
134        std::string levelName;
135        CommandLine::getValue("level", &levelName);
136        if (levelName == "")
137            return levelName_;
138        else
139            return levelName;
140    }
141
[2805]142    /**
143    @brief
144        Main loop of the orxonox game.
145    @note
146        We use the Ogre::Timer to measure time since it uses the most precise
147        method an any platform (however the windows timer lacks time when under
148        heavy kernel load!).
149    */
150    void Game::run()
151    {
[2845]152        // Always start with the ROOT state
[2844]153        this->requestedStateNodes_.push_back(this->rootStateNode_);
154        this->activeStateNode_ = this->rootStateNode_;
155        this->loadState(this->rootStateNode_->state_);
[2805]156
[2845]157        // START GAME
[2817]158        this->gameClock_->capture(); // first delta time should be about 0 seconds
[3299]159        // A first item is required for the fps limiter
160        statisticsTickInfo tickInfo = {0, 0};
161        statisticsTickTimes_.push_back(tickInfo);
[2844]162        while (!this->abort_ && !this->activeStates_.empty())
[2805]163        {
[3289]164            uint64_t currentTime = this->gameClock_->getRealMicroseconds();
165
166            uint64_t nextTickTime = statisticsTickTimes_.back().tickTime + 1000000.f/this->FPSLimit_;
167            if( currentTime < nextTickTime )
168            {
169                usleep( nextTickTime - currentTime );
170                continue;
171            }
[2807]172            this->gameClock_->capture();
[2805]173
[2817]174            // STATISTICS
175            statisticsTickInfo tickInfo = {currentTime, 0};
176            statisticsTickTimes_.push_back(tickInfo);
177            this->periodTime_ += this->gameClock_->getDeltaTimeMicroseconds();
178
[2844]179            // UPDATE STATE STACK
180            while (this->requestedStateNodes_.size() > 1)
181            {
182                // Note: this->requestedStateNodes_.front() is the currently active state node
[3196]183                std::vector<shared_ptr<GameStateTreeNode> >::iterator it = this->requestedStateNodes_.begin() + 1;
184                if (*it == this->activeStateNode_->parent_.lock())
[2844]185                    this->unloadState(this->activeStateNode_->state_);
186                else // has to be child
187                    this->loadState((*it)->state_);
188                this->activeStateNode_ = *it;
189                this->requestedStateNodes_.erase(this->requestedStateNodes_.begin());
190            }
[2805]191
[2844]192            // UPDATE, bottom to top in the stack
[2846]193            this->core_->update(*this->gameClock_);
[2844]194            for (std::vector<GameState*>::const_iterator it = this->activeStates_.begin();
195                it != this->activeStates_.end(); ++it)
[3084]196            {
197                // Add tick time for most of the states
198                uint64_t timeBeforeTick;
199                if ((*it)->getCountTickTime())
200                    timeBeforeTick = this->gameClock_->getRealMicroseconds();
201               
[2844]202                (*it)->update(*this->gameClock_);
203
[3084]204                if ((*it)->getCountTickTime())
[3196]205                    this->addTickTime(static_cast<uint32_t>(this->gameClock_->getRealMicroseconds() - timeBeforeTick));
[3084]206            }
207
[2817]208            // STATISTICS
209            if (this->periodTime_ > statisticsRefreshCycle_)
210            {
211                std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
212                assert(it != this->statisticsTickTimes_.end());
213                int64_t lastTime = currentTime - this->statisticsAvgLength_;
214                if ((int64_t)it->tickTime < lastTime)
215                {
216                    do
217                    {
218                        assert(this->periodTickTime_ > it->tickLength);
219                        this->periodTickTime_ -= it->tickLength;
220                        ++it;
221                        assert(it != this->statisticsTickTimes_.end());
222                    } while ((int64_t)it->tickTime < lastTime);
223                    this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it);
224                }
225
226                uint32_t framesPerPeriod = this->statisticsTickTimes_.size();
[3196]227                this->avgFPS_ = static_cast<float>(framesPerPeriod) / (currentTime - this->statisticsTickTimes_.front().tickTime) * 1000000.0f;
228                this->avgTickTime_ = static_cast<float>(this->periodTickTime_) / framesPerPeriod / 1000.0f;
[2817]229
230                this->periodTime_ -= this->statisticsRefreshCycle_;
231            }
[2805]232        }
233
[2845]234        // UNLOAD all remaining states
[2844]235        while (!this->activeStates_.empty())
236            this->unloadState(this->activeStates_.back());
[3196]237        this->activeStateNode_.reset();
[2844]238        this->requestedStateNodes_.clear();
[2805]239    }
240
241    void Game::stop()
242    {
243        this->abort_ = true;
244    }
[2817]245
246    void Game::addTickTime(uint32_t length)
247    {
248        assert(!this->statisticsTickTimes_.empty());
249        this->statisticsTickTimes_.back().tickLength += length;
250        this->periodTickTime_+=length;
251    }
[2844]252
253
254    /***** GameState related *****/
255
256    void Game::requestState(const std::string& name)
257    {
258        GameState* state = this->getState(name);
259        if (state == NULL || this->activeStateNode_ == NULL)
260            return;
261
[3196]262        shared_ptr<GameStateTreeNode> requestedNode;
[2844]263
264        // this->requestedStateNodes_.back() is the currently active state
[3196]265        shared_ptr<GameStateTreeNode> lastRequestedNode = this->requestedStateNodes_.back();
[2844]266
267        // Already the active node?
268        if (state == lastRequestedNode->state_)
269        {
270            COUT(2) << "Warning: Requesting the currently active state! Ignoring." << std::endl;
271            return;
272        }
273
274        // Check children first
275        for (unsigned int i = 0; i < lastRequestedNode->children_.size(); ++i)
276        {
277            if (lastRequestedNode->children_[i]->state_ == state)
278            {
279                requestedNode = lastRequestedNode->children_[i];
280                break;
281            }
282        }
283
284        // Check parent and all its grand parents
[3196]285        shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;
[2850]286        while (requestedNode == NULL && currentNode != NULL)
[2844]287        {
288            if (currentNode->state_ == state)
289                requestedNode = currentNode;
[3196]290            currentNode = currentNode->parent_.lock();
[2844]291        }
292
293        if (requestedNode == NULL)
294            COUT(1) << "Error: Requested GameState transition is not allowed. Ignoring." << std::endl;
295        else
296            this->requestedStateNodes_.push_back(requestedNode);
297    }
298
[2850]299    void Game::requestStates(const std::string& names)
300    {
301        SubString tokens(names, ",;", " ");
302        for (unsigned int i = 0; i < tokens.size(); ++i)
303            this->requestState(tokens[i]);
304    }
305
[2844]306    void Game::popState()
307    {
[3196]308        if (this->activeStateNode_ != NULL && this->requestedStateNodes_.back()->parent_.lock())
309            this->requestState(this->requestedStateNodes_.back()->parent_.lock()->state_->getName());
[2844]310        else
311            COUT(2) << "Warning: Could not pop GameState. Ignoring." << std::endl;
312    }
313
314    GameState* Game::getState(const std::string& name)
315    {
[2850]316        std::map<std::string, GameState*>::const_iterator it = allStates_s.find(getLowercase(name));
[2844]317        if (it != allStates_s.end())
318            return it->second;
319        else
320        {
321            COUT(1) << "Error: Could not find GameState '" << name << "'. Ignoring." << std::endl;
322            return 0;
323        }
324    }
325
326    void Game::setStateHierarchy(const std::string& str)
327    {
328        // Split string into pieces of the form whitespacesText
329        std::vector<std::pair<std::string, unsigned> > stateStrings;
330        size_t pos = 0;
331        size_t startPos = 0;
332        while (pos < str.size())
333        {
334            unsigned indentation = 0;
335            while(pos < str.size() && str[pos] == ' ')
336                ++indentation, ++pos;
337            startPos = pos;
338            while(pos < str.size() && str[pos] != ' ')
339                ++pos;
340            stateStrings.push_back(std::pair<std::string, unsigned>(
341                str.substr(startPos, pos - startPos), indentation));
342        }
343        unsigned int currentLevel = 0;
[3196]344        shared_ptr<GameStateTreeNode> currentNode;
[2844]345        for (std::vector<std::pair<std::string, unsigned> >::const_iterator it = stateStrings.begin(); it != stateStrings.end(); ++it)
346        {
347            std::string newStateName = it->first;
348            unsigned newLevel = it->second;
349            GameState* newState = this->getState(newStateName);
350            if (!newState)
351                ThrowException(GameState, std::string("GameState with name '") + newStateName + "' not found!");
352            if (newLevel == 0)
353            {
354                // root
355                if (this->rootStateNode_ != NULL)
356                    ThrowException(GameState, "No two root GameStates are allowed!");
[3196]357                shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
[2844]358                newNode->state_ = newState;
359                this->rootStateNode_ = newNode;
360                currentNode = this->rootStateNode_;
361            }
362            else if (currentNode)
363            {
[3196]364                shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
[2844]365                newNode->state_ = newState;
366                if (newLevel < currentLevel)
367                {
368                    // Get down the hierarchy
369                    do
[3196]370                        currentNode = currentNode->parent_.lock();
[2844]371                    while (newLevel < --currentLevel);
372                }
373                if (newLevel == currentLevel)
374                {
375                    // same level
376                    newNode->parent_ = currentNode->parent_;
[3196]377                    newNode->parent_.lock()->children_.push_back(newNode);
[2844]378                }
379                else if (newLevel == currentLevel + 1)
380                {
381                    // child
382                    newNode->parent_ = currentNode;
383                    currentNode->children_.push_back(newNode);
384                }
385                else
386                    ThrowException(GameState, "Indentation error while parsing the hierarchy.");
387                currentNode = newNode;
388                currentLevel = newLevel;
389            }
390            else
391            {
392                ThrowException(GameState, "No root GameState specified!");
393            }
394        }
395    }
396
397    /*** Internal ***/
398
399    void Game::loadState(GameState* state)
400    {
[2850]401        if (!this->activeStates_.empty())
402            this->activeStates_.back()->activity_.topState = false;
[2844]403        state->activate();
[2850]404        state->activity_.topState = true;
[2844]405        this->activeStates_.push_back(state);
406    }
407
408    void Game::unloadState(orxonox::GameState* state)
409    {
[2850]410        state->activity_.topState = false;
[2844]411        state->deactivate();
412        this->activeStates_.pop_back();
[2850]413        if (!this->activeStates_.empty())
414            this->activeStates_.back()->activity_.topState = true;
[2844]415    }
416
417    /*static*/ bool Game::addGameState(GameState* state)
418    {
[2850]419        std::map<std::string, GameState*>::const_iterator it = allStates_s.find(getLowercase(state->getName()));
[2844]420        if (it == allStates_s.end())
[2850]421            allStates_s[getLowercase(state->getName())] = state;
[2844]422        else
423            ThrowException(GameState, "Cannot add two GameStates with the same name to 'Game'.");
424
425        // just a required dummy return value
426        return true;
427    }
[2927]428
429    /*static*/ void Game::destroyStates()
430    {
431        // Delete all GameStates created by the macros
432        for (std::map<std::string, GameState*>::const_iterator it = allStates_s.begin(); it != allStates_s.end(); ++it)
433            delete it->second;
434        allStates_s.clear();
435    }
[2805]436}
Note: See TracBrowser for help on using the repository browser.