| [1038] | 1 | /* | 
|---|
| [1056] | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
| [1038] | 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 |  *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | /** | 
|---|
 | 30 |  @file | 
|---|
 | 31 |  @brief Orxonox Main Class | 
|---|
 | 32 |  */ | 
|---|
 | 33 |  | 
|---|
 | 34 | // Precompiled Headers | 
|---|
 | 35 | #include "OrxonoxStableHeaders.h" | 
|---|
| [1039] | 36 | #include "Orxonox.h" | 
|---|
| [1038] | 37 |  | 
|---|
 | 38 | //****** STD ******* | 
|---|
 | 39 | #include <deque> | 
|---|
 | 40 |  | 
|---|
 | 41 | //****** OGRE ****** | 
|---|
 | 42 | #include <OgreFrameListener.h> | 
|---|
 | 43 | #include <OgreOverlay.h> | 
|---|
 | 44 | #include <OgreOverlayManager.h> | 
|---|
 | 45 | #include <OgreRoot.h> | 
|---|
 | 46 | #include <OgreTimer.h> | 
|---|
 | 47 | #include <OgreWindowEventUtilities.h> | 
|---|
 | 48 |  | 
|---|
 | 49 | //***** ORXONOX **** | 
|---|
 | 50 | // util | 
|---|
 | 51 | //#include "util/Sleep.h" | 
|---|
 | 52 | #include "util/ArgReader.h" | 
|---|
| [1120] | 53 | #include "util/ExprParser.h" | 
|---|
| [1038] | 54 |  | 
|---|
| [1054] | 55 | // core | 
|---|
 | 56 | #include "core/ConfigFileManager.h" | 
|---|
| [1052] | 57 | #include "core/ConsoleCommand.h" | 
|---|
| [1038] | 58 | #include "core/Debug.h" | 
|---|
 | 59 | #include "core/Factory.h" | 
|---|
 | 60 | #include "core/Loader.h" | 
|---|
 | 61 | #include "core/Tickable.h" | 
|---|
| [1052] | 62 | #include "core/InputBuffer.h" | 
|---|
 | 63 | #include "core/InputManager.h" | 
|---|
| [1243] | 64 | #include "core/TclBind.h" | 
|---|
| [1038] | 65 |  | 
|---|
 | 66 | // audio | 
|---|
 | 67 | #include "audio/AudioManager.h" | 
|---|
 | 68 |  | 
|---|
 | 69 | // network | 
|---|
 | 70 | #include "network/Server.h" | 
|---|
 | 71 | #include "network/Client.h" | 
|---|
 | 72 |  | 
|---|
 | 73 | // objects and tools | 
|---|
 | 74 | #include "tools/Timer.h" | 
|---|
 | 75 | #include "hud/HUD.h" | 
|---|
| [1214] | 76 | #include "console/InGameConsole.h" | 
|---|
| [1038] | 77 |  | 
|---|
 | 78 | // FIXME: is this really file scope? | 
|---|
 | 79 | // globals for the server or client | 
|---|
 | 80 | network::Client *client_g; | 
|---|
 | 81 | network::Server *server_g; | 
|---|
 | 82 |  | 
|---|
 | 83 | namespace orxonox | 
