[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" |
---|
[2662] | 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" |
---|
[2662] | 43 | #include "core/LuaBind.h" |
---|
[1826] | 44 | #include "tools/Timer.h" |
---|
[2171] | 45 | #include "objects/Tickable.h" |
---|
[1661] | 46 | |
---|
[2710] | 47 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[1674] | 48 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 49 | # define WIN32_LEAN_AND_MEAN |
---|
| 50 | # endif |
---|
[2797] | 51 | # ifndef NOMINMAX |
---|
| 52 | # define NOMINMAX // required to stop windows.h screwing up std::min definition |
---|
| 53 | # endif |
---|
[1674] | 54 | # include "windows.h" |
---|
| 55 | #endif |
---|
| 56 | |
---|
[1661] | 57 | namespace orxonox |
---|
| 58 | { |
---|
[2087] | 59 | SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu"); |
---|
[1663] | 60 | |
---|
[1661] | 61 | GSRoot::GSRoot() |
---|
[1672] | 62 | : RootGameState("root") |
---|
[2662] | 63 | , timeFactor_(1.0f) |
---|
| 64 | , bPaused_(false) |
---|
| 65 | , timeFactorPauseBackup_(1.0f) |
---|
[1792] | 66 | , tclBind_(0) |
---|
| 67 | , tclThreadManager_(0) |
---|
| 68 | , shell_(0) |
---|
[1661] | 69 | { |
---|
[1686] | 70 | RegisterRootObject(GSRoot); |
---|
[1891] | 71 | setConfigValues(); |
---|
[2662] | 72 | |
---|
| 73 | this->ccSetTimeFactor_ = 0; |
---|
| 74 | this->ccPause_ = 0; |
---|
[1661] | 75 | } |
---|
| 76 | |
---|
| 77 | GSRoot::~GSRoot() |
---|
| 78 | { |
---|
| 79 | } |
---|
| 80 | |
---|
[1686] | 81 | void GSRoot::setConfigValues() |
---|
| 82 | { |
---|
[2662] | 83 | SetConfigValue(statisticsRefreshCycle_, 250000) |
---|
| 84 | .description("Sets the time in microseconds interval at which average fps, etc. get updated."); |
---|
| 85 | SetConfigValue(statisticsAvgLength_, 1000000) |
---|
| 86 | .description("Sets the time in microseconds interval at which average fps, etc. gets calculated."); |
---|
[1686] | 87 | } |
---|
| 88 | |
---|
[1661] | 89 | void GSRoot::enter() |
---|
| 90 | { |
---|
| 91 | // creates the class hierarchy for all classes with factories |
---|
| 92 | Factory::createClassHierarchy(); |
---|
| 93 | |
---|
[2662] | 94 | // reset game speed to normal |
---|
| 95 | timeFactor_ = 1.0f; |
---|
| 96 | |
---|
| 97 | // reset frame counter |
---|
| 98 | this->statisticsStartTime_ = 0; |
---|
| 99 | this->statisticsTickTimes_.clear(); |
---|
| 100 | this->periodTickTime_ = 0; |
---|
| 101 | this->avgFPS_ = 0.0f; |
---|
| 102 | this->avgTickTime_ = 0.0f; |
---|
| 103 | |
---|
| 104 | // Create the lua interface |
---|
| 105 | this->luaBind_ = new LuaBind(); |
---|
| 106 | |
---|
[1661] | 107 | // initialise TCL |
---|
[2759] | 108 | this->tclBind_ = new TclBind(Core::getMediaPathString()); |
---|
[1792] | 109 | this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter()); |
---|
[1661] | 110 | |
---|
[1792] | 111 | // create a shell |
---|
| 112 | this->shell_ = new Shell(); |
---|
| 113 | |
---|
[1674] | 114 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
| 115 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
| 116 | // the timer though). |
---|
[2087] | 117 | int limitToCPU = CommandLine::getValue("limitToCPU"); |
---|
[1691] | 118 | if (limitToCPU > 0) |
---|
| 119 | setThreadAffinity((unsigned int)(limitToCPU - 1)); |
---|
[1674] | 120 | |
---|
[2662] | 121 | { |
---|
| 122 | // add console commands |
---|
| 123 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame); |
---|
| 124 | functor->setObject(this); |
---|
| 125 | this->ccExit_ = createConsoleCommand(functor, "exit"); |
---|
| 126 | CommandExecutor::addConsoleCommandShortcut(this->ccExit_); |
---|
| 127 | } |
---|
[1664] | 128 | |
---|
[2662] | 129 | { |
---|
| 130 | // add console commands |
---|
| 131 | FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState); |
---|
| 132 | functor->setObject(this); |
---|
| 133 | this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState"); |
---|
| 134 | CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | { |
---|
| 138 | // time factor console command |
---|
| 139 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); |
---|
| 140 | functor->setObject(this); |
---|
| 141 | this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); |
---|
| 142 | CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | { |
---|
| 146 | // time factor console command |
---|
| 147 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); |
---|
| 148 | functor->setObject(this); |
---|
| 149 | this->ccPause_ = createConsoleCommand(functor, "pause"); |
---|
| 150 | CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); |
---|
| 151 | } |
---|
[1661] | 152 | } |
---|
| 153 | |
---|
| 154 | void GSRoot::leave() |
---|
| 155 | { |
---|
[2662] | 156 | // destroy console commands |
---|
| 157 | delete this->ccExit_; |
---|
| 158 | delete this->ccSelectGameState_; |
---|
[1792] | 159 | |
---|
| 160 | delete this->shell_; |
---|
| 161 | delete this->tclThreadManager_; |
---|
| 162 | delete this->tclBind_; |
---|
| 163 | |
---|
[2662] | 164 | delete this->luaBind_; |
---|
[1663] | 165 | |
---|
[2662] | 166 | if (this->ccSetTimeFactor_) |
---|
| 167 | { |
---|
| 168 | delete this->ccSetTimeFactor_; |
---|
| 169 | this->ccSetTimeFactor_ = 0; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | if (this->ccPause_) |
---|
| 173 | { |
---|
| 174 | delete this->ccPause_; |
---|
| 175 | this->ccPause_ = 0; |
---|
| 176 | } |
---|
[1661] | 177 | } |
---|
| 178 | |
---|
[1674] | 179 | void GSRoot::ticked(const Clock& time) |
---|
[1661] | 180 | { |
---|
[2662] | 181 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
| 182 | |
---|
[1674] | 183 | TclThreadManager::getInstance().tick(time.getDeltaTime()); |
---|
[1662] | 184 | |
---|
[1826] | 185 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
| 186 | it->tick(time); |
---|
| 187 | |
---|
[2171] | 188 | /*** HACK *** HACK ***/ |
---|
| 189 | // Call the Tickable objects |
---|
[2662] | 190 | float leveldt = time.getDeltaTime(); |
---|
| 191 | if (leveldt > 1.0f) |
---|
| 192 | { |
---|
| 193 | // just loaded |
---|
| 194 | leveldt = 0.0f; |
---|
| 195 | } |
---|
[2171] | 196 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
[2662] | 197 | it->tick(leveldt * this->timeFactor_); |
---|
[2171] | 198 | /*** HACK *** HACK ***/ |
---|
| 199 | |
---|
[2662] | 200 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
| 201 | |
---|
| 202 | // STATISTICS |
---|
| 203 | assert(timeAfterTick - timeBeforeTick >= 0 ); |
---|
| 204 | statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick}; |
---|
| 205 | statisticsTickTimes_.push_back(tickInfo); |
---|
| 206 | assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength); |
---|
| 207 | this->periodTickTime_ += tickInfo.tickLength; |
---|
| 208 | |
---|
| 209 | // Ticks GSGraphics or GSDedicated |
---|
[1674] | 210 | this->tickChild(time); |
---|
[2662] | 211 | |
---|
| 212 | if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_) |
---|
| 213 | { |
---|
| 214 | std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin(); |
---|
| 215 | assert(it != this->statisticsTickTimes_.end()); |
---|
| 216 | int64_t lastTime = timeAfterTick - statisticsAvgLength_; |
---|
| 217 | if ((int64_t)it->tickTime < lastTime) |
---|
| 218 | { |
---|
| 219 | do |
---|
| 220 | { |
---|
| 221 | assert(this->periodTickTime_ > it->tickLength); |
---|
| 222 | this->periodTickTime_ -= it->tickLength; |
---|
| 223 | ++it; |
---|
| 224 | assert(it != this->statisticsTickTimes_.end()); |
---|
| 225 | } while ((int64_t)it->tickTime < lastTime); |
---|
| 226 | this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | uint32_t framesPerPeriod = this->statisticsTickTimes_.size(); |
---|
| 230 | this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0; |
---|
| 231 | this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0; |
---|
| 232 | |
---|
| 233 | statisticsStartTime_ = timeAfterTick; |
---|
| 234 | } |
---|
| 235 | |
---|
[1662] | 236 | } |
---|
[1674] | 237 | |
---|
| 238 | /** |
---|
| 239 | @note |
---|
[1691] | 240 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
[1674] | 241 | (Object-oriented Graphics Rendering Engine) |
---|
| 242 | For the latest info, see http://www.ogre3d.org/ |
---|
| 243 | |
---|
| 244 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
[2662] | 245 | |
---|
[2087] | 246 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
[1674] | 247 | */ |
---|
[1691] | 248 | void GSRoot::setThreadAffinity(unsigned int limitToCPU) |
---|
[1674] | 249 | { |
---|
[2710] | 250 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[1674] | 251 | // Get the current process core mask |
---|
[2662] | 252 | DWORD procMask; |
---|
| 253 | DWORD sysMask; |
---|
[1891] | 254 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
[2662] | 255 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
[1891] | 256 | # else |
---|
[2662] | 257 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
[1891] | 258 | # endif |
---|
[1674] | 259 | |
---|
[2662] | 260 | // If procMask is 0, consider there is only one core available |
---|
| 261 | // (using 0 as procMask will cause an infinite loop below) |
---|
| 262 | if (procMask == 0) |
---|
| 263 | procMask = 1; |
---|
[1674] | 264 | |
---|
[1691] | 265 | // if the core specified with limitToCPU is not available, take the lowest one |
---|
| 266 | if (!(procMask & (1 << limitToCPU))) |
---|
| 267 | limitToCPU = 0; |
---|
| 268 | |
---|
[2662] | 269 | // Find the lowest core that this process uses and limitToCPU suggests |
---|
[1674] | 270 | DWORD threadMask = 1; |
---|
[2662] | 271 | while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU))) |
---|
| 272 | threadMask <<= 1; |
---|
[1674] | 273 | |
---|
[2662] | 274 | // Set affinity to the first core |
---|
| 275 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
[1674] | 276 | #endif |
---|
| 277 | } |
---|
[2662] | 278 | |
---|
| 279 | /** |
---|
| 280 | @brief |
---|
| 281 | Changes the speed of Orxonox |
---|
| 282 | */ |
---|
| 283 | void GSRoot::setTimeFactor(float factor) |
---|
| 284 | { |
---|
| 285 | if (Core::isMaster()) |
---|
| 286 | { |
---|
| 287 | if (!this->bPaused_) |
---|
| 288 | { |
---|
| 289 | TimeFactorListener::timefactor_s = factor; |
---|
| 290 | |
---|
| 291 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
| 292 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
| 293 | |
---|
| 294 | this->timeFactor_ = factor; |
---|
| 295 | } |
---|
| 296 | else |
---|
| 297 | this->timeFactorPauseBackup_ = factor; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | void GSRoot::pause() |
---|
| 302 | { |
---|
| 303 | if (Core::isMaster()) |
---|
| 304 | { |
---|
| 305 | if (!this->bPaused_) |
---|
| 306 | { |
---|
| 307 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
| 308 | this->setTimeFactor(0.0f); |
---|
| 309 | this->bPaused_ = true; |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | { |
---|
| 313 | this->bPaused_ = false; |
---|
| 314 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | //////////////////////// |
---|
| 320 | // TimeFactorListener // |
---|
| 321 | //////////////////////// |
---|
| 322 | float TimeFactorListener::timefactor_s = 1.0f; |
---|
| 323 | |
---|
| 324 | TimeFactorListener::TimeFactorListener() |
---|
| 325 | { |
---|
| 326 | RegisterRootObject(TimeFactorListener); |
---|
| 327 | } |
---|
[1661] | 328 | } |
---|