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