Changeset 1629 for code/branches/input/src/orxonox/Orxonox.cc
- Timestamp:
- Jun 27, 2008, 8:07:29 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/orxonox/Orxonox.cc
r1535 r1629 69 69 70 70 // objects and tools 71 #include "hud/HUD.h" 71 #include "overlays/OverlayGroup.h" 72 #include "overlays/console/InGameConsole.h" 72 73 #include "objects/Tickable.h" 74 #include "objects/Backlight.h" 75 #include "tools/ParticleInterface.h" 73 76 74 77 #include "GraphicsEngine.h" 75 78 #include "Settings.h" 76 77 // FIXME: is this really file scope? 79 #include "Radar.h" 80 78 81 // globals for the server or client 79 network::Client *client_g = 0;80 network::Server *server_g = 0;82 static network::Client *client_g = 0; 83 static network::Server *server_g = 0; 81 84 82 85 namespace orxonox … … 94 97 * Create a new instance of Orxonox. Avoid doing any actual work here. 95 98 */ 96 Orxonox::Orxonox() :97 ogre_(0),98 //auMan_(0),99 timer_(0),100 // turn on frame smoothing by setting a value different from 0101 frameSmoothingTime_(0.0f),102 orxonoxHUD_(0),103 bAbort_(false),104 timefactor_(1.0f),105 mode_(STANDALONE),106 serverIp_(""),107 serverPort_(NETWORK_PORT)99 Orxonox::Orxonox() 100 : ogre_(0) 101 , startLevel_(0) 102 , hud_(0) 103 , radar_(0) 104 //, auMan_(0) 105 , timer_(0) 106 , bAbort_(false) 107 , timefactor_(1.0f) 108 , mode_(STANDALONE) 109 , serverIp_("") 110 , serverPort_(NETWORK_PORT) 108 111 { 109 112 } … … 115 118 { 116 119 // keep in mind: the order of deletion is very important! 117 // if (this->orxonoxHUD_) 118 // delete this->orxonoxHUD_; 120 Loader::unload(startLevel_); 121 if (this->startLevel_) 122 delete this->startLevel_; 123 124 Loader::unload(hud_); 125 if (this->hud_) 126 delete this->hud_; 127 128 if (this->radar_) 129 delete this->radar_; 130 119 131 Loader::close(); 120 InputManager::destroy();121 132 //if (this->auMan_) 122 133 // delete this->auMan_; 134 InGameConsole::getInstance().destroy(); 123 135 if (this->timer_) 124 136 delete this->timer_; 137 InputManager::destroy(); 125 138 GraphicsEngine::getSingleton().destroy(); 126 139 … … 159 172 delete singletonRef_s; 160 173 singletonRef_s = 0; 174 } 175 176 /** 177 @brief Changes the speed of Orxonox 178 */ 179 void Orxonox::setTimeFactor(float factor) 180 { 181 float change = factor / Orxonox::getSingleton()->getTimeFactor(); 182 Orxonox::getSingleton()->timefactor_ = factor; 183 for (Iterator<ParticleInterface> it = ObjectList<ParticleInterface>::begin(); it; ++it) 184 it->setSpeedFactor(it->getSpeedFactor() * change); 185 186 for (Iterator<Backlight> it = ObjectList<Backlight>::begin(); it; ++it) 187 it->setTimeFactor(Orxonox::getSingleton()->getTimeFactor()); 161 188 } 162 189 … … 251 278 return false; 252 279 280 // TODO: Spread this so that this call only initialises things needed for the Console 281 if (!ogre_->initialiseResources()) 282 return false; 283 284 // Load the InGameConsole 285 InGameConsole::getInstance().initialise(); 286 253 287 // Calls the InputManager which sets up the input devices. 254 288 // The render window width and height are used to set up the mouse movement. … … 257 291 return false; 258 292 259 // TODO: Spread this so that this call only initialises things needed for the GUI260 if (!ogre_->initialiseResources())261 return false;262 263 293 // TOOD: load the GUI here 264 294 // set InputManager to GUI mode … … 303 333 /** 304 334 * Loads everything in the scene except for the actual objects. 305 * This includes HUD, Console..335 * This includes HUD, audio.. 306 336 */ 307 337 bool Orxonox::loadPlayground() … … 316 346 317 347 // Load the HUD 318 COUT(3) << "Orxonox: Loading HUD..." << std::endl; 319 orxonoxHUD_ = &HUD::getSingleton(); 348 COUT(3) << "Orxonox: Loading HUD" << std::endl; 349 hud_ = new Level(Settings::getDataPath() + "overlay/hud.oxo"); 350 Loader::load(hud_); 351 352 // Start the Radar 353 this->radar_ = new Radar(); 354 320 355 return true; 321 356 } … … 326 361 bool Orxonox::serverLoad() 327 362 { 328 COUT( 2) << "Loading level in server mode" << std::endl;363 COUT(0) << "Loading level in server mode" << std::endl; 329 364 330 365 //server_g = new network::Server(serverPort_); … … 344 379 bool Orxonox::clientLoad() 345 380 { 346 COUT( 2) << "Loading level in client mode" << std::endl;\381 COUT(0) << "Loading level in client mode" << std::endl;\ 347 382 348 383 if (serverIp_.compare("") == 0) … … 364 399 bool Orxonox::standaloneLoad() 365 400 { 366 COUT( 2) << "Loading level in standalone mode" << std::endl;401 COUT(0) << "Loading level in standalone mode" << std::endl; 367 402 368 403 if (!loadScene()) … … 377 412 bool Orxonox::loadScene() 378 413 { 379 Level* startlevel= new Level("levels/sample.oxw");380 Loader::open(start level);381 414 startLevel_ = new Level("levels/sample.oxw"); 415 Loader::open(startLevel_); 416 382 417 return true; 383 418 } … … 403 438 404 439 405 // Contains the times of recently fired events406 // eventTimes[4] is the list for the times required for the fps counter407 std::deque<unsigned long> eventTimes[3];408 // Clear event times409 for (int i = 0; i < 3; ++i)410 eventTimes[i].clear();411 412 440 // use the ogre timer class to measure time. 413 441 if (!timer_) 414 442 timer_ = new Ogre::Timer(); 443 444 unsigned long frameCount = 0; 445 446 // TODO: this would very well fit into a configValue 447 const unsigned long refreshTime = 200000; 448 unsigned long refreshStartTime = 0; 449 unsigned long tickTime = 0; 450 unsigned long oldFrameCount = 0; 451 452 unsigned long timeBeforeTick = 0; 453 unsigned long timeBeforeTickOld = 0; 454 unsigned long timeAfterTick = 0; 455 456 COUT(3) << "Orxonox: Starting the main loop." << std::endl; 457 415 458 timer_->reset(); 416 417 float renderTime = 0.0f;418 float frameTime = 0.0f;419 clock_t time = 0;420 421 //Ogre::SceneManager* mSceneMgr = GraphicsEngine::getSingleton().getSceneManager();422 //Ogre::Viewport* mViewport = mSceneMgr->getCurrentViewport();423 424 //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Bloom");425 //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "MotionBlur");426 427 COUT(3) << "Orxonox: Starting the main loop." << std::endl;428 459 while (!bAbort_) 429 460 { 430 461 // get current time 431 unsigned long now = timer_->getMilliseconds(); 432 433 // create an event to pass to the frameStarted method in ogre 434 Ogre::FrameEvent evt; 435 evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); 436 evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]); 437 frameTime += evt.timeSinceLastFrame; 438 439 // show the current time in the HUD 440 // HUD::getSingleton().setTime(now); 441 if (mode_ != DEDICATED && frameTime > 0.4f) 442 { 443 HUD::getSingleton().setRenderTimeRatio(renderTime / frameTime); 444 frameTime = 0.0f; 445 renderTime = 0.0f; 446 } 447 448 // tick the core 449 Core::tick((float)evt.timeSinceLastFrame); 462 timeBeforeTickOld = timeBeforeTick; 463 timeBeforeTick = timer_->getMicroseconds(); 464 float dt = (timeBeforeTick - timeBeforeTickOld) / 1000000.0; 465 466 467 // tick the core (needs real time for input and tcl thread management) 468 Core::tick(dt); 469 450 470 // Call those objects that need the real time 451 471 for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it) 452 it->tick( (float)evt.timeSinceLastFrame);472 it->tick(dt); 453 473 // Call the scene objects 454 474 for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it) 455 it->tick((float)evt.timeSinceLastFrame * this->timefactor_); 456 //AudioManager::tick(); 475 it->tick(dt * this->timefactor_); 476 477 // call server/client with normal dt 457 478 if (client_g) 458 client_g->tick( (float)evt.timeSinceLastFrame);479 client_g->tick(dt * this->timefactor_); 459 480 if (server_g) 460 server_g->tick((float)evt.timeSinceLastFrame); 481 server_g->tick(dt * this->timefactor_); 482 483 484 // get current time once again 485 timeAfterTick = timer_->getMicroseconds(); 486 487 tickTime += timeAfterTick - timeBeforeTick; 488 if (timeAfterTick > refreshStartTime + refreshTime) 489 { 490 GraphicsEngine::getSingleton().setAverageTickTime( 491 (float)tickTime * 0.001 / (frameCount - oldFrameCount)); 492 GraphicsEngine::getSingleton().setAverageFramesPerSecond( 493 (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0); 494 oldFrameCount = frameCount; 495 tickTime = 0; 496 refreshStartTime = timeAfterTick; 497 } 498 461 499 462 500 // don't forget to call _fireFrameStarted in ogre to make sure 463 501 // everything goes smoothly 502 Ogre::FrameEvent evt; 503 evt.timeSinceLastFrame = dt; 504 evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway 464 505 ogreRoot._fireFrameStarted(evt); 465 466 // get current time467 now = timer_->getMilliseconds();468 calculateEventTime(now, eventTimes[2]);469 506 470 507 if (mode_ != DEDICATED) … … 473 510 // This calls the WindowEventListener objects. 474 511 Ogre::WindowEventUtilities::messagePump(); 512 // make sure the window stays active even when not focused 513 // (probably only necessary on windows) 514 GraphicsEngine::getSingleton().setWindowActivity(true); 475 515 476 516 // render … … 478 518 } 479 519 480 // get current time481 now = timer_->getMilliseconds();482 483 // create an event to pass to the frameEnded method in ogre484 evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]);485 renderTime += calculateEventTime(now, eventTimes[2]);486 487 520 // again, just to be sure ogre works fine 488 ogreRoot._fireFrameEnded(evt); 489 //msleep(200); 521 ogreRoot._fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted 522 523 ++frameCount; 490 524 } 491 525 … … 497 531 return true; 498 532 } 499 500 /**501 Method for calculating the average time between recently fired events.502 Code directly taken from OgreRoot.cc503 @param now The current time in ms.504 @param type The type of event to be considered.505 */506 float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> ×)507 {508 // Calculate the average time passed between events of the given type509 // during the last frameSmoothingTime_ seconds.510 511 times.push_back(now);512 513 if(times.size() == 1)514 return 0;515 516 // Times up to frameSmoothingTime_ seconds old should be kept517 unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f);518 519 // Find the oldest time to keep520 std::deque<unsigned long>::iterator it = times.begin();521 // We need at least two times522 std::deque<unsigned long>::iterator end = times.end() - 2;523 524 while(it != end)525 {526 if (now - *it > discardThreshold)527 ++it;528 else529 break;530 }531 532 // Remove old times533 times.erase(times.begin(), it);534 535 return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000);536 }537 533 }
Note: See TracChangeset
for help on using the changeset viewer.