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