[1672] | 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 | #include "RootGameState.h" |
---|
| 30 | |
---|
[1755] | 31 | #include "util/Debug.h" |
---|
[1764] | 32 | #include "util/Exception.h" |
---|
[1755] | 33 | #include "Core.h" |
---|
[1674] | 34 | #include "Clock.h" |
---|
[1672] | 35 | #include "CommandLine.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[2087] | 39 | SetCommandLineArgument(state, "gui").shortcut("s"); |
---|
[1672] | 40 | |
---|
| 41 | RootGameState::RootGameState(const std::string& name) |
---|
[1689] | 42 | : GameState<GameStateBase>(name) |
---|
[1672] | 43 | , stateRequest_("") |
---|
| 44 | { |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | RootGameState::~RootGameState() |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Internal method that actually makes the state transition. Since it is internal, |
---|
| 54 | the method can assume certain things to be granted (like 'this' is always active). |
---|
| 55 | */ |
---|
[1689] | 56 | void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination) |
---|
[1672] | 57 | { |
---|
| 58 | if (source != 0) |
---|
| 59 | { |
---|
| 60 | // transition was not initiated by root itself |
---|
| 61 | this->activeChild_ = 0; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if (destination == this) |
---|
| 65 | { |
---|
| 66 | // this marks the end of the game. |
---|
| 67 | return; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // Check for 'destination' in the children map first |
---|
[1689] | 71 | std::map<GameStateBase*, GameStateBase*>::const_iterator it |
---|
[1672] | 72 | = this->grandchildrenToChildren_.find(destination); |
---|
| 73 | if (it != this->grandchildrenToChildren_.end()) |
---|
| 74 | { |
---|
[1689] | 75 | OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0, |
---|
[1672] | 76 | "There was a mix with RootGameState and GameState, could not cast."); |
---|
[1689] | 77 | GameStateBase* child = static_cast<GameStateBase*>(it->second); |
---|
[1672] | 78 | // child state. Don't use 'state', might be a grandchild! |
---|
| 79 | this->activeChild_ = child; |
---|
| 80 | child->makeTransition(this, destination); |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | // root doesn't have a parent.. |
---|
| 85 | OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root."); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void RootGameState::gotoState(const std::string& name) |
---|
| 90 | { |
---|
[1689] | 91 | GameStateBase* request = getState(name); |
---|
[1672] | 92 | if (request) |
---|
| 93 | { |
---|
[1689] | 94 | GameStateBase* current = getCurrentState(); |
---|
[1672] | 95 | if (current) |
---|
| 96 | { |
---|
| 97 | current->makeTransition(0, request); |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
| 101 | // Root is not yet active. This is a violation. |
---|
| 102 | ThrowException(GameState, "Activate Root before requesting a state."); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | @brief |
---|
| 113 | Makes a state transition according to the state tree. You can choose any state |
---|
| 114 | in the tree to do the call. The function finds the current state on its own. |
---|
| 115 | @param state |
---|
| 116 | The state to be entered, has to exist in the tree. |
---|
| 117 | */ |
---|
| 118 | void RootGameState::requestState(const std::string& name) |
---|
| 119 | { |
---|
| 120 | this->stateRequest_ = name; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | @brief |
---|
| 125 | Starts the game. The little 'while' denotes the main loop. |
---|
| 126 | Whenever the root state is selected, the game ends. |
---|
| 127 | @param name |
---|
| 128 | State to start with (usually main menu or specified by command line) |
---|
| 129 | */ |
---|
[2103] | 130 | void RootGameState::start() |
---|
[1672] | 131 | { |
---|
[2305] | 132 | // Don't catch errors when having a debugger in msvc |
---|
| 133 | #if ORXONOX_COMPILER != ORXONOX_COMPILER_MSVC || defined(NDEBUG) |
---|
[1802] | 134 | try |
---|
| 135 | { |
---|
[1824] | 136 | #endif |
---|
[1802] | 137 | // start global orxonox time |
---|
| 138 | Clock clock; |
---|
[1695] | 139 | |
---|
[1802] | 140 | // create the Core settings to configure the output level |
---|
| 141 | Core::getInstance(); |
---|
[1756] | 142 | |
---|
[1802] | 143 | this->activate(); |
---|
[1672] | 144 | |
---|
[1802] | 145 | // get initial state from command line |
---|
[2087] | 146 | gotoState(CommandLine::getValue("state")); |
---|
[1672] | 147 | |
---|
[1802] | 148 | while (this->activeChild_) |
---|
| 149 | { |
---|
| 150 | clock.capture(); |
---|
[1672] | 151 | |
---|
[1802] | 152 | this->tick(clock); |
---|
[1672] | 153 | |
---|
[1802] | 154 | if (this->stateRequest_ != "") |
---|
| 155 | gotoState(stateRequest_); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | this->deactivate(); |
---|
[2305] | 159 | #if ORXONOX_COMPILER != ORXONOX_COMPILER_MSVC || defined(NDEBUG) |
---|
[1672] | 160 | } |
---|
[1802] | 161 | // Note: These are all unhandled exceptions that should not have made its way here! |
---|
| 162 | // almost complete game catch block to display the messages appropriately. |
---|
| 163 | catch (std::exception& ex) |
---|
| 164 | { |
---|
[2305] | 165 | COUT(0) << ex.what() << std::endl; |
---|
| 166 | COUT(0) << "Program aborted." << std::endl; |
---|
| 167 | abort(); |
---|
[1802] | 168 | } |
---|
| 169 | // anything that doesn't inherit from std::exception |
---|
| 170 | catch (...) |
---|
| 171 | { |
---|
[2305] | 172 | COUT(0) << "An unidentifiable exception has occured. Program aborted." << std::endl; |
---|
| 173 | abort(); |
---|
[1802] | 174 | } |
---|
[1824] | 175 | #endif |
---|
[1672] | 176 | } |
---|
| 177 | } |
---|