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