|---|
| [1052] | 84 | { | 
|---|
| [1219] | 85 |   ConsoleCommandShortcut(Orxonox, exit, AccessLevel::None); | 
|---|
 | 86 |   ConsoleCommandShortcut(Orxonox, slomo, AccessLevel::Offline).setDefaultValue(0, 1.0); | 
|---|
 | 87 |   ConsoleCommandShortcut(Orxonox, setTimeFactor, AccessLevel::Offline).setDefaultValue(0, 1.0); | 
|---|
 | 88 |   ConsoleCommandShortcut(Orxonox, activateConsole, AccessLevel::None); | 
|---|
| [1052] | 89 |   class Testconsole : public InputBufferListener | 
|---|
 | 90 |   { | 
|---|
 | 91 |     public: | 
|---|
 | 92 |       Testconsole(InputBuffer* ib) : ib_(ib) {} | 
|---|
 | 93 |       void listen() const | 
|---|
 | 94 |       { | 
|---|
 | 95 |         std::cout << "> " << this->ib_->get() << std::endl; | 
|---|
 | 96 |       } | 
|---|
 | 97 |       void execute() const | 
|---|
 | 98 |       { | 
|---|
 | 99 |         std::cout << ">> " << this->ib_->get() << std::endl; | 
|---|
 | 100 |         if (!CommandExecutor::execute(this->ib_->get())) | 
|---|
 | 101 |           std::cout << "Error" << std::endl; | 
|---|
 | 102 |         this->ib_->clear(); | 
|---|
 | 103 |       } | 
|---|
 | 104 |       void hintandcomplete() const | 
|---|
 | 105 |       { | 
|---|
 | 106 |         std::cout << CommandExecutor::hint(this->ib_->get()) << std::endl; | 
|---|
 | 107 |         this->ib_->set(CommandExecutor::complete(this->ib_->get())); | 
|---|
 | 108 |       } | 
|---|
 | 109 |       void clear() const | 
|---|
 | 110 |       { | 
|---|
 | 111 |         this->ib_->clear(); | 
|---|
 | 112 |       } | 
|---|
 | 113 |       void removeLast() const | 
|---|
 | 114 |       { | 
|---|
 | 115 |         this->ib_->removeLast(); | 
|---|
 | 116 |       } | 
|---|
| [1084] | 117 |       void exit() const | 
|---|
 | 118 |       { | 
|---|
| [1219] | 119 |         InputManager::setInputState(InputManager::IS_NORMAL); | 
|---|
| [1084] | 120 |       } | 
|---|
| [1052] | 121 |  | 
|---|
 | 122 |     private: | 
|---|
 | 123 |       InputBuffer* ib_; | 
|---|
 | 124 |   }; | 
|---|
| [1120] | 125 |  | 
|---|
 | 126 |   class Calculator | 
|---|
 | 127 |   { | 
|---|
 | 128 |   public: | 
|---|
| [1214] | 129 |     static float calculate(const std::string& calculation) | 
|---|
| [1120] | 130 |     { | 
|---|
 | 131 |       ExprParser expr(calculation); | 
|---|
 | 132 |       if (expr.getSuccess()) | 
|---|
 | 133 |       { | 
|---|
 | 134 |         if (expr.getResult() == 42.0) | 
|---|
 | 135 |           std::cout << "Greetings from the restaurant at the end of the universe." << std::endl; | 
|---|
 | 136 |         // FIXME: insert modifier to display in full precision | 
|---|
 | 137 |         std::cout << "Result is: " << expr.getResult() << std::endl; | 
|---|
 | 138 |         if (expr.getRemains() != "") | 
|---|
 | 139 |           std::cout << "Warning: Expression could not be parsed to the end! Remains: '" | 
|---|
 | 140 |               << expr.getRemains() << "'" << std::endl; | 
|---|
| [1214] | 141 |         return expr.getResult(); | 
|---|
| [1120] | 142 |       } | 
|---|
 | 143 |       else | 
|---|
| [1214] | 144 |       { | 
|---|
| [1120] | 145 |         std::cout << "Cannot calculate expression: Parse error" << std::endl; | 
|---|
| [1214] | 146 |         return 0; | 
|---|
 | 147 |       } | 
|---|
| [1120] | 148 |     } | 
|---|
 | 149 |   }; | 
|---|
 | 150 |   ConsoleCommandShortcut(Calculator, calculate, AccessLevel::None); | 
|---|
| [1052] | 151 |  | 
|---|
| [1038] | 152 |   /** | 
|---|
 | 153 |     @brief Reference to the only instance of the class. | 
|---|
 | 154 |   */ | 
|---|
 | 155 |   Orxonox *Orxonox::singletonRef_s = 0; | 
|---|
 | 156 |  | 
|---|
 | 157 |   /** | 
|---|
| [1244] | 158 |    * Create a new instance of Orxonox. Avoid doing any actual work here. | 
|---|
| [1038] | 159 |    */ | 
|---|
| [1249] | 160 |   Orxonox::Orxonox() : | 
|---|
 | 161 |     ogre_(&GraphicsEngine::getSingleton()), | 
|---|
 | 162 |     //auMan_(0), | 
|---|
 | 163 |     timer_(0), | 
|---|
 | 164 |     // turn on frame smoothing by setting a value different from 0 | 
|---|
 | 165 |     frameSmoothingTime_(0.0f), | 
|---|
 | 166 |     orxonoxConsole_(0), | 
|---|
 | 167 |     orxonoxHUD_(0), | 
|---|
 | 168 |     bAbort_(false), | 
|---|
 | 169 |     timefactor_(1.0f), | 
|---|
 | 170 |     mode_(STANDALONE), | 
|---|
 | 171 |     serverIp_("") | 
|---|
| [1038] | 172 |   { | 
|---|
 | 173 |   } | 
|---|
 | 174 |  | 
|---|
 | 175 |   /** | 
|---|
| [1244] | 176 |    * Destruct Orxonox. | 
|---|
| [1038] | 177 |    */ | 
|---|
 | 178 |   Orxonox::~Orxonox() | 
|---|
 | 179 |   { | 
|---|
 | 180 |     // keep in mind: the order of deletion is very important! | 
|---|
 | 181 |     if (this->orxonoxHUD_) | 
|---|
 | 182 |       delete this->orxonoxHUD_; | 
|---|
 | 183 |     Loader::close(); | 
|---|
| [1219] | 184 |     InputManager::destroy(); | 
|---|
| [1243] | 185 |     //if (this->auMan_) | 
|---|
 | 186 |     //  delete this->auMan_; | 
|---|
| [1038] | 187 |     if (this->timer_) | 
|---|
 | 188 |       delete this->timer_; | 
|---|
 | 189 |     GraphicsEngine::getSingleton().destroy(); | 
|---|
 | 190 |  | 
|---|
 | 191 |     if (client_g) | 
|---|
 | 192 |       delete client_g; | 
|---|
 | 193 |     if (server_g) | 
|---|
 | 194 |       delete server_g; | 
|---|
 | 195 |   } | 
|---|
 | 196 |  | 
|---|
 | 197 |   /** | 
|---|
 | 198 |     Asks the mainloop nicely to abort. | 
|---|
 | 199 |   */ | 
|---|
 | 200 |   void Orxonox::abortRequest() | 
|---|
 | 201 |   { | 
|---|
| [1089] | 202 |     COUT(3) << "*** Orxonox: Abort requested." << std::endl; | 
|---|
| [1038] | 203 |     bAbort_ = true; | 
|---|
 | 204 |   } | 
|---|
 | 205 |  | 
|---|
 | 206 |   /** | 
|---|
| [1244] | 207 |    * @return singleton reference | 
|---|
| [1038] | 208 |    */ | 
|---|
 | 209 |   Orxonox* Orxonox::getSingleton() | 
|---|
 | 210 |   { | 
|---|
 | 211 |     if (!singletonRef_s) | 
|---|
 | 212 |       singletonRef_s = new Orxonox(); | 
|---|
 | 213 |     return singletonRef_s; | 
|---|
 | 214 |   } | 
|---|
 | 215 |  | 
|---|
 | 216 |   /** | 
|---|
 | 217 |     @brief Destroys the Orxonox singleton. | 
|---|
 | 218 |   */ | 
|---|
 | 219 |   void Orxonox::destroySingleton() | 
|---|
 | 220 |   { | 
|---|
 | 221 |     if (singletonRef_s) | 
|---|
 | 222 |       delete singletonRef_s; | 
|---|
 | 223 |     singletonRef_s = 0; | 
|---|
 | 224 |   } | 
|---|
 | 225 |  | 
|---|
 | 226 |   /** | 
|---|
 | 227 |    * initialization of Orxonox object | 
|---|
 | 228 |    * @param argc argument counter | 
|---|
 | 229 |    * @param argv list of argumenst | 
|---|
 | 230 |    * @param path path to config (in home dir or something) | 
|---|
 | 231 |    */ | 
|---|
| [1243] | 232 |   bool Orxonox::init(int argc, char **argv, std::string path) | 
|---|
| [1038] | 233 |   { | 
|---|
 | 234 |     //TODO: find config file (assuming executable directory) | 
|---|
 | 235 |     //TODO: read config file | 
|---|
 | 236 |     //TODO: give config file to Ogre | 
|---|
 | 237 |     std::string mode; | 
|---|
| [1243] | 238 |     std::string dataPath; | 
|---|
| [1038] | 239 |  | 
|---|
 | 240 |     ArgReader ar(argc, argv); | 
|---|
 | 241 |     ar.checkArgument("mode", mode, false); | 
|---|
| [1243] | 242 |     ar.checkArgument("data", dataPath, false); | 
|---|
| [1038] | 243 |     ar.checkArgument("ip", serverIp_, false); | 
|---|
| [1243] | 244 |     if(ar.errorHandling()) | 
|---|
 | 245 |       return false; | 
|---|
 | 246 |  | 
|---|
| [1244] | 247 |     COUT(3) << "*** Orxonox: Mode is " << mode << "." << std::endl; | 
|---|
| [1243] | 248 |     if (mode == "client") | 
|---|
| [1038] | 249 |       mode_ = CLIENT; | 
|---|
| [1243] | 250 |     else if (mode == "server") | 
|---|
| [1038] | 251 |       mode_ = SERVER; | 
|---|
| [1243] | 252 |     else | 
|---|
| [1038] | 253 |       mode_ = STANDALONE; | 
|---|
 | 254 |  | 
|---|
| [1243] | 255 |     //if (mode_ == DEDICATED) | 
|---|
 | 256 |       // TODO: decide what to do here | 
|---|
 | 257 |     //else | 
|---|
| [1052] | 258 |  | 
|---|
| [1243] | 259 |     // for playable server, client and standalone, the startup | 
|---|
| [1244] | 260 |     // procedure until the GUI is identical | 
|---|
| [1052] | 261 |  | 
|---|
| [1243] | 262 |     TclBind::getInstance().setDataPath(dataPath); | 
|---|
 | 263 |     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); | 
|---|
 | 264 |     Factory::createClassHierarchy(); | 
|---|
| [1038] | 265 |  | 
|---|
| [1243] | 266 |     if (!ogre_->setup(path))       // creates ogre root and other essentials | 
|---|
 | 267 |       return false; | 
|---|
| [1052] | 268 |  | 
|---|
| [1243] | 269 |     return true; | 
|---|
| [1038] | 270 |   } | 
|---|
| [1052] | 271 |  | 
|---|
| [1038] | 272 |   /** | 
|---|
 | 273 |    * start modules | 
|---|
 | 274 |    */ | 
