- Timestamp:
- Apr 8, 2009, 12:36:08 AM (16 years ago)
- Location:
- code/branches/questsystem5
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/questsystem5
- Property svn:mergeinfo changed
-
code/branches/questsystem5/src/orxonox/gamestates/GSRoot.cc
r2759 r2907 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 # define NOMINMAX // required to stop windows.h screwing up std::min definition52 # include "windows.h"53 #endif54 55 43 namespace orxonox 56 44 { 57 SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu"); 58 59 GSRoot::GSRoot() 60 : 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) 61 55 , timeFactor_(1.0f) 62 56 , bPaused_(false) 63 57 , timeFactorPauseBackup_(1.0f) 64 , tclBind_(0) 65 , tclThreadManager_(0) 66 , shell_(0) 67 { 68 RegisterRootObject(GSRoot); 69 setConfigValues(); 70 58 { 71 59 this->ccSetTimeFactor_ = 0; 72 60 this->ccPause_ = 0; … … 77 65 } 78 66 79 void GSRoot::setConfigValues() 80 { 81 SetConfigValue(statisticsRefreshCycle_, 250000) 82 .description("Sets the time in microseconds interval at which average fps, etc. get updated."); 83 SetConfigValue(statisticsAvgLength_, 1000000) 84 .description("Sets the time in microseconds interval at which average fps, etc. gets calculated."); 85 } 86 87 void GSRoot::enter() 88 { 89 // creates the class hierarchy for all classes with factories 90 Factory::createClassHierarchy(); 91 67 void GSRoot::activate() 68 { 92 69 // reset game speed to normal 93 timeFactor_ = 1.0f; 94 95 // reset frame counter 96 this->statisticsStartTime_ = 0; 97 this->statisticsTickTimes_.clear(); 98 this->periodTickTime_ = 0; 99 this->avgFPS_ = 0.0f; 100 this->avgTickTime_ = 0.0f; 101 102 // Create the lua interface 103 this->luaBind_ = new LuaBind(); 104 105 // initialise TCL 106 this->tclBind_ = new TclBind(Core::getMediaPathString()); 107 this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter()); 108 109 // create a shell 110 this->shell_ = new Shell(); 111 112 // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump 113 // do this after ogre has initialised. Somehow Ogre changes the settings again (not through 114 // the timer though). 115 int limitToCPU = CommandLine::getValue("limitToCPU"); 116 if (limitToCPU > 0) 117 setThreadAffinity((unsigned int)(limitToCPU - 1)); 118 119 { 120 // add console commands 121 FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame); 122 functor->setObject(this); 123 this->ccExit_ = createConsoleCommand(functor, "exit"); 124 CommandExecutor::addConsoleCommandShortcut(this->ccExit_); 125 } 126 127 { 128 // add console commands 129 FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState); 130 functor->setObject(this); 131 this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState"); 132 CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_); 133 } 70 this->timeFactor_ = 1.0f; 134 71 135 72 { … … 148 85 CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); 149 86 } 150 } 151 152 void GSRoot::leave() 153 { 154 // destroy console commands 155 delete this->ccExit_; 156 delete this->ccSelectGameState_; 157 158 delete this->shell_; 159 delete this->tclThreadManager_; 160 delete this->tclBind_; 161 162 delete this->luaBind_; 163 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 { 164 121 if (this->ccSetTimeFactor_) 165 122 { … … 175 132 } 176 133 177 void GSRoot::ticked(const Clock& time) 178 { 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 179 143 uint64_t timeBeforeTick = time.getRealMicroseconds(); 180 181 TclThreadManager::getInstance().tick(time.getDeltaTime());182 144 183 145 for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) … … 198 160 uint64_t timeAfterTick = time.getRealMicroseconds(); 199 161 200 // STATISTICS 201 assert(timeAfterTick - timeBeforeTick >= 0 ); 202 statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick}; 203 statisticsTickTimes_.push_back(tickInfo); 204 assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength); 205 this->periodTickTime_ += tickInfo.tickLength; 206 207 // Ticks GSGraphics or GSDedicated 208 this->tickChild(time); 209 210 if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_) 211 { 212 std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin(); 213 assert(it != this->statisticsTickTimes_.end()); 214 int64_t lastTime = timeAfterTick - statisticsAvgLength_; 215 if ((int64_t)it->tickTime < lastTime) 216 { 217 do 218 { 219 assert(this->periodTickTime_ > it->tickLength); 220 this->periodTickTime_ -= it->tickLength; 221 ++it; 222 assert(it != this->statisticsTickTimes_.end()); 223 } while ((int64_t)it->tickTime < lastTime); 224 this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it); 225 } 226 227 uint32_t framesPerPeriod = this->statisticsTickTimes_.size(); 228 this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0; 229 this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0; 230 231 statisticsStartTime_ = timeAfterTick; 232 } 233 234 } 235 236 /** 237 @note 238 The code of this function has been copied and adjusted from OGRE, an open source graphics engine. 239 (Object-oriented Graphics Rendering Engine) 240 For the latest info, see http://www.ogre3d.org/ 241 242 Copyright (c) 2000-2008 Torus Knot Software Ltd 243 244 OGRE is licensed under the LGPL. For more info, see OGRE license. 245 */ 246 void GSRoot::setThreadAffinity(unsigned int limitToCPU) 247 { 248 #ifdef ORXONOX_PLATFORM_WINDOWS 249 // Get the current process core mask 250 DWORD procMask; 251 DWORD sysMask; 252 # if _MSC_VER >= 1400 && defined (_M_X64) 253 GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); 254 # else 255 GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); 256 # endif 257 258 // If procMask is 0, consider there is only one core available 259 // (using 0 as procMask will cause an infinite loop below) 260 if (procMask == 0) 261 procMask = 1; 262 263 // if the core specified with limitToCPU is not available, take the lowest one 264 if (!(procMask & (1 << limitToCPU))) 265 limitToCPU = 0; 266 267 // Find the lowest core that this process uses and limitToCPU suggests 268 DWORD threadMask = 1; 269 while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU))) 270 threadMask <<= 1; 271 272 // Set affinity to the first core 273 SetThreadAffinityMask(GetCurrentThread(), threadMask); 274 #endif 162 // Also add our tick time 163 Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); 275 164 } 276 165 … … 281 170 void GSRoot::setTimeFactor(float factor) 282 171 { 283 if ( Core::isMaster())172 if (GameMode::isMaster()) 284 173 { 285 174 if (!this->bPaused_) … … 299 188 void GSRoot::pause() 300 189 { 301 if ( Core::isMaster())190 if (GameMode::isMaster()) 302 191 { 303 192 if (!this->bPaused_) … … 314 203 } 315 204 } 316 317 ////////////////////////318 // TimeFactorListener //319 ////////////////////////320 float TimeFactorListener::timefactor_s = 1.0f;321 322 TimeFactorListener::TimeFactorListener()323 {324 RegisterRootObject(TimeFactorListener);325 }326 205 }
Note: See TracChangeset
for help on using the changeset viewer.