Changeset 6417 for code/trunk/src/libraries/core/Game.cc
- Timestamp:
- Dec 25, 2009, 10:23:58 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/Game.cc
r6021 r6417 51 51 #include "GameMode.h" 52 52 #include "GameState.h" 53 #include "GUIManager.h" 53 54 54 55 namespace orxonox … … 67 68 Game* Game::singletonPtr_s = 0; 68 69 69 70 /** 71 @brief 72 Represents one node of the game state tree. 73 */ 70 //! Represents one node of the game state tree. 74 71 struct GameStateTreeNode 75 72 { … … 79 76 }; 80 77 81 82 /**83 @brief84 Another helper class for the Game singleton: we cannot derive85 Game from OrxonoxClass because we need to handle the Identifier86 destruction in the Core destructor.87 */88 class GameConfiguration : public OrxonoxClass89 {90 public:91 GameConfiguration()92 {93 RegisterRootObject(GameConfiguration);94 this->setConfigValues();95 }96 97 void setConfigValues()98 {99 SetConfigValue(statisticsRefreshCycle_, 250000)100 .description("Sets the time in microseconds interval at which average fps, etc. get updated.");101 SetConfigValue(statisticsAvgLength_, 1000000)102 .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");103 SetConfigValue(fpsLimit_, 50)104 .description("Sets the desired framerate (0 for no limit).");105 }106 107 unsigned int statisticsRefreshCycle_;108 unsigned int statisticsAvgLength_;109 unsigned int fpsLimit_;110 };111 112 113 /**114 @brief115 Non-initialising constructor.116 */117 78 Game::Game(const std::string& cmdLine) 118 79 // Destroy factories before the Core! … … 128 89 #endif 129 90 91 // reset statistics 92 this->statisticsStartTime_ = 0; 93 this->statisticsTickTimes_.clear(); 94 this->periodTickTime_ = 0; 95 this->periodTime_ = 0; 96 this->avgFPS_ = 0.0f; 97 this->avgTickTime_ = 0.0f; 98 this->excessSleepTime_ = 0; 99 130 100 // Create an empty root state 131 101 this->declareGameState<GameState>("GameState", "emptyRootGameState", true, false); … … 136 106 // Create the Core 137 107 this->core_.reset(new Core(cmdLine)); 108 109 // Do this after the Core creation! 110 ClassIdentifier<Game>::getIdentifier("Game")->initialiseObject(this, "Game", true); 111 this->setConfigValues(); 138 112 139 113 // After the core has been created, we can safely instantiate the GameStates that don't require graphics … … 150 124 this->loadedTopStateNode_ = this->rootStateNode_; 151 125 this->loadedStates_.push_back(this->getState(rootStateNode_->name_)); 152 153 // Do this after the Core creation!154 this->configuration_.reset(new GameConfiguration());155 126 } 156 127 … … 161 132 Game::~Game() 162 133 { 134 // Remove us from the object lists again to avoid problems when destroying them 135 this->unregisterObject(); 136 } 137 138 void Game::setConfigValues() 139 { 140 SetConfigValue(statisticsRefreshCycle_, 250000) 141 .description("Sets the time in microseconds interval at which average fps, etc. get updated."); 142 SetConfigValue(statisticsAvgLength_, 1000000) 143 .description("Sets the time in microseconds interval at which average fps, etc. gets calculated."); 144 SetConfigValue(fpsLimit_, 50) 145 .description("Sets the desired frame rate (0 for no limit)."); 163 146 } 164 147 … … 176 159 COUT(0) << "Warning: Starting game without requesting GameState. This automatically terminates the program." << std::endl; 177 160 178 // reset statistics179 this->statisticsStartTime_ = 0;180 this->statisticsTickTimes_.clear();181 this->periodTickTime_ = 0;182 this->periodTime_ = 0;183 this->avgFPS_ = 0.0f;184 this->avgTickTime_ = 0.0f;185 this->excessSleepTime_ = 0;186 187 161 // START GAME 188 162 // first delta time should be about 0 seconds … … 204 178 this->updateGameStateStack(); 205 179 206 // Core preUpdate (doesn't throw)180 // Core preUpdate 207 181 try 208 182 { this->core_->preUpdate(*this->gameClock_); } … … 218 192 this->updateGameStates(); 219 193 220 // Core postUpdate (doesn't throw)194 // Core postUpdate 221 195 try 222 196 { this->core_->postUpdate(*this->gameClock_); } … … 232 206 this->updateStatistics(); 233 207 234 // Limit frame rate208 // Limit frame rate 235 209 this->updateFPSLimiter(); 236 210 } … … 312 286 this->statisticsTickTimes_.back().tickLength += currentRealTime - currentTime; 313 287 this->periodTickTime_ += currentRealTime - currentTime; 314 if (this->periodTime_ > this-> configuration_->statisticsRefreshCycle_)288 if (this->periodTime_ > this->statisticsRefreshCycle_) 315 289 { 316 290 std::list<StatisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin(); 317 291 assert(it != this->statisticsTickTimes_.end()); 318 int64_t lastTime = currentTime - this-> configuration_->statisticsAvgLength_;292 int64_t lastTime = currentTime - this->statisticsAvgLength_; 319 293 if (static_cast<int64_t>(it->tickTime) < lastTime) 320 294 { … … 330 304 331 305 uint32_t framesPerPeriod = this->statisticsTickTimes_.size(); 332 this->avgFPS_ = static_cast<float>(framesPerPeriod) / (currentTime - this->statisticsTickTimes_.front().tickTime) * 1000000.0f; 306 // Why minus 1? No idea, but otherwise the fps rate is always (from 10 to 200!) one frame too low 307 this->avgFPS_ = -1 + static_cast<float>(framesPerPeriod) / (currentTime - this->statisticsTickTimes_.front().tickTime) * 1000000.0f; 333 308 this->avgTickTime_ = static_cast<float>(this->periodTickTime_) / framesPerPeriod / 1000.0f; 334 309 335 this->periodTime_ -= this-> configuration_->statisticsRefreshCycle_;310 this->periodTime_ -= this->statisticsRefreshCycle_; 336 311 } 337 312 } … … 339 314 void Game::updateFPSLimiter() 340 315 { 341 // Why configuration_->fpsLimit_ - 1? No idea, but otherwise the fps rate is always (from 10 to 200!) one frame too high 342 uint32_t nextTime = gameClock_->getMicroseconds() - excessSleepTime_ + static_cast<uint32_t>(1000000.0f / (configuration_->fpsLimit_ - 1)); 316 uint64_t nextTime = gameClock_->getMicroseconds() - excessSleepTime_ + static_cast<uint32_t>(1000000.0f / fpsLimit_); 343 317 uint64_t currentRealTime = gameClock_->getRealMicroseconds(); 344 318 while (currentRealTime < nextTime - minimumSleepTime_) … … 584 558 585 559 shared_ptr<GameState> state = this->getState(name); 586 state->activate ();560 state->activateInternal(); 587 561 if (!this->loadedStates_.empty()) 588 562 this->loadedStates_.back()->activity_.topState = false; … … 603 577 if (!this->loadedStates_.empty()) 604 578 this->loadedStates_.back()->activity_.topState = true; 605 state->deactivate ();579 state->deactivateInternal(); 606 580 } 607 581 catch (...)
Note: See TracChangeset
for help on using the changeset viewer.