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