Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 602 was 601, checked in by dumenim, 17 years ago

blub

File size: 15.1 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
[462]33#include "orxonox.h"
34#include "graphicsEngine.h"
35
[379]36//#include <Ogre.h>
37// 40% speed up: (instead of Ogre.h)
38#include <OgreSceneNode.h>
39#include <OgreSceneManager.h>
40#include <OgreRoot.h>
41#include <OgreFrameListener.h>
42#include <OgreConfigFile.h>
43#include <OgreTextureManager.h>
44#include <OgreEntity.h>
45#include <OgreRenderWindow.h>
46
[137]47#include <OIS/OIS.h>
[346]48//#include <CEGUI/CEGUI.h>
49//#include <OgreCEGUIRenderer.h>
[74]50
[164]51#include <string>
52#include <iostream>
53
[496]54#include "objects/Tickable.h"
55#include "objects/Timer.h"
[542]56#include "core/ArgReader.h"
[496]57#include "core/Factory.h"
[568]58#include "core/Debug.h"
[496]59
[462]60#include "../loader/LevelLoader.h"
61#include "../audio/AudioManager.h"
[164]62
[337]63#include "spaceship_steering.h"
[164]64
[535]65#include "particle/ParticleInterface.h"
[337]66
[588]67#include "hud/HUD.h"
68
[459]69//network stuff
[462]70#include "../network/Server.h"
71#include "../network/Client.h"
[473]72#include "../network/NetworkFrameListener.h"
[588]73
74#ifdef WIN32
75#include <windows.h>
76#define usleep(x) Sleep((x)/1000)
77#else
78#include <unistd.h>
[576]79#endif
[459]80
[337]81namespace orxonox
82{
[462]83  using namespace Ogre;
[74]84
[462]85   // put this in seperate Class or solve the problem in another fashion
86  class OrxListener : public FrameListener, public OIS::MouseListener
87  {
88    public:
[601]89      OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager*  auMan, SpaceshipSteering* steering, gameMode mode)
[462]90      : mKeyboard(keyboard), mMouse(mouse)
91      {
[530]92
93
[601]94        mode_=mode;
[462]95        speed = 250;
96        loop = 100;
97        rotate = 10;
98        mouseX = 0;
99        mouseY = 0;
100        maxMouseX = 0;
101        minMouseX = 0;
[337]102        moved = false;
[530]103
[519]104        steering_ = steering;
105
106        steering_->brakeRotate(rotate*10);
107        steering_->brakeLoop(loop);
108
[530]109
[462]110        mMouse->setEventCallback(this);
111        auMan_ = auMan;
[265]112      }
[462]113      bool frameStarted(const FrameEvent& evt)
114      {
[135]115
[462]116        auMan_->update();
[74]117
[462]118        mKeyboard->capture();
119        mMouse->capture();
120        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
[519]121          steering_->moveForward(speed);
[462]122        else
[519]123          steering_->moveForward(0);
[462]124        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
[519]125          steering_->brakeForward(speed);
[462]126        else
[519]127          steering_->brakeForward(speed/10);
[462]128        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
[519]129          steering_->loopRight(loop);
[462]130        else
[519]131          steering_->loopRight(0);
[462]132        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
[519]133          steering_->loopLeft(loop);
[462]134        else
[519]135          steering_->loopLeft(0);
[337]136
[462]137        if(moved) {
[512]138          if (mouseY<=0)
[519]139            steering_->rotateUp(-mouseY*rotate);
[462]140          if (mouseY>0)
[519]141            steering_->rotateDown(mouseY*rotate);
[462]142          if (mouseX>0)
[519]143            steering_->rotateRight(mouseX*rotate);
[512]144          if (mouseX<=0)
[519]145            steering_->rotateLeft(-mouseX*rotate);
[512]146          mouseY = 0;
147          mouseX = 0;
[462]148          moved = false;
149        }
150        else {
[519]151          steering_->rotateUp(0);
152          steering_->rotateDown(0);
153          steering_->rotateRight(0);
154          steering_->rotateLeft(0);
[462]155        }
[337]156
[519]157                steering_->tick(evt.timeSinceLastFrame);
[530]158
159
160
[462]161//      scenemanager->spacehip->tick(evt.timesincelastframe);
[542]162        //if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
163          //cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl;
[601]164        if(mode_==SERVER)
165          server_g->tick(evt.timeSinceLastFrame);
[565]166        usleep(10);
[462]167        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
168      }
[337]169
[462]170      bool mouseMoved(const OIS::MouseEvent &e)
[265]171      {
[511]172        mouseX += e.state.X.rel;
173        mouseY += e.state.Y.rel;
[462]174        if(mouseX>maxMouseX) maxMouseX = mouseX;
175        if(mouseX<minMouseX) minMouseX = mouseX;
[542]176        //cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
[462]177        moved = true;
178        return true;
[265]179      }
[74]180
[462]181      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
182      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
[137]183
[265]184    private:
[601]185      gameMode mode_;
[462]186      float speed;
187      float rotate;
188      float loop;
189      float mouseY;
190      float mouseX;
191      float maxMouseX;
192      float minMouseX;
193      bool moved;
[265]194      OIS::Keyboard *mKeyboard;
195      OIS::Mouse *mMouse;
[462]196      audio::AudioManager*  auMan_;
[519]197      SpaceshipSteering* steering_;
[462]198  };
199  // init static singleton reference of Orxonox
200  Orxonox* Orxonox::singletonRef_ = NULL;
[137]201
[462]202  /**
203   * create a new instance of Orxonox
204   */
205  Orxonox::Orxonox()
206  {
207    ogre_ = new GraphicsEngine();
[546]208    dataPath_ = "";
[462]209  }
[74]210
[462]211  /**
212   * destruct Orxonox
213   */
214  Orxonox::~Orxonox()
215  {
216    // nothing to delete as for now
217  }
[265]218
[462]219  /**
220   * initialization of Orxonox object
221   * @param argc argument counter
222   * @param argv list of arguments
223   * @param path path to config (in home dir or something)
224   */
225  void Orxonox::init(int argc, char **argv, std::string path)
226  {
227    //TODO: find config file (assuming executable directory)
228    //TODO: read config file
229    //TODO: give config file to Ogre
[568]230    std::string mode;
[599]231//     if(argc>=2)
232//       mode = std::string(argv[1]);
233//     else
234//       mode = "";
[542]235    ArgReader ar = ArgReader(argc, argv);
236    ar.checkArgument("mode", mode, false);
[546]237    ar.checkArgument("data", this->dataPath_, false);
[599]238    ar.checkArgument("ip", serverIp_, false);
[542]239    if(ar.errorHandling()) die();
[534]240
[542]241    if(mode == std::string("server"))
[462]242    {
243      serverInit(path);
[599]244      mode_ = SERVER;
[462]245    }
[542]246    else if(mode == std::string("client"))
[462]247    {
248      clientInit(path);
[599]249      mode_ = CLIENT;
[534]250    }
[542]251    else if(mode == std::string("presentation"))
[531]252    {
[568]253      serverInit(path);
[599]254      mode_ = PRESENTATION;
[534]255    }
[568]256    else{
257      standaloneInit(path);
[599]258      mode_ = STANDALONE;
[568]259    }
[462]260  }
[263]261
[462]262  /**
263   * start modules
264   */
265  void Orxonox::start()
266  {
267    //TODO: start modules
[534]268    ogre_->startRender();
[462]269    //TODO: run engine
[534]270    createScene();
271    setupScene();
272    setupInputSystem();
[599]273    if(mode_!=CLIENT){ // remove this in future ---- presentation hack
274    }
275    else
276      std::cout << "client here" << std::endl;
[534]277    createFrameListener();
278    Factory::createClassHierarchy();
[568]279    switch(mode_){
280    case PRESENTATION:
[601]281      //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
282      //std::cout << "could not add framelistener" << std::endl;
[568]283      server_g->open();
284      break;
[599]285    case CLIENT:
286      client_g->establishConnection();
287      break;
[568]288    case SERVER:
289    case STANDALONE:
290    default:
291      break;
292    }
[534]293    startRenderLoop();
[462]294  }
[74]295
[462]296  /**
297   * @return singleton object
298   */
299  Orxonox* Orxonox::getSingleton()
300  {
301    if (!singletonRef_)
302      singletonRef_ = new Orxonox();
303    return singletonRef_;
304  }
[74]305
[462]306  /**
307   * error kills orxonox
308   */
309  void Orxonox::die(/* some error code */)
310  {
311    //TODO: destroy and destruct everything and print nice error msg
[542]312    delete this;
[462]313  }
[74]314
[568]315  void Orxonox::standaloneInit(std::string path)
[462]316  {
317    ogre_->setConfigPath(path);
318    ogre_->setup();
[473]319    root_ = ogre_->getRoot();
[534]320    if(!ogre_->load()) die(/* unable to load */);
[473]321
[534]322    //defineResources();
323    //setupRenderSystem();
324    //createRenderWindow();
325    //initializeResourceGroups();
326    /*createScene();
[473]327    setupScene();
328    setupInputSystem();
329    createFrameListener();
[496]330    Factory::createClassHierarchy();
[534]331    startRenderLoop();*/
[462]332  }
[534]333
[531]334  void Orxonox::playableServer(std::string path)
335  {
336    ogre_->setConfigPath(path);
337    ogre_->setup();
338    root_ = ogre_->getRoot();
339    defineResources();
340    setupRenderSystem();
341    createRenderWindow();
342    initializeResourceGroups();
343    createScene();
344    setupScene();
345    setupInputSystem();
346    Factory::createClassHierarchy();
347    createFrameListener();
348    try{
[534]349      server_g = new network::Server(); // add port and bindadress
350      server_g->open(); // open server and create listener thread
351      if(ogre_ && ogre_->getRoot())
352        ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server
[560]353      COUT(3) << "Info: network framelistener added" << std::endl;
[534]354    }
[531]355    catch(exception &e)
356    {
[560]357      COUT(1) << "Error: There was a problem initialising the server :(" << std::endl;
[531]358    }
359    startRenderLoop();
360  }
[576]361
[568]362  void Orxonox::standalone(){
[576]363
364
365
[568]366  }
[74]367
[462]368  void Orxonox::serverInit(std::string path)
369  {
[599]370    COUT(2) << "initialising server" << std::endl;
[462]371    ogre_->setConfigPath(path);
372    ogre_->setup();
[473]373    server_g = new network::Server(); // add some settings if wanted
[462]374    if(!ogre_->load()) die(/* unable to load */);
[568]375    // add network framelistener
[462]376  }
[164]377
[462]378  void Orxonox::clientInit(std::string path)
379  {
[599]380    COUT(2) << "initialising client" << std::endl;
[462]381    ogre_->setConfigPath(path);
382    ogre_->setup();
[599]383    if(serverIp_.compare("")==0)
384      client_g = new network::Client();
385    else
386      client_g = new network::Client(serverIp_, 55556);
[462]387    if(!ogre_->load()) die(/* unable to load */);
[505]388    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
[462]389  }
[258]390
[473]391  void Orxonox::defineResources()
392  {
393    Ogre::String secName, typeName, archName;
394    Ogre::ConfigFile cf;
395#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
396    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
397#else
[546]398    cf.load(dataPath_ + "resources.cfg");
[473]399#endif
400
401    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
402    while (seci.hasMoreElements())
403    {
404      secName = seci.peekNextKey();
405      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
406      Ogre::ConfigFile::SettingsMultiMap::iterator i;
407      for (i = settings->begin(); i != settings->end(); ++i)
408      {
409        typeName = i->first;
410        archName = i->second;
411#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
412        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
413#else
414        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
415#endif
416      }
417    }
418  }
419
420  void Orxonox::setupRenderSystem()
421  {
[588]422    if (!root_->restoreConfig() && !root_->showConfigDialog())
[473]423      throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
424  }
425
426  void Orxonox::createRenderWindow()
427  {
428    root_->initialise(true, "OrxonoxV2");
429  }
430
431  void Orxonox::initializeResourceGroups()
432  {
433    TextureManager::getSingleton().setDefaultNumMipmaps(5);
434    ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
435  }
436
[537]437  /**
438   *
439   * @param
440   */
[462]441  void Orxonox::createScene(void)
442  {
[480]443        // Init audio
[462]444    auMan_ = new audio::AudioManager();
[487]445
[480]446    // load this file from config
447    loader_ = new loader::LevelLoader("sample.oxw");
448    loader_->loadLevel();
[337]449
[588]450    Overlay* hudOverlay = OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
451    hud::HUD* orxonoxHud;
452    orxonoxHud = new hud::HUD();
453    orxonoxHud->setEnergyValue(20);
454    orxonoxHud->setEnergyDistr(20,20,60);
455    hudOverlay->show();
456
457
458        /*
459    auMan_->ambientAdd("a1");
460    auMan_->ambientAdd("a2");
461    auMan_->ambientAdd("a3");
462                                //auMan->ambientAdd("ambient1");
463    auMan_->ambientStart();*/
[462]464  }
[337]465
466
[537]467  /**
468   *
469   */
[462]470  void Orxonox::setupScene()
471  {
472    SceneManager *mgr = ogre_->getSceneManager();
[512]473
[515]474
[525]475    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
[541]476//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
[515]477
[337]478
[525]479    steering_ = new SpaceshipSteering(500, 200, 200, 200);
480    steering_->addNode(node);
[74]481
[584]482/*
[537]483    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
[535]484    e->particleSystem_->setParameter("local_space","true");
[592]485    e->setPositionOfEmitter(0, Vector3(0,-10,0));
[537]486    e->setDirection(Vector3(0,0,-1));
487    e->addToSceneNode(node);
[584]488*/
[535]489
[537]490    particle::ParticleInterface *w = new particle::ParticleInterface(mgr,"schuss","Orxonox/schuss");
491    w->particleSystem_->setParameter("local_space","true");
492    w->newEmitter();
493    w->setDirection(Vector3(0,0,1));
494    w->setPositionOfEmitter(0, Vector3(10,10,0));
495    w->setPositionOfEmitter(1, Vector3(-10,10,0));
496    w->addToSceneNode(node);
[584]497
[592]498    particle::ParticleInterface *tt = new particle::ParticleInterface(mgr,"twinthruster","Orxonox/engineglow");
[584]499    tt->particleSystem_->setParameter("local_space","true");
500    tt->newEmitter();
[592]501    tt->setDirection(Vector3(0,0,-1));
502    tt->setPositionOfEmitter(0, Vector3(20,-1,-15));
503    tt->setPositionOfEmitter(1, Vector3(-20,-1,-15));
[584]504    tt->addToSceneNode(node);
505
[462]506  }
[137]507
508
[462]509  void Orxonox::setupInputSystem()
510  {
511    size_t windowHnd = 0;
512    std::ostringstream windowHndStr;
513    OIS::ParamList pl;
514    Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
[137]515
[462]516    win->getCustomAttribute("WINDOW", &windowHnd);
517    windowHndStr << windowHnd;
518    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
519    inputManager_ = OIS::InputManager::createInputSystem(pl);
[265]520
[462]521    try
[337]522    {
[462]523      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
524      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
[337]525    }
[462]526    catch (const OIS::Exception &e)
[459]527    {
[462]528      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
[459]529    }
[462]530  }
[137]531
[462]532  // we actually want to do this differently...
533  void Orxonox::createFrameListener()
[137]534  {
[496]535    TickFrameListener* TickFL = new TickFrameListener();
536    ogre_->getRoot()->addFrameListener(TickFL);
537
538    TimerFrameListener* TimerFL = new TimerFrameListener();
539    ogre_->getRoot()->addFrameListener(TimerFL);
540
[599]541    //if(mode_!=CLIENT) // just a hack ------- remove this in future
[601]542      frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_, mode_);
[462]543    ogre_->getRoot()->addFrameListener(frameListener_);
[137]544  }
[462]545
546  void Orxonox::startRenderLoop()
[137]547  {
[511]548    // this is a hack!!!
549    // the call to reset the mouse clipping size should probably be somewhere
550    // else, however this works for the moment.
[523]551    unsigned int width, height, depth;
552    int left, top;
553    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
[511]554
[599]555    if(mode_!=CLIENT){
556      const OIS::MouseState &ms = mouse_->getMouseState();
557      ms.width = width;
558      ms.height = height;
559    }
[462]560    ogre_->getRoot()->startRendering();
[74]561  }
562}
Note: See TracBrowser for help on using the repository browser.