| [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" | 
|---|
| [1826] | 42 | #include "tools/Timer.h" | 
|---|
| [2171] | 43 | #include "objects/Tickable.h" | 
|---|
| [1661] | 44 | #include "Settings.h" | 
|---|
 | 45 |  | 
|---|
| [1674] | 46 | #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32  | 
|---|
 | 47 | #  ifndef WIN32_LEAN_AND_MEAN | 
|---|
 | 48 | #    define WIN32_LEAN_AND_MEAN | 
|---|
 | 49 | #  endif | 
|---|
 | 50 | #  include "windows.h" | 
|---|
 | 51 |  | 
|---|
 | 52 |    //Get around Windows hackery | 
|---|
 | 53 | #  ifdef max | 
|---|
 | 54 | #    undef max | 
|---|
 | 55 | #  endif | 
|---|
 | 56 | #  ifdef min | 
|---|
 | 57 | #    undef min | 
|---|
 | 58 | #  endif | 
|---|
 | 59 | #endif | 
|---|
 | 60 |  | 
|---|
| [1661] | 61 | namespace orxonox | 
|---|
 | 62 | { | 
|---|
| [2087] | 63 |     SetCommandLineArgument(dataPath, "").information("PATH"); | 
|---|
 | 64 |     SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu"); | 
|---|
| [1663] | 65 |  | 
|---|
| [1661] | 66 |     GSRoot::GSRoot() | 
|---|
| [1672] | 67 |         : RootGameState("root") | 
|---|
| [1663] | 68 |         , settings_(0) | 
|---|
| [1792] | 69 |         , tclBind_(0) | 
|---|
 | 70 |         , tclThreadManager_(0) | 
|---|
 | 71 |         , shell_(0) | 
|---|
| [1661] | 72 |     { | 
|---|
| [1686] | 73 |         RegisterRootObject(GSRoot); | 
|---|
| [1891] | 74 |         setConfigValues(); | 
|---|
| [1661] | 75 |     } | 
|---|
 | 76 |  | 
|---|
 | 77 |     GSRoot::~GSRoot() | 
|---|
 | 78 |     { | 
|---|
 | 79 |     } | 
|---|
 | 80 |  | 
|---|
| [1686] | 81 |     void GSRoot::setConfigValues() | 
|---|
 | 82 |     { | 
|---|
 | 83 |     } | 
|---|
 | 84 |  | 
|---|
| [1661] | 85 |     void GSRoot::enter() | 
|---|
 | 86 |     { | 
|---|
 | 87 |         // creates the class hierarchy for all classes with factories | 
|---|
 | 88 |         Factory::createClassHierarchy(); | 
|---|
 | 89 |  | 
|---|
| [1663] | 90 |         // instantiate Settings class | 
|---|
 | 91 |         this->settings_ = new Settings(); | 
|---|
 | 92 |  | 
|---|
| [2087] | 93 |         std::string dataPath = CommandLine::getValue("dataPath"); | 
|---|
| [1664] | 94 |         if (dataPath != "") | 
|---|
| [1661] | 95 |         { | 
|---|
| [1664] | 96 |             if (*dataPath.end() != '/' && *dataPath.end() != '\\') | 
|---|
 | 97 |                 Settings::tsetDataPath(dataPath + "/"); | 
|---|
| [1661] | 98 |             else | 
|---|
| [1664] | 99 |                 Settings::tsetDataPath(dataPath); | 
|---|
| [1661] | 100 |         } | 
|---|
 | 101 |  | 
|---|
 | 102 |         // initialise TCL | 
|---|
| [1792] | 103 |         this->tclBind_ = new TclBind(Settings::getDataPath()); | 
|---|
 | 104 |         this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter()); | 
|---|
| [1661] | 105 |  | 
|---|
| [1792] | 106 |         // create a shell | 
|---|
 | 107 |         this->shell_ = new Shell(); | 
|---|
 | 108 |  | 
|---|
| [1674] | 109 |         // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump | 
|---|
 | 110 |         // do this after ogre has initialised. Somehow Ogre changes the settings again (not through | 
|---|
 | 111 |         // the timer though). | 
|---|
| [2087] | 112 |         int limitToCPU = CommandLine::getValue("limitToCPU"); | 
|---|
| [1691] | 113 |         if (limitToCPU > 0) | 
|---|
 | 114 |             setThreadAffinity((unsigned int)(limitToCPU - 1)); | 
|---|
| [1674] | 115 |  | 
|---|
| [1672] | 116 |         // add console commands | 
|---|
 | 117 |         FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame); | 
|---|
 | 118 |         functor1->setObject(this); | 
|---|
 | 119 |         CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "exit")); | 
