[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: |
---|
[1535] | 23 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
[1038] | 25 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Orxonox Main Class |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | // Precompiled Headers |
---|
| 35 | #include "OrxonoxStableHeaders.h" |
---|
[1777] | 36 | #include "util/MathConvert.h" |
---|
[1039] | 37 | #include "Orxonox.h" |
---|
[1038] | 38 | |
---|
| 39 | //****** STD ******* |
---|
| 40 | #include <deque> |
---|
| 41 | |
---|
| 42 | //****** OGRE ****** |
---|
| 43 | #include <OgreFrameListener.h> |
---|
| 44 | #include <OgreOverlay.h> |
---|
| 45 | #include <OgreOverlayManager.h> |
---|
| 46 | #include <OgreRoot.h> |
---|
| 47 | #include <OgreTimer.h> |
---|
| 48 | #include <OgreWindowEventUtilities.h> |
---|
| 49 | |
---|
| 50 | //***** ORXONOX **** |
---|
| 51 | // util |
---|
| 52 | //#include "util/Sleep.h" |
---|
| 53 | #include "util/ArgReader.h" |
---|
| 54 | |
---|
[1293] | 55 | // core |
---|
[1054] | 56 | #include "core/ConfigFileManager.h" |
---|
[1591] | 57 | #include "core/Iterator.h" |
---|
[1293] | 58 | #include "core/ConsoleCommand.h" |
---|
[1038] | 59 | #include "core/Loader.h" |
---|
[1535] | 60 | #include "core/input/InputManager.h" |
---|
[1293] | 61 | #include "core/TclBind.h" |
---|
[1535] | 62 | #include "core/Core.h" |
---|
[1586] | 63 | #include "util/Debug.h" |
---|
[1038] | 64 | |
---|
| 65 | // audio |
---|
| 66 | #include "audio/AudioManager.h" |
---|
| 67 | |
---|
| 68 | // network |
---|
| 69 | #include "network/Server.h" |
---|
| 70 | #include "network/Client.h" |
---|
| 71 | |
---|
| 72 | // objects and tools |
---|
| 73 | #include "hud/HUD.h" |
---|
[1535] | 74 | #include "objects/Tickable.h" |
---|
[1563] | 75 | #include "tools/ParticleInterface.h" |
---|
[1038] | 76 | |
---|
[1502] | 77 | #include "GraphicsEngine.h" |
---|
[1535] | 78 | #include "Settings.h" |
---|
[1502] | 79 | |
---|
[1777] | 80 | |
---|
[1766] | 81 | #define TestConv(nr, Type1, var1, Type2, res) \ |
---|
| 82 | Type1 var##nr##1##var1; \ |
---|
| 83 | Type2 var##nr##2; \ |
---|
| 84 | assert(ConvertValue(&var##nr##2, var##nr##1)); \ |
---|
| 85 | COUT(0) << "Converting " << #var1 << " (" << #Type1 << ") to " << var##nr##2 << " (" #Type2 << ")." << std::endl; \ |
---|
| 86 | assert(res == var##nr##2) |
---|
[1591] | 87 | |
---|
[1778] | 88 | //template <> |
---|
| 89 | //struct ConverterExplicit<orxonox::Radian, const char*> |
---|
| 90 | //{ |
---|
| 91 | // static bool convert(orxonox::Radian* output, const char* input) |
---|
| 92 | // { |
---|
| 93 | // //std::string asdf("asfd"); |
---|
| 94 | // //std::cout << asdf; |
---|
| 95 | // float temp; |
---|
| 96 | // convertValue(&temp, input); |
---|
| 97 | // *output = temp; |
---|
| 98 | // } |
---|
| 99 | //}; |
---|
[1766] | 100 | |
---|
[1768] | 101 | class FooBar { }; |
---|
[1766] | 102 | |
---|
[1777] | 103 | inline bool explicitConversion(FooBar* output, std::string input) |
---|
| 104 | { |
---|
| 105 | return true; |
---|
| 106 | } |
---|
[1768] | 107 | |
---|
[1777] | 108 | //inline bool explicitConversion(std::string* output, const orxonox::Vector3& input) |
---|
| 109 | //{ |
---|
| 110 | // std::ostringstream ostream; |
---|
| 111 | // if (ostream << input.x << "," << input.y << "," << input.z) |
---|
| 112 | // { |
---|
| 113 | // (*output) = ostream.str(); |
---|
| 114 | // return true; |
---|
| 115 | // } |
---|
| 116 | // return false; |
---|
| 117 | //} |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
[1038] | 121 | // FIXME: is this really file scope? |
---|
| 122 | // globals for the server or client |
---|
[1535] | 123 | network::Client *client_g = 0; |
---|
| 124 | network::Server *server_g = 0; |
---|
[1038] | 125 | |
---|
| 126 | namespace orxonox |
---|
[1293] | 127 | { |
---|
[1594] | 128 | SetConsoleCommandShortcut(Orxonox, exit).keybindMode(KeybindMode::OnPress); |
---|
| 129 | SetConsoleCommandShortcut(Orxonox, slomo).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false); |
---|
| 130 | SetConsoleCommandShortcut(Orxonox, setTimeFactor).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); |
---|
[1505] | 131 | |
---|
[1038] | 132 | /** |
---|
| 133 | @brief Reference to the only instance of the class. |
---|
| 134 | */ |
---|
| 135 | Orxonox *Orxonox::singletonRef_s = 0; |
---|
| 136 | |
---|
| 137 | /** |
---|
[1293] | 138 | * Create a new instance of Orxonox. Avoid doing any actual work here. |
---|
[1038] | 139 | */ |
---|
[1293] | 140 | Orxonox::Orxonox() : |
---|
| 141 | ogre_(0), |
---|
| 142 | //auMan_(0), |
---|
| 143 | timer_(0), |
---|
| 144 | // turn on frame smoothing by setting a value different from 0 |
---|
| 145 | frameSmoothingTime_(0.0f), |
---|
| 146 | orxonoxHUD_(0), |
---|
| 147 | bAbort_(false), |
---|
| 148 | timefactor_(1.0f), |
---|
| 149 | mode_(STANDALONE), |
---|
[1502] | 150 | serverIp_(""), |
---|
| 151 | serverPort_(NETWORK_PORT) |
---|
[1038] | 152 | { |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
[1293] | 156 | * Destruct Orxonox. |
---|
[1038] | 157 | */ |
---|
| 158 | Orxonox::~Orxonox() |
---|
| 159 | { |
---|
| 160 | // keep in mind: the order of deletion is very important! |
---|
[1567] | 161 | this->orxonoxHUD_->destroy(); |
---|
[1038] | 162 | Loader::close(); |
---|
[1219] | 163 | InputManager::destroy(); |
---|
[1293] | 164 | //if (this->auMan_) |
---|
| 165 | // delete this->auMan_; |
---|
[1038] | 166 | if (this->timer_) |
---|
| 167 | delete this->timer_; |
---|
| 168 | GraphicsEngine::getSingleton().destroy(); |
---|
| 169 | |
---|
[1293] | 170 | if (network::Client::getSingleton()) |
---|
| 171 | network::Client::destroySingleton(); |
---|
[1038] | 172 | if (server_g) |
---|
[1534] | 173 | delete network::Server::getSingleton(); |
---|
[1038] | 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | Asks the mainloop nicely to abort. |
---|
| 179 | */ |
---|
| 180 | void Orxonox::abortRequest() |
---|
| 181 | { |
---|
[1293] | 182 | COUT(3) << "Orxonox: Abort requested." << std::endl; |
---|
[1038] | 183 | bAbort_ = true; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
[1293] | 187 | * @return singleton reference |
---|
[1038] | 188 | */ |
---|
| 189 | Orxonox* Orxonox::getSingleton() |
---|
| 190 | { |
---|
| 191 | if (!singletonRef_s) |
---|
| 192 | singletonRef_s = new Orxonox(); |
---|
| 193 | return singletonRef_s; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | @brief Destroys the Orxonox singleton. |
---|
| 198 | */ |
---|
| 199 | void Orxonox::destroySingleton() |
---|
| 200 | { |
---|
| 201 | if (singletonRef_s) |
---|
| 202 | delete singletonRef_s; |
---|
| 203 | singletonRef_s = 0; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /** |
---|
[1563] | 207 | @brief Changes the speed of Orxonox |
---|
| 208 | */ |
---|
| 209 | void Orxonox::setTimeFactor(float factor) |
---|
| 210 | { |
---|
| 211 | float change = factor / Orxonox::getSingleton()->getTimeFactor(); |
---|
| 212 | Orxonox::getSingleton()->timefactor_ = factor; |
---|
| 213 | |
---|
[1591] | 214 | for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it) |
---|
[1563] | 215 | it->setSpeedFactor(it->getSpeedFactor() * change); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
[1038] | 219 | * initialization of Orxonox object |
---|
| 220 | * @param argc argument counter |
---|
| 221 | * @param argv list of argumenst |
---|
| 222 | * @param path path to config (in home dir or something) |
---|
| 223 | */ |
---|
[1535] | 224 | bool Orxonox::init(int argc, char **argv) |
---|
[1038] | 225 | { |
---|
[1535] | 226 | #ifdef _DEBUG |
---|
[1658] | 227 | ConfigFileManager::getInstance()->setFile(CFT_Settings, "orxonox_d.ini"); |
---|
[1535] | 228 | #else |
---|
[1658] | 229 | ConfigFileManager::getInstance()->setFile(CFT_Settings, "orxonox.ini"); |
---|
[1535] | 230 | #endif |
---|
| 231 | Factory::createClassHierarchy(); |
---|
| 232 | |
---|
[1777] | 233 | std::string nmbr; |
---|
| 234 | char res('a'); |
---|
[1766] | 235 | //const char* nmbr; |
---|
| 236 | //const char* str; |
---|
[1777] | 237 | convertValue(&nmbr, res); |
---|
[1766] | 238 | //const unsigned int blah = 4; |
---|
| 239 | //convertValue(nmbr, blah); |
---|
| 240 | //convertValue(&str, 4.0f); |
---|
| 241 | |
---|
[1768] | 242 | //using ::operator<<; |
---|
[1766] | 243 | using std::string; |
---|
| 244 | int a = 3; |
---|
[1768] | 245 | FooBar asdf; |
---|
| 246 | std::string asdf2; |
---|
[1777] | 247 | std::ostringstream oss; |
---|
| 248 | char blah(' '); |
---|
| 249 | oss << blah; |
---|
[1768] | 250 | std::cout << asdf2; |
---|
| 251 | ConvertValue(&asdf2, asdf); |
---|
[1777] | 252 | //if (conversionTests::OStreamOperator<FooBar>::exists) |
---|
| 253 | // int asdfasdfasdf = 7; |
---|
[1768] | 254 | //COUT(3) << asdf; |
---|
[1766] | 255 | |
---|
| 256 | TestConv(1, int, (3), float, 3.0); |
---|
| 257 | TestConv(2, int, (3), string, "3"); |
---|
| 258 | TestConv(3, string, ("3.0"), float, 3.0f); |
---|
| 259 | TestConv(4, char, ('a'), string, "a"); |
---|
| 260 | TestConv(5, string, ("df"), char, 'd'); |
---|
| 261 | TestConv(6, Vector2, (3,4), string, "3,4"); |
---|
| 262 | TestConv(7, const char*, ("4.3"), float, 4.3f); |
---|
| 263 | TestConv(8, const char*, ("4,3"), Vector2, Vector2(4,3)); |
---|
[1768] | 264 | //TestConv(9, const char*, ("4.4"), Radian, Radian(4.4)); |
---|
[1766] | 265 | TestConv(100, int, (3), const char*, "3"); |
---|
| 266 | TestConv(101, Vector3, (1, 2, 3), float, 3.0); |
---|
| 267 | |
---|
| 268 | std::ostringstream out; |
---|
| 269 | |
---|
[1038] | 270 | std::string mode; |
---|
[1535] | 271 | std::string tempDataPath; |
---|
[1038] | 272 | |
---|
| 273 | ArgReader ar(argc, argv); |
---|
[1535] | 274 | ar.checkArgument("mode", &mode, false); |
---|
| 275 | ar.checkArgument("data", &tempDataPath, false); |
---|
| 276 | ar.checkArgument("ip", &serverIp_, false); |
---|
| 277 | ar.checkArgument("port", &serverPort_, false); |
---|
[1293] | 278 | if(ar.errorHandling()) |
---|
[1535] | 279 | { |
---|
| 280 | COUT(1) << "Error while parsing command line arguments" << std::endl; |
---|
| 281 | COUT(1) << ar.getErrorString(); |
---|
| 282 | COUT(0) << "Usage:" << std::endl << "orxonox [mode client|server|dedicated|standalone] " |
---|
| 283 | << "[--data PATH] [--ip IP] [--port PORT]" << std::endl; |
---|
[1293] | 284 | return false; |
---|
[1535] | 285 | } |
---|
[1293] | 286 | |
---|
| 287 | if (mode == "client") |
---|
[1038] | 288 | mode_ = CLIENT; |
---|
[1293] | 289 | else if (mode == "server") |
---|
[1038] | 290 | mode_ = SERVER; |
---|
[1502] | 291 | else if (mode == "dedicated") |
---|
| 292 | mode_ = DEDICATED; |
---|
[1293] | 293 | else |
---|
| 294 | { |
---|
[1535] | 295 | if (mode == "") |
---|
| 296 | mode = "standalone"; |
---|
| 297 | if (mode != "standalone") |
---|
| 298 | { |
---|
| 299 | COUT(2) << "Warning: mode \"" << mode << "\" doesn't exist. " |
---|
| 300 | << "Defaulting to standalone" << std::endl; |
---|
| 301 | mode = "standalone"; |
---|
| 302 | } |
---|
[1038] | 303 | mode_ = STANDALONE; |
---|
| 304 | } |
---|
[1293] | 305 | COUT(3) << "Orxonox: Mode is " << mode << "." << std::endl; |
---|
[1038] | 306 | |
---|
[1535] | 307 | if (tempDataPath != "") |
---|
| 308 | { |
---|
| 309 | if (tempDataPath[tempDataPath.size() - 1] != '/') |
---|
| 310 | tempDataPath += "/"; |
---|
| 311 | Settings::tsetDataPath(tempDataPath); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | // initialise TCL |
---|
| 315 | TclBind::getInstance().setDataPath(Settings::getDataPath()); |
---|
| 316 | |
---|
[1293] | 317 | //if (mode_ == DEDICATED) |
---|
| 318 | // TODO: decide what to do here |
---|
| 319 | //else |
---|
[1052] | 320 | |
---|
[1293] | 321 | // for playable server, client and standalone, the startup |
---|
| 322 | // procedure until the GUI is identical |
---|
[1052] | 323 | |
---|
[1293] | 324 | ogre_ = &GraphicsEngine::getSingleton(); |
---|
[1535] | 325 | if (!ogre_->setup()) // creates ogre root and other essentials |
---|
[1293] | 326 | return false; |
---|
[1052] | 327 | |
---|
[1293] | 328 | return true; |
---|
[1038] | 329 | } |
---|
[1052] | 330 | |
---|
[1038] | 331 | /** |
---|
| 332 | * start modules |
---|
| 333 | */ |
---|
[1293] | 334 | bool Orxonox::start() |
---|
[1038] | 335 | { |
---|
[1502] | 336 | if (mode_ == DEDICATED) |
---|
| 337 | { |
---|
| 338 | // do something else |
---|
| 339 | } |
---|
| 340 | else |
---|
| 341 | { // not dedicated server |
---|
| 342 | if (!ogre_->loadRenderer()) // creates the render window |
---|
| 343 | return false; |
---|
[1293] | 344 | |
---|
[1502] | 345 | // Calls the InputManager which sets up the input devices. |
---|
| 346 | // The render window width and height are used to set up the mouse movement. |
---|
| 347 | if (!InputManager::initialise(ogre_->getWindowHandle(), |
---|
| 348 | ogre_->getWindowWidth(), ogre_->getWindowHeight(), true, true, true)) |
---|
| 349 | return false; |
---|
[1293] | 350 | |
---|
[1502] | 351 | // TODO: Spread this so that this call only initialises things needed for the GUI |
---|
| 352 | if (!ogre_->initialiseResources()) |
---|
| 353 | return false; |
---|
[1293] | 354 | |
---|
[1502] | 355 | // TOOD: load the GUI here |
---|
| 356 | // set InputManager to GUI mode |
---|
| 357 | InputManager::setInputState(InputManager::IS_GUI); |
---|
| 358 | // TODO: run GUI here |
---|
[1293] | 359 | |
---|
[1502] | 360 | // The following lines depend very much on the GUI output, so they're probably misplaced here.. |
---|
[1293] | 361 | |
---|
[1502] | 362 | InputManager::setInputState(InputManager::IS_NONE); |
---|
[1293] | 363 | |
---|
[1502] | 364 | // create Ogre SceneManager |
---|
| 365 | ogre_->createNewScene(); |
---|
[1293] | 366 | |
---|
[1502] | 367 | if (!loadPlayground()) |
---|
| 368 | return false; |
---|
| 369 | } |
---|
[1293] | 370 | |
---|
| 371 | switch (mode_) |
---|
| 372 | { |
---|
| 373 | case SERVER: |
---|
| 374 | if (!serverLoad()) |
---|
| 375 | return false; |
---|
| 376 | break; |
---|
[1038] | 377 | case CLIENT: |
---|
[1293] | 378 | if (!clientLoad()) |
---|
| 379 | return false; |
---|
[1038] | 380 | break; |
---|
[1502] | 381 | case DEDICATED: |
---|
| 382 | if (!serverLoad()) |
---|
| 383 | return false; |
---|
| 384 | break; |
---|
[1038] | 385 | default: |
---|
[1293] | 386 | if (!standaloneLoad()) |
---|
| 387 | return false; |
---|
[1038] | 388 | } |
---|
[1293] | 389 | |
---|
| 390 | InputManager::setInputState(InputManager::IS_NORMAL); |
---|
| 391 | |
---|
| 392 | return startRenderLoop(); |
---|
[1038] | 393 | } |
---|
[1052] | 394 | |
---|
[1293] | 395 | /** |
---|
| 396 | * Loads everything in the scene except for the actual objects. |
---|
| 397 | * This includes HUD, Console.. |
---|
| 398 | */ |
---|
| 399 | bool Orxonox::loadPlayground() |
---|
| 400 | { |
---|
[1502] | 401 | // Init audio |
---|
[1293] | 402 | //auMan_ = new audio::AudioManager(); |
---|
| 403 | //auMan_->ambientAdd("a1"); |
---|
| 404 | //auMan_->ambientAdd("a2"); |
---|
| 405 | //auMan_->ambientAdd("a3"); |
---|
| 406 | //auMan->ambientAdd("ambient1"); |
---|
| 407 | //auMan_->ambientStart(); |
---|
[1052] | 408 | |
---|
[1293] | 409 | // Load the HUD |
---|
| 410 | COUT(3) << "Orxonox: Loading HUD..." << std::endl; |
---|
[1502] | 411 | orxonoxHUD_ = &HUD::getSingleton(); |
---|
[1567] | 412 | orxonoxHUD_->initialise(); |
---|
[1293] | 413 | return true; |
---|
| 414 | } |
---|
[1052] | 415 | |
---|
[1293] | 416 | /** |
---|
| 417 | * Level loading method for server mode. |
---|
| 418 | */ |
---|
| 419 | bool Orxonox::serverLoad() |
---|
| 420 | { |
---|
| 421 | COUT(2) << "Loading level in server mode" << std::endl; |
---|
[1052] | 422 | |
---|
[1534] | 423 | //server_g = new network::Server(serverPort_); |
---|
| 424 | server_g = network::Server::createSingleton(serverPort_); |
---|
[1052] | 425 | |
---|
[1293] | 426 | if (!loadScene()) |
---|
| 427 | return false; |
---|
[1052] | 428 | |
---|
[1038] | 429 | server_g->open(); |
---|
[1052] | 430 | |
---|
[1293] | 431 | return true; |
---|
[1038] | 432 | } |
---|
[1052] | 433 | |
---|
[1293] | 434 | /** |
---|
| 435 | * Level loading method for client mode. |
---|
| 436 | */ |
---|
| 437 | bool Orxonox::clientLoad() |
---|
| 438 | { |
---|
| 439 | COUT(2) << "Loading level in client mode" << std::endl;\ |
---|
[1052] | 440 | |
---|
[1293] | 441 | if (serverIp_.compare("") == 0) |
---|
| 442 | client_g = network::Client::createSingleton(); |
---|
| 443 | else |
---|
| 444 | |
---|
[1502] | 445 | client_g = network::Client::createSingleton(serverIp_, serverPort_); |
---|
| 446 | |
---|
| 447 | if(!client_g->establishConnection()) |
---|
| 448 | return false; |
---|
[1293] | 449 | client_g->tick(0); |
---|
| 450 | |
---|
| 451 | return true; |
---|
[1038] | 452 | } |
---|
| 453 | |
---|
[1293] | 454 | /** |
---|
| 455 | * Level loading method for standalone mode. |
---|
| 456 | */ |
---|
| 457 | bool Orxonox::standaloneLoad() |
---|
[1038] | 458 | { |
---|
[1293] | 459 | COUT(2) << "Loading level in standalone mode" << std::endl; |
---|
[1038] | 460 | |
---|
[1293] | 461 | if (!loadScene()) |
---|
| 462 | return false; |
---|
[1038] | 463 | |
---|
[1293] | 464 | return true; |
---|
[1038] | 465 | } |
---|
| 466 | |
---|
| 467 | /** |
---|
[1293] | 468 | * Helper method to load a level. |
---|
| 469 | */ |
---|
| 470 | bool Orxonox::loadScene() |
---|
[1038] | 471 | { |
---|
[1293] | 472 | Level* startlevel = new Level("levels/sample.oxw"); |
---|
| 473 | Loader::open(startlevel); |
---|
[1563] | 474 | |
---|
[1293] | 475 | return true; |
---|
[1038] | 476 | } |
---|
| 477 | |
---|
[1293] | 478 | |
---|
[1038] | 479 | /** |
---|
| 480 | Main loop of the orxonox game. |
---|
| 481 | About the loop: The design is almost exactly like the one in ogre, so that |
---|
| 482 | if any part of ogre registers a framelisteners, it will still behave |
---|
| 483 | correctly. Furthermore the time smoothing feature from ogre has been |
---|
| 484 | implemented too. If turned on (see orxonox constructor), it will calculate |
---|
| 485 | the dt_n by means of the recent most dt_n-1, dt_n-2, etc. |
---|
| 486 | */ |
---|
[1293] | 487 | bool Orxonox::startRenderLoop() |
---|
[1038] | 488 | { |
---|
| 489 | // first check whether ogre root object has been created |
---|
| 490 | if (Ogre::Root::getSingletonPtr() == 0) |
---|
| 491 | { |
---|
[1293] | 492 | COUT(2) << "Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl; |
---|
| 493 | return false; |
---|
[1038] | 494 | } |
---|
[1120] | 495 | Ogre::Root& ogreRoot = Ogre::Root::getSingleton(); |
---|
[1038] | 496 | |
---|
[1120] | 497 | |
---|
[1038] | 498 | // Contains the times of recently fired events |
---|
| 499 | // eventTimes[4] is the list for the times required for the fps counter |
---|
[1502] | 500 | std::deque<unsigned long> eventTimes[3]; |
---|
[1038] | 501 | // Clear event times |
---|
[1502] | 502 | for (int i = 0; i < 3; ++i) |
---|
[1038] | 503 | eventTimes[i].clear(); |
---|
| 504 | |
---|
| 505 | // use the ogre timer class to measure time. |
---|
| 506 | if (!timer_) |
---|
| 507 | timer_ = new Ogre::Timer(); |
---|
| 508 | timer_->reset(); |
---|
| 509 | |
---|
[1502] | 510 | float renderTime = 0.0f; |
---|
| 511 | float frameTime = 0.0f; |
---|
[1556] | 512 | // clock_t time = 0; |
---|
[1502] | 513 | |
---|
| 514 | //Ogre::SceneManager* mSceneMgr = GraphicsEngine::getSingleton().getSceneManager(); |
---|
| 515 | //Ogre::Viewport* mViewport = mSceneMgr->getCurrentViewport(); |
---|
[1563] | 516 | |
---|
[1502] | 517 | //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Bloom"); |
---|
| 518 | //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "MotionBlur"); |
---|
| 519 | |
---|
[1293] | 520 | COUT(3) << "Orxonox: Starting the main loop." << std::endl; |
---|
[1502] | 521 | while (!bAbort_) |
---|
| 522 | { |
---|
[1038] | 523 | // get current time |
---|
| 524 | unsigned long now = timer_->getMilliseconds(); |
---|
| 525 | |
---|
| 526 | // create an event to pass to the frameStarted method in ogre |
---|
| 527 | Ogre::FrameEvent evt; |
---|
| 528 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
| 529 | evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]); |
---|
[1502] | 530 | frameTime += evt.timeSinceLastFrame; |
---|
[1038] | 531 | |
---|
| 532 | // show the current time in the HUD |
---|
[1502] | 533 | // HUD::getSingleton().setTime(now); |
---|
| 534 | if (mode_ != DEDICATED && frameTime > 0.4f) |
---|
| 535 | { |
---|
| 536 | HUD::getSingleton().setRenderTimeRatio(renderTime / frameTime); |
---|
| 537 | frameTime = 0.0f; |
---|
| 538 | renderTime = 0.0f; |
---|
| 539 | } |
---|
[1038] | 540 | |
---|
[1535] | 541 | // tick the core |
---|
| 542 | Core::tick((float)evt.timeSinceLastFrame); |
---|
[1502] | 543 | // Call those objects that need the real time |
---|
[1591] | 544 | for (ObjectList<TickableReal>::iterator it = ObjectList<TickableReal>::begin(); it; ++it) |
---|
[1502] | 545 | it->tick((float)evt.timeSinceLastFrame); |
---|
| 546 | // Call the scene objects |
---|
[1591] | 547 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
[1092] | 548 | it->tick((float)evt.timeSinceLastFrame * this->timefactor_); |
---|
[1535] | 549 | //AudioManager::tick(); |
---|
| 550 | if (client_g) |
---|
| 551 | client_g->tick((float)evt.timeSinceLastFrame); |
---|
| 552 | if (server_g) |
---|
| 553 | server_g->tick((float)evt.timeSinceLastFrame); |
---|
[1038] | 554 | |
---|
| 555 | // don't forget to call _fireFrameStarted in ogre to make sure |
---|
| 556 | // everything goes smoothly |
---|
[1120] | 557 | ogreRoot._fireFrameStarted(evt); |
---|
[1038] | 558 | |
---|
[1502] | 559 | // get current time |
---|
| 560 | now = timer_->getMilliseconds(); |
---|
| 561 | calculateEventTime(now, eventTimes[2]); |
---|
[1038] | 562 | |
---|
[1502] | 563 | if (mode_ != DEDICATED) |
---|
| 564 | { |
---|
| 565 | // Pump messages in all registered RenderWindows |
---|
| 566 | // This calls the WindowEventListener objects. |
---|
| 567 | Ogre::WindowEventUtilities::messagePump(); |
---|
| 568 | |
---|
| 569 | // render |
---|
| 570 | ogreRoot._updateAllRenderTargets(); |
---|
| 571 | } |
---|
| 572 | |
---|
[1038] | 573 | // get current time |
---|
| 574 | now = timer_->getMilliseconds(); |
---|
| 575 | |
---|
| 576 | // create an event to pass to the frameEnded method in ogre |
---|
| 577 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
[1502] | 578 | renderTime += calculateEventTime(now, eventTimes[2]); |
---|
[1038] | 579 | |
---|
| 580 | // again, just to be sure ogre works fine |
---|
[1120] | 581 | ogreRoot._fireFrameEnded(evt); |
---|
[1502] | 582 | //msleep(200); |
---|
| 583 | } |
---|
[1293] | 584 | |
---|
[1502] | 585 | if (mode_ == CLIENT) |
---|
[1293] | 586 | network::Client::getSingleton()->closeConnection(); |
---|
[1502] | 587 | else if (mode_ == SERVER) |
---|
[1293] | 588 | server_g->close(); |
---|
[1502] | 589 | |
---|
[1293] | 590 | return true; |
---|
[1038] | 591 | } |
---|
| 592 | |
---|
| 593 | /** |
---|
| 594 | Method for calculating the average time between recently fired events. |
---|
| 595 | Code directly taken from OgreRoot.cc |
---|
| 596 | @param now The current time in ms. |
---|
| 597 | @param type The type of event to be considered. |
---|
| 598 | */ |
---|
| 599 | float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> ×) |
---|
| 600 | { |
---|
| 601 | // Calculate the average time passed between events of the given type |
---|
| 602 | // during the last frameSmoothingTime_ seconds. |
---|
| 603 | |
---|
| 604 | times.push_back(now); |
---|
| 605 | |
---|
| 606 | if(times.size() == 1) |
---|
| 607 | return 0; |
---|
| 608 | |
---|
| 609 | // Times up to frameSmoothingTime_ seconds old should be kept |
---|
| 610 | unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f); |
---|
| 611 | |
---|
| 612 | // Find the oldest time to keep |
---|
| 613 | std::deque<unsigned long>::iterator it = times.begin(); |
---|
| 614 | // We need at least two times |
---|
| 615 | std::deque<unsigned long>::iterator end = times.end() - 2; |
---|
| 616 | |
---|
| 617 | while(it != end) |
---|
| 618 | { |
---|
| 619 | if (now - *it > discardThreshold) |
---|
| 620 | ++it; |
---|
| 621 | else |
---|
| 622 | break; |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | // Remove old times |
---|
| 626 | times.erase(times.begin(), it); |
---|
| 627 | |
---|
| 628 | return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000); |
---|
| 629 | } |
---|
| 630 | } |
---|