|---|
| [1243] | 275 |   bool Orxonox::start() | 
|---|
| [1038] | 276 |   { | 
|---|
| [1243] | 277 |     //if (mode == DEDICATED) | 
|---|
 | 278 |     // do something else | 
|---|
 | 279 |     //else | 
|---|
 | 280 |  | 
|---|
 | 281 |     if (!ogre_->loadRenderer())    // creates the render window | 
|---|
 | 282 |       return false; | 
|---|
 | 283 |  | 
|---|
| [1244] | 284 |     // Calls the InputManager which sets up the input devices. | 
|---|
| [1243] | 285 |     // The render window width and height are used to set up the mouse movement. | 
|---|
 | 286 |     if (!InputManager::initialise(ogre_->getWindowHandle(), | 
|---|
 | 287 |           ogre_->getWindowWidth(), ogre_->getWindowHeight())) | 
|---|
 | 288 |       return false; | 
|---|
 | 289 |  | 
|---|
| [1244] | 290 |     // TODO: Spread this so that this call only initialises things needed for the GUI | 
|---|
 | 291 |     if (!ogre_->initialiseResources()) | 
|---|
 | 292 |       return false; | 
|---|
| [1243] | 293 |  | 
|---|
 | 294 |     // TOOD: load the GUI here | 
|---|
 | 295 |     // set InputManager to GUI mode | 
|---|
 | 296 |     InputManager::setInputState(InputManager::IS_GUI); | 
|---|
 | 297 |     // TODO: run GUI here | 
|---|
 | 298 |  | 
|---|
| [1244] | 299 |     // The following lines depend very much on the GUI output, so they're probably misplaced here.. | 
|---|
| [1243] | 300 |  | 
|---|
 | 301 |     InputManager::setInputState(InputManager::IS_NONE); | 
|---|
 | 302 |  | 
|---|
 | 303 |     if (!loadPlayground()) | 
|---|
 | 304 |       return false; | 
|---|
 | 305 |  | 
|---|
 | 306 |     switch (mode_) | 
|---|
 | 307 |     { | 
|---|
 | 308 |     case SERVER: | 
|---|
 | 309 |       if (!serverLoad()) | 
|---|
 | 310 |         return false; | 
|---|
 | 311 |       break; | 
|---|
| [1038] | 312 |     case CLIENT: | 
|---|
| [1243] | 313 |       if (!clientLoad()) | 
|---|
 | 314 |         return false; | 
|---|
| [1038] | 315 |       break; | 
|---|
 | 316 |     default: | 
|---|
| [1243] | 317 |       if (!standaloneLoad()) | 
|---|
 | 318 |         return false; | 
|---|
| [1038] | 319 |     } | 
|---|
| [1243] | 320 |  | 
|---|
 | 321 |     InputManager::setInputState(InputManager::IS_NORMAL); | 
|---|
 | 322 |  | 
|---|
 | 323 |     return startRenderLoop(); | 
|---|
| [1038] | 324 |   } | 
|---|
| [1052] | 325 |  | 
|---|
| [1244] | 326 |   /** | 
|---|
 | 327 |    * Loads everything in the scene except for the actual objects. | 
|---|
 | 328 |    * This includes HUD, Console.. | 
|---|
 | 329 |    */ | 