|---|
| [1664] | 120 |  | 
|---|
| [1670] | 121 |         // add console commands | 
|---|
| [1689] | 122 |         FunctorMember01<GameStateBase, const std::string&>* functor2 = createFunctor(&GameStateBase::requestState); | 
|---|
| [1672] | 123 |         functor2->setObject(this); | 
|---|
 | 124 |         CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "selectGameState")); | 
|---|
| [1661] | 125 |     } | 
|---|
 | 126 |  | 
|---|
 | 127 |     void GSRoot::leave() | 
|---|
 | 128 |     { | 
|---|
| [1792] | 129 |         // TODO: remove and destroy console commands | 
|---|
 | 130 |  | 
|---|
 | 131 |         delete this->shell_; | 
|---|
 | 132 |         delete this->tclThreadManager_; | 
|---|
 | 133 |         delete this->tclBind_; | 
|---|
 | 134 |  | 
|---|
| [1670] | 135 |         delete settings_; | 
|---|
| [1663] | 136 |  | 
|---|
| [1661] | 137 |     } | 
|---|
 | 138 |  | 
|---|
| [1674] | 139 |     void GSRoot::ticked(const Clock& time) | 
|---|
| [1661] | 140 |     { | 
|---|
| [1674] | 141 |         TclThreadManager::getInstance().tick(time.getDeltaTime()); | 
|---|
| [1662] | 142 |  | 
|---|
| [1826] | 143 |         for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) | 
|---|
 | 144 |             it->tick(time); | 
|---|
 | 145 |  | 
|---|
| [2171] | 146 |         /*** HACK *** HACK ***/ | 
|---|
 | 147 |         // Call the Tickable objects | 
|---|
 | 148 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
 | 149 |             it->tick(time.getDeltaTime()); | 
|---|
 | 150 |         /*** HACK *** HACK ***/ | 
|---|
 | 151 |  | 
|---|
| [1674] | 152 |         this->tickChild(time); | 
|---|
| [1662] | 153 |     } | 
|---|
| [1674] | 154 |  | 
|---|
 | 155 |     /** | 
|---|
 | 156 |     @note | 
|---|
| [1691] | 157 |         The code of this function has been copied and adjusted from OGRE, an open source graphics engine. | 
|---|
| [1674] | 158 |             (Object-oriented Graphics Rendering Engine) | 
|---|
 | 159 |         For the latest info, see http://www.ogre3d.org/ | 
|---|
 | 160 |  | 
|---|
 | 161 |         Copyright (c) 2000-2008 Torus Knot Software Ltd | 
|---|
 | 162 |          | 
|---|
| [2087] | 163 |         OGRE is licensed under the LGPL. For more info, see OGRE license. | 
|---|
| [1674] | 164 |     */ | 
|---|
| [1691] | 165 |     void GSRoot::setThreadAffinity(unsigned int limitToCPU) | 
|---|
| [1674] | 166 |     { | 
|---|
 | 167 | #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32 | 
|---|
 | 168 |         // Get the current process core mask | 
|---|
 | 169 |             DWORD procMask; | 
|---|
 | 170 |             DWORD sysMask; | 
|---|
| [1891] | 171 | #  if _MSC_VER >= 1400 && defined (_M_X64) | 
|---|
| [1674] | 172 |             GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); | 
|---|
| [1891] | 173 | #  else | 
|---|
| [1674] | 174 |             GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); | 
|---|
| [1891] | 175 | #  endif | 
|---|
| [1674] | 176 |  | 
|---|
 | 177 |             // If procMask is 0, consider there is only one core available | 
|---|
 | 178 |             // (using 0 as procMask will cause an infinite loop below) | 
|---|
 | 179 |             if (procMask == 0) | 
|---|
 | 180 |                     procMask = 1; | 
|---|
 | 181 |  | 
|---|
| [1691] | 182 |         // if the core specified with limitToCPU is not available, take the lowest one | 
|---|
 | 183 |         if (!(procMask & (1 << limitToCPU))) | 
|---|
 | 184 |             limitToCPU = 0; | 
|---|
 | 185 |  | 
|---|
 | 186 |             // Find the lowest core that this process uses and limitToCPU suggests | 
|---|
| [1674] | 187 |         DWORD threadMask = 1; | 
|---|
| [1691] | 188 |             while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU))) | 
|---|
| [1674] | 189 |                     threadMask <<= 1; | 
|---|
 | 190 |  | 
|---|
 | 191 |             // Set affinity to the first core | 
|---|
 | 192 |             SetThreadAffinityMask(GetCurrentThread(), threadMask); | 
|---|
 | 193 | #endif | 
|---|
 | 194 |     } | 
|---|
| [1661] | 195 | } | 
|---|