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