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