Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSRoot.cc @ 2687

Last change on this file since 2687 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 11.1 KB
RevLine 
[1661]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#include "OrxonoxStableHeaders.h"
30#include "GSRoot.h"
31
[1764]32#include "util/Exception.h"
33#include "util/Debug.h"
[2662]34#include "core/Core.h"
[1661]35#include "core/Factory.h"
36#include "core/ConfigValueIncludes.h"
[1686]37#include "core/CoreIncludes.h"
[1661]38#include "core/ConsoleCommand.h"
[1664]39#include "core/CommandLine.h"
[1792]40#include "core/Shell.h"
[1661]41#include "core/TclBind.h"
[1672]42#include "core/TclThreadManager.h"
[2662]43#include "core/LuaBind.h"
[1826]44#include "tools/Timer.h"
[2171]45#include "objects/Tickable.h"
[1661]46#include "Settings.h"
47
[2662]48#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
[1674]49#  ifndef WIN32_LEAN_AND_MEAN
50#    define WIN32_LEAN_AND_MEAN
51#  endif
52#  include "windows.h"
53
54   //Get around Windows hackery
55#  ifdef max
56#    undef max
57#  endif
58#  ifdef min
59#    undef min
60#  endif
61#endif
62
[1661]63namespace orxonox
64{
[2087]65    SetCommandLineArgument(dataPath, "").information("PATH");
66    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
[1663]67
[1661]68    GSRoot::GSRoot()
[1672]69        : RootGameState("root")
[2662]70        , timeFactor_(1.0f)
71        , bPaused_(false)
72        , timeFactorPauseBackup_(1.0f)
[1663]73        , settings_(0)
[1792]74        , tclBind_(0)
75        , tclThreadManager_(0)
76        , shell_(0)
[1661]77    {
[1686]78        RegisterRootObject(GSRoot);
[1891]79        setConfigValues();
[2662]80
81        this->ccSetTimeFactor_ = 0;
82        this->ccPause_ = 0;
[1661]83    }
84
85    GSRoot::~GSRoot()
86    {
87    }
88
[1686]89    void GSRoot::setConfigValues()
90    {
[2662]91        SetConfigValue(statisticsRefreshCycle_, 250000)
92            .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
93        SetConfigValue(statisticsAvgLength_, 1000000)
94            .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
[1686]95    }
96
[1661]97    void GSRoot::enter()
98    {
99        // creates the class hierarchy for all classes with factories
100        Factory::createClassHierarchy();
101
[2662]102        // reset game speed to normal
103        timeFactor_ = 1.0f;
104
105        // reset frame counter
106        this->statisticsStartTime_ = 0;
107        this->statisticsTickTimes_.clear();
108        this->periodTickTime_ = 0;
109        this->avgFPS_ = 0.0f;
110        this->avgTickTime_ = 0.0f;
111
112        // Create the lua interface
113        this->luaBind_ = new LuaBind();
114
[1663]115        // instantiate Settings class
116        this->settings_ = new Settings();
117
[2087]118        std::string dataPath = CommandLine::getValue("dataPath");
[1664]119        if (dataPath != "")
[1661]120        {
[1664]121            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
122                Settings::tsetDataPath(dataPath + "/");
[1661]123            else
[1664]124                Settings::tsetDataPath(dataPath);
[1661]125        }
126
127        // initialise TCL
[1792]128        this->tclBind_ = new TclBind(Settings::getDataPath());
129        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
[1661]130
[1792]131        // create a shell
132        this->shell_ = new Shell();
133
[1674]134        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
135        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
136        // the timer though).
[2087]137        int limitToCPU = CommandLine::getValue("limitToCPU");
[1691]138        if (limitToCPU > 0)
139            setThreadAffinity((unsigned int)(limitToCPU - 1));
[1674]140
[2662]141        {
142            // add console commands
143            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame);
144            functor->setObject(this);
145            this->ccExit_ = createConsoleCommand(functor, "exit");
146            CommandExecutor::addConsoleCommandShortcut(this->ccExit_);
147        }
[1664]148
[2662]149        {
150            // add console commands
151            FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
152            functor->setObject(this);
153            this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
154            CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
155        }
156
157        {
158            // time factor console command
159            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
160            functor->setObject(this);
161            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
162            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
163        }
164
165        {
166            // time factor console command
167            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
168            functor->setObject(this);
169            this->ccPause_ = createConsoleCommand(functor, "pause");
170            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
171        }
[1661]172    }
173
174    void GSRoot::leave()
175    {
[2662]176        // destroy console commands
177        delete this->ccExit_;
178        delete this->ccSelectGameState_;
[1792]179
180        delete this->shell_;
181        delete this->tclThreadManager_;
182        delete this->tclBind_;
183
[2662]184        delete this->settings_;
185        delete this->luaBind_;
[1663]186
[2662]187        if (this->ccSetTimeFactor_)
188        {
189            delete this->ccSetTimeFactor_;
190            this->ccSetTimeFactor_ = 0;
191        }
192
193        if (this->ccPause_)
194        {
195            delete this->ccPause_;
196            this->ccPause_ = 0;
197        }
[1661]198    }
199
[1674]200    void GSRoot::ticked(const Clock& time)
[1661]201    {
[2662]202        uint64_t timeBeforeTick = time.getRealMicroseconds();
203
[1674]204        TclThreadManager::getInstance().tick(time.getDeltaTime());
[1662]205
[1826]206        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
207            it->tick(time);
208
[2171]209        /*** HACK *** HACK ***/
210        // Call the Tickable objects
[2662]211        float leveldt = time.getDeltaTime();
212        if (leveldt > 1.0f)
213        {
214            // just loaded
215            leveldt = 0.0f;
216        }
[2171]217        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
[2662]218            it->tick(leveldt * this->timeFactor_);
[2171]219        /*** HACK *** HACK ***/
220
[2662]221        uint64_t timeAfterTick = time.getRealMicroseconds();
222
223        // STATISTICS
224        assert(timeAfterTick - timeBeforeTick >= 0 );
225        statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick};
226        statisticsTickTimes_.push_back(tickInfo);
227        assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength);
228        this->periodTickTime_ += tickInfo.tickLength;
229
230        // Ticks GSGraphics or GSDedicated
[1674]231        this->tickChild(time);
[2662]232
233        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
234        {
235            std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
236            assert(it != this->statisticsTickTimes_.end());
237            int64_t lastTime = timeAfterTick - statisticsAvgLength_;
238            if ((int64_t)it->tickTime < lastTime)
239            {
240                do
241                {
242                    assert(this->periodTickTime_ > it->tickLength);
243                    this->periodTickTime_ -= it->tickLength;
244                    ++it;
245                    assert(it != this->statisticsTickTimes_.end());
246                } while ((int64_t)it->tickTime < lastTime);
247                this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it);
248            }
249
250            uint32_t framesPerPeriod = this->statisticsTickTimes_.size();
251            this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0;
252            this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0;
253
254            statisticsStartTime_ = timeAfterTick;
255        }
256
[1662]257    }
[1674]258
259    /**
260    @note
[1691]261        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
[1674]262            (Object-oriented Graphics Rendering Engine)
263        For the latest info, see http://www.ogre3d.org/
264
265        Copyright (c) 2000-2008 Torus Knot Software Ltd
[2662]266
[2087]267        OGRE is licensed under the LGPL. For more info, see OGRE license.
[1674]268    */
[1691]269    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
[1674]270    {
271#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
272        // Get the current process core mask
[2662]273        DWORD procMask;
274        DWORD sysMask;
[1891]275#  if _MSC_VER >= 1400 && defined (_M_X64)
[2662]276        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
[1891]277#  else
[2662]278        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
[1891]279#  endif
[1674]280
[2662]281        // If procMask is 0, consider there is only one core available
282        // (using 0 as procMask will cause an infinite loop below)
283        if (procMask == 0)
284            procMask = 1;
[1674]285
[1691]286        // if the core specified with limitToCPU is not available, take the lowest one
287        if (!(procMask & (1 << limitToCPU)))
288            limitToCPU = 0;
289
[2662]290        // Find the lowest core that this process uses and limitToCPU suggests
[1674]291        DWORD threadMask = 1;
[2662]292        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
293            threadMask <<= 1;
[1674]294
[2662]295        // Set affinity to the first core
296        SetThreadAffinityMask(GetCurrentThread(), threadMask);
[1674]297#endif
298    }
[2662]299
300    /**
301    @brief
302        Changes the speed of Orxonox
303    */
304    void GSRoot::setTimeFactor(float factor)
305    {
306        if (Core::isMaster())
307        {
308            if (!this->bPaused_)
309            {
310                TimeFactorListener::timefactor_s = factor;
311
312                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
313                    it->changedTimeFactor(factor, this->timeFactor_);
314
315                this->timeFactor_ = factor;
316            }
317            else
318                this->timeFactorPauseBackup_ = factor;
319        }
320    }
321
322    void GSRoot::pause()
323    {
324        if (Core::isMaster())
325        {
326            if (!this->bPaused_)
327            {
328                this->timeFactorPauseBackup_ = this->timeFactor_;
329                this->setTimeFactor(0.0f);
330                this->bPaused_ = true;
331            }
332            else
333            {
334                this->bPaused_ = false;
335                this->setTimeFactor(this->timeFactorPauseBackup_);
336            }
337        }
338    }
339
340    ////////////////////////
341    // TimeFactorListener //
342    ////////////////////////
343    float TimeFactorListener::timefactor_s = 1.0f;
344
345    TimeFactorListener::TimeFactorListener()
346    {
347        RegisterRootObject(TimeFactorListener);
348    }
[1661]349}
Note: See TracBrowser for help on using the repository browser.