Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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