Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/orxonox.cc @ 610

Last change on this file since 610 was 610, checked in by rgrieder, 16 years ago
  • added OrxonoxPrereq.h, containing all forward declarations (please remove all the classes that should not be used outside)
  • cleaned up orxonox.h header inclusion part
File size: 11.8 KB
RevLine 
[74]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
[462]7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
[74]11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
[462]18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
[74]20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
[142]28/**
29 @file  orxonox.cc
[462]30 @brief Orxonox Main Class
[142]31 */
32
[609]33//****** OGRE ******
34#include <OgreString.h>
35#include <OgreException.h>
[379]36#include <OgreRoot.h>
37#include <OgreFrameListener.h>
[609]38#include <OgreRenderWindow.h>
39#include <OgreTextureManager.h>
40#include <OgreResourceGroupManager.h>
[379]41#include <OgreConfigFile.h>
[609]42#include <OgreOverlay.h>
43#include <OgreOverlayManager.h>
[379]44
[609]45//****** OIS *******
[137]46#include <OIS/OIS.h>
[74]47
[609]48//****** STD *******
[164]49#include <string>
50#include <iostream>
51
[609]52//***** ORXONOX ****
53// loader and audio
54#include "loader/LevelLoader.h"
55#include "audio/AudioManager.h"
56
57// network
58#include "../network/Server.h"
59#include "../network/Client.h"
60#include "../network/NetworkFrameListener.h"
61
62// objects
[496]63#include "objects/Tickable.h"
64#include "objects/Timer.h"
[542]65#include "core/ArgReader.h"
[496]66#include "core/Factory.h"
[568]67#include "core/Debug.h"
[588]68#include "hud/HUD.h"
69
[609]70#include "graphicsEngine.h"
71#include "orxonox.h"
[588]72
[459]73
[337]74namespace orxonox
75{
[462]76  using namespace Ogre;
[74]77
[609]78   // put this in a seperate Class or solve the problem in another fashion
[608]79  class OrxListener : public FrameListener
[462]80  {
81    public:
[608]82      OrxListener(OIS::Keyboard *keyboard, audio::AudioManager*  auMan, gameMode mode)
83      {
84        mKeyboard = keyboard;
[601]85        mode_=mode;
[608]86        auMan_ = auMan;
87      }
[530]88
[462]89      bool frameStarted(const FrameEvent& evt)
90      {
91        auMan_->update();
[74]92
[608]93        if(mode_==PRESENTATION)
94          server_g->tick(evt.timeSinceLastFrame);
95        else if(mode_==CLIENT)
96          client_g->tick(evt.timeSinceLastFrame);
[337]97
[608]98        usleep(10);
[337]99
[608]100        mKeyboard->capture();
[462]101        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
102      }
[337]103
[265]104    private:
[601]105      gameMode mode_;
[265]106      OIS::Keyboard *mKeyboard;
[462]107      audio::AudioManager*  auMan_;
[608]108  };
109
[462]110  // init static singleton reference of Orxonox
111  Orxonox* Orxonox::singletonRef_ = NULL;
[137]112
[462]113  /**
114   * create a new instance of Orxonox
115   */
116  Orxonox::Orxonox()
117  {
[608]118    this->ogre_ = new GraphicsEngine();
119    this->dataPath_ = "";
120    this->loader_ = 0;
121    this->auMan_ = 0;
122    this->singletonRef_ = 0;
123    this->keyboard_ = 0;
124    this->mouse_ = 0;
125    this->inputManager_ = 0;
126    this->frameListener_ = 0;
127    this->root_ = 0;
[462]128  }
[74]129
[462]130  /**
131   * destruct Orxonox
132   */
133  Orxonox::~Orxonox()
134  {
135    // nothing to delete as for now
136  }
[265]137
[462]138  /**
139   * initialization of Orxonox object
140   * @param argc argument counter
141   * @param argv list of arguments
142   * @param path path to config (in home dir or something)
143   */
144  void Orxonox::init(int argc, char **argv, std::string path)
145  {
146    //TODO: find config file (assuming executable directory)
147    //TODO: read config file
148    //TODO: give config file to Ogre
[568]149    std::string mode;
[599]150//     if(argc>=2)
151//       mode = std::string(argv[1]);
152//     else
153//       mode = "";
[542]154    ArgReader ar = ArgReader(argc, argv);
155    ar.checkArgument("mode", mode, false);
[546]156    ar.checkArgument("data", this->dataPath_, false);
[599]157    ar.checkArgument("ip", serverIp_, false);
[542]158    if(ar.errorHandling()) die();
159    if(mode == std::string("server"))
[462]160    {
161      serverInit(path);
[599]162      mode_ = SERVER;
[462]163    }
[542]164    else if(mode == std::string("client"))
[462]165    {
166      clientInit(path);
[599]167      mode_ = CLIENT;
[534]168    }
[542]169    else if(mode == std::string("presentation"))
[531]170    {
[568]171      serverInit(path);
[599]172      mode_ = PRESENTATION;
[534]173    }
[568]174    else{
175      standaloneInit(path);
[599]176      mode_ = STANDALONE;
[568]177    }
[462]178  }
[263]179
[462]180  /**
181   * start modules
182   */
183  void Orxonox::start()
184  {
185    //TODO: start modules
[534]186    ogre_->startRender();
[462]187    //TODO: run engine
[534]188    createScene();
189    setupScene();
190    setupInputSystem();
[599]191    if(mode_!=CLIENT){ // remove this in future ---- presentation hack
192    }
193    else
194      std::cout << "client here" << std::endl;
[534]195    createFrameListener();
196    Factory::createClassHierarchy();
[568]197    switch(mode_){
198    case PRESENTATION:
[601]199      //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
200      //std::cout << "could not add framelistener" << std::endl;
[568]201      server_g->open();
202      break;
[599]203    case CLIENT:
204      client_g->establishConnection();
205      break;
[568]206    case SERVER:
207    case STANDALONE:
208    default:
209      break;
210    }
[534]211    startRenderLoop();
[462]212  }
[74]213
[462]214  /**
215   * @return singleton object
216   */
217  Orxonox* Orxonox::getSingleton()
218  {
219    if (!singletonRef_)
220      singletonRef_ = new Orxonox();
221    return singletonRef_;
222  }
[74]223
[462]224  /**
225   * error kills orxonox
226   */
227  void Orxonox::die(/* some error code */)
228  {
229    //TODO: destroy and destruct everything and print nice error msg
[542]230    delete this;
[462]231  }
[74]232
[568]233  void Orxonox::standaloneInit(std::string path)
[462]234  {
235    ogre_->setConfigPath(path);
236    ogre_->setup();
[473]237    root_ = ogre_->getRoot();
[534]238    if(!ogre_->load()) die(/* unable to load */);
[473]239
[534]240    //defineResources();
241    //setupRenderSystem();
242    //createRenderWindow();
243    //initializeResourceGroups();
244    /*createScene();
[473]245    setupScene();
246    setupInputSystem();
247    createFrameListener();
[496]248    Factory::createClassHierarchy();
[534]249    startRenderLoop();*/
[462]250  }
[534]251
[531]252  void Orxonox::playableServer(std::string path)
253  {
254    ogre_->setConfigPath(path);
255    ogre_->setup();
256    root_ = ogre_->getRoot();
257    defineResources();
258    setupRenderSystem();
259    createRenderWindow();
260    initializeResourceGroups();
[608]261    setupInputSystem();
262    Factory::createClassHierarchy();
[531]263    createScene();
264    setupScene();
265    createFrameListener();
266    try{
[534]267      server_g = new network::Server(); // add port and bindadress
268      server_g->open(); // open server and create listener thread
269      if(ogre_ && ogre_->getRoot())
270        ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server
[560]271      COUT(3) << "Info: network framelistener added" << std::endl;
[534]272    }
[531]273    catch(exception &e)
274    {
[560]275      COUT(1) << "Error: There was a problem initialising the server :(" << std::endl;
[531]276    }
277    startRenderLoop();
278  }
[576]279
[568]280  void Orxonox::standalone(){
[576]281
282
283
[568]284  }
[74]285
[462]286  void Orxonox::serverInit(std::string path)
287  {
[599]288    COUT(2) << "initialising server" << std::endl;
[462]289    ogre_->setConfigPath(path);
290    ogre_->setup();
[473]291    server_g = new network::Server(); // add some settings if wanted
[462]292    if(!ogre_->load()) die(/* unable to load */);
[568]293    // add network framelistener
[462]294  }
[164]295
[462]296  void Orxonox::clientInit(std::string path)
297  {
[599]298    COUT(2) << "initialising client" << std::endl;
[462]299    ogre_->setConfigPath(path);
300    ogre_->setup();
[599]301    if(serverIp_.compare("")==0)
302      client_g = new network::Client();
303    else
304      client_g = new network::Client(serverIp_, 55556);
[462]305    if(!ogre_->load()) die(/* unable to load */);
[505]306    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
[462]307  }
[258]308
[473]309  void Orxonox::defineResources()
310  {
311    Ogre::String secName, typeName, archName;
312    Ogre::ConfigFile cf;
313#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
314    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
315#else
[546]316    cf.load(dataPath_ + "resources.cfg");
[473]317#endif
318
319    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
320    while (seci.hasMoreElements())
321    {
322      secName = seci.peekNextKey();
323      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
324      Ogre::ConfigFile::SettingsMultiMap::iterator i;
325      for (i = settings->begin(); i != settings->end(); ++i)
326      {
327        typeName = i->first;
328        archName = i->second;
329#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
[609]330        ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
[473]331#else
[609]332        ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
[473]333#endif
334      }
335    }
336  }
337
338  void Orxonox::setupRenderSystem()
339  {
[588]340    if (!root_->restoreConfig() && !root_->showConfigDialog())
[473]341      throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
342  }
343
344  void Orxonox::createRenderWindow()
345  {
346    root_->initialise(true, "OrxonoxV2");
347  }
348
349  void Orxonox::initializeResourceGroups()
350  {
351    TextureManager::getSingleton().setDefaultNumMipmaps(5);
352    ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
353  }
354
[537]355  /**
356   *
357   * @param
358   */
[462]359  void Orxonox::createScene(void)
360  {
[480]361        // Init audio
[462]362    auMan_ = new audio::AudioManager();
[487]363
[480]364    // load this file from config
365    loader_ = new loader::LevelLoader("sample.oxw");
366    loader_->loadLevel();
[337]367
[588]368    Overlay* hudOverlay = OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
369    hud::HUD* orxonoxHud;
370    orxonoxHud = new hud::HUD();
371    orxonoxHud->setEnergyValue(20);
372    orxonoxHud->setEnergyDistr(20,20,60);
373    hudOverlay->show();
374
375
376        /*
377    auMan_->ambientAdd("a1");
378    auMan_->ambientAdd("a2");
379    auMan_->ambientAdd("a3");
380                                //auMan->ambientAdd("ambient1");
381    auMan_->ambientStart();*/
[462]382  }
[337]383
384
[537]385  /**
386   *
387   */
[462]388  void Orxonox::setupScene()
389  {
[608]390//    SceneManager *mgr = ogre_->getSceneManager();
[512]391
[515]392
[608]393//    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
[541]394//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
[515]395
[337]396
[584]397/*
[537]398    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
[535]399    e->particleSystem_->setParameter("local_space","true");
[592]400    e->setPositionOfEmitter(0, Vector3(0,-10,0));
[537]401    e->setDirection(Vector3(0,0,-1));
402    e->addToSceneNode(node);
[584]403*/
[462]404  }
[137]405
406
[462]407  void Orxonox::setupInputSystem()
408  {
409    size_t windowHnd = 0;
410    std::ostringstream windowHndStr;
411    OIS::ParamList pl;
[609]412    RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
[137]413
[462]414    win->getCustomAttribute("WINDOW", &windowHnd);
415    windowHndStr << windowHnd;
416    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
417    inputManager_ = OIS::InputManager::createInputSystem(pl);
[265]418
[462]419    try
[337]420    {
[462]421      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
422      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
[337]423    }
[462]424    catch (const OIS::Exception &e)
[459]425    {
[462]426      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
[459]427    }
[462]428  }
[137]429
[462]430  // we actually want to do this differently...
431  void Orxonox::createFrameListener()
[137]432  {
[496]433    TickFrameListener* TickFL = new TickFrameListener();
434    ogre_->getRoot()->addFrameListener(TickFL);
435
436    TimerFrameListener* TimerFL = new TimerFrameListener();
437    ogre_->getRoot()->addFrameListener(TimerFL);
438
[599]439    //if(mode_!=CLIENT) // just a hack ------- remove this in future
[608]440      frameListener_ = new OrxListener(keyboard_, auMan_, mode_);
[462]441    ogre_->getRoot()->addFrameListener(frameListener_);
[137]442  }
[462]443
444  void Orxonox::startRenderLoop()
[137]445  {
[511]446    // this is a hack!!!
447    // the call to reset the mouse clipping size should probably be somewhere
448    // else, however this works for the moment.
[523]449    unsigned int width, height, depth;
450    int left, top;
451    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
[511]452
[599]453    if(mode_!=CLIENT){
454      const OIS::MouseState &ms = mouse_->getMouseState();
455      ms.width = width;
456      ms.height = height;
457    }
[462]458    ogre_->getRoot()->startRendering();
[74]459  }
460}
Note: See TracBrowser for help on using the repository browser.