|---|
| [1243] | 330 |   bool Orxonox::loadPlayground() | 
|---|
 | 331 |   { | 
|---|
 | 332 |     ogre_->createNewScene(); | 
|---|
| [1052] | 333 |  | 
|---|
| [1243] | 334 |           // Init audio | 
|---|
 | 335 |     //auMan_ = new audio::AudioManager(); | 
|---|
 | 336 |     //auMan_->ambientAdd("a1"); | 
|---|
 | 337 |     //auMan_->ambientAdd("a2"); | 
|---|
 | 338 |     //auMan_->ambientAdd("a3"); | 
|---|
 | 339 |     //auMan->ambientAdd("ambient1"); | 
|---|
 | 340 |     //auMan_->ambientStart(); | 
|---|
| [1052] | 341 |  | 
|---|
| [1243] | 342 |     // Load the HUD | 
|---|
| [1244] | 343 |     COUT(3) << "*** Orxonox: Loading HUD..." << std::endl; | 
|---|
| [1038] | 344 |     Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); | 
|---|
| [1156] | 345 |     orxonoxHUD_ = new HUD(); | 
|---|
 | 346 |     orxonoxHUD_->setEnergyValue(20); | 
|---|
 | 347 |     orxonoxHUD_->setEnergyDistr(20,20,60); | 
