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