Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2396 was 2344, checked in by rgrieder, 16 years ago

Completed destruction of static elements like XMLPort, Identifier, etc.
Of initially about 250 memory leaks (not in the actual meaning but the memory was never freed anyway) only 1 remains in TinyCpp.

  • Core class is now a normal Singleton that gets created and destroyed in main.
  • The same goes for Language, LuaBind, SignalHandler and PlayerManager.
  • Added a new std::set to the CommandExecutor so that the external ConsoleCommands can get destroyed too.
  • Code for destroying CommandLineArguments
  • Added destruction code for ConstructionCallbacks in Identifier
  • Moved internal identifier map (the one with the typeid(.) names) in a static function in Identifier. This was necessary in order to destroy ALL Identifiers with the static destruction function. Before it was possible to create an Identifier with having a class instance (that would call RegisterObject) for instance by simply accessing it via getIdentifier.
  • Removed a big memory leak in Button (forgot to destroy the ConfigValueContainers)
  • Added destruction code for InputBufferListenerTuples in InputBuffer destructor.
  • Added destruction code for load and save executors in both XMLPortParam and XMLPortObject
  • Added destruction code for ConsoleCommands in GSRoot, GSGraphics and GSLevel (temporary solution anyway)
  • Deleting the CEGUILua script module seems to work properly now, one memory leak less (GUIManager.cc)
  • Added global destruction calls in Main.cc
  • Property svn:eol-style set to native
File size: 6.1 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"
[1661]34#include "core/Factory.h"
35#include "core/ConfigValueIncludes.h"
[1686]36#include "core/CoreIncludes.h"
[1661]37#include "core/ConsoleCommand.h"
[1664]38#include "core/CommandLine.h"
[1792]39#include "core/Shell.h"
[1661]40#include "core/TclBind.h"
[1672]41#include "core/TclThreadManager.h"
[2344]42#include "core/LuaBind.h"
[1826]43#include "tools/Timer.h"
[2171]44#include "objects/Tickable.h"
[1661]45#include "Settings.h"
46
[1674]47#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
48#  ifndef WIN32_LEAN_AND_MEAN
49#    define WIN32_LEAN_AND_MEAN
50#  endif
51#  include "windows.h"
52
53   //Get around Windows hackery
54#  ifdef max
55#    undef max
56#  endif
57#  ifdef min
58#    undef min
59#  endif
60#endif
61
[1661]62namespace orxonox
63{
[2087]64    SetCommandLineArgument(dataPath, "").information("PATH");
65    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
[1663]66
[1661]67    GSRoot::GSRoot()
[1672]68        : RootGameState("root")
[1663]69        , settings_(0)
[1792]70        , tclBind_(0)
71        , tclThreadManager_(0)
72        , shell_(0)
[1661]73    {
[1686]74        RegisterRootObject(GSRoot);
[1891]75        setConfigValues();
[1661]76    }
77
78    GSRoot::~GSRoot()
79    {
80    }
81
[1686]82    void GSRoot::setConfigValues()
83    {
84    }
85
[1661]86    void GSRoot::enter()
87    {
88        // creates the class hierarchy for all classes with factories
89        Factory::createClassHierarchy();
90
[2344]91        // Create the lua interface
92        this->luaBind_ = new LuaBind();
93
[1663]94        // instantiate Settings class
95        this->settings_ = new Settings();
96
[2087]97        std::string dataPath = CommandLine::getValue("dataPath");
[1664]98        if (dataPath != "")
[1661]99        {
[1664]100            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
101                Settings::tsetDataPath(dataPath + "/");
[1661]102            else
[1664]103                Settings::tsetDataPath(dataPath);
[1661]104        }
105
106        // initialise TCL
[1792]107        this->tclBind_ = new TclBind(Settings::getDataPath());
108        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
[1661]109
[1792]110        // create a shell
111        this->shell_ = new Shell();
112
[1674]113        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
114        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
115        // the timer though).
[2087]116        int limitToCPU = CommandLine::getValue("limitToCPU");
[1691]117        if (limitToCPU > 0)
118            setThreadAffinity((unsigned int)(limitToCPU - 1));
[1674]119
[1672]120        // add console commands
121        FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame);
122        functor1->setObject(this);
[2344]123        ccExit_ = createConsoleCommand(functor1, "exit");
124        CommandExecutor::addConsoleCommandShortcut(ccExit_);
[1664]125
[1670]126        // add console commands
[1689]127        FunctorMember01<GameStateBase, const std::string&>* functor2 = createFunctor(&GameStateBase::requestState);
[1672]128        functor2->setObject(this);
[2344]129        ccSelectGameState_ = createConsoleCommand(functor2, "selectGameState");
130        CommandExecutor::addConsoleCommandShortcut(ccSelectGameState_);
[1661]131    }
132
133    void GSRoot::leave()
134    {
[2344]135        // destroy console commands
136        delete this->ccExit_;
137        delete this->ccSelectGameState_;
[1792]138
139        delete this->shell_;
140        delete this->tclThreadManager_;
141        delete this->tclBind_;
142
[2344]143        delete this->settings_;
144        delete this->luaBind_;
[1661]145    }
146
[1674]147    void GSRoot::ticked(const Clock& time)
[1661]148    {
[1674]149        TclThreadManager::getInstance().tick(time.getDeltaTime());
[1662]150
[1826]151        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
152            it->tick(time);
153
[2171]154        /*** HACK *** HACK ***/
155        // Call the Tickable objects
156        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
157            it->tick(time.getDeltaTime());
158        /*** HACK *** HACK ***/
159
[1674]160        this->tickChild(time);
[1662]161    }
[1674]162
163    /**
164    @note
[1691]165        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
[1674]166            (Object-oriented Graphics Rendering Engine)
167        For the latest info, see http://www.ogre3d.org/
168
169        Copyright (c) 2000-2008 Torus Knot Software Ltd
170       
[2087]171        OGRE is licensed under the LGPL. For more info, see OGRE license.
[1674]172    */
[1691]173    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
[1674]174    {
175#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
176        // Get the current process core mask
177            DWORD procMask;
178            DWORD sysMask;
[1891]179#  if _MSC_VER >= 1400 && defined (_M_X64)
[1674]180            GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
[1891]181#  else
[1674]182            GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
[1891]183#  endif
[1674]184
185            // If procMask is 0, consider there is only one core available
186            // (using 0 as procMask will cause an infinite loop below)
187            if (procMask == 0)
188                    procMask = 1;
189
[1691]190        // if the core specified with limitToCPU is not available, take the lowest one
191        if (!(procMask & (1 << limitToCPU)))
192            limitToCPU = 0;
193
194            // Find the lowest core that this process uses and limitToCPU suggests
[1674]195        DWORD threadMask = 1;
[1691]196            while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
[1674]197                    threadMask <<= 1;
198
199            // Set affinity to the first core
200            SetThreadAffinityMask(GetCurrentThread(), threadMask);
201#endif
202    }
[1661]203}
Note: See TracBrowser for help on using the repository browser.