|---|
| [1038] | 348 |     hudOverlay->show(); | 
|---|
| [1052] | 349 |  | 
|---|
| [1244] | 350 |     COUT(3) << "*** Orxonox: Loading Console..." << std::endl; | 
|---|
| [1243] | 351 |     InputBuffer* ib = dynamic_cast<InputBuffer*>(InputManager::getKeyHandler("buffer")); | 
|---|
 | 352 |     /* | 
|---|
 | 353 |     Testconsole* console = new Testconsole(ib); | 
|---|
 | 354 |     ib->registerListener(console, &Testconsole::listen, true); | 
|---|
 | 355 |     ib->registerListener(console, &Testconsole::execute, '\r', false); | 
|---|
 | 356 |     ib->registerListener(console, &Testconsole::hintandcomplete, '\t', true); | 
|---|
 | 357 |     ib->registerListener(console, &Testconsole::clear, '§', true); | 
|---|
 | 358 |     ib->registerListener(console, &Testconsole::removeLast, '\b', true); | 
|---|
 | 359 |     ib->registerListener(console, &Testconsole::exit, (char)0x1B, true); | 
|---|
 | 360 |     */ | 
|---|
 | 361 |     orxonoxConsole_ = new InGameConsole(ib); | 
|---|
 | 362 |     ib->registerListener(orxonoxConsole_, &InGameConsole::listen, true); | 
