Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem3/src/orxonox/gamestates/GSRoot.cc @ 2685

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

Fixed install target:

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