Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2514 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
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"
[2485]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"
[2485]43#include "core/LuaBind.h"
[1826]44#include "tools/Timer.h"
[2171]45#include "objects/Tickable.h"
[1661]46#include "Settings.h"
47
[2485]48#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
[1674]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
[1661]63namespace orxonox
64{
[2087]65    SetCommandLineArgument(dataPath, "").information("PATH");
66    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
[1663]67
[1661]68    GSRoot::GSRoot()
[1672]69        : RootGameState("root")
[2485]70        , timeFactor_(1.0f)
71        , bPaused_(false)
72        , timeFactorPauseBackup_(1.0f)
[1663]73        , settings_(0)
[1792]74        , tclBind_(0)
75        , tclThreadManager_(0)
76        , shell_(0)
[1661]77    {
[1686]78        RegisterRootObject(GSRoot);
[1891]79        setConfigValues();
[2485]80
81        this->ccSetTimeFactor_ = 0;
82        this->ccPause_ = 0;
[1661]83    }
84
85    GSRoot::~GSRoot()
86    {
87    }
88
[1686]89    void GSRoot::setConfigValues()
90    {
91    }
92
[1661]93    void GSRoot::enter()
94    {
95        // creates the class hierarchy for all classes with factories
96        Factory::createClassHierarchy();
97
[2485]98        // reset game speed to normal
99        timeFactor_ = 1.0f;
100
101        // Create the lua interface
102        this->luaBind_ = new LuaBind();
103
[1663]104        // instantiate Settings class
105        this->settings_ = new Settings();
106
[2087]107        std::string dataPath = CommandLine::getValue("dataPath");
[1664]108        if (dataPath != "")
[1661]109        {
[1664]110            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
111                Settings::tsetDataPath(dataPath + "/");
[1661]112            else
[1664]113                Settings::tsetDataPath(dataPath);
[1661]114        }
115
116        // initialise TCL
[1792]117        this->tclBind_ = new TclBind(Settings::getDataPath());
118        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
[1661]119
[1792]120        // create a shell
121        this->shell_ = new Shell();
122
[1674]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).
[2087]126        int limitToCPU = CommandLine::getValue("limitToCPU");
[1691]127        if (limitToCPU > 0)
128            setThreadAffinity((unsigned int)(limitToCPU - 1));
[1674]129
[2485]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        }
[1664]137
[2485]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        }
[1661]161    }
162
163    void GSRoot::leave()
164    {
[2485]165        // destroy console commands
166        delete this->ccExit_;
167        delete this->ccSelectGameState_;
[1792]168
169        delete this->shell_;
170        delete this->tclThreadManager_;
171        delete this->tclBind_;
172
[2485]173        delete this->settings_;
174        delete this->luaBind_;
[1663]175
[2485]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        }
[1661]187    }
188
[1674]189    void GSRoot::ticked(const Clock& time)
[1661]190    {
[1674]191        TclThreadManager::getInstance().tick(time.getDeltaTime());
[1662]192
[1826]193        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
194            it->tick(time);
195
[2171]196        /*** HACK *** HACK ***/
197        // Call the Tickable objects
[2459]198        float leveldt = time.getDeltaTime();
199        if (leveldt > 1.0f)
200        {
201            // just loaded
202            leveldt = 0.0f;
203        }
[2171]204        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
[2485]205            it->tick(leveldt * this->timeFactor_);
[2171]206        /*** HACK *** HACK ***/
207
[1674]208        this->tickChild(time);
[1662]209    }
[1674]210
211    /**
212    @note
[1691]213        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
[1674]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
[2485]218
[2087]219        OGRE is licensed under the LGPL. For more info, see OGRE license.
[1674]220    */
[1691]221    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
[1674]222    {
223#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
224        // Get the current process core mask
[2485]225        DWORD procMask;
226        DWORD sysMask;
[1891]227#  if _MSC_VER >= 1400 && defined (_M_X64)
[2485]228        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
[1891]229#  else
[2485]230        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
[1891]231#  endif
[1674]232
[2485]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;
[1674]237
[1691]238        // if the core specified with limitToCPU is not available, take the lowest one
239        if (!(procMask & (1 << limitToCPU)))
240            limitToCPU = 0;
241
[2485]242        // Find the lowest core that this process uses and limitToCPU suggests
[1674]243        DWORD threadMask = 1;
[2485]244        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
245            threadMask <<= 1;
[1674]246
[2485]247        // Set affinity to the first core
248        SetThreadAffinityMask(GetCurrentThread(), threadMask);
[1674]249#endif
250    }
[2485]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    }
[1661]301}
Note: See TracBrowser for help on using the repository browser.