| 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 "OrxonoxStableHeaders.h" |
|---|
| 36 | #include "Game.h" |
|---|
| 37 | |
|---|
| 38 | #include <exception> |
|---|
| 39 | #include <cassert> |
|---|
| 40 | |
|---|
| 41 | #include "util/Debug.h" |
|---|
| 42 | #include "util/Exception.h" |
|---|
| 43 | #include "core/CommandLine.h" |
|---|
| 44 | #include "core/ConsoleCommand.h" |
|---|
| 45 | #include "core/Core.h" |
|---|
| 46 | #include "core/Identifier.h" |
|---|
| 47 | |
|---|
| 48 | #include "gamestates/GSRoot.h" |
|---|
| 49 | #include "gamestates/GSGraphics.h" |
|---|
| 50 | #include "gamestates/GSStandalone.h" |
|---|
| 51 | #include "gamestates/GSServer.h" |
|---|
| 52 | #include "gamestates/GSClient.h" |
|---|
| 53 | #include "gamestates/GSDedicated.h" |
|---|
| 54 | #include "gamestates/GSGUI.h" |
|---|
| 55 | #include "gamestates/GSIOConsole.h" |
|---|
| 56 | |
|---|
| 57 | /* |
|---|
| 58 | @brief |
|---|
| 59 | Main method. Game starts here (except for static initialisations). |
|---|
| 60 | */ |
|---|
| 61 | int main(int argc, char** argv) |
|---|
| 62 | { |
|---|
| 63 | orxonox::Game orxonox(argc, argv); |
|---|
| 64 | orxonox.run(); |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | namespace orxonox |
|---|
| 69 | { |
|---|
| 70 | void stop_game() |
|---|
| 71 | { |
|---|
| 72 | Game::getInstance().stop(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | SetCommandLineArgument(state, "gui").shortcut("s"); |
|---|
| 76 | SetConsoleCommandShortcutExternAlias(stop_game, "exit"); |
|---|
| 77 | |
|---|
| 78 | Game* Game::singletonRef_s = 0; |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | @brief |
|---|
| 82 | Non-initialising constructor. |
|---|
| 83 | */ |
|---|
| 84 | Game::Game(int argc, char** argv) |
|---|
| 85 | { |
|---|
| 86 | assert(singletonRef_s == 0); |
|---|
| 87 | singletonRef_s = this; |
|---|
| 88 | |
|---|
| 89 | this->abort_ = false; |
|---|
| 90 | |
|---|
| 91 | this->core_ = new orxonox::Core(argc, argv); |
|---|
| 92 | if (!this->core_->isLoaded()) |
|---|
| 93 | { |
|---|
| 94 | COUT(0) << "Core was not fully loaded, probably an exception occurred during consruction. Aborting" << std::endl; |
|---|
| 95 | abort(); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | @brief |
|---|
| 101 | */ |
|---|
| 102 | Game::~Game() |
|---|
| 103 | { |
|---|
| 104 | // Destroy pretty much everyhting left |
|---|
| 105 | delete this->core_; |
|---|
| 106 | |
|---|
| 107 | // Clean up class hierarchy stuff (identifiers, xmlport, configvalue, consolecommand) |
|---|
| 108 | // Needs to be done after 'delete core' because of ~OrxonoxClass |
|---|
| 109 | orxonox::Identifier::destroyAllIdentifiers(); |
|---|
| 110 | |
|---|
| 111 | assert(singletonRef_s); |
|---|
| 112 | singletonRef_s = 0; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | @brief |
|---|
| 117 | Main loop of the orxonox game. |
|---|
| 118 | @note |
|---|
| 119 | We use the Ogre::Timer to measure time since it uses the most precise |
|---|
| 120 | method an any platform (however the windows timer lacks time when under |
|---|
| 121 | heavy kernel load!). |
|---|
| 122 | */ |
|---|
| 123 | void Game::run() |
|---|
| 124 | { |
|---|
| 125 | // create the gamestates |
|---|
| 126 | GSRoot root; |
|---|
| 127 | GSGraphics graphics; |
|---|
| 128 | GSStandalone standalone; |
|---|
| 129 | GSServer server; |
|---|
| 130 | GSClient client; |
|---|
| 131 | GSDedicated dedicated; |
|---|
| 132 | GSGUI gui; |
|---|
| 133 | GSIOConsole ioConsole; |
|---|
| 134 | |
|---|
| 135 | // make the hierarchy |
|---|
| 136 | root.addChild(&graphics); |
|---|
| 137 | graphics.addChild(&standalone); |
|---|
| 138 | graphics.addChild(&server); |
|---|
| 139 | graphics.addChild(&client); |
|---|
| 140 | graphics.addChild(&gui); |
|---|
| 141 | root.addChild(&ioConsole); |
|---|
| 142 | root.addChild(&dedicated); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | // start global orxonox time |
|---|
| 146 | Clock clock; |
|---|
| 147 | |
|---|
| 148 | root.activate(); |
|---|
| 149 | |
|---|
| 150 | // get initial state from command line |
|---|
| 151 | root.gotoState(CommandLine::getValue("state")); |
|---|
| 152 | |
|---|
| 153 | while (!this->abort_) |
|---|
| 154 | { |
|---|
| 155 | clock.capture(); |
|---|
| 156 | |
|---|
| 157 | root.tick(clock); |
|---|
| 158 | |
|---|
| 159 | if (root.stateRequest_ != "") |
|---|
| 160 | root.gotoState(root.stateRequest_); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | root.gotoState("root"); |
|---|
| 164 | root.deactivate(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void Game::stop() |
|---|
| 168 | { |
|---|
| 169 | this->abort_ = true; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|