Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 560 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
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//network stuff
68#include "../network/Server.h"
69#include "../network/Client.h"
70#include "../network/NetworkFrameListener.h"
71
72
73namespace orxonox
74{
75  using namespace Ogre;
76
77   // put this in seperate Class or solve the problem in another fashion
78  class OrxListener : public FrameListener, public OIS::MouseListener
79  {
80    public:
81      OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager*  auMan, SpaceshipSteering* steering)
82      : mKeyboard(keyboard), mMouse(mouse)
83      {
84
85
86
87        speed = 250;
88        loop = 100;
89        rotate = 10;
90        mouseX = 0;
91        mouseY = 0;
92        maxMouseX = 0;
93        minMouseX = 0;
94        moved = false;
95
96        steering_ = steering;
97
98        steering_->brakeRotate(rotate*10);
99        steering_->brakeLoop(loop);
100
101
102        mMouse->setEventCallback(this);
103        auMan_ = auMan;
104      }
105      bool frameStarted(const FrameEvent& evt)
106      {
107
108        auMan_->update();
109
110        mKeyboard->capture();
111        mMouse->capture();
112        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
113          steering_->moveForward(speed);
114        else
115          steering_->moveForward(0);
116        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
117          steering_->brakeForward(speed);
118        else
119          steering_->brakeForward(speed/10);
120        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
121          steering_->loopRight(loop);
122        else
123          steering_->loopRight(0);
124        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
125          steering_->loopLeft(loop);
126        else
127          steering_->loopLeft(0);
128
129        if(moved) {
130          if (mouseY<=0)
131            steering_->rotateUp(-mouseY*rotate);
132          if (mouseY>0)
133            steering_->rotateDown(mouseY*rotate);
134          if (mouseX>0)
135            steering_->rotateRight(mouseX*rotate);
136          if (mouseX<=0)
137            steering_->rotateLeft(-mouseX*rotate);
138          mouseY = 0;
139          mouseX = 0;
140          moved = false;
141        }
142        else {
143          steering_->rotateUp(0);
144          steering_->rotateDown(0);
145          steering_->rotateRight(0);
146          steering_->rotateLeft(0);
147        }
148
149                steering_->tick(evt.timeSinceLastFrame);
150
151
152
153//      scenemanager->spacehip->tick(evt.timesincelastframe);
154        //if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
155          //cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl;
156        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
157      }
158
159      bool mouseMoved(const OIS::MouseEvent &e)
160      {
161        mouseX += e.state.X.rel;
162        mouseY += e.state.Y.rel;
163        if(mouseX>maxMouseX) maxMouseX = mouseX;
164        if(mouseX<minMouseX) minMouseX = mouseX;
165        //cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
166        moved = true;
167        return true;
168      }
169
170      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
171      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
172
173    private:
174      float speed;
175      float rotate;
176      float loop;
177      float mouseY;
178      float mouseX;
179      float maxMouseX;
180      float minMouseX;
181      bool moved;
182      OIS::Keyboard *mKeyboard;
183      OIS::Mouse *mMouse;
184      audio::AudioManager*  auMan_;
185      SpaceshipSteering* steering_;
186  };
187  // init static singleton reference of Orxonox
188  Orxonox* Orxonox::singletonRef_ = NULL;
189
190  /**
191   * create a new instance of Orxonox
192   */
193  Orxonox::Orxonox()
194  {
195    ogre_ = new GraphicsEngine();
196    dataPath_ = "";
197  }
198
199  /**
200   * destruct Orxonox
201   */
202  Orxonox::~Orxonox()
203  {
204    // nothing to delete as for now
205  }
206
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
218    std::string mode = "";
219    ArgReader ar = ArgReader(argc, argv);
220    ar.checkArgument("mode", mode, false);
221    ar.checkArgument("data", this->dataPath_, false);
222    if(ar.errorHandling()) die();
223
224    if(mode == std::string("server"))
225    {
226      serverInit(path);
227    }
228    else if(mode == std::string("client"))
229    {
230      clientInit(path);
231    }
232    else if(mode == std::string("presentation"))
233    {
234      playableServer(path);
235    }
236    else
237      standalone(path);
238  }
239
240  /**
241   * start modules
242   */
243  void Orxonox::start()
244  {
245    //TODO: start modules
246    ogre_->startRender();
247    //TODO: run engine
248    createScene();
249    setupScene();
250    setupInputSystem();
251    createFrameListener();
252    Factory::createClassHierarchy();
253    startRenderLoop();
254  }
255
256  /**
257   * @return singleton object
258   */
259  Orxonox* Orxonox::getSingleton()
260  {
261    if (!singletonRef_)
262      singletonRef_ = new Orxonox();
263    return singletonRef_;
264  }
265
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
272    delete this;
273  }
274
275  void Orxonox::standalone(std::string path)
276  {
277    ogre_->setConfigPath(path);
278    ogre_->setup();
279    root_ = ogre_->getRoot();
280    if(!ogre_->load()) die(/* unable to load */);
281
282    //defineResources();
283    //setupRenderSystem();
284    //createRenderWindow();
285    //initializeResourceGroups();
286    /*createScene();
287    setupScene();
288    setupInputSystem();
289    createFrameListener();
290    Factory::createClassHierarchy();
291    startRenderLoop();*/
292  }
293
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{
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
313      COUT(3) << "Info: network framelistener added" << std::endl;
314    }
315    catch(exception &e)
316    {
317      COUT(1) << "Error: There was a problem initialising the server :(" << std::endl;
318    }
319    startRenderLoop();
320  }
321
322  void Orxonox::serverInit(std::string path)
323  {
324    ogre_->setConfigPath(path);
325    ogre_->setup();
326    server_g = new network::Server(); // add some settings if wanted
327    if(!ogre_->load()) die(/* unable to load */);
328    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
329    ogre_->startRender();
330
331    createScene();
332    setupScene();
333  }
334
335  void Orxonox::clientInit(std::string path)
336  {
337    ogre_->setConfigPath(path);
338    ogre_->setup();
339    client_g = new network::Client(); // address here
340    if(!ogre_->load()) die(/* unable to load */);
341    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
342    ogre_->startRender();
343
344    createScene();
345    setupScene();
346    setupInputSystem();
347    createFrameListener();
348    startRenderLoop();
349  }
350
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
358    cf.load(dataPath_ + "resources.cfg");
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  {
382    if (/*!root_->restoreConfig() &&*/ !root_->showConfigDialog())
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
397  /**
398   *
399   * @param
400   */
401  void Orxonox::createScene(void)
402  {
403        // Init audio
404    auMan_ = new audio::AudioManager();
405
406    // load this file from config
407    loader_ = new loader::LevelLoader("sample.oxw");
408    loader_->loadLevel();
409
410        /*
411    auMan_->ambientAdd("a1");
412    auMan_->ambientAdd("a2");
413    auMan_->ambientAdd("a3");
414                                //auMan->ambientAdd("ambient1");
415    auMan_->ambientStart();
416        */
417  }
418
419
420  /**
421   *
422   */
423  void Orxonox::setupScene()
424  {
425    SceneManager *mgr = ogre_->getSceneManager();
426
427
428    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
429//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
430
431
432    steering_ = new SpaceshipSteering(500, 200, 200, 200);
433    steering_->addNode(node);
434
435
436    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
437    e->particleSystem_->setParameter("local_space","true");
438    e->setPositionOfEmitter(0, Vector3(0,-10,200));
439    e->setDirection(Vector3(0,0,-1));
440    e->addToSceneNode(node);
441
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);
449  }
450
451
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();
458
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);
463
464    try
465    {
466      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
467      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
468    }
469    catch (const OIS::Exception &e)
470    {
471      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
472    }
473  }
474
475  // we actually want to do this differently...
476  void Orxonox::createFrameListener()
477  {
478    TickFrameListener* TickFL = new TickFrameListener();
479    ogre_->getRoot()->addFrameListener(TickFL);
480
481    TimerFrameListener* TimerFL = new TimerFrameListener();
482    ogre_->getRoot()->addFrameListener(TimerFL);
483
484    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_);
485    ogre_->getRoot()->addFrameListener(frameListener_);
486  }
487
488  void Orxonox::startRenderLoop()
489  {
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.
493    unsigned int width, height, depth;
494    int left, top;
495    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
496
497    const OIS::MouseState &ms = mouse_->getMouseState();
498    ms.width = width;
499    ms.height = height;
500
501    ogre_->getRoot()->startRendering();
502  }
503}
Note: See TracBrowser for help on using the repository browser.