|---|
 | 363 |     ib->registerListener(orxonoxConsole_, &InGameConsole::execute, '\r', false); | 
|---|
 | 364 |     ib->registerListener(orxonoxConsole_, &InGameConsole::hintandcomplete, '\t', true); | 
|---|
 | 365 |     ib->registerListener(orxonoxConsole_, &InGameConsole::clear, '§', true); | 
|---|
 | 366 |     ib->registerListener(orxonoxConsole_, &InGameConsole::removeLast, '\b', true); | 
|---|
 | 367 |     ib->registerListener(orxonoxConsole_, &InGameConsole::exit, (char)0x1B, true); | 
|---|
| [1052] | 368 |  | 
|---|
| [1243] | 369 |     return true; | 
|---|
 | 370 |   } | 
|---|
| [1052] | 371 |  | 
|---|
| [1244] | 372 |   /** | 
|---|
 | 373 |    * Level loading method for server mode. | 
|---|
 | 374 |    */ | 
|---|
| [1243] | 375 |   bool Orxonox::serverLoad() | 
|---|
 | 376 |   { | 
|---|
| [1244] | 377 |     COUT(2) << "Loading level in server mode" << std::endl; | 
|---|
| [1052] | 378 |  | 
|---|
| [1243] | 379 |     server_g = new network::Server(); | 
|---|
| [1052] | 380 |  | 
|---|
| [1243] | 381 |     if (!loadScene()) | 
|---|
 | 382 |       return false; | 
|---|
| [1052] | 383 |  | 
|---|
| [1038] | 384 |     server_g->open(); | 
|---|
| [1052] | 385 |  | 
|---|
| [1243] | 386 |     return true; | 
|---|
| [1038] | 387 |   } | 
|---|
| [1052] | 388 |  | 
|---|
| [1244] | 389 |   /** | 
|---|
 | 390 |    * Level loading method for client mode. | 
|---|
 | 391 |    */ | 
|---|
| [1243] | 392 |   bool Orxonox::clientLoad() | 
|---|
 | 393 |   { | 
|---|
| [1244] | 394 |     COUT(2) << "Loading level in client mode" << std::endl;\ | 
|---|
| [1052] | 395 |  | 
|---|
| [1243] | 396 |     if (serverIp_.compare("") == 0) | 
|---|
 | 397 |       client_g = new network::Client(); | 
|---|
 | 398 |     else | 
|---|
 | 399 |       client_g = new network::Client(serverIp_, NETWORK_PORT); | 
|---|
 | 400 |  | 
|---|
 | 401 |     client_g->establishConnection(); | 
|---|
 | 402 |     client_g->tick(0); | 
|---|
 | 403 |  | 
|---|
 | 404 |     return true; | 
|---|
| [1038] | 405 |   } | 
|---|
 | 406 |  | 
|---|
| [1244] | 407 |   /** | 
|---|
 | 408 |    * Level loading method for standalone mode. | 
|---|
 | 409 |    */ | 
|---|
| [1243] | 410 |   bool Orxonox::standaloneLoad() | 
|---|
| [1038] | 411 |   { | 
|---|
| [1244] | 412 |     COUT(2) << "Loading level in standalone mode" << std::endl; | 
|---|
| [1038] | 413 |  | 
|---|
| [1243] | 414 |     if (!loadScene()) | 
|---|
 | 415 |       return false; | 
|---|
| [1038] | 416 |  | 
|---|
| [1243] | 417 |     return true; | 
|---|
| [1038] | 418 |   } | 
|---|
 | 419 |  | 
|---|
| [1244] | 420 |   /** | 
|---|
 | 421 |    * Helper method to load a level. | 
|---|
 | 422 |    */ | 
|---|
| [1243] | 423 |   bool Orxonox::loadScene() | 
|---|
| [1038] | 424 |   { | 
|---|
| [1243] | 425 |     Level* startlevel = new Level("levels/sample.oxw"); | 
|---|
 | 426 |     Loader::open(startlevel); | 
|---|
 | 427 |  | 
|---|
 | 428 |     return true; | 
|---|
| [1038] | 429 |   } | 
|---|
 | 430 |  | 
