Changeset 2896 for code/trunk/src/orxonox/gamestates/GSRoot.cc
- Timestamp:
- Apr 6, 2009, 1:59:00 AM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/gui merged: 2796,2798-2801,2805,2807-2808,2811,2814-2817,2834,2840-2850,2853-2854,2859,2862-2863,2869,2875,2887,2892
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/gamestates/GSRoot.cc
r2797 r2896 32 32 #include "util/Exception.h" 33 33 #include "util/Debug.h" 34 #include "core/C ore.h"35 #include "core/ Factory.h"36 #include "core/ ConfigValueIncludes.h"37 #include "core/Co reIncludes.h"34 #include "core/Clock.h" 35 #include "core/Game.h" 36 #include "core/GameMode.h" 37 #include "core/CommandLine.h" 38 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" 39 #include "tools/TimeFactorListener.h" 44 40 #include "tools/Timer.h" 45 41 #include "objects/Tickable.h" 46 42 47 #ifdef ORXONOX_PLATFORM_WINDOWS48 # ifndef WIN32_LEAN_AND_MEAN49 # define WIN32_LEAN_AND_MEAN50 # endif51 # ifndef NOMINMAX52 # define NOMINMAX // required to stop windows.h screwing up std::min definition53 # endif54 # include "windows.h"55 #endif56 57 43 namespace orxonox 58 44 { 59 SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu"); 60 61 GSRoot::GSRoot() 62 : RootGameState("root") 45 AddGameState(GSRoot, "root"); 46 SetCommandLineSwitch(console); 47 // Shortcuts for easy direct loading 48 SetCommandLineSwitch(server); 49 SetCommandLineSwitch(client); 50 SetCommandLineSwitch(dedicated); 51 SetCommandLineSwitch(standalone); 52 53 GSRoot::GSRoot(const std::string& name) 54 : GameState(name) 63 55 , timeFactor_(1.0f) 64 56 , bPaused_(false) 65 57 , timeFactorPauseBackup_(1.0f) 66 , tclBind_(0) 67 , tclThreadManager_(0) 68 , shell_(0) 69 { 70 RegisterRootObject(GSRoot); 71 setConfigValues(); 72 58 { 73 59 this->ccSetTimeFactor_ = 0; 74 60 this->ccPause_ = 0; … … 79 65 } 80 66 81 void GSRoot::setConfigValues() 82 { 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."); 87 } 88 89 void GSRoot::enter() 90 { 91 // creates the class hierarchy for all classes with factories 92 Factory::createClassHierarchy(); 93 67 void GSRoot::activate() 68 { 94 69 // 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 107 // initialise TCL 108 this->tclBind_ = new TclBind(Core::getMediaPathString()); 109 this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter()); 110 111 // create a shell 112 this->shell_ = new Shell(); 113 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). 117 int limitToCPU = CommandLine::getValue("limitToCPU"); 118 if (limitToCPU > 0) 119 setThreadAffinity((unsigned int)(limitToCPU - 1)); 120 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 } 128 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 } 70 this->timeFactor_ = 1.0f; 136 71 137 72 { … … 150 85 CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); 151 86 } 152 } 153 154 void GSRoot::leave() 155 { 156 // destroy console commands 157 delete this->ccExit_; 158 delete this->ccSelectGameState_; 159 160 delete this->shell_; 161 delete this->tclThreadManager_; 162 delete this->tclBind_; 163 164 delete this->luaBind_; 165 87 88 // Load level directly? 89 bool loadLevel = false; 90 if (CommandLine::getValue("standalone").getBool()) 91 { 92 Game::getInstance().requestStates("graphics, standalone, level"); 93 loadLevel = true; 94 } 95 if (CommandLine::getValue("server").getBool()) 96 { 97 Game::getInstance().requestStates("graphics, server, level"); 98 loadLevel = true; 99 } 100 if (CommandLine::getValue("client").getBool()) 101 { 102 Game::getInstance().requestStates("graphics, client, level"); 103 loadLevel = true; 104 } 105 if (CommandLine::getValue("dedicated").getBool()) 106 { 107 Game::getInstance().requestStates("dedicated, level"); 108 loadLevel = true; 109 } 110 111 // Determine where to start otherwise 112 if (!loadLevel && !CommandLine::getValue("console").getBool()) 113 { 114 // Also load graphics 115 Game::getInstance().requestState("graphics"); 116 } 117 } 118 119 void GSRoot::deactivate() 120 { 166 121 if (this->ccSetTimeFactor_) 167 122 { … … 177 132 } 178 133 179 void GSRoot::ticked(const Clock& time) 180 { 134 void GSRoot::update(const Clock& time) 135 { 136 if (this->getActivity().topState) 137 { 138 // This state can not 'survive' on its own. 139 // Load a user interface therefore 140 Game::getInstance().requestState("ioConsole"); 141 } 142 181 143 uint64_t timeBeforeTick = time.getRealMicroseconds(); 182 183 TclThreadManager::getInstance().tick(time.getDeltaTime());184 144 185 145 for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) … … 200 160 uint64_t timeAfterTick = time.getRealMicroseconds(); 201 161 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 210 this->tickChild(time); 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 236 } 237 238 /** 239 @note 240 The code of this function has been copied and adjusted from OGRE, an open source graphics engine. 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 245 246 OGRE is licensed under the LGPL. For more info, see OGRE license. 247 */ 248 void GSRoot::setThreadAffinity(unsigned int limitToCPU) 249 { 250 #ifdef ORXONOX_PLATFORM_WINDOWS 251 // Get the current process core mask 252 DWORD procMask; 253 DWORD sysMask; 254 # if _MSC_VER >= 1400 && defined (_M_X64) 255 GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); 256 # else 257 GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); 258 # endif 259 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; 264 265 // if the core specified with limitToCPU is not available, take the lowest one 266 if (!(procMask & (1 << limitToCPU))) 267 limitToCPU = 0; 268 269 // Find the lowest core that this process uses and limitToCPU suggests 270 DWORD threadMask = 1; 271 while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU))) 272 threadMask <<= 1; 273 274 // Set affinity to the first core 275 SetThreadAffinityMask(GetCurrentThread(), threadMask); 276 #endif 162 // Also add our tick time 163 Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); 277 164 } 278 165 … … 283 170 void GSRoot::setTimeFactor(float factor) 284 171 { 285 if ( Core::isMaster())172 if (GameMode::isMaster()) 286 173 { 287 174 if (!this->bPaused_) … … 301 188 void GSRoot::pause() 302 189 { 303 if ( Core::isMaster())190 if (GameMode::isMaster()) 304 191 { 305 192 if (!this->bPaused_) … … 316 203 } 317 204 } 318 319 ////////////////////////320 // TimeFactorListener //321 ////////////////////////322 float TimeFactorListener::timefactor_s = 1.0f;323 324 TimeFactorListener::TimeFactorListener()325 {326 RegisterRootObject(TimeFactorListener);327 }328 205 }
Note: See TracChangeset
for help on using the changeset viewer.