[2805] | 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 | /** |
---|
| 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Implementation of the Game class. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "Game.h" |
---|
| 36 | |
---|
| 37 | #include <exception> |
---|
[3196] | 38 | #include <boost/weak_ptr.hpp> |
---|
[7266] | 39 | #include <loki/ScopeGuard.h> |
---|
[2805] | 40 | |
---|
[5929] | 41 | #include "util/Clock.h" |
---|
[8858] | 42 | #include "util/Output.h" |
---|
[2805] | 43 | #include "util/Exception.h" |
---|
[3304] | 44 | #include "util/Sleep.h" |
---|
[2850] | 45 | #include "util/SubString.h" |
---|
[6021] | 46 | #include "CommandLineParser.h" |
---|
[2844] | 47 | #include "Core.h" |
---|
| 48 | #include "CoreIncludes.h" |
---|
| 49 | #include "ConfigValueIncludes.h" |
---|
[5781] | 50 | #include "GameMode.h" |
---|
[2844] | 51 | #include "GameState.h" |
---|
[8079] | 52 | #include "GraphicsManager.h" |
---|
[6417] | 53 | #include "GUIManager.h" |
---|
[7284] | 54 | #include "command/ConsoleCommand.h" |
---|
[2805] | 55 | |
---|
| 56 | namespace orxonox |
---|
| 57 | { |
---|
[5781] | 58 | static void stop_game() |
---|
| 59 | { Game::getInstance().stop(); } |
---|
[7284] | 60 | SetConsoleCommand("exit", &stop_game); |
---|
[5929] | 61 | static void printFPS() |
---|
[8858] | 62 | { orxout(message) << Game::getInstance().getAvgFPS() << endl; } |
---|
[8079] | 63 | SetConsoleCommand("Stats", "printFPS", &printFPS); |
---|
[5929] | 64 | static void printTickTime() |
---|
[8858] | 65 | { orxout(message) << Game::getInstance().getAvgTickTime() << endl; } |
---|
[8079] | 66 | SetConsoleCommand("Stats", "printTickTime", &printTickTime); |
---|
[5781] | 67 | |
---|
[3370] | 68 | std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s; |
---|
| 69 | Game* Game::singletonPtr_s = 0; |
---|
[3280] | 70 | |
---|
[6417] | 71 | //! Represents one node of the game state tree. |
---|
[3280] | 72 | struct GameStateTreeNode |
---|
[2844] | 73 | { |
---|
[3370] | 74 | std::string name_; |
---|
[3196] | 75 | weak_ptr<GameStateTreeNode> parent_; |
---|
| 76 | std::vector<shared_ptr<GameStateTreeNode> > children_; |
---|
[2844] | 77 | }; |
---|
| 78 | |
---|
[3323] | 79 | Game::Game(const std::string& cmdLine) |
---|
[8423] | 80 | : gameClock_(NULL) |
---|
| 81 | , core_(NULL) |
---|
| 82 | , bChangingState_(false) |
---|
| 83 | , bAbort_(false) |
---|
| 84 | , destructionHelper_(this) |
---|
[2805] | 85 | { |
---|
[8858] | 86 | orxout(internal_status) << "initializing Game object..." << endl; |
---|
| 87 | |
---|
[3370] | 88 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 89 | minimumSleepTime_ = 1000/*us*/; |
---|
| 90 | #else |
---|
| 91 | minimumSleepTime_ = 0/*us*/; |
---|
| 92 | #endif |
---|
| 93 | |
---|
[6417] | 94 | // reset statistics |
---|
| 95 | this->statisticsStartTime_ = 0; |
---|
| 96 | this->statisticsTickTimes_.clear(); |
---|
| 97 | this->periodTickTime_ = 0; |
---|
| 98 | this->periodTime_ = 0; |
---|
| 99 | this->avgFPS_ = 0.0f; |
---|
| 100 | this->avgTickTime_ = 0.0f; |
---|
| 101 | this->excessSleepTime_ = 0; |
---|
| 102 | |
---|
[3280] | 103 | // Create an empty root state |
---|
[3370] | 104 | this->declareGameState<GameState>("GameState", "emptyRootGameState", true, false); |
---|
[3280] | 105 | |
---|
[2846] | 106 | // Set up a basic clock to keep time |
---|
[8423] | 107 | this->gameClock_ = new Clock(); |
---|
[2846] | 108 | |
---|
[3280] | 109 | // Create the Core |
---|
[8858] | 110 | orxout(internal_info) << "creating Core object:" << endl; |
---|
[8423] | 111 | this->core_ = new Core(cmdLine); |
---|
[2817] | 112 | |
---|
[6417] | 113 | // Do this after the Core creation! |
---|
[8729] | 114 | RegisterRootObject(Game); |
---|
[6417] | 115 | this->setConfigValues(); |
---|
| 116 | |
---|
[3370] | 117 | // After the core has been created, we can safely instantiate the GameStates that don't require graphics |
---|
[3280] | 118 | for (std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.begin(); |
---|
| 119 | it != gameStateDeclarations_s.end(); ++it) |
---|
| 120 | { |
---|
[3370] | 121 | if (!it->second.bGraphicsMode) |
---|
| 122 | constructedStates_[it->second.stateName] = GameStateFactory::fabricate(it->second); |
---|
[3280] | 123 | } |
---|
| 124 | |
---|
| 125 | // The empty root state is ALWAYS loaded! |
---|
| 126 | this->rootStateNode_ = shared_ptr<GameStateTreeNode>(new GameStateTreeNode()); |
---|
[3370] | 127 | this->rootStateNode_->name_ = "emptyRootGameState"; |
---|
| 128 | this->loadedTopStateNode_ = this->rootStateNode_; |
---|
| 129 | this->loadedStates_.push_back(this->getState(rootStateNode_->name_)); |
---|
[8858] | 130 | |
---|
| 131 | orxout(internal_status) << "finished initializing Game object" << endl; |
---|
[2805] | 132 | } |
---|
| 133 | |
---|
[8423] | 134 | void Game::destroy() |
---|
[2805] | 135 | { |
---|
[8858] | 136 | orxout(internal_status) << "destroying Game object..." << endl; |
---|
| 137 | |
---|
[6417] | 138 | // Remove us from the object lists again to avoid problems when destroying them |
---|
| 139 | this->unregisterObject(); |
---|
[8423] | 140 | |
---|
| 141 | assert(loadedStates_.size() <= 1); // Just empty root GameState |
---|
| 142 | // Destroy all GameStates (shared_ptrs take care of actual destruction) |
---|
| 143 | constructedStates_.clear(); |
---|
| 144 | |
---|
| 145 | GameStateFactory::getFactories().clear(); |
---|
| 146 | safeObjectDelete(&core_); |
---|
| 147 | safeObjectDelete(&gameClock_); |
---|
[8858] | 148 | |
---|
| 149 | orxout(internal_status) << "finished destroying Game object..." << endl; |
---|
[2817] | 150 | } |
---|
| 151 | |
---|
[6417] | 152 | void Game::setConfigValues() |
---|
| 153 | { |
---|
| 154 | SetConfigValue(statisticsRefreshCycle_, 250000) |
---|
| 155 | .description("Sets the time in microseconds interval at which average fps, etc. get updated."); |
---|
| 156 | SetConfigValue(statisticsAvgLength_, 1000000) |
---|
| 157 | .description("Sets the time in microseconds interval at which average fps, etc. gets calculated."); |
---|
[8079] | 158 | |
---|
| 159 | SetConfigValueExternal(fpsLimit_, "GraphicsSettings", "fpsLimit", 50) |
---|
[6417] | 160 | .description("Sets the desired frame rate (0 for no limit)."); |
---|
| 161 | } |
---|
| 162 | |
---|
[2805] | 163 | /** |
---|
| 164 | @brief |
---|
| 165 | Main loop of the orxonox game. |
---|
| 166 | @note |
---|
| 167 | We use the Ogre::Timer to measure time since it uses the most precise |
---|
| 168 | method an any platform (however the windows timer lacks time when under |
---|
| 169 | heavy kernel load!). |
---|
| 170 | */ |
---|
| 171 | void Game::run() |
---|
| 172 | { |
---|
[3280] | 173 | if (this->requestedStateNodes_.empty()) |
---|
[8858] | 174 | orxout(user_error) << "Starting game without requesting GameState. This automatically terminates the program." << endl; |
---|
[2805] | 175 | |
---|
[8858] | 176 | // Update the GameState stack if required. We do this already here to have a properly initialized game before entering the main loop |
---|
| 177 | this->updateGameStateStack(); |
---|
| 178 | |
---|
| 179 | orxout(user_status) << "Game loaded" << endl; |
---|
| 180 | orxout(internal_status) << "--------------------------------------------------" << endl; |
---|
| 181 | orxout(internal_status) << "starting main loop..." << endl; |
---|
| 182 | |
---|
[2845] | 183 | // START GAME |
---|
[3304] | 184 | // first delta time should be about 0 seconds |
---|
| 185 | this->gameClock_->capture(); |
---|
| 186 | // A first item is required for the fps limiter |
---|
| 187 | StatisticsTickInfo tickInfo = {0, 0}; |
---|
| 188 | statisticsTickTimes_.push_back(tickInfo); |
---|
[3370] | 189 | while (!this->bAbort_ && (!this->loadedStates_.empty() || this->requestedStateNodes_.size() > 0)) |
---|
[2805] | 190 | { |
---|
[3370] | 191 | // Generate the dt |
---|
[2807] | 192 | this->gameClock_->capture(); |
---|
[2805] | 193 | |
---|
[3370] | 194 | // Statistics init |
---|
| 195 | StatisticsTickInfo tickInfo = {gameClock_->getMicroseconds(), 0}; |
---|
[2817] | 196 | statisticsTickTimes_.push_back(tickInfo); |
---|
| 197 | this->periodTime_ += this->gameClock_->getDeltaTimeMicroseconds(); |
---|
| 198 | |
---|
[3370] | 199 | // Update the GameState stack if required |
---|
| 200 | this->updateGameStateStack(); |
---|
| 201 | |
---|
[6417] | 202 | // Core preUpdate |
---|
[5695] | 203 | try |
---|
| 204 | { this->core_->preUpdate(*this->gameClock_); } |
---|
| 205 | catch (...) |
---|
[2844] | 206 | { |
---|
[8858] | 207 | orxout(user_error) << "An exception occurred in the Core preUpdate: " << Exception::handleMessage() << endl; |
---|
| 208 | orxout(user_error) << "This should really never happen! Closing the program." << endl; |
---|
[3370] | 209 | this->stop(); |
---|
| 210 | break; |
---|
[2844] | 211 | } |
---|
[2805] | 212 | |
---|
[3370] | 213 | // Update the GameStates bottom up in the stack |
---|
| 214 | this->updateGameStates(); |
---|
| 215 | |
---|
[6417] | 216 | // Core postUpdate |
---|
[5695] | 217 | try |
---|
| 218 | { this->core_->postUpdate(*this->gameClock_); } |
---|
| 219 | catch (...) |
---|
[3280] | 220 | { |
---|
[8858] | 221 | orxout(user_error) << "An exception occurred in the Core postUpdate: " << Exception::handleMessage() << endl; |
---|
| 222 | orxout(user_error) << "This should really never happen! Closing the program." << endl; |
---|
[3280] | 223 | this->stop(); |
---|
| 224 | break; |
---|
| 225 | } |
---|
| 226 | |
---|
[3370] | 227 | // Evaluate statistics |
---|
| 228 | this->updateStatistics(); |
---|
| 229 | |
---|
[6417] | 230 | // Limit frame rate |
---|
[8245] | 231 | static bool hasVSync = GameMode::showsGraphics() && GraphicsManager::getInstance().hasVSyncEnabled(); // can be static since changes of VSync currently require a restart |
---|
| 232 | if (this->fpsLimit_ > 0 && !hasVSync) |
---|
| 233 | this->updateFPSLimiter(); |
---|
[3370] | 234 | } |
---|
| 235 | |
---|
[8858] | 236 | orxout(internal_status) << "finished main loop" << endl; |
---|
| 237 | orxout(internal_status) << "--------------------------------------------------" << endl; |
---|
| 238 | |
---|
[3370] | 239 | // UNLOAD all remaining states |
---|
| 240 | while (this->loadedStates_.size() > 1) |
---|
| 241 | this->unloadState(this->loadedStates_.back()->getName()); |
---|
| 242 | this->loadedTopStateNode_ = this->rootStateNode_; |
---|
| 243 | this->requestedStateNodes_.clear(); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void Game::updateGameStateStack() |
---|
| 247 | { |
---|
| 248 | while (this->requestedStateNodes_.size() > 0) |
---|
| 249 | { |
---|
| 250 | shared_ptr<GameStateTreeNode> requestedStateNode = this->requestedStateNodes_.front(); |
---|
| 251 | assert(this->loadedTopStateNode_); |
---|
| 252 | if (!this->loadedTopStateNode_->parent_.expired() && requestedStateNode == this->loadedTopStateNode_->parent_.lock()) |
---|
| 253 | this->unloadState(loadedTopStateNode_->name_); |
---|
| 254 | else // has to be child |
---|
[3084] | 255 | { |
---|
[3280] | 256 | try |
---|
| 257 | { |
---|
[3370] | 258 | this->loadState(requestedStateNode->name_); |
---|
[3280] | 259 | } |
---|
[5695] | 260 | catch (...) |
---|
[3280] | 261 | { |
---|
[8858] | 262 | orxout(user_error) << "Loading GameState '" << requestedStateNode->name_ << "' failed: " << Exception::handleMessage() << endl; |
---|
[3370] | 263 | // All scheduled operations have now been rendered inert --> flush them and issue a warning |
---|
| 264 | if (this->requestedStateNodes_.size() > 1) |
---|
[8858] | 265 | orxout(internal_info) << "All " << this->requestedStateNodes_.size() - 1 << " scheduled transitions have been ignored." << endl; |
---|
[3370] | 266 | this->requestedStateNodes_.clear(); |
---|
[3280] | 267 | break; |
---|
| 268 | } |
---|
[3370] | 269 | } |
---|
| 270 | this->loadedTopStateNode_ = requestedStateNode; |
---|
| 271 | this->requestedStateNodes_.erase(this->requestedStateNodes_.begin()); |
---|
| 272 | } |
---|
| 273 | } |
---|
[2844] | 274 | |
---|
[3370] | 275 | void Game::updateGameStates() |
---|
| 276 | { |
---|
| 277 | // Note: The first element is the empty root state, which doesn't need ticking |
---|
| 278 | for (GameStateVector::const_iterator it = this->loadedStates_.begin() + 1; |
---|
| 279 | it != this->loadedStates_.end(); ++it) |
---|
| 280 | { |
---|
| 281 | try |
---|
| 282 | { |
---|
| 283 | // Add tick time for most of the states |
---|
[5695] | 284 | uint64_t timeBeforeTick = 0; |
---|
[3370] | 285 | if ((*it)->getInfo().bIgnoreTickTime) |
---|
| 286 | timeBeforeTick = this->gameClock_->getRealMicroseconds(); |
---|
| 287 | (*it)->update(*this->gameClock_); |
---|
| 288 | if ((*it)->getInfo().bIgnoreTickTime) |
---|
| 289 | this->subtractTickTime(static_cast<int32_t>(this->gameClock_->getRealMicroseconds() - timeBeforeTick)); |
---|
[3084] | 290 | } |
---|
[3370] | 291 | catch (...) |
---|
| 292 | { |
---|
[8858] | 293 | orxout(user_error) << "An exception occurred while updating '" << (*it)->getName() << "': " << Exception::handleMessage() << endl; |
---|
| 294 | orxout(user_error) << "This should really never happen!" << endl; |
---|
| 295 | orxout(user_error) << "Unloading all GameStates depending on the one that crashed." << endl; |
---|
[3370] | 296 | shared_ptr<GameStateTreeNode> current = this->loadedTopStateNode_; |
---|
| 297 | while (current->name_ != (*it)->getName() && current) |
---|
| 298 | current = current->parent_.lock(); |
---|
| 299 | if (current && current->parent_.lock()) |
---|
| 300 | this->requestState(current->parent_.lock()->name_); |
---|
| 301 | else |
---|
| 302 | this->stop(); |
---|
| 303 | break; |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | } |
---|
[3084] | 307 | |
---|
[3370] | 308 | void Game::updateStatistics() |
---|
| 309 | { |
---|
| 310 | // Add the tick time of this frame (rendering time has already been subtracted) |
---|
| 311 | uint64_t currentTime = gameClock_->getMicroseconds(); |
---|
| 312 | uint64_t currentRealTime = gameClock_->getRealMicroseconds(); |
---|
[6502] | 313 | this->statisticsTickTimes_.back().tickLength += (uint32_t)(currentRealTime - currentTime); |
---|
| 314 | this->periodTickTime_ += (uint32_t)(currentRealTime - currentTime); |
---|
[6417] | 315 | if (this->periodTime_ > this->statisticsRefreshCycle_) |
---|
[3370] | 316 | { |
---|
| 317 | std::list<StatisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin(); |
---|
| 318 | assert(it != this->statisticsTickTimes_.end()); |
---|
[6417] | 319 | int64_t lastTime = currentTime - this->statisticsAvgLength_; |
---|
[3370] | 320 | if (static_cast<int64_t>(it->tickTime) < lastTime) |
---|
[2817] | 321 | { |
---|
[3370] | 322 | do |
---|
[2817] | 323 | { |
---|
[3370] | 324 | assert(this->periodTickTime_ >= it->tickLength); |
---|
| 325 | this->periodTickTime_ -= it->tickLength; |
---|
| 326 | ++it; |
---|
| 327 | assert(it != this->statisticsTickTimes_.end()); |
---|
| 328 | } while (static_cast<int64_t>(it->tickTime) < lastTime); |
---|
| 329 | this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it); |
---|
| 330 | } |
---|
[2817] | 331 | |
---|
[3370] | 332 | uint32_t framesPerPeriod = this->statisticsTickTimes_.size(); |
---|
[6417] | 333 | // Why minus 1? No idea, but otherwise the fps rate is always (from 10 to 200!) one frame too low |
---|
| 334 | this->avgFPS_ = -1 + static_cast<float>(framesPerPeriod) / (currentTime - this->statisticsTickTimes_.front().tickTime) * 1000000.0f; |
---|
[3370] | 335 | this->avgTickTime_ = static_cast<float>(this->periodTickTime_) / framesPerPeriod / 1000.0f; |
---|
[2817] | 336 | |
---|
[6417] | 337 | this->periodTime_ -= this->statisticsRefreshCycle_; |
---|
[2805] | 338 | } |
---|
[3370] | 339 | } |
---|
[2805] | 340 | |
---|
[3370] | 341 | void Game::updateFPSLimiter() |
---|
| 342 | { |
---|
[6417] | 343 | uint64_t nextTime = gameClock_->getMicroseconds() - excessSleepTime_ + static_cast<uint32_t>(1000000.0f / fpsLimit_); |
---|
[3370] | 344 | uint64_t currentRealTime = gameClock_->getRealMicroseconds(); |
---|
| 345 | while (currentRealTime < nextTime - minimumSleepTime_) |
---|
| 346 | { |
---|
[6502] | 347 | usleep((unsigned long)(nextTime - currentRealTime)); |
---|
[3370] | 348 | currentRealTime = gameClock_->getRealMicroseconds(); |
---|
| 349 | } |
---|
| 350 | // Integrate excess to avoid steady state error |
---|
[6502] | 351 | excessSleepTime_ = (int)(currentRealTime - nextTime); |
---|
[3370] | 352 | // Anti windup |
---|
| 353 | if (excessSleepTime_ > 50000) // 20ms is about the maximum time Windows would sleep for too long |
---|
| 354 | excessSleepTime_ = 50000; |
---|
[2805] | 355 | } |
---|
| 356 | |
---|
| 357 | void Game::stop() |
---|
| 358 | { |
---|
[8858] | 359 | orxout(user_status) << "Exit" << endl; |
---|
[3280] | 360 | this->bAbort_ = true; |
---|
[2805] | 361 | } |
---|
[2817] | 362 | |
---|
[3370] | 363 | void Game::subtractTickTime(int32_t length) |
---|
[2817] | 364 | { |
---|
| 365 | assert(!this->statisticsTickTimes_.empty()); |
---|
[3370] | 366 | this->statisticsTickTimes_.back().tickLength -= length; |
---|
| 367 | this->periodTickTime_ -= length; |
---|
[2817] | 368 | } |
---|
[2844] | 369 | |
---|
| 370 | |
---|
| 371 | /***** GameState related *****/ |
---|
| 372 | |
---|
| 373 | void Game::requestState(const std::string& name) |
---|
| 374 | { |
---|
[3370] | 375 | if (!this->checkState(name)) |
---|
| 376 | { |
---|
[8858] | 377 | orxout(user_warning) << "GameState named '" << name << "' doesn't exist!" << endl; |
---|
[2844] | 378 | return; |
---|
[3370] | 379 | } |
---|
[2844] | 380 | |
---|
[3370] | 381 | if (this->bChangingState_) |
---|
| 382 | { |
---|
[8858] | 383 | orxout(user_warning) << "Requesting GameStates while loading/unloading a GameState is illegal! Ignoring." << endl; |
---|
[3370] | 384 | return; |
---|
| 385 | } |
---|
[2844] | 386 | |
---|
[3280] | 387 | shared_ptr<GameStateTreeNode> lastRequestedNode; |
---|
| 388 | if (this->requestedStateNodes_.empty()) |
---|
[3370] | 389 | lastRequestedNode = this->loadedTopStateNode_; |
---|
[3280] | 390 | else |
---|
| 391 | lastRequestedNode = this->requestedStateNodes_.back(); |
---|
[3370] | 392 | if (name == lastRequestedNode->name_) |
---|
[2844] | 393 | { |
---|
[8858] | 394 | orxout(user_warning) << "Requesting the currently active state! Ignoring." << endl; |
---|
[2844] | 395 | return; |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | // Check children first |
---|
[3280] | 399 | std::vector<shared_ptr<GameStateTreeNode> > requestedNodes; |
---|
[2844] | 400 | for (unsigned int i = 0; i < lastRequestedNode->children_.size(); ++i) |
---|
| 401 | { |
---|
[3370] | 402 | if (lastRequestedNode->children_[i]->name_ == name) |
---|
[2844] | 403 | { |
---|
[3280] | 404 | requestedNodes.push_back(lastRequestedNode->children_[i]); |
---|
[2844] | 405 | break; |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
[3280] | 409 | if (requestedNodes.empty()) |
---|
[2844] | 410 | { |
---|
[3280] | 411 | // Check parent and all its grand parents |
---|
| 412 | shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode; |
---|
| 413 | while (currentNode != NULL) |
---|
| 414 | { |
---|
[3370] | 415 | if (currentNode->name_ == name) |
---|
[3280] | 416 | break; |
---|
| 417 | currentNode = currentNode->parent_.lock(); |
---|
| 418 | requestedNodes.push_back(currentNode); |
---|
| 419 | } |
---|
[5929] | 420 | if (currentNode == NULL) |
---|
| 421 | requestedNodes.clear(); |
---|
[2844] | 422 | } |
---|
| 423 | |
---|
[3280] | 424 | if (requestedNodes.empty()) |
---|
[8858] | 425 | orxout(user_error) << "Requested GameState transition is not allowed. Ignoring." << endl; |
---|
[2844] | 426 | else |
---|
[3280] | 427 | this->requestedStateNodes_.insert(requestedStateNodes_.end(), requestedNodes.begin(), requestedNodes.end()); |
---|
[2844] | 428 | } |
---|
| 429 | |
---|
[2850] | 430 | void Game::requestStates(const std::string& names) |
---|
| 431 | { |
---|
| 432 | SubString tokens(names, ",;", " "); |
---|
| 433 | for (unsigned int i = 0; i < tokens.size(); ++i) |
---|
| 434 | this->requestState(tokens[i]); |
---|
| 435 | } |
---|
| 436 | |
---|
[2844] | 437 | void Game::popState() |
---|
| 438 | { |
---|
[3280] | 439 | shared_ptr<GameStateTreeNode> lastRequestedNode; |
---|
| 440 | if (this->requestedStateNodes_.empty()) |
---|
[3370] | 441 | lastRequestedNode = this->loadedTopStateNode_; |
---|
[2844] | 442 | else |
---|
[3280] | 443 | lastRequestedNode = this->requestedStateNodes_.back(); |
---|
| 444 | if (lastRequestedNode != this->rootStateNode_) |
---|
[3370] | 445 | this->requestState(lastRequestedNode->parent_.lock()->name_); |
---|
[3280] | 446 | else |
---|
[8858] | 447 | orxout(internal_warning) << "Can't pop the internal dummy root GameState" << endl; |
---|
[2844] | 448 | } |
---|
| 449 | |
---|
[3370] | 450 | shared_ptr<GameState> Game::getState(const std::string& name) |
---|
[2844] | 451 | { |
---|
[3370] | 452 | GameStateMap::const_iterator it = constructedStates_.find(name); |
---|
| 453 | if (it != constructedStates_.end()) |
---|
[2844] | 454 | return it->second; |
---|
| 455 | else |
---|
| 456 | { |
---|
[3370] | 457 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(name); |
---|
| 458 | if (it != gameStateDeclarations_s.end()) |
---|
[8858] | 459 | orxout(internal_error) << "GameState '" << name << "' has not yet been loaded." << endl; |
---|
[3370] | 460 | else |
---|
[8858] | 461 | orxout(internal_error) << "Could not find GameState '" << name << "'." << endl; |
---|
[3370] | 462 | return shared_ptr<GameState>(); |
---|
[2844] | 463 | } |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | void Game::setStateHierarchy(const std::string& str) |
---|
| 467 | { |
---|
| 468 | // Split string into pieces of the form whitespacesText |
---|
[5929] | 469 | std::vector<std::pair<std::string, int> > stateStrings; |
---|
[2844] | 470 | size_t pos = 0; |
---|
| 471 | size_t startPos = 0; |
---|
| 472 | while (pos < str.size()) |
---|
| 473 | { |
---|
[5929] | 474 | int indentation = 0; |
---|
[6422] | 475 | while (pos < str.size() && str[pos] == ' ') |
---|
[2844] | 476 | ++indentation, ++pos; |
---|
| 477 | startPos = pos; |
---|
[6422] | 478 | while (pos < str.size() && str[pos] != ' ') |
---|
[2844] | 479 | ++pos; |
---|
[3280] | 480 | stateStrings.push_back(std::make_pair(str.substr(startPos, pos - startPos), indentation)); |
---|
[2844] | 481 | } |
---|
[5929] | 482 | if (stateStrings.empty()) |
---|
| 483 | ThrowException(GameState, "Emtpy GameState hierarchy provided, terminating."); |
---|
| 484 | // Add element with large identation to detect the last with just an iterator |
---|
[8351] | 485 | stateStrings.push_back(std::make_pair(std::string(), -1)); |
---|
[5929] | 486 | |
---|
| 487 | // Parse elements recursively |
---|
| 488 | std::vector<std::pair<std::string, int> >::const_iterator begin = stateStrings.begin(); |
---|
| 489 | parseStates(begin, this->rootStateNode_); |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | /*** Internal ***/ |
---|
| 493 | |
---|
| 494 | void Game::parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode) |
---|
| 495 | { |
---|
| 496 | SubString tokens(it->first, ","); |
---|
| 497 | std::vector<std::pair<std::string, int> >::const_iterator startIt = it; |
---|
| 498 | |
---|
| 499 | for (unsigned int i = 0; i < tokens.size(); ++i) |
---|
[2844] | 500 | { |
---|
[5929] | 501 | it = startIt; // Reset iterator to the beginning of the sub tree |
---|
| 502 | if (!this->checkState(tokens[i])) |
---|
| 503 | ThrowException(GameState, "GameState with name '" << tokens[i] << "' not found!"); |
---|
| 504 | if (tokens[i] == this->rootStateNode_->name_) |
---|
[3280] | 505 | ThrowException(GameState, "You shouldn't use 'emptyRootGameState' in the hierarchy..."); |
---|
[5929] | 506 | shared_ptr<GameStateTreeNode> node(new GameStateTreeNode()); |
---|
| 507 | node->name_ = tokens[i]; |
---|
| 508 | node->parent_ = currentNode; |
---|
| 509 | currentNode->children_.push_back(node); |
---|
[3280] | 510 | |
---|
[5929] | 511 | int currentLevel = it->second; |
---|
| 512 | ++it; |
---|
| 513 | while (it->second != -1) |
---|
[2844] | 514 | { |
---|
[5929] | 515 | if (it->second <= currentLevel) |
---|
| 516 | break; |
---|
| 517 | else if (it->second == currentLevel + 1) |
---|
| 518 | parseStates(it, node); |
---|
| 519 | else |
---|
| 520 | ThrowException(GameState, "Indentation error while parsing the hierarchy."); |
---|
[2844] | 521 | } |
---|
| 522 | } |
---|
| 523 | } |
---|
| 524 | |
---|
[3370] | 525 | void Game::loadGraphics() |
---|
[2844] | 526 | { |
---|
[5929] | 527 | if (!GameMode::showsGraphics()) |
---|
[5781] | 528 | { |
---|
[8858] | 529 | orxout(user_status) << "Loading graphics" << endl; |
---|
| 530 | orxout(internal_info) << "loading graphics in Game" << endl; |
---|
| 531 | |
---|
[5781] | 532 | core_->loadGraphics(); |
---|
| 533 | Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics); |
---|
| 534 | |
---|
| 535 | // Construct all the GameStates that require graphics |
---|
| 536 | for (std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.begin(); |
---|
| 537 | it != gameStateDeclarations_s.end(); ++it) |
---|
| 538 | { |
---|
| 539 | if (it->second.bGraphicsMode) |
---|
| 540 | { |
---|
| 541 | // Game state loading failure is serious --> don't catch |
---|
| 542 | shared_ptr<GameState> gameState = GameStateFactory::fabricate(it->second); |
---|
| 543 | if (!constructedStates_.insert(std::make_pair( |
---|
| 544 | it->second.stateName, gameState)).second) |
---|
| 545 | assert(false); // GameState was already created! |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | graphicsUnloader.Dismiss(); |
---|
[8858] | 549 | |
---|
| 550 | orxout(internal_info) << "finished loading graphics in Game" << endl; |
---|
[5781] | 551 | } |
---|
[3370] | 552 | } |
---|
| 553 | |
---|
| 554 | void Game::unloadGraphics() |
---|
| 555 | { |
---|
[5929] | 556 | if (GameMode::showsGraphics()) |
---|
[5781] | 557 | { |
---|
[8858] | 558 | orxout(user_status) << "Unloading graphics" << endl; |
---|
| 559 | orxout(internal_info) << "unloading graphics in Game" << endl; |
---|
| 560 | |
---|
[5781] | 561 | // Destroy all the GameStates that require graphics |
---|
| 562 | for (GameStateMap::iterator it = constructedStates_.begin(); it != constructedStates_.end();) |
---|
| 563 | { |
---|
| 564 | if (it->second->getInfo().bGraphicsMode) |
---|
| 565 | constructedStates_.erase(it++); |
---|
| 566 | else |
---|
| 567 | ++it; |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | core_->unloadGraphics(); |
---|
| 571 | } |
---|
[3370] | 572 | } |
---|
| 573 | |
---|
| 574 | bool Game::checkState(const std::string& name) const |
---|
| 575 | { |
---|
| 576 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(name); |
---|
| 577 | if (it == gameStateDeclarations_s.end()) |
---|
| 578 | return false; |
---|
| 579 | else |
---|
| 580 | return true; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | void Game::loadState(const std::string& name) |
---|
| 584 | { |
---|
[8858] | 585 | orxout(internal_status) << "loading state '" << name << "'" << endl; |
---|
| 586 | |
---|
[3280] | 587 | this->bChangingState_ = true; |
---|
[8706] | 588 | LOKI_ON_BLOCK_EXIT_OBJ(*this, &Game::resetChangingState); (void)LOKI_ANONYMOUS_VARIABLE(scopeGuard); |
---|
[3370] | 589 | |
---|
| 590 | // If state requires graphics, load it |
---|
| 591 | Loki::ScopeGuard graphicsUnloader = Loki::MakeObjGuard(*this, &Game::unloadGraphics); |
---|
[5781] | 592 | if (gameStateDeclarations_s[name].bGraphicsMode && !GameMode::showsGraphics()) |
---|
[3370] | 593 | this->loadGraphics(); |
---|
| 594 | else |
---|
| 595 | graphicsUnloader.Dismiss(); |
---|
| 596 | |
---|
| 597 | shared_ptr<GameState> state = this->getState(name); |
---|
[6417] | 598 | state->activateInternal(); |
---|
[3370] | 599 | if (!this->loadedStates_.empty()) |
---|
| 600 | this->loadedStates_.back()->activity_.topState = false; |
---|
| 601 | this->loadedStates_.push_back(state); |
---|
[2850] | 602 | state->activity_.topState = true; |
---|
[3370] | 603 | |
---|
| 604 | graphicsUnloader.Dismiss(); |
---|
[2844] | 605 | } |
---|
| 606 | |
---|
[3370] | 607 | void Game::unloadState(const std::string& name) |
---|
[2844] | 608 | { |
---|
[8858] | 609 | orxout(internal_status) << "unloading state '" << name << "'" << endl; |
---|
| 610 | |
---|
[3280] | 611 | this->bChangingState_ = true; |
---|
| 612 | try |
---|
| 613 | { |
---|
[3370] | 614 | shared_ptr<GameState> state = this->getState(name); |
---|
| 615 | state->activity_.topState = false; |
---|
| 616 | this->loadedStates_.pop_back(); |
---|
| 617 | if (!this->loadedStates_.empty()) |
---|
| 618 | this->loadedStates_.back()->activity_.topState = true; |
---|
[6417] | 619 | state->deactivateInternal(); |
---|
[3280] | 620 | } |
---|
[5695] | 621 | catch (...) |
---|
[3280] | 622 | { |
---|
[8858] | 623 | orxout(internal_warning) << "Unloading GameState '" << name << "' threw an exception: " << Exception::handleMessage() << endl; |
---|
| 624 | orxout(internal_warning) << "There might be potential resource leaks involved! To avoid this, improve exception-safety." << endl; |
---|
[3280] | 625 | } |
---|
[3370] | 626 | // Check if graphics is still required |
---|
[5695] | 627 | if (!bAbort_) |
---|
| 628 | { |
---|
| 629 | bool graphicsRequired = false; |
---|
| 630 | for (unsigned i = 0; i < loadedStates_.size(); ++i) |
---|
| 631 | graphicsRequired |= loadedStates_[i]->getInfo().bGraphicsMode; |
---|
| 632 | if (!graphicsRequired) |
---|
| 633 | this->unloadGraphics(); |
---|
| 634 | } |
---|
[3280] | 635 | this->bChangingState_ = false; |
---|
[2844] | 636 | } |
---|
| 637 | |
---|
[5929] | 638 | /*static*/ std::map<std::string, shared_ptr<Game::GameStateFactory> >& Game::GameStateFactory::getFactories() |
---|
| 639 | { |
---|
| 640 | static std::map<std::string, shared_ptr<GameStateFactory> > factories; |
---|
| 641 | return factories; |
---|
| 642 | } |
---|
[3280] | 643 | |
---|
[3370] | 644 | /*static*/ shared_ptr<GameState> Game::GameStateFactory::fabricate(const GameStateInfo& info) |
---|
[2844] | 645 | { |
---|
[5929] | 646 | std::map<std::string, shared_ptr<Game::GameStateFactory> >::const_iterator it = getFactories().find(info.className); |
---|
| 647 | assert(it != getFactories().end()); |
---|
[3370] | 648 | return it->second->fabricateInternal(info); |
---|
[2844] | 649 | } |
---|
[2805] | 650 | } |
---|