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