Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 15, 2008, 5:44:55 PM (16 years ago)
Author:
scheusso
Message:

merged changes from input & camera & network branch into trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/Orxonox.cc

    r1219 r1293  
    3737
    3838//****** STD *******
    39 //#include <iostream>
    40 //#include <exception>
    4139#include <deque>
    4240
    4341//****** OGRE ******
    44 //#include <OgreException.h>
    4542#include <OgreFrameListener.h>
    4643#include <OgreOverlay.h>
     
    6562#include "core/InputBuffer.h"
    6663#include "core/InputManager.h"
     64#include "core/TclBind.h"
    6765
    6866// audio
     
    158156
    159157  /**
    160    * create a new instance of Orxonox
    161    */
    162   Orxonox::Orxonox()
    163   {
    164     this->mode_ = STANDALONE;
    165     this->serverIp_ = "";
    166     this->ogre_ = &GraphicsEngine::getSingleton();
    167     this->timer_ = 0;
    168     this->dataPath_ = "";
    169     this->auMan_ = 0;
    170     this->orxonoxHUD_ = 0;
    171     this->inputHandler_ = 0;
     158   * Create a new instance of Orxonox. Avoid doing any actual work here.
     159   */
     160  Orxonox::Orxonox() :
     161    ogre_(0),
     162    //auMan_(0),
     163    timer_(0),
    172164    // turn on frame smoothing by setting a value different from 0
    173     this->frameSmoothingTime_ = 0.0f;
    174     this->bAbort_ = false;
    175     this->timefactor_ = 1.0f;
    176   }
    177 
    178   /**
    179    * destruct Orxonox
     165    frameSmoothingTime_(0.0f),
     166    orxonoxConsole_(0),
     167    orxonoxHUD_(0),
     168    bAbort_(false),
     169    timefactor_(1.0f),
     170    mode_(STANDALONE),
     171    serverIp_("")
     172  {
     173  }
     174
     175  /**
     176   * Destruct Orxonox.
    180177   */
    181178  Orxonox::~Orxonox()
     
    186183    Loader::close();
    187184    InputManager::destroy();
    188     if (this->auMan_)
    189       delete this->auMan_;
     185    //if (this->auMan_)
     186    //  delete this->auMan_;
    190187    if (this->timer_)
    191188      delete this->timer_;
    192189    GraphicsEngine::getSingleton().destroy();
    193190
    194     if (client_g)
    195       delete client_g;
     191    if (network::Client::getSingleton())
     192      network::Client::destroySingleton();
    196193    if (server_g)
    197194      delete server_g;
    198195  }
    199196
    200   /**
    201     @brief Immediately deletes the orxonox object.
    202     Never use if you can help it while rendering!
    203   */
    204   void Orxonox::abortImmediateForce()
    205   {
    206     COUT(1) << "*** Orxonox Error: Orxonox object was unexpectedly destroyed." << std::endl;
    207     delete this;
    208   }
    209197
    210198  /**
     
    213201  void Orxonox::abortRequest()
    214202  {
    215     COUT(3) << "*** Orxonox: Abort requested." << std::endl;
     203    COUT(3) << "Orxonox: Abort requested." << std::endl;
    216204    bAbort_ = true;
    217205  }
    218206
    219207  /**
    220    * @return singleton object
     208   * @return singleton reference
    221209   */
    222210  Orxonox* Orxonox::getSingleton()
     
    243231   * @param path path to config (in home dir or something)
    244232   */
    245   void Orxonox::init(int argc, char **argv, std::string path)
     233  bool Orxonox::init(int argc, char **argv, std::string path)
    246234  {
    247235    //TODO: find config file (assuming executable directory)
     
    249237    //TODO: give config file to Ogre
    250238    std::string mode;
     239    std::string dataPath;
    251240
    252241    ArgReader ar(argc, argv);
    253242    ar.checkArgument("mode", mode, false);
    254     ar.checkArgument("data", this->dataPath_, false);
     243    ar.checkArgument("data", dataPath, false);
    255244    ar.checkArgument("ip", serverIp_, false);
    256     if(ar.errorHandling()) abortImmediateForce();
    257     if(mode == std::string("client"))
     245    if(ar.errorHandling())
     246      return false;
     247
     248    if (mode == "client")
     249      mode_ = CLIENT;
     250    else if (mode == "server")
     251      mode_ = SERVER;
     252    else
    258253    {
    259       mode_ = CLIENT;
    260       clientInit(path);
     254      mode = "standalone";
     255      mode_ = STANDALONE;
    261256    }
    262     else if(mode== std::string("server")){
    263       mode_ = SERVER;
    264       serverInit(path);
    265     }
    266     else{
    267       mode_ = STANDALONE;
    268       standaloneInit(path);
    269     }
    270   }
    271 
    272   void Orxonox::serverInit(std::string path)
    273   {
    274     COUT(2) << "initialising server" << std::endl;
    275 
    276     ogre_->setConfigPath(path);
    277     ogre_->setup();
    278     //root_ = ogre_->getRoot();
    279     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
    280 
    281     server_g = new network::Server();
    282   }
    283 
    284   void Orxonox::clientInit(std::string path)
    285   {
    286     COUT(2) << "initialising client" << std::endl;\
    287 
    288     ogre_->setConfigPath(path);
    289     ogre_->setup();
    290     if(serverIp_.compare("")==0)
    291       client_g = new network::Client();
    292     else
    293       client_g = new network::Client(serverIp_, NETWORK_PORT);
    294     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
    295   }
    296 
    297   void Orxonox::standaloneInit(std::string path)
    298   {
    299     COUT(2) << "initialising standalone mode" << std::endl;
    300 
    301     ogre_->setConfigPath(path);
    302     ogre_->setup();
    303     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
     257    COUT(3) << "Orxonox: Mode is " << mode << "." << std::endl;
     258
     259    //if (mode_ == DEDICATED)
     260      // TODO: decide what to do here
     261    //else
     262
     263    // for playable server, client and standalone, the startup
     264    // procedure until the GUI is identical
     265
     266    TclBind::getInstance().setDataPath(dataPath);
     267    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
     268    Factory::createClassHierarchy();
     269
     270    ogre_ = &GraphicsEngine::getSingleton();
     271    if (!ogre_->setup(path))       // creates ogre root and other essentials
     272      return false;
     273
     274    return true;
    304275  }
    305276
     
    307278   * start modules
    308279   */
    309   void Orxonox::start()
    310   {
    311     switch(mode_){
     280  bool Orxonox::start()
     281  {
     282    //if (mode == DEDICATED)
     283    // do something else
     284    //else
     285
     286    if (!ogre_->loadRenderer())    // creates the render window
     287      return false;
     288
     289    // Calls the InputManager which sets up the input devices.
     290    // The render window width and height are used to set up the mouse movement.
     291    if (!InputManager::initialise(ogre_->getWindowHandle(),
     292          ogre_->getWindowWidth(), ogre_->getWindowHeight(), true, true, true))
     293      return false;
     294
     295    // TODO: Spread this so that this call only initialises things needed for the GUI
     296    if (!ogre_->initialiseResources())
     297      return false;
     298
     299    // TOOD: load the GUI here
     300    // set InputManager to GUI mode
     301    InputManager::setInputState(InputManager::IS_GUI);
     302    // TODO: run GUI here
     303
     304    // The following lines depend very much on the GUI output, so they're probably misplaced here..
     305
     306    InputManager::setInputState(InputManager::IS_NONE);
     307
     308    if (!loadPlayground())
     309      return false;
     310
     311    switch (mode_)
     312    {
     313    case SERVER:
     314      if (!serverLoad())
     315        return false;
     316      break;
    312317    case CLIENT:
    313       clientStart();
    314       break;
    315     case SERVER:
    316       serverStart();
     318      if (!clientLoad())
     319        return false;
    317320      break;
    318321    default:
    319       standaloneStart();
     322      if (!standaloneLoad())
     323        return false;
    320324    }
    321   }
    322 
    323   void Orxonox::clientStart(){
    324     ogre_->initialise();
    325     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    326     Factory::createClassHierarchy();
    327 
    328 
    329     auMan_ = new audio::AudioManager();
    330 
     325
     326    InputManager::setInputState(InputManager::IS_NORMAL);
     327
     328    return startRenderLoop();
     329  }
     330
     331  /**
     332   * Loads everything in the scene except for the actual objects.
     333   * This includes HUD, Console..
     334   */
     335  bool Orxonox::loadPlayground()
     336  {
     337    ogre_->createNewScene();
     338
     339          // Init audio
     340    //auMan_ = new audio::AudioManager();
     341    //auMan_->ambientAdd("a1");
     342    //auMan_->ambientAdd("a2");
     343    //auMan_->ambientAdd("a3");
     344    //auMan->ambientAdd("ambient1");
     345    //auMan_->ambientStart();
     346
     347    // Load the HUD
     348    COUT(3) << "Orxonox: Loading HUD..." << std::endl;
    331349    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
    332350    orxonoxHUD_ = new HUD();
     
    335353    hudOverlay->show();
    336354
    337     client_g->establishConnection();
    338     client_g->tick(0);
    339 
    340 
    341     //setupInputSystem();
    342 
    343     startRenderLoop();
    344   }
    345 
    346   void Orxonox::serverStart(){
    347     //TODO: start modules
    348     ogre_->initialise();
    349     //TODO: run engine
    350     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    351     Factory::createClassHierarchy();
    352     createScene();
    353     setupInputSystem();
    354 
    355     server_g->open();
    356 
    357     startRenderLoop();
    358   }
    359 
    360   void Orxonox::standaloneStart(){
    361     //TODO: start modules
    362     ogre_->initialise();
    363     //TODO: run engine
    364     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    365     Factory::createClassHierarchy();
    366     createScene();
    367     setupInputSystem();
    368 
    369     startRenderLoop();
    370   }
    371 
    372   void Orxonox::createScene(void)
    373   {
    374           // Init audio
    375     auMan_ = new audio::AudioManager();
    376 
    377     // load this file from config
    378     Level* startlevel = new Level("levels/sample.oxw");
    379     Loader::open(startlevel);
    380 
    381     Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
    382     orxonoxHUD_ = new HUD();
    383     orxonoxHUD_->setEnergyValue(20);
    384     orxonoxHUD_->setEnergyDistr(20,20,60);
    385     hudOverlay->show();
    386 
    387         /*
    388     auMan_->ambientAdd("a1");
    389     auMan_->ambientAdd("a2");
    390     auMan_->ambientAdd("a3");
    391     //auMan->ambientAdd("ambient1");
    392     auMan_->ambientStart();
    393   */
    394   }
    395 
    396   /**
    397     @brief Calls the InputHandler which sets up the input devices.
    398     The render window width and height are used to set up the mouse movement.
    399   */
    400   void Orxonox::setupInputSystem()
    401   {
    402     if (!InputManager::initialise(ogre_->getWindowHandle(),
    403           ogre_->getWindowWidth(), ogre_->getWindowHeight()))
    404       abortImmediateForce();
    405     InputManager::setInputState(InputManager::IS_NORMAL);
    406   }
    407 
    408   /**
    409     Main loop of the orxonox game.
    410     This is a new solution, using the ogre engine instead of being used by it.
    411     An alternative solution would be to simply use the timer of the Root object,
    412     but that implies using Ogre in any case. There would be no way to test
    413     our code without the help of the root object.
    414     There's even a chance that we can dispose of the root object entirely
    415     in server mode.
    416     About the loop: The design is almost exactly like the one in ogre, so that
    417     if any part of ogre registers a framelisteners, it will still behave
    418     correctly. Furthermore the time smoothing feature from ogre has been
    419     implemented too. If turned on (see orxonox constructor), it will calculate
    420     the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
    421   */
    422   void Orxonox::startRenderLoop()
    423   {
     355    COUT(3) << "Orxonox: Loading Console..." << std::endl;
    424356    InputBuffer* ib = dynamic_cast<InputBuffer*>(InputManager::getKeyHandler("buffer"));
    425357    /*
     
    432364    ib->registerListener(console, &Testconsole::exit, (char)0x1B, true);
    433365    */
    434 
    435366    orxonoxConsole_ = new InGameConsole(ib);
    436367    ib->registerListener(orxonoxConsole_, &InGameConsole::listen, true);
     
    441372    ib->registerListener(orxonoxConsole_, &InGameConsole::exit, (char)0x1B, true);
    442373
     374    return true;
     375  }
     376
     377  /**
     378   * Level loading method for server mode.
     379   */
     380  bool Orxonox::serverLoad()
     381  {
     382    COUT(2) << "Loading level in server mode" << std::endl;
     383
     384    server_g = new network::Server();
     385
     386    if (!loadScene())
     387      return false;
     388
     389    server_g->open();
     390
     391    return true;
     392  }
     393
     394  /**
     395   * Level loading method for client mode.
     396   */
     397  bool Orxonox::clientLoad()
     398  {
     399    COUT(2) << "Loading level in client mode" << std::endl;\
     400
     401    if (serverIp_.compare("") == 0)
     402      client_g = network::Client::createSingleton();
     403    else
     404      client_g = network::Client::createSingleton(serverIp_, NETWORK_PORT);
     405
     406    client_g->establishConnection();
     407    client_g->tick(0);
     408
     409    return true;
     410  }
     411
     412  /**
     413   * Level loading method for standalone mode.
     414   */
     415  bool Orxonox::standaloneLoad()
     416  {
     417    COUT(2) << "Loading level in standalone mode" << std::endl;
     418
     419    if (!loadScene())
     420      return false;
     421
     422    return true;
     423  }
     424
     425  /**
     426   * Helper method to load a level.
     427   */
     428  bool Orxonox::loadScene()
     429  {
     430    Level* startlevel = new Level("levels/sample.oxw");
     431    Loader::open(startlevel);
     432
     433    return true;
     434  }
     435
     436
     437  /**
     438    Main loop of the orxonox game.
     439    About the loop: The design is almost exactly like the one in ogre, so that
     440    if any part of ogre registers a framelisteners, it will still behave
     441    correctly. Furthermore the time smoothing feature from ogre has been
     442    implemented too. If turned on (see orxonox constructor), it will calculate
     443    the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
     444  */
     445  bool Orxonox::startRenderLoop()
     446  {
    443447    // first check whether ogre root object has been created
    444448    if (Ogre::Root::getSingletonPtr() == 0)
    445449    {
    446       COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;
    447       return;
     450      COUT(2) << "Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;
     451      return false;
    448452    }
    449453    Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
     
    465469    timer_->reset();
    466470
     471    COUT(3) << "Orxonox: Starting the main loop." << std::endl;
    467472          while (!bAbort_)
    468473          {
     
    490495      for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it)
    491496        it->tick((float)evt.timeSinceLastFrame * this->timefactor_);
    492       orxonoxConsole_->tick((float)evt.timeSinceLastFrame * this->timefactor_);
     497      // Iterate through all TickableReals and call their tick(dt) function
     498      for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it)
     499        it->tick((float)evt.timeSinceLastFrame);
     500      orxonoxConsole_->tick((float)evt.timeSinceLastFrame);
    493501
    494502      // don't forget to call _fireFrameStarted in ogre to make sure
     
    510518      ogreRoot._fireFrameEnded(evt);
    511519          }
     520
     521    if(mode_==CLIENT)
     522      network::Client::getSingleton()->closeConnection();
     523    else if(mode_==SERVER)
     524      server_g->close();
     525    return true;
    512526  }
    513527
     
    550564  }
    551565
     566  /**
     567   * Static function that shows the console in game mode.
     568   */
    552569  void Orxonox::activateConsole()
    553570  {
     571    // currently, the console shows itself when feeded with input.
    554572    InputManager::setInputState(InputManager::IS_CONSOLE);
    555573  }
Note: See TracChangeset for help on using the changeset viewer.