Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/gamestates/GSRoot.cc @ 2400

Last change on this file since 2400 was 2400, checked in by landauf, 15 years ago

moved setTimeFactor to GSRoot because tickables are currently ticked there

  • Property svn:eol-style set to native
File size: 7.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#include "objects/worldentities/Backlight.h"
47#include "tools/ParticleInterface.h"
48#include "Settings.h"
49
50#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
51#  ifndef WIN32_LEAN_AND_MEAN
52#    define WIN32_LEAN_AND_MEAN
53#  endif
54#  include "windows.h"
55
56   //Get around Windows hackery
57#  ifdef max
58#    undef max
59#  endif
60#  ifdef min
61#    undef min
62#  endif
63#endif
64
65namespace orxonox
66{
67    SetCommandLineArgument(dataPath, "").information("PATH");
68    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
69
70    GSRoot::GSRoot()
71        : RootGameState("root")
72        , timeFactor_(1.0f)
73        , settings_(0)
74        , tclBind_(0)
75        , tclThreadManager_(0)
76        , shell_(0)
77    {
78        RegisterRootObject(GSRoot);
79        setConfigValues();
80
81        this->ccSetTimeFactor_ = 0;
82    }
83
84    GSRoot::~GSRoot()
85    {
86    }
87
88    void GSRoot::setConfigValues()
89    {
90    }
91
92    void GSRoot::enter()
93    {
94        // creates the class hierarchy for all classes with factories
95        Factory::createClassHierarchy();
96
97        // reset game speed to normal
98        timeFactor_ = 1.0f;
99
100        // Create the lua interface
101        this->luaBind_ = new LuaBind();
102
103        // instantiate Settings class
104        this->settings_ = new Settings();
105
106        std::string dataPath = CommandLine::getValue("dataPath");
107        if (dataPath != "")
108        {
109            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
110                Settings::tsetDataPath(dataPath + "/");
111            else
112                Settings::tsetDataPath(dataPath);
113        }
114
115        // initialise TCL
116        this->tclBind_ = new TclBind(Settings::getDataPath());
117        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
118
119        // create a shell
120        this->shell_ = new Shell();
121
122        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
123        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
124        // the timer though).
125        int limitToCPU = CommandLine::getValue("limitToCPU");
126        if (limitToCPU > 0)
127            setThreadAffinity((unsigned int)(limitToCPU - 1));
128
129        // add console commands
130        FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame);
131        functor1->setObject(this);
132        ccExit_ = createConsoleCommand(functor1, "exit");
133        CommandExecutor::addConsoleCommandShortcut(ccExit_);
134
135        // add console commands
136        FunctorMember01<GameStateBase, const std::string&>* functor2 = createFunctor(&GameStateBase::requestState);
137        functor2->setObject(this);
138        ccSelectGameState_ = createConsoleCommand(functor2, "selectGameState");
139        CommandExecutor::addConsoleCommandShortcut(ccSelectGameState_);
140
141        // time factor console command
142        FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
143        functor->setObject(this);
144        ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
145        CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
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->settings_;
159        delete this->luaBind_;
160
161        if (this->ccSetTimeFactor_)
162        {
163            delete this->ccSetTimeFactor_;
164            this->ccSetTimeFactor_ = 0;
165        }
166    }
167
168    void GSRoot::ticked(const Clock& time)
169    {
170        TclThreadManager::getInstance().tick(time.getDeltaTime());
171
172        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
173            it->tick(time);
174
175        /*** HACK *** HACK ***/
176        // Call the Tickable objects
177        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
178            it->tick(time.getDeltaTime() * this->timeFactor_);
179        /*** HACK *** HACK ***/
180
181        this->tickChild(time);
182    }
183
184    /**
185    @note
186        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
187            (Object-oriented Graphics Rendering Engine)
188        For the latest info, see http://www.ogre3d.org/
189
190        Copyright (c) 2000-2008 Torus Knot Software Ltd
191
192        OGRE is licensed under the LGPL. For more info, see OGRE license.
193    */
194    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
195    {
196#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
197        // Get the current process core mask
198        DWORD procMask;
199        DWORD sysMask;
200#  if _MSC_VER >= 1400 && defined (_M_X64)
201        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
202#  else
203        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
204#  endif
205
206        // If procMask is 0, consider there is only one core available
207        // (using 0 as procMask will cause an infinite loop below)
208        if (procMask == 0)
209            procMask = 1;
210
211        // if the core specified with limitToCPU is not available, take the lowest one
212        if (!(procMask & (1 << limitToCPU)))
213            limitToCPU = 0;
214
215        // Find the lowest core that this process uses and limitToCPU suggests
216        DWORD threadMask = 1;
217        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
218            threadMask <<= 1;
219
220        // Set affinity to the first core
221        SetThreadAffinityMask(GetCurrentThread(), threadMask);
222#endif
223    }
224
225    /**
226    @brief
227        Changes the speed of Orxonox
228    */
229    void GSRoot::setTimeFactor(float factor)
230    {
231        if (Core::isMaster())
232        {
233            float change = factor / this->timeFactor_;
234
235            this->timeFactor_ = factor;
236/*
237            for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it != ObjectList<ParticleInterface>::end(); ++it)
238                it->setSpeedFactor(it->getSpeedFactor() * change);
239
240            for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it != ObjectList<Backlight>::end(); ++it)
241                it->setTimeFactor(timeFactor_);
242*/
243        }
244    }
245}
Note: See TracBrowser for help on using the repository browser.