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