Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2701 was 2462, checked in by landauf, 16 years ago
  • fixed a small speedbar-initialization problem
  • added new console-commands:
    • pause
    • suicide
    • addBots [number]
    • killBots [number]
  • Property svn:eol-style set to native
File size: 8.8 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"
[2400]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"
[2344]43#include "core/LuaBind.h"
[1826]44#include "tools/Timer.h"
[2171]45#include "objects/Tickable.h"
[1661]46#include "Settings.h"
47
[2406]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")
[2400]70        , timeFactor_(1.0f)
[2462]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();
[2400]80
81        this->ccSetTimeFactor_ = 0;
[2462]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
[2400]98        // reset game speed to normal
99        timeFactor_ = 1.0f;
100
[2344]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
[2462]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
[2462]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        }
[2400]145
[2462]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    {
[2344]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
[2344]173        delete this->settings_;
174        delete this->luaBind_;
[2400]175
176        if (this->ccSetTimeFactor_)
177        {
178            delete this->ccSetTimeFactor_;
179            this->ccSetTimeFactor_ = 0;
180        }
[2462]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
198        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
[2400]199            it->tick(time.getDeltaTime() * this->timeFactor_);
[2171]200        /*** HACK *** HACK ***/
201
[1674]202        this->tickChild(time);
[1662]203    }
[1674]204
205    /**
206    @note
[1691]207        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
[1674]208            (Object-oriented Graphics Rendering Engine)
209        For the latest info, see http://www.ogre3d.org/
210
211        Copyright (c) 2000-2008 Torus Knot Software Ltd
[2400]212
[2087]213        OGRE is licensed under the LGPL. For more info, see OGRE license.
[1674]214    */
[1691]215    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
[1674]216    {
217#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
218        // Get the current process core mask
[2400]219        DWORD procMask;
220        DWORD sysMask;
[1891]221#  if _MSC_VER >= 1400 && defined (_M_X64)
[2400]222        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
[1891]223#  else
[2400]224        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
[1891]225#  endif
[1674]226
[2400]227        // If procMask is 0, consider there is only one core available
228        // (using 0 as procMask will cause an infinite loop below)
229        if (procMask == 0)
230            procMask = 1;
[1674]231
[1691]232        // if the core specified with limitToCPU is not available, take the lowest one
233        if (!(procMask & (1 << limitToCPU)))
234            limitToCPU = 0;
235
[2400]236        // Find the lowest core that this process uses and limitToCPU suggests
[1674]237        DWORD threadMask = 1;
[2400]238        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
239            threadMask <<= 1;
[1674]240
[2400]241        // Set affinity to the first core
242        SetThreadAffinityMask(GetCurrentThread(), threadMask);
[1674]243#endif
244    }
[2400]245
246    /**
247    @brief
248        Changes the speed of Orxonox
249    */
250    void GSRoot::setTimeFactor(float factor)
251    {
252        if (Core::isMaster())
253        {
[2462]254            if (!this->bPaused_)
255            {
256                TimeFactorListener::timefactor_s = factor;
[2400]257
[2462]258                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
259                    it->changedTimeFactor(factor, this->timeFactor_);
[2406]260
[2462]261                this->timeFactor_ = factor;
262            }
263            else
264                this->timeFactorPauseBackup_ = factor;
[2400]265        }
266    }
[2406]267
[2462]268    void GSRoot::pause()
269    {
270        if (Core::isMaster())
271        {
272            if (!this->bPaused_)
273            {
274                this->timeFactorPauseBackup_ = this->timeFactor_;
275                this->setTimeFactor(0.0f);
276                this->bPaused_ = true;
277            }
278            else
279            {
280                this->bPaused_ = false;
281                this->setTimeFactor(this->timeFactorPauseBackup_);
282            }
283        }
284    }
285
[2406]286    ////////////////////////
287    // TimeFactorListener //
288    ////////////////////////
289    float TimeFactorListener::timefactor_s = 1.0f;
290
291    TimeFactorListener::TimeFactorListener()
292    {
293        RegisterRootObject(TimeFactorListener);
294    }
[1661]295}
Note: See TracBrowser for help on using the repository browser.