Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 567 was 565, checked in by scheusso, 18 years ago

added class BaseEntity:

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