Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

blub

File size: 15.1 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, gameMode mode)
90      : mKeyboard(keyboard), mMouse(mouse)
91      {
92
93
94        mode_=mode;
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        if(mode_==SERVER)
165          server_g->tick(evt.timeSinceLastFrame);
166        usleep(10);
167        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
168      }
169
170      bool mouseMoved(const OIS::MouseEvent &e)
171      {
172        mouseX += e.state.X.rel;
173        mouseY += e.state.Y.rel;
174        if(mouseX>maxMouseX) maxMouseX = mouseX;
175        if(mouseX<minMouseX) minMouseX = mouseX;
176        //cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
177        moved = true;
178        return true;
179      }
180
181      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
182      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
183
184    private:
185      gameMode mode_;
186      float speed;
187      float rotate;
188      float loop;
189      float mouseY;
190      float mouseX;
191      float maxMouseX;
192      float minMouseX;
193      bool moved;
194      OIS::Keyboard *mKeyboard;
195      OIS::Mouse *mMouse;
196      audio::AudioManager*  auMan_;
197      SpaceshipSteering* steering_;
198  };
199  // init static singleton reference of Orxonox
200  Orxonox* Orxonox::singletonRef_ = NULL;
201
202  /**
203   * create a new instance of Orxonox
204   */
205  Orxonox::Orxonox()
206  {
207    ogre_ = new GraphicsEngine();
208    dataPath_ = "";
209  }
210
211  /**
212   * destruct Orxonox
213   */
214  Orxonox::~Orxonox()
215  {
216    // nothing to delete as for now
217  }
218
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
230    std::string mode;
231//     if(argc>=2)
232//       mode = std::string(argv[1]);
233//     else
234//       mode = "";
235    ArgReader ar = ArgReader(argc, argv);
236    ar.checkArgument("mode", mode, false);
237    ar.checkArgument("data", this->dataPath_, false);
238    ar.checkArgument("ip", serverIp_, false);
239    if(ar.errorHandling()) die();
240
241    if(mode == std::string("server"))
242    {
243      serverInit(path);
244      mode_ = SERVER;
245    }
246    else if(mode == std::string("client"))
247    {
248      clientInit(path);
249      mode_ = CLIENT;
250    }
251    else if(mode == std::string("presentation"))
252    {
253      serverInit(path);
254      mode_ = PRESENTATION;
255    }
256    else{
257      standaloneInit(path);
258      mode_ = STANDALONE;
259    }
260  }
261
262  /**
263   * start modules
264   */
265  void Orxonox::start()
266  {
267    //TODO: start modules
268    ogre_->startRender();
269    //TODO: run engine
270    createScene();
271    setupScene();
272    setupInputSystem();
273    if(mode_!=CLIENT){ // remove this in future ---- presentation hack
274    }
275    else
276      std::cout << "client here" << std::endl;
277    createFrameListener();
278    Factory::createClassHierarchy();
279    switch(mode_){
280    case PRESENTATION:
281      //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
282      //std::cout << "could not add framelistener" << std::endl;
283      server_g->open();
284      break;
285    case CLIENT:
286      client_g->establishConnection();
287      break;
288    case SERVER:
289    case STANDALONE:
290    default:
291      break;
292    }
293    startRenderLoop();
294  }
295
296  /**
297   * @return singleton object
298   */
299  Orxonox* Orxonox::getSingleton()
300  {
301    if (!singletonRef_)
302      singletonRef_ = new Orxonox();
303    return singletonRef_;
304  }
305
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
312    delete this;
313  }
314
315  void Orxonox::standaloneInit(std::string path)
316  {
317    ogre_->setConfigPath(path);
318    ogre_->setup();
319    root_ = ogre_->getRoot();
320    if(!ogre_->load()) die(/* unable to load */);
321
322    //defineResources();
323    //setupRenderSystem();
324    //createRenderWindow();
325    //initializeResourceGroups();
326    /*createScene();
327    setupScene();
328    setupInputSystem();
329    createFrameListener();
330    Factory::createClassHierarchy();
331    startRenderLoop();*/
332  }
333
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{
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
353      COUT(3) << "Info: network framelistener added" << std::endl;
354    }
355    catch(exception &e)
356    {
357      COUT(1) << "Error: There was a problem initialising the server :(" << std::endl;
358    }
359    startRenderLoop();
360  }
361
362  void Orxonox::standalone(){
363
364
365
366  }
367
368  void Orxonox::serverInit(std::string path)
369  {
370    COUT(2) << "initialising server" << std::endl;
371    ogre_->setConfigPath(path);
372    ogre_->setup();
373    server_g = new network::Server(); // add some settings if wanted
374    if(!ogre_->load()) die(/* unable to load */);
375    // add network framelistener
376  }
377
378  void Orxonox::clientInit(std::string path)
379  {
380    COUT(2) << "initialising client" << std::endl;
381    ogre_->setConfigPath(path);
382    ogre_->setup();
383    if(serverIp_.compare("")==0)
384      client_g = new network::Client();
385    else
386      client_g = new network::Client(serverIp_, 55556);
387    if(!ogre_->load()) die(/* unable to load */);
388    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
389  }
390
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
398    cf.load(dataPath_ + "resources.cfg");
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  {
422    if (!root_->restoreConfig() && !root_->showConfigDialog())
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
437  /**
438   *
439   * @param
440   */
441  void Orxonox::createScene(void)
442  {
443        // Init audio
444    auMan_ = new audio::AudioManager();
445
446    // load this file from config
447    loader_ = new loader::LevelLoader("sample.oxw");
448    loader_->loadLevel();
449
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();*/
464  }
465
466
467  /**
468   *
469   */
470  void Orxonox::setupScene()
471  {
472    SceneManager *mgr = ogre_->getSceneManager();
473
474
475    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
476//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
477
478
479    steering_ = new SpaceshipSteering(500, 200, 200, 200);
480    steering_->addNode(node);
481
482/*
483    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
484    e->particleSystem_->setParameter("local_space","true");
485    e->setPositionOfEmitter(0, Vector3(0,-10,0));
486    e->setDirection(Vector3(0,0,-1));
487    e->addToSceneNode(node);
488*/
489
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);
497
498    particle::ParticleInterface *tt = new particle::ParticleInterface(mgr,"twinthruster","Orxonox/engineglow");
499    tt->particleSystem_->setParameter("local_space","true");
500    tt->newEmitter();
501    tt->setDirection(Vector3(0,0,-1));
502    tt->setPositionOfEmitter(0, Vector3(20,-1,-15));
503    tt->setPositionOfEmitter(1, Vector3(-20,-1,-15));
504    tt->addToSceneNode(node);
505
506  }
507
508
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();
515
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);
520
521    try
522    {
523      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
524      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
525    }
526    catch (const OIS::Exception &e)
527    {
528      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
529    }
530  }
531
532  // we actually want to do this differently...
533  void Orxonox::createFrameListener()
534  {
535    TickFrameListener* TickFL = new TickFrameListener();
536    ogre_->getRoot()->addFrameListener(TickFL);
537
538    TimerFrameListener* TimerFL = new TimerFrameListener();
539    ogre_->getRoot()->addFrameListener(TimerFL);
540
541    //if(mode_!=CLIENT) // just a hack ------- remove this in future
542      frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_, mode_);
543    ogre_->getRoot()->addFrameListener(frameListener_);
544  }
545
546  void Orxonox::startRenderLoop()
547  {
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.
551    unsigned int width, height, depth;
552    int left, top;
553    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
554
555    if(mode_!=CLIENT){
556      const OIS::MouseState &ms = mouse_->getMouseState();
557      ms.width = width;
558      ms.height = height;
559    }
560    ogre_->getRoot()->startRendering();
561  }
562}
Note: See TracBrowser for help on using the repository browser.