Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/gamestates/GSRoot.cc @ 2518

Last change on this file since 2518 was 2485, checked in by landauf, 16 years ago

Merged objecthierarchy2 into presentation branch

Couln't merge 2 lines in Gamestate.cc and a whole block of code in GSDedicated.cc (it seems like oli implemented in both branches something like a network-tick-limiter but with different approaches)

Not yet tested in network mode and with bots
The SpaceShips movement is also not yet fully adopted to the new physics (see Engine class)

  • Property svn:eol-style set to native
File size: 8.9 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 "Settings.h"
47
48#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
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
63namespace orxonox
64{
65    SetCommandLineArgument(dataPath, "").information("PATH");
66    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
67
68    GSRoot::GSRoot()
69        : RootGameState("root")
70        , timeFactor_(1.0f)
71        , bPaused_(false)
72        , timeFactorPauseBackup_(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        this->ccPause_ = 0;
83    }
84
85    GSRoot::~GSRoot()
86    {
87    }
88
89    void GSRoot::setConfigValues()
90    {
91    }
92
93    void GSRoot::enter()
94    {
95        // creates the class hierarchy for all classes with factories
96        Factory::createClassHierarchy();
97
98        // reset game speed to normal
99        timeFactor_ = 1.0f;
100
101        // Create the lua interface
102        this->luaBind_ = new LuaBind();
103
104        // instantiate Settings class
105        this->settings_ = new Settings();
106
107        std::string dataPath = CommandLine::getValue("dataPath");
108        if (dataPath != "")
109        {
110            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
111                Settings::tsetDataPath(dataPath + "/");
112            else
113                Settings::tsetDataPath(dataPath);
114        }
115
116        // initialise TCL
117        this->tclBind_ = new TclBind(Settings::getDataPath());
118        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
119
120        // create a shell
121        this->shell_ = new Shell();
122
123        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
124        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
125        // the timer though).
126        int limitToCPU = CommandLine::getValue("limitToCPU");
127        if (limitToCPU > 0)
128            setThreadAffinity((unsigned int)(limitToCPU - 1));
129
130        {
131            // add console commands
132            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame);
133            functor->setObject(this);
134            this->ccExit_ = createConsoleCommand(functor, "exit");
135            CommandExecutor::addConsoleCommandShortcut(this->ccExit_);
136        }
137
138        {
139            // add console commands
140            FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
141            functor->setObject(this);
142            this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
143            CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
144        }
145
146        {
147            // time factor console command
148            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
149            functor->setObject(this);
150            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
151            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
152        }
153
154        {
155            // time factor console command
156            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
157            functor->setObject(this);
158            this->ccPause_ = createConsoleCommand(functor, "pause");
159            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
160        }
161    }
162
163    void GSRoot::leave()
164    {
165        // destroy console commands
166        delete this->ccExit_;
167        delete this->ccSelectGameState_;
168
169        delete this->shell_;
170        delete this->tclThreadManager_;
171        delete this->tclBind_;
172
173        delete this->settings_;
174        delete this->luaBind_;
175
176        if (this->ccSetTimeFactor_)
177        {
178            delete this->ccSetTimeFactor_;
179            this->ccSetTimeFactor_ = 0;
180        }
181
182        if (this->ccPause_)
183        {
184            delete this->ccPause_;
185            this->ccPause_ = 0;
186        }
187    }
188
189    void GSRoot::ticked(const Clock& time)
190    {
191        TclThreadManager::getInstance().tick(time.getDeltaTime());
192
193        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
194            it->tick(time);
195
196        /*** HACK *** HACK ***/
197        // Call the Tickable objects
198        float leveldt = time.getDeltaTime();
199        if (leveldt > 1.0f)
200        {
201            // just loaded
202            leveldt = 0.0f;
203        }
204        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
205            it->tick(leveldt * this->timeFactor_);
206        /*** HACK *** HACK ***/
207
208        this->tickChild(time);
209    }
210
211    /**
212    @note
213        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
214            (Object-oriented Graphics Rendering Engine)
215        For the latest info, see http://www.ogre3d.org/
216
217        Copyright (c) 2000-2008 Torus Knot Software Ltd
218
219        OGRE is licensed under the LGPL. For more info, see OGRE license.
220    */
221    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
222    {
223#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
224        // Get the current process core mask
225        DWORD procMask;
226        DWORD sysMask;
227#  if _MSC_VER >= 1400 && defined (_M_X64)
228        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
229#  else
230        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
231#  endif
232
233        // If procMask is 0, consider there is only one core available
234        // (using 0 as procMask will cause an infinite loop below)
235        if (procMask == 0)
236            procMask = 1;
237
238        // if the core specified with limitToCPU is not available, take the lowest one
239        if (!(procMask & (1 << limitToCPU)))
240            limitToCPU = 0;
241
242        // Find the lowest core that this process uses and limitToCPU suggests
243        DWORD threadMask = 1;
244        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
245            threadMask <<= 1;
246
247        // Set affinity to the first core
248        SetThreadAffinityMask(GetCurrentThread(), threadMask);
249#endif
250    }
251
252    /**
253    @brief
254        Changes the speed of Orxonox
255    */
256    void GSRoot::setTimeFactor(float factor)
257    {
258        if (Core::isMaster())
259        {
260            if (!this->bPaused_)
261            {
262                TimeFactorListener::timefactor_s = factor;
263
264                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
265                    it->changedTimeFactor(factor, this->timeFactor_);
266
267                this->timeFactor_ = factor;
268            }
269            else
270                this->timeFactorPauseBackup_ = factor;
271        }
272    }
273
274    void GSRoot::pause()
275    {
276        if (Core::isMaster())
277        {
278            if (!this->bPaused_)
279            {
280                this->timeFactorPauseBackup_ = this->timeFactor_;
281                this->setTimeFactor(0.0f);
282                this->bPaused_ = true;
283            }
284            else
285            {
286                this->bPaused_ = false;
287                this->setTimeFactor(this->timeFactorPauseBackup_);
288            }
289        }
290    }
291
292    ////////////////////////
293    // TimeFactorListener //
294    ////////////////////////
295    float TimeFactorListener::timefactor_s = 1.0f;
296
297    TimeFactorListener::TimeFactorListener()
298    {
299        RegisterRootObject(TimeFactorListener);
300    }
301}
Note: See TracBrowser for help on using the repository browser.