Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 594 was 592, checked in by nicolasc, 17 years ago

added engineglow particle effect - based of treibwerk
other various changes

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