Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/orxonox/gamestates/GSRoot.cc @ 2937

Last change on this file since 2937 was 2797, checked in by landauf, 16 years ago

Added some #ifndef's around #define NOMINMAX and #define FORCEINLINE to avoid "symbol redefined" compiler-warnings.

Reto, I hope this doesn't cause any troubles. If it does, I suggest using "#undef symbolname" instead of "#ifndef symbolname". This should then work basically as before, but without warnings.

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