Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2768 was 2759, checked in by scheusso, 16 years ago

merged network branch (windows,multiplayer fixes) back to trunk

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