[1038] | 1 | /* |
---|
[1293] | 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 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" |
---|
| 53 | |
---|
[1293] | 54 | // core |
---|
[1054] | 55 | #include "core/ConfigFileManager.h" |
---|
[1293] | 56 | #include "core/ConsoleCommand.h" |
---|
[1038] | 57 | #include "core/Debug.h" |
---|
| 58 | #include "core/Loader.h" |
---|
| 59 | #include "core/Tickable.h" |
---|
[1293] | 60 | #include "core/InputManager.h" |
---|
| 61 | #include "core/TclBind.h" |
---|
[1038] | 62 | |
---|
| 63 | // audio |
---|
| 64 | #include "audio/AudioManager.h" |
---|
| 65 | |
---|
| 66 | // network |
---|
| 67 | #include "network/Server.h" |
---|
| 68 | #include "network/Client.h" |
---|
| 69 | |
---|
| 70 | // objects and tools |
---|
| 71 | #include "hud/HUD.h" |
---|
| 72 | |
---|
| 73 | // FIXME: is this really file scope? |
---|
| 74 | // globals for the server or client |
---|
| 75 | network::Client *client_g; |
---|
| 76 | network::Server *server_g; |
---|
| 77 | |
---|
| 78 | namespace orxonox |
---|
[1293] | 79 | { |
---|
[1446] | 80 | SetConsoleCommandShortcut(Orxonox, exit).setKeybindMode(KeybindMode::OnPress); |
---|
| 81 | SetConsoleCommandShortcut(Orxonox, slomo).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0).setAxisParamIndex(0).setIsAxisRelative(false); |
---|
| 82 | SetConsoleCommandShortcut(Orxonox, setTimeFactor).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0); |
---|
| 83 | |
---|
[1038] | 84 | /** |
---|
| 85 | @brief Reference to the only instance of the class. |
---|
| 86 | */ |
---|
| 87 | Orxonox *Orxonox::singletonRef_s = 0; |
---|
| 88 | |
---|
| 89 | /** |
---|
[1293] | 90 | * Create a new instance of Orxonox. Avoid doing any actual work here. |
---|
[1038] | 91 | */ |
---|
[1293] | 92 | Orxonox::Orxonox() : |
---|
| 93 | ogre_(0), |
---|
| 94 | //auMan_(0), |
---|
| 95 | timer_(0), |
---|
| 96 | // turn on frame smoothing by setting a value different from 0 |
---|
| 97 | frameSmoothingTime_(0.0f), |
---|
| 98 | orxonoxHUD_(0), |
---|
| 99 | bAbort_(false), |
---|
| 100 | timefactor_(1.0f), |
---|
| 101 | mode_(STANDALONE), |
---|
[1379] | 102 | serverIp_(""), |
---|
| 103 | serverPort_(NETWORK_PORT) |
---|
[1038] | 104 | { |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
[1293] | 108 | * Destruct Orxonox. |
---|
[1038] | 109 | */ |
---|
| 110 | Orxonox::~Orxonox() |
---|
| 111 | { |
---|
| 112 | // keep in mind: the order of deletion is very important! |
---|
[1406] | 113 | // if (this->orxonoxHUD_) |
---|
| 114 | // delete this->orxonoxHUD_; |
---|
[1038] | 115 | Loader::close(); |
---|
[1219] | 116 | InputManager::destroy(); |
---|
[1293] | 117 | //if (this->auMan_) |
---|
| 118 | // delete this->auMan_; |
---|
[1038] | 119 | if (this->timer_) |
---|
| 120 | delete this->timer_; |
---|
| 121 | GraphicsEngine::getSingleton().destroy(); |
---|
| 122 | |
---|
[1293] | 123 | if (network::Client::getSingleton()) |
---|
| 124 | network::Client::destroySingleton(); |
---|
[1038] | 125 | if (server_g) |
---|
| 126 | delete server_g; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | Asks the mainloop nicely to abort. |
---|
| 132 | */ |
---|
| 133 | void Orxonox::abortRequest() |
---|
| 134 | { |
---|
[1293] | 135 | COUT(3) << "Orxonox: Abort requested." << std::endl; |
---|
[1038] | 136 | bAbort_ = true; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
[1293] | 140 | * @return singleton reference |
---|
[1038] | 141 | */ |
---|
| 142 | Orxonox* Orxonox::getSingleton() |
---|
| 143 | { |
---|
| 144 | if (!singletonRef_s) |
---|
| 145 | singletonRef_s = new Orxonox(); |
---|
| 146 | return singletonRef_s; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | @brief Destroys the Orxonox singleton. |
---|
| 151 | */ |
---|
| 152 | void Orxonox::destroySingleton() |
---|
| 153 | { |
---|
| 154 | if (singletonRef_s) |
---|
| 155 | delete singletonRef_s; |
---|
| 156 | singletonRef_s = 0; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * initialization of Orxonox object |
---|
| 161 | * @param argc argument counter |
---|
| 162 | * @param argv list of argumenst |
---|
| 163 | * @param path path to config (in home dir or something) |
---|
| 164 | */ |
---|
[1293] | 165 | bool Orxonox::init(int argc, char **argv, std::string path) |
---|
[1038] | 166 | { |
---|
| 167 | //TODO: find config file (assuming executable directory) |
---|
| 168 | //TODO: read config file |
---|
| 169 | //TODO: give config file to Ogre |
---|
| 170 | std::string mode; |
---|
[1293] | 171 | std::string dataPath; |
---|
[1038] | 172 | |
---|
| 173 | ArgReader ar(argc, argv); |
---|
| 174 | ar.checkArgument("mode", mode, false); |
---|
[1293] | 175 | ar.checkArgument("data", dataPath, false); |
---|
[1038] | 176 | ar.checkArgument("ip", serverIp_, false); |
---|
[1379] | 177 | ar.checkArgument("port", serverPort_, false); |
---|
[1293] | 178 | if(ar.errorHandling()) |
---|
| 179 | return false; |
---|
| 180 | |
---|
| 181 | if (mode == "client") |
---|
[1038] | 182 | mode_ = CLIENT; |
---|
[1293] | 183 | else if (mode == "server") |
---|
[1038] | 184 | mode_ = SERVER; |
---|
[1293] | 185 | else |
---|
| 186 | { |
---|
| 187 | mode = "standalone"; |
---|
[1038] | 188 | mode_ = STANDALONE; |
---|
| 189 | } |
---|
[1293] | 190 | COUT(3) << "Orxonox: Mode is " << mode << "." << std::endl; |
---|
[1038] | 191 | |
---|
[1293] | 192 | //if (mode_ == DEDICATED) |
---|
| 193 | // TODO: decide what to do here |
---|
| 194 | //else |
---|
[1052] | 195 | |
---|
[1293] | 196 | // for playable server, client and standalone, the startup |
---|
| 197 | // procedure until the GUI is identical |
---|
[1052] | 198 | |
---|
[1293] | 199 | TclBind::getInstance().setDataPath(dataPath); |
---|
| 200 | ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); |
---|
| 201 | Factory::createClassHierarchy(); |
---|
[1038] | 202 | |
---|
[1293] | 203 | ogre_ = &GraphicsEngine::getSingleton(); |
---|
| 204 | if (!ogre_->setup(path)) // creates ogre root and other essentials |
---|
| 205 | return false; |
---|
[1052] | 206 | |
---|
[1293] | 207 | return true; |
---|
[1038] | 208 | } |
---|
[1052] | 209 | |
---|
[1038] | 210 | /** |
---|
| 211 | * start modules |
---|
| 212 | */ |
---|
[1293] | 213 | bool Orxonox::start() |
---|
[1038] | 214 | { |
---|
[1293] | 215 | //if (mode == DEDICATED) |
---|
| 216 | // do something else |
---|
| 217 | //else |
---|
| 218 | |
---|
| 219 | if (!ogre_->loadRenderer()) // creates the render window |
---|
| 220 | return false; |
---|
| 221 | |
---|
| 222 | // Calls the InputManager which sets up the input devices. |
---|
| 223 | // The render window width and height are used to set up the mouse movement. |
---|
| 224 | if (!InputManager::initialise(ogre_->getWindowHandle(), |
---|
| 225 | ogre_->getWindowWidth(), ogre_->getWindowHeight(), true, true, true)) |
---|
| 226 | return false; |
---|
| 227 | |
---|
| 228 | // TODO: Spread this so that this call only initialises things needed for the GUI |
---|
| 229 | if (!ogre_->initialiseResources()) |
---|
| 230 | return false; |
---|
| 231 | |
---|
| 232 | // TOOD: load the GUI here |
---|
| 233 | // set InputManager to GUI mode |
---|
| 234 | InputManager::setInputState(InputManager::IS_GUI); |
---|
| 235 | // TODO: run GUI here |
---|
| 236 | |
---|
| 237 | // The following lines depend very much on the GUI output, so they're probably misplaced here.. |
---|
| 238 | |
---|
| 239 | InputManager::setInputState(InputManager::IS_NONE); |
---|
| 240 | |
---|
| 241 | if (!loadPlayground()) |
---|
| 242 | return false; |
---|
| 243 | |
---|
| 244 | switch (mode_) |
---|
| 245 | { |
---|
| 246 | case SERVER: |
---|
| 247 | if (!serverLoad()) |
---|
| 248 | return false; |
---|
| 249 | break; |
---|
[1038] | 250 | case CLIENT: |
---|
[1293] | 251 | if (!clientLoad()) |
---|
| 252 | return false; |
---|
[1038] | 253 | break; |
---|
| 254 | default: |
---|
[1293] | 255 | if (!standaloneLoad()) |
---|
| 256 | return false; |
---|
[1038] | 257 | } |
---|
[1293] | 258 | |
---|
| 259 | InputManager::setInputState(InputManager::IS_NORMAL); |
---|
| 260 | |
---|
| 261 | return startRenderLoop(); |
---|
[1038] | 262 | } |
---|
[1052] | 263 | |
---|
[1293] | 264 | /** |
---|
| 265 | * Loads everything in the scene except for the actual objects. |
---|
| 266 | * This includes HUD, Console.. |
---|
| 267 | */ |
---|
| 268 | bool Orxonox::loadPlayground() |
---|
| 269 | { |
---|
| 270 | ogre_->createNewScene(); |
---|
[1052] | 271 | |
---|
[1293] | 272 | // Init audio |
---|
| 273 | //auMan_ = new audio::AudioManager(); |
---|
| 274 | //auMan_->ambientAdd("a1"); |
---|
| 275 | //auMan_->ambientAdd("a2"); |
---|
| 276 | //auMan_->ambientAdd("a3"); |
---|
| 277 | //auMan->ambientAdd("ambient1"); |
---|
| 278 | //auMan_->ambientStart(); |
---|
[1052] | 279 | |
---|
[1293] | 280 | // Load the HUD |
---|
| 281 | COUT(3) << "Orxonox: Loading HUD..." << std::endl; |
---|
[1406] | 282 | orxonoxHUD_ = &HUD::getSingleton(); |
---|
[1293] | 283 | return true; |
---|
| 284 | } |
---|
[1052] | 285 | |
---|
[1293] | 286 | /** |
---|
| 287 | * Level loading method for server mode. |
---|
| 288 | */ |
---|
| 289 | bool Orxonox::serverLoad() |
---|
| 290 | { |
---|
| 291 | COUT(2) << "Loading level in server mode" << std::endl; |
---|
[1052] | 292 | |
---|
[1379] | 293 | server_g = new network::Server(serverPort_); |
---|
[1052] | 294 | |
---|
[1293] | 295 | if (!loadScene()) |
---|
| 296 | return false; |
---|
[1052] | 297 | |
---|
[1038] | 298 | server_g->open(); |
---|
[1052] | 299 | |
---|
[1293] | 300 | return true; |
---|
[1038] | 301 | } |
---|
[1052] | 302 | |
---|
[1293] | 303 | /** |
---|
| 304 | * Level loading method for client mode. |
---|
| 305 | */ |
---|
| 306 | bool Orxonox::clientLoad() |
---|
| 307 | { |
---|
| 308 | COUT(2) << "Loading level in client mode" << std::endl;\ |
---|
[1052] | 309 | |
---|
[1293] | 310 | if (serverIp_.compare("") == 0) |
---|
| 311 | client_g = network::Client::createSingleton(); |
---|
| 312 | else |
---|
[1411] | 313 | |
---|
[1379] | 314 | client_g = network::Client::createSingleton(serverIp_, serverPort_); |
---|
[1293] | 315 | |
---|
[1443] | 316 | if(!client_g->establishConnection()) |
---|
| 317 | return false; |
---|
[1293] | 318 | client_g->tick(0); |
---|
| 319 | |
---|
| 320 | return true; |
---|
[1038] | 321 | } |
---|
| 322 | |
---|
[1293] | 323 | /** |
---|
| 324 | * Level loading method for standalone mode. |
---|
| 325 | */ |
---|
| 326 | bool Orxonox::standaloneLoad() |
---|
[1038] | 327 | { |
---|
[1293] | 328 | COUT(2) << "Loading level in standalone mode" << std::endl; |
---|
[1038] | 329 | |
---|
[1293] | 330 | if (!loadScene()) |
---|
| 331 | return false; |
---|
[1038] | 332 | |
---|
[1293] | 333 | return true; |
---|
[1038] | 334 | } |
---|
| 335 | |
---|
| 336 | /** |
---|
[1293] | 337 | * Helper method to load a level. |
---|
| 338 | */ |
---|
| 339 | bool Orxonox::loadScene() |
---|
[1038] | 340 | { |
---|
[1293] | 341 | Level* startlevel = new Level("levels/sample.oxw"); |
---|
| 342 | Loader::open(startlevel); |
---|
| 343 | |
---|
| 344 | return true; |
---|
[1038] | 345 | } |
---|
| 346 | |
---|
[1293] | 347 | |
---|
[1038] | 348 | /** |
---|
| 349 | Main loop of the orxonox game. |
---|
| 350 | About the loop: The design is almost exactly like the one in ogre, so that |
---|
| 351 | if any part of ogre registers a framelisteners, it will still behave |
---|
| 352 | correctly. Furthermore the time smoothing feature from ogre has been |
---|
| 353 | implemented too. If turned on (see orxonox constructor), it will calculate |
---|
| 354 | the dt_n by means of the recent most dt_n-1, dt_n-2, etc. |
---|
| 355 | */ |
---|
[1293] | 356 | bool Orxonox::startRenderLoop() |
---|
[1038] | 357 | { |
---|
| 358 | // first check whether ogre root object has been created |
---|
| 359 | if (Ogre::Root::getSingletonPtr() == 0) |
---|
| 360 | { |
---|
[1293] | 361 | COUT(2) << "Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl; |
---|
| 362 | return false; |
---|
[1038] | 363 | } |
---|
[1120] | 364 | Ogre::Root& ogreRoot = Ogre::Root::getSingleton(); |
---|
[1038] | 365 | |
---|
[1120] | 366 | |
---|
[1038] | 367 | // Contains the times of recently fired events |
---|
| 368 | // eventTimes[4] is the list for the times required for the fps counter |
---|
[1414] | 369 | std::deque<unsigned long> eventTimes[3]; |
---|
[1038] | 370 | // Clear event times |
---|
[1414] | 371 | for (int i = 0; i < 3; ++i) |
---|
[1038] | 372 | eventTimes[i].clear(); |
---|
| 373 | |
---|
| 374 | // use the ogre timer class to measure time. |
---|
| 375 | if (!timer_) |
---|
| 376 | timer_ = new Ogre::Timer(); |
---|
| 377 | timer_->reset(); |
---|
| 378 | |
---|
[1422] | 379 | float renderTime = 0.0f; |
---|
| 380 | float frameTime = 0.0f; |
---|
| 381 | clock_t time = 0; |
---|
| 382 | |
---|
[1293] | 383 | COUT(3) << "Orxonox: Starting the main loop." << std::endl; |
---|
[1038] | 384 | while (!bAbort_) |
---|
| 385 | { |
---|
| 386 | // Pump messages in all registered RenderWindows |
---|
[1120] | 387 | // This calls the WindowEventListener objects. |
---|
[1038] | 388 | Ogre::WindowEventUtilities::messagePump(); |
---|
| 389 | |
---|
| 390 | // get current time |
---|
| 391 | unsigned long now = timer_->getMilliseconds(); |
---|
| 392 | |
---|
| 393 | // create an event to pass to the frameStarted method in ogre |
---|
| 394 | Ogre::FrameEvent evt; |
---|
| 395 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
| 396 | evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]); |
---|
[1422] | 397 | frameTime += evt.timeSinceLastFrame; |
---|
[1038] | 398 | |
---|
| 399 | // show the current time in the HUD |
---|
[1414] | 400 | // HUD::getSingleton().setTime(now); |
---|
[1422] | 401 | if (frameTime > 0.4f) |
---|
| 402 | { |
---|
| 403 | HUD::getSingleton().setRenderTimeRatio(renderTime / frameTime); |
---|
| 404 | frameTime = 0.0f; |
---|
| 405 | renderTime = 0.0f; |
---|
| 406 | } |
---|
[1446] | 407 | |
---|
[1414] | 408 | // Call those objects that need the real time |
---|
[1422] | 409 | for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it) |
---|
| 410 | it->tick((float)evt.timeSinceLastFrame); |
---|
| 411 | // Call the scene objects |
---|
[1092] | 412 | for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it) |
---|
| 413 | it->tick((float)evt.timeSinceLastFrame * this->timefactor_); |
---|
[1038] | 414 | |
---|
| 415 | // don't forget to call _fireFrameStarted in ogre to make sure |
---|
| 416 | // everything goes smoothly |
---|
[1120] | 417 | ogreRoot._fireFrameStarted(evt); |
---|
[1038] | 418 | |
---|
[1422] | 419 | // get current time |
---|
| 420 | now = timer_->getMilliseconds(); |
---|
| 421 | calculateEventTime(now, eventTimes[2]); |
---|
| 422 | |
---|
[1120] | 423 | ogreRoot._updateAllRenderTargets(); // only render in non-server mode |
---|
[1038] | 424 | |
---|
| 425 | // get current time |
---|
| 426 | now = timer_->getMilliseconds(); |
---|
| 427 | |
---|
| 428 | // create an event to pass to the frameEnded method in ogre |
---|
| 429 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
[1422] | 430 | renderTime += calculateEventTime(now, eventTimes[2]); |
---|
[1038] | 431 | |
---|
| 432 | // again, just to be sure ogre works fine |
---|
[1120] | 433 | ogreRoot._fireFrameEnded(evt); |
---|
[1038] | 434 | } |
---|
[1293] | 435 | |
---|
[1414] | 436 | if (mode_==CLIENT) |
---|
[1293] | 437 | network::Client::getSingleton()->closeConnection(); |
---|
[1414] | 438 | else if (mode_==SERVER) |
---|
[1293] | 439 | server_g->close(); |
---|
[1414] | 440 | |
---|
[1293] | 441 | return true; |
---|
[1038] | 442 | } |
---|
| 443 | |
---|
| 444 | /** |
---|
| 445 | Method for calculating the average time between recently fired events. |
---|
| 446 | Code directly taken from OgreRoot.cc |
---|
| 447 | @param now The current time in ms. |
---|
| 448 | @param type The type of event to be considered. |
---|
| 449 | */ |
---|
| 450 | float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> ×) |
---|
| 451 | { |
---|
| 452 | // Calculate the average time passed between events of the given type |
---|
| 453 | // during the last frameSmoothingTime_ seconds. |
---|
| 454 | |
---|
| 455 | times.push_back(now); |
---|
| 456 | |
---|
| 457 | if(times.size() == 1) |
---|
| 458 | return 0; |
---|
| 459 | |
---|
| 460 | // Times up to frameSmoothingTime_ seconds old should be kept |
---|
| 461 | unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f); |
---|
| 462 | |
---|
| 463 | // Find the oldest time to keep |
---|
| 464 | std::deque<unsigned long>::iterator it = times.begin(); |
---|
| 465 | // We need at least two times |
---|
| 466 | std::deque<unsigned long>::iterator end = times.end() - 2; |
---|
| 467 | |
---|
| 468 | while(it != end) |
---|
| 469 | { |
---|
| 470 | if (now - *it > discardThreshold) |
---|
| 471 | ++it; |
---|
| 472 | else |
---|
| 473 | break; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | // Remove old times |
---|
| 477 | times.erase(times.begin(), it); |
---|
| 478 | |
---|
| 479 | return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000); |
---|
| 480 | } |
---|
| 481 | } |
---|