Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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