Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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