Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 17, 2008, 3:52:38 PM (17 years ago)
Author:
dumenim
Message:

smile :-)

File:
1 edited

Legend:

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

    r1103 r1104  
    11/*
    22 *   ORXONOX - the hottest 3D action shooter ever to exist
    3  *                    > www.orxonox.net <
    43 *
    54 *
     
    2827
    2928/**
    30  @file
     29 @file  Orxonox.cc
    3130 @brief Orxonox Main Class
    3231 */
     
    3433// Precompiled Headers
    3534#include "OrxonoxStableHeaders.h"
    36 #include "Orxonox.h"
    37 
    38 //****** STD *******
    39 //#include <iostream>
    40 //#include <exception>
    41 #include <deque>
    4235
    4336//****** OGRE ******
     
    4639#include <OgreOverlay.h>
    4740#include <OgreOverlayManager.h>
    48 #include <OgreRoot.h>
    4941#include <OgreTimer.h>
    5042#include <OgreWindowEventUtilities.h>
    5143
     44//****** STD *******
     45//#include <iostream>
     46//#include <exception>
     47#include <deque>
     48
    5249//***** ORXONOX ****
    53 // util
     50//misc
    5451//#include "util/Sleep.h"
    55 #include "util/ArgReader.h"
    56 
    57 // core
    58 #include "core/ConfigFileManager.h"
    59 #include "core/ConsoleCommand.h"
     52
     53// audio
     54#include "audio/AudioManager.h"
     55
     56// network
     57#include "network/Server.h"
     58#include "network/Client.h"
     59network::Client *client_g;
     60network::Server *server_g;
     61
     62// objects
     63#include "core/ArgReader.h"
    6064#include "core/Debug.h"
    6165#include "core/Factory.h"
    6266#include "core/Loader.h"
    6367#include "core/Tickable.h"
    64 #include "core/InputBuffer.h"
    65 #include "core/InputManager.h"
    66 
    67 // audio
    68 #include "audio/AudioManager.h"
    69 
    70 // network
    71 #include "network/Server.h"
    72 #include "network/Client.h"
    73 
    74 // objects and tools
     68#include "hud/HUD.h"
    7569#include "tools/Timer.h"
    76 #include "hud/HUD.h"
    77 
    78 // FIXME: is this really file scope?
    79 // globals for the server or client
    80 network::Client *client_g;
    81 network::Server *server_g;
     70#include "objects/weapon/BulletManager.h"
     71
     72#include "InputHandler.h"
     73
     74#include "Orxonox.h"
    8275
    8376namespace orxonox
    8477{
    85   ConsoleCommand(Orxonox, exit, AccessLevel::None, true);
    86   ConsoleCommand(Orxonox, slomo, AccessLevel::Offline, true).setDefaultValue(0, 1.0);
    87   ConsoleCommand(Orxonox, setTimeFactor, AccessLevel::Offline, false).setDefaultValue(0, 1.0);
    88 
    89   class Testconsole : public InputBufferListener
    90   {
    91     public:
    92       Testconsole(InputBuffer* ib) : ib_(ib) {}
    93       void listen() const
    94       {
    95         std::cout << "> " << this->ib_->get() << std::endl;
    96       }
    97       void execute() const
    98       {
    99         std::cout << ">> " << this->ib_->get() << std::endl;
    100         if (!CommandExecutor::execute(this->ib_->get()))
    101           std::cout << "Error" << std::endl;
    102         this->ib_->clear();
    103       }
    104       void hintandcomplete() const
    105       {
    106         std::cout << CommandExecutor::hint(this->ib_->get()) << std::endl;
    107         this->ib_->set(CommandExecutor::complete(this->ib_->get()));
    108       }
    109       void clear() const
    110       {
    111         this->ib_->clear();
    112       }
    113       void removeLast() const
    114       {
    115         this->ib_->removeLast();
    116       }
    117       void exit() const
    118       {
    119         CommandExecutor::execute("setInputMode 2");
    120       }
    121 
    122     private:
    123       InputBuffer* ib_;
    124   };
    125 
    12678  /**
    12779    @brief Reference to the only instance of the class.
     
    13486  Orxonox::Orxonox()
    13587  {
    136     this->ogre_ = &GraphicsEngine::getSingleton();
     88    this->ogre_ = new GraphicsEngine();
    13789    this->timer_ = 0;
    13890    this->dataPath_ = "";
    13991    this->auMan_ = 0;
    14092    this->inputHandler_ = 0;
     93    //this->root_ = 0;
    14194    // turn on frame smoothing by setting a value different from 0
    14295    this->frameSmoothingTime_ = 0.0f;
    14396    this->bAbort_ = false;
    144     this->timefactor_ = 1.0f;
    14597  }
    14698
     
    151103  {
    152104    // keep in mind: the order of deletion is very important!
     105    if (this->bulletMgr_)
     106      delete this->bulletMgr_;
    153107    if (this->orxonoxHUD_)
    154108      delete this->orxonoxHUD_;
    155109    Loader::close();
    156     InputManager::getSingleton().destroy();
     110    InputHandler::destroy();
    157111    if (this->auMan_)
    158112      delete this->auMan_;
    159113    if (this->timer_)
    160114      delete this->timer_;
    161     GraphicsEngine::getSingleton().destroy();
     115    if (this->ogre_)
     116      delete this->ogre_;
    162117
    163118    if (client_g)
     
    168123
    169124  /**
    170     @brief Immediately deletes the orxonox object.
    171     Never use if you can help it while rendering!
    172   */
    173   void Orxonox::abortImmediateForce()
    174   {
    175     COUT(1) << "*** Orxonox Error: Orxonox object was unexpectedly destroyed." << std::endl;
     125   * error kills orxonox
     126   */
     127  void Orxonox::abortImmediate(/* some error code */)
     128  {
     129    //TODO: destroy and destruct everything and print nice error msg
    176130    delete this;
    177131  }
     
    182136  void Orxonox::abortRequest()
    183137  {
    184     COUT(3) << "*** Orxonox: Abort requested." << std::endl;
    185138    bAbort_ = true;
    186139  }
     
    194147      singletonRef_s = new Orxonox();
    195148    return singletonRef_s;
     149    //static Orxonox theOnlyInstance;
     150    //return &theOnlyInstance;
    196151  }
    197152
     
    199154    @brief Destroys the Orxonox singleton.
    200155  */
    201   void Orxonox::destroySingleton()
     156  void Orxonox::destroy()
    202157  {
    203158    if (singletonRef_s)
     
    219174    std::string mode;
    220175
    221     ArgReader ar(argc, argv);
     176    ArgReader ar = ArgReader(argc, argv);
    222177    ar.checkArgument("mode", mode, false);
    223178    ar.checkArgument("data", this->dataPath_, false);
    224179    ar.checkArgument("ip", serverIp_, false);
    225     if(ar.errorHandling()) abortImmediateForce();
     180    if(ar.errorHandling()) abortImmediate();
    226181    if(mode == std::string("client"))
    227182    {
     
    242197  {
    243198    COUT(2) << "initialising server" << std::endl;
    244 
     199   
    245200    ogre_->setConfigPath(path);
    246201    ogre_->setup();
    247202    //root_ = ogre_->getRoot();
    248     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
    249 
     203    if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */);
     204   
    250205    server_g = new network::Server();
    251206  }
     
    254209  {
    255210    COUT(2) << "initialising client" << std::endl;\
    256 
     211   
    257212    ogre_->setConfigPath(path);
    258213    ogre_->setup();
     
    261216    else
    262217      client_g = new network::Client(serverIp_, NETWORK_PORT);
    263     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
    264   }
    265 
     218    if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */);
     219  }
     220 
    266221  void Orxonox::standaloneInit(std::string path)
    267222  {
    268223    COUT(2) << "initialising standalone mode" << std::endl;
    269 
     224   
    270225    ogre_->setConfigPath(path);
    271226    ogre_->setup();
    272     if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
    273   }
    274 
     227    //root_ = ogre_->getRoot();
     228    if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */);
     229  }
     230 
    275231  /**
    276232   * start modules
     
    289245    }
    290246  }
    291 
     247 
    292248  void Orxonox::clientStart(){
    293249    ogre_->initialise();
    294     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    295250    Factory::createClassHierarchy();
    296 
    297 
     251   
     252   
    298253    auMan_ = new audio::AudioManager();
    299254
     255    bulletMgr_ = new BulletManager();
     256   
    300257    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
    301258    orxonoxHUD_ = new HUD();
     
    303260    orxonoxHUD_->setEnergyDistr(20,20,60);
    304261    hudOverlay->show();
    305 
     262   
    306263    if( !client_g->establishConnection() )
    307       COUT(1) <<"CLIENT COULD NOT ESTABLISH CONNECTION" << std::endl;
     264      COUT(1) <<"CLIENT COULD NOT ESTABLISH CONNECTION :-)" << std::endl;
    308265    client_g->tick(0);
    309 
    310 
     266   
     267   
    311268    //setupInputSystem();
    312 
     269   
    313270    startRenderLoop();
    314271  }
    315 
     272 
    316273  void Orxonox::serverStart(){
    317274    //TODO: start modules
    318275    ogre_->initialise();
    319276    //TODO: run engine
    320     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    321277    Factory::createClassHierarchy();
    322278    createScene();
    323279    setupInputSystem();
    324 
     280   
    325281    server_g->open();
    326282
    327283    startRenderLoop();
    328284  }
    329 
     285 
    330286  void Orxonox::standaloneStart(){
    331287    //TODO: start modules
    332288    ogre_->initialise();
    333289    //TODO: run engine
    334     ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
    335290    Factory::createClassHierarchy();
    336291    createScene();
    337292    setupInputSystem();
    338 
     293   
    339294    startRenderLoop();
    340295  }
    341296
    342   void Orxonox::createScene()
     297  void Orxonox::createScene(void)
    343298  {
    344299          // Init audio
    345300    auMan_ = new audio::AudioManager();
     301
     302    bulletMgr_ = new BulletManager();
    346303
    347304    // load this file from config
     
    370327  void Orxonox::setupInputSystem()
    371328  {
    372     inputHandler_ = &InputManager::getSingleton();
     329    inputHandler_ = InputHandler::getSingleton();
    373330    if (!inputHandler_->initialise(ogre_->getWindowHandle(),
    374331          ogre_->getWindowWidth(), ogre_->getWindowHeight()))
    375       abortImmediateForce();
    376     inputHandler_->setInputMode(IM_INGAME);
     332      abortImmediate();
    377333  }
    378334
     
    387343    About the loop: The design is almost exactly like the one in ogre, so that
    388344    if any part of ogre registers a framelisteners, it will still behave
    389     correctly. Furthermore the time smoothing feature from ogre has been
    390     implemented too. If turned on (see orxonox constructor), it will calculate
    391     the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
     345    correctly. Furthermore I have taken over the time smoothing feature from
     346    ogre. If turned on (see orxonox constructor), it will calculate the dt_n by
     347    means of the recent most dt_n-1, dt_n-2, etc.
    392348  */
    393349  void Orxonox::startRenderLoop()
    394350  {
    395     InputBuffer* ib = new InputBuffer();
    396     InputManager::getSingleton().feedInputBuffer(ib);
    397     Testconsole* console = new Testconsole(ib);
    398     ib->registerListener(console, &Testconsole::listen, true);
    399     ib->registerListener(console, &Testconsole::execute, '\r', false);
    400     ib->registerListener(console, &Testconsole::execute, '\n', false);
    401     ib->registerListener(console, &Testconsole::hintandcomplete, '\t', true);
    402     ib->registerListener(console, &Testconsole::clear, '§', true);
    403     ib->registerListener(console, &Testconsole::removeLast, '\b', true);
    404     ib->registerListener(console, &Testconsole::exit, (char)0x1B, true);
    405 
    406     // first check whether ogre root object has been created
    407     if (Ogre::Root::getSingletonPtr() == 0)
    408     {
    409       COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;
    410       return;
    411     }
    412 
    413     // Contains the times of recently fired events
    414     // eventTimes[4] is the list for the times required for the fps counter
    415     std::deque<unsigned long> eventTimes[4];
    416     // Clear event times
    417     for (int i = 0; i < 4; ++i)
    418       eventTimes[i].clear();
    419     // fill the fps time list with zeros
    420     for (int i = 0; i < 20; i++)
    421       eventTimes[3].push_back(0);
    422 
    423351    // use the ogre timer class to measure time.
    424352    if (!timer_)
     
    426354    timer_->reset();
    427355
     356    // Contains the times of recently fired events
     357    std::deque<unsigned long> eventTimes[3];
     358    // Clear event times
     359    for (int i = 0; i < 3; ++i)
     360      eventTimes[i].clear();
     361
    428362          while (!bAbort_)
    429363          {
     
    433367      // get current time
    434368      unsigned long now = timer_->getMilliseconds();
    435       eventTimes[3].push_back(now);
    436       eventTimes[3].erase(eventTimes[3].begin());
    437369
    438370      // create an event to pass to the frameStarted method in ogre
     
    442374
    443375      // show the current time in the HUD
    444       orxonoxHUD_->setTime((int)now, 0);
    445       if (eventTimes[3].back() - eventTimes[3].front() != 0)
    446         orxonoxHUD_->setRocket1((int)(20000.0f/(eventTimes[3].back() - eventTimes[3].front())));
     376      //orxonoxHUD_->setTime((int)now, 0);
    447377
    448378      // Iterate through all Tickables and call their tick(dt) function
    449       for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it)
    450         it->tick((float)evt.timeSinceLastFrame * this->timefactor_);
     379      for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; )
     380        (it++)->tick((float)evt.timeSinceLastFrame);
    451381
    452382      // don't forget to call _fireFrameStarted in ogre to make sure
    453383      // everything goes smoothly
    454       Ogre::Root::getSingleton()._fireFrameStarted(evt);
    455 
    456       // server still renders at the moment
     384      ogre_->frameStarted(evt);
     385
    457386      //if (mode_ != SERVER)
    458       Ogre::Root::getSingleton()._updateAllRenderTargets(); // only render in non-server mode
     387        ogre_->renderOneFrame(); // only render in non-server mode
    459388
    460389      // get current time
     
    466395
    467396      // again, just to be sure ogre works fine
    468       Ogre::Root::getSingleton()._fireFrameEnded(evt);
     397      ogre_->frameEnded(evt);
    469398          }
    470399  }
     
    479408  {
    480409    // Calculate the average time passed between events of the given type
    481     // during the last frameSmoothingTime_ seconds.
     410    // during the last mFrameSmoothingTime seconds.
    482411
    483412    times.push_back(now);
     
    486415      return 0;
    487416
    488     // Times up to frameSmoothingTime_ seconds old should be kept
    489     unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f);
     417    // Times up to mFrameSmoothingTime seconds old should be kept
     418    unsigned long discardThreshold =
     419      static_cast<unsigned long>(frameSmoothingTime_ * 1000.0f);
    490420
    491421    // Find the oldest time to keep
    492     std::deque<unsigned long>::iterator it  = times.begin();
    493     // We need at least two times
    494     std::deque<unsigned long>::iterator end = times.end() - 2;
    495 
     422    std::deque<unsigned long>::iterator it = times.begin(),
     423      end = times.end()-2; // We need at least two times
    496424    while(it != end)
    497425    {
     
    505433    times.erase(times.begin(), it);
    506434
    507     return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000);
     435    return (float)(times.back() - times.front()) / ((times.size()-1) * 1000);
    508436  }
    509437
Note: See TracChangeset for help on using the changeset viewer.