|---|
| [1243] | 431 |  | 
|---|
| [1038] | 432 |   /** | 
|---|
 | 433 |     Main loop of the orxonox game. | 
|---|
 | 434 |     About the loop: The design is almost exactly like the one in ogre, so that | 
|---|
 | 435 |     if any part of ogre registers a framelisteners, it will still behave | 
|---|
 | 436 |     correctly. Furthermore the time smoothing feature from ogre has been | 
|---|
 | 437 |     implemented too. If turned on (see orxonox constructor), it will calculate | 
|---|
 | 438 |     the dt_n by means of the recent most dt_n-1, dt_n-2, etc. | 
|---|
 | 439 |   */ | 
|---|
| [1243] | 440 |   bool Orxonox::startRenderLoop() | 
|---|
| [1038] | 441 |   { | 
|---|
 | 442 |     // first check whether ogre root object has been created | 
|---|
 | 443 |     if (Ogre::Root::getSingletonPtr() == 0) | 
|---|
 | 444 |     { | 
|---|
| [1089] | 445 |       COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl; | 
|---|
| [1243] | 446 |       return false; | 
|---|
| [1038] | 447 |     } | 
|---|
| [1120] | 448 |     Ogre::Root& ogreRoot = Ogre::Root::getSingleton(); | 
|---|
| [1038] | 449 |  | 
|---|
| [1120] | 450 |  | 
|---|
| [1038] | 451 |     // Contains the times of recently fired events | 
|---|
 | 452 |     // eventTimes[4] is the list for the times required for the fps counter | 
|---|
 | 453 |     std::deque<unsigned long> eventTimes[4]; | 
|---|
 | 454 |     // Clear event times | 
|---|
 | 455 |     for (int i = 0; i < 4; ++i) | 
|---|
 | 456 |       eventTimes[i].clear(); | 
|---|
 | 457 |     // fill the fps time list with zeros | 
|---|
| [1219] | 458 |     for (int i = 0; i < 50; i++) | 
|---|
| [1038] | 459 |       eventTimes[3].push_back(0); | 
|---|
 | 460 |  | 
|---|
 | 461 |     // use the ogre timer class to measure time. | 
|---|
 | 462 |     if (!timer_) | 
|---|
 | 463 |       timer_ = new Ogre::Timer(); | 
|---|
 | 464 |     timer_->reset(); | 
|---|
 | 465 |  | 
|---|
| [1244] | 466 |     COUT(3) << "*** Orxonox: Starting the main loop." << std::endl; | 
|---|
| [1038] | 467 |           while (!bAbort_) | 
|---|
 | 468 |           { | 
|---|
 | 469 |                   // Pump messages in all registered RenderWindows | 
|---|
| [1120] | 470 |       // This calls the WindowEventListener objects. | 
|---|
| [1038] | 471 |       Ogre::WindowEventUtilities::messagePump(); | 
|---|
 | 472 |  | 
|---|
 | 473 |       // get current time | 
|---|
 | 474 |       unsigned long now = timer_->getMilliseconds(); | 
|---|
 | 475 |       eventTimes[3].push_back(now); | 
|---|
 | 476 |       eventTimes[3].erase(eventTimes[3].begin()); | 
|---|
 | 477 |  | 
|---|
 | 478 |       // create an event to pass to the frameStarted method in ogre | 
|---|
 | 479 |       Ogre::FrameEvent evt; | 
|---|
 | 480 |       evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); | 
|---|
 | 481 |       evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]); | 
|---|
 | 482 |  | 
|---|
 | 483 |       // show the current time in the HUD | 
|---|
 | 484 |       orxonoxHUD_->setTime((int)now, 0); | 
|---|
| [1219] | 485 |       orxonoxHUD_->setRocket2(ogreRoot.getCurrentFrameNumber()); | 
|---|
| [1038] | 486 |       if (eventTimes[3].back() - eventTimes[3].front() != 0) | 
|---|
| [1219] | 487 |         orxonoxHUD_->setRocket1((int)(50000.0f/(eventTimes[3].back() - eventTimes[3].front()))); | 
|---|
| [1038] | 488 |  | 
|---|
 | 489 |       // Iterate through all Tickables and call their tick(dt) function | 
