Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2344 was 2344, checked in by rgrieder, 15 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
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/Factory.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/CoreIncludes.h"
37#include "core/ConsoleCommand.h"
38#include "core/CommandLine.h"
39#include "core/Shell.h"
40#include "core/TclBind.h"
41#include "core/TclThreadManager.h"
42#include "core/LuaBind.h"
43#include "tools/Timer.h"
44#include "objects/Tickable.h"
45#include "Settings.h"
46
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
62namespace orxonox
63{
64    SetCommandLineArgument(dataPath, "").information("PATH");
65    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
66
67    GSRoot::GSRoot()
68        : RootGameState("root")
69        , settings_(0)
70        , tclBind_(0)
71        , tclThreadManager_(0)
72        , shell_(0)
73    {
74        RegisterRootObject(GSRoot);
75        setConfigValues();
76    }
77
78    GSRoot::~GSRoot()
79    {
80    }
81
82    void GSRoot::setConfigValues()
83    {
84    }
85
86    void GSRoot::enter()
87    {
88        // creates the class hierarchy for all classes with factories
89        Factory::createClassHierarchy();
90
91        // Create the lua interface
92        this->luaBind_ = new LuaBind();
93
94        // instantiate Settings class
95        this->settings_ = new Settings();
96
97        std::string dataPath = CommandLine::getValue("dataPath");
98        if (dataPath != "")
99        {
100            if (*dataPath.end() != '/' && *dataPath.end() != '\\')
101                Settings::tsetDataPath(dataPath + "/");
102            else
103                Settings::tsetDataPath(dataPath);
104        }
105
106        // initialise TCL
107        this->tclBind_ = new TclBind(Settings::getDataPath());
108        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
109
110        // create a shell
111        this->shell_ = new Shell();
112
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).
116        int limitToCPU = CommandLine::getValue("limitToCPU");
117        if (limitToCPU > 0)
118            setThreadAffinity((unsigned int)(limitToCPU - 1));
119
120        // add console commands
121        FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame);
122        functor1->setObject(this);
123        ccExit_ = createConsoleCommand(functor1, "exit");
124        CommandExecutor::addConsoleCommandShortcut(ccExit_);
125
126        // add console commands
127        FunctorMember01<GameStateBase, const std::string&>* functor2 = createFunctor(&GameStateBase::requestState);
128        functor2->setObject(this);
129        ccSelectGameState_ = createConsoleCommand(functor2, "selectGameState");
130        CommandExecutor::addConsoleCommandShortcut(ccSelectGameState_);
131    }
132
133    void GSRoot::leave()
134    {
135        // destroy console commands
136        delete this->ccExit_;
137        delete this->ccSelectGameState_;
138
139        delete this->shell_;
140        delete this->tclThreadManager_;
141        delete this->tclBind_;
142
143        delete this->settings_;
144        delete this->luaBind_;
145    }
146
147    void GSRoot::ticked(const Clock& time)
148    {
149        TclThreadManager::getInstance().tick(time.getDeltaTime());
150
151        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
152            it->tick(time);
153
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
160        this->tickChild(time);
161    }
162
163    /**
164    @note
165        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
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       
171        OGRE is licensed under the LGPL. For more info, see OGRE license.
172    */
173    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
174    {
175#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
176        // Get the current process core mask
177            DWORD procMask;
178            DWORD sysMask;
179#  if _MSC_VER >= 1400 && defined (_M_X64)
180            GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
181#  else
182            GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
183#  endif
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
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
195        DWORD threadMask = 1;
196            while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
197                    threadMask <<= 1;
198
199            // Set affinity to the first core
200            SetThreadAffinityMask(GetCurrentThread(), threadMask);
201#endif
202    }
203}
Note: See TracBrowser for help on using the repository browser.