Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 27, 2008, 8:07:29 AM (17 years ago)
Author:
rgrieder
Message:

updated input branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/orxonox/Orxonox.cc

    r1535 r1629  
    6969
    7070// objects and tools
    71 #include "hud/HUD.h"
     71#include "overlays/OverlayGroup.h"
     72#include "overlays/console/InGameConsole.h"
    7273#include "objects/Tickable.h"
     74#include "objects/Backlight.h"
     75#include "tools/ParticleInterface.h"
    7376
    7477#include "GraphicsEngine.h"
    7578#include "Settings.h"
    76 
    77 // FIXME: is this really file scope?
     79#include "Radar.h"
     80
    7881// globals for the server or client
    79 network::Client *client_g = 0;
    80 network::Server *server_g = 0;
     82static network::Client *client_g = 0;
     83static network::Server *server_g = 0;
    8184
    8285namespace orxonox
     
    9497   * Create a new instance of Orxonox. Avoid doing any actual work here.
    9598   */
    96   Orxonox::Orxonox() :
    97     ogre_(0),
    98     //auMan_(0),
    99     timer_(0),
    100     // turn on frame smoothing by setting a value different from 0
    101     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)
    108111  {
    109112  }
     
    115118  {
    116119    // 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
    119131    Loader::close();
    120     InputManager::destroy();
    121132    //if (this->auMan_)
    122133    //  delete this->auMan_;
     134    InGameConsole::getInstance().destroy();
    123135    if (this->timer_)
    124136      delete this->timer_;
     137    InputManager::destroy();
    125138    GraphicsEngine::getSingleton().destroy();
    126139
     
    159172      delete singletonRef_s;
    160173    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());
    161188  }
    162189
     
    251278        return false;
    252279
     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
    253287      // Calls the InputManager which sets up the input devices.
    254288      // The render window width and height are used to set up the mouse movement.
     
    257291        return false;
    258292
    259       // TODO: Spread this so that this call only initialises things needed for the GUI
    260       if (!ogre_->initialiseResources())
    261         return false;
    262 
    263293      // TOOD: load the GUI here
    264294      // set InputManager to GUI mode
     
    303333  /**
    304334   * Loads everything in the scene except for the actual objects.
    305    * This includes HUD, Console..
     335   * This includes HUD, audio..
    306336   */
    307337  bool Orxonox::loadPlayground()
     
    316346
    317347    // 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
    320355    return true;
    321356  }
     
    326361  bool Orxonox::serverLoad()
    327362  {
    328     COUT(2) << "Loading level in server mode" << std::endl;
     363    COUT(0) << "Loading level in server mode" << std::endl;
    329364
    330365    //server_g = new network::Server(serverPort_);
     
    344379  bool Orxonox::clientLoad()
    345380  {
    346     COUT(2) << "Loading level in client mode" << std::endl;\
     381    COUT(0) << "Loading level in client mode" << std::endl;\
    347382
    348383    if (serverIp_.compare("") == 0)
     
    364399  bool Orxonox::standaloneLoad()
    365400  {
    366     COUT(2) << "Loading level in standalone mode" << std::endl;
     401    COUT(0) << "Loading level in standalone mode" << std::endl;
    367402
    368403    if (!loadScene())
     
    377412  bool Orxonox::loadScene()
    378413  {
    379     Level* startlevel = new Level("levels/sample.oxw");
    380     Loader::open(startlevel);
    381    
     414    startLevel_ = new Level("levels/sample.oxw");
     415    Loader::open(startLevel_);
     416
    382417    return true;
    383418  }
     
    403438
    404439
    405     // Contains the times of recently fired events
    406     // eventTimes[4] is the list for the times required for the fps counter
    407     std::deque<unsigned long> eventTimes[3];
    408     // Clear event times
    409     for (int i = 0; i < 3; ++i)
    410       eventTimes[i].clear();
    411 
    412440    // use the ogre timer class to measure time.
    413441    if (!timer_)
    414442      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
    415458    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;
    428459    while (!bAbort_)
    429460    {
    430461      // 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
    450470      // Call those objects that need the real time
    451471      for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it)
    452         it->tick((float)evt.timeSinceLastFrame);
     472        it->tick(dt);
    453473      // Call the scene objects
    454474      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
    457478      if (client_g)
    458         client_g->tick((float)evt.timeSinceLastFrame);
     479        client_g->tick(dt * this->timefactor_);
    459480      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
    461499
    462500      // don't forget to call _fireFrameStarted in ogre to make sure
    463501      // everything goes smoothly
     502      Ogre::FrameEvent evt;
     503      evt.timeSinceLastFrame = dt;
     504      evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
    464505      ogreRoot._fireFrameStarted(evt);
    465 
    466       // get current time
    467       now = timer_->getMilliseconds();
    468       calculateEventTime(now, eventTimes[2]);
    469506
    470507      if (mode_ != DEDICATED)
     
    473510        // This calls the WindowEventListener objects.
    474511        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);
    475515
    476516        // render
     
    478518      }
    479519
    480       // get current time
    481       now = timer_->getMilliseconds();
    482 
    483       // create an event to pass to the frameEnded method in ogre
    484       evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]);
    485       renderTime += calculateEventTime(now, eventTimes[2]);
    486 
    487520      // 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;
    490524    }
    491525
     
    497531    return true;
    498532  }
    499 
    500   /**
    501     Method for calculating the average time between recently fired events.
    502     Code directly taken from OgreRoot.cc
    503     @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> &times)
    507   {
    508     // Calculate the average time passed between events of the given type
    509     // 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 kept
    517     unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f);
    518 
    519     // Find the oldest time to keep
    520     std::deque<unsigned long>::iterator it  = times.begin();
    521     // We need at least two times
    522     std::deque<unsigned long>::iterator end = times.end() - 2;
    523 
    524     while(it != end)
    525     {
    526       if (now - *it > discardThreshold)
    527         ++it;
    528       else
    529         break;
    530     }
    531 
    532     // Remove old times
    533     times.erase(times.begin(), it);
    534 
    535     return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000);
    536   }
    537533}
Note: See TracChangeset for help on using the changeset viewer.