|---|
| [1092] | 490 |       for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it) | 
|---|
 | 491 |         it->tick((float)evt.timeSinceLastFrame * this->timefactor_); | 
|---|
| [1214] | 492 |       orxonoxConsole_->tick((float)evt.timeSinceLastFrame * this->timefactor_); | 
|---|
| [1038] | 493 |  | 
|---|
 | 494 |       // don't forget to call _fireFrameStarted in ogre to make sure | 
|---|
 | 495 |       // everything goes smoothly | 
|---|
| [1120] | 496 |       ogreRoot._fireFrameStarted(evt); | 
|---|
| [1038] | 497 |  | 
|---|
 | 498 |       // server still renders at the moment | 
|---|
 | 499 |       //if (mode_ != SERVER) | 
|---|
| [1120] | 500 |       ogreRoot._updateAllRenderTargets(); // only render in non-server mode | 
|---|
| [1038] | 501 |  | 
|---|
 | 502 |       // get current time | 
|---|
 | 503 |       now = timer_->getMilliseconds(); | 
|---|
 | 504 |  | 
|---|
 | 505 |       // create an event to pass to the frameEnded method in ogre | 
|---|
 | 506 |       evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); | 
|---|
 | 507 |       evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[2]); | 
|---|
 | 508 |  | 
|---|
 | 509 |       // again, just to be sure ogre works fine | 
|---|
| [1120] | 510 |       ogreRoot._fireFrameEnded(evt); | 
|---|
| [1038] | 511 |           } | 
|---|
| [1243] | 512 |  | 
|---|
 | 513 |     return true; | 
|---|
| [1038] | 514 |   } | 
|---|
 | 515 |  | 
|---|
 | 516 |   /** | 
|---|
 | 517 |     Method for calculating the average time between recently fired events. | 
|---|
 | 518 |     Code directly taken from OgreRoot.cc | 
|---|
 | 519 |     @param now The current time in ms. | 
|---|
 | 520 |     @param type The type of event to be considered. | 
|---|
 | 521 |   */ | 
|---|
 | 522 |   float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> ×) | 
|---|
 | 523 |   { | 
|---|
 | 524 |     // Calculate the average time passed between events of the given type | 
|---|
 | 525 |     // during the last frameSmoothingTime_ seconds. | 
|---|
 | 526 |  | 
|---|
 | 527 |     times.push_back(now); | 
|---|
 | 528 |  | 
|---|
 | 529 |     if(times.size() == 1) | 
|---|
 | 530 |       return 0; | 
|---|
 | 531 |  | 
|---|
 | 532 |     // Times up to frameSmoothingTime_ seconds old should be kept | 
|---|
 | 533 |     unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f); | 
|---|
 | 534 |  | 
|---|
 | 535 |     // Find the oldest time to keep | 
|---|
 | 536 |     std::deque<unsigned long>::iterator it  = times.begin(); | 
|---|
 | 537 |     // We need at least two times | 
|---|
 | 538 |     std::deque<unsigned long>::iterator end = times.end() - 2; | 
|---|
 | 539 |  | 
|---|
 | 540 |     while(it != end) | 
|---|
 | 541 |     { | 
|---|
 | 542 |       if (now - *it > discardThreshold) | 
|---|
 | 543 |         ++it; | 
|---|
 | 544 |       else | 
|---|
 | 545 |         break; | 
|---|
 | 546 |     } | 
|---|
 | 547 |  | 
|---|
 | 548 |     // Remove old times | 
|---|
 | 549 |     times.erase(times.begin(), it); | 
|---|
 | 550 |  | 
|---|
 | 551 |     return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000); | 
|---|
 | 552 |   } | 
|---|
 | 553 |  | 
|---|
| [1244] | 554 |   /** | 
|---|
 | 555 |    * Static function that shows the console in game mode. | 
|---|
 | 556 |    */ | 
|---|
| [1219] | 557 |   void Orxonox::activateConsole() | 
|---|
| [1038] | 558 |   { | 
|---|
| [1244] | 559 |     // currently, the console shows itself when feeded with input. | 
|---|
| [1219] | 560 |     InputManager::setInputState(InputManager::IS_CONSOLE); | 
|---|
| [1038] | 561 |   } | 
|---|
 | 562 | } | 
|---|