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, 17 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
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// some tests to see if enet works without includsion
71//#include <enet/enet.h>
72//#include <enet/protocol.h>
73
74
75namespace orxonox
76{
77  using namespace Ogre;
78
79   // put this in seperate Class or solve the problem in another fashion
80  class OrxListener : public FrameListener, public OIS::MouseListener
81  {
82    public:
83      OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager*  auMan, SpaceshipSteering* steering)
84      : mKeyboard(keyboard), mMouse(mouse)
85      {
86
87
88
89        speed = 250;
90        loop = 100;
91        rotate = 10;
92        mouseX = 0;
93        mouseY = 0;
94        maxMouseX = 0;
95        minMouseX = 0;
96        moved = false;
97
98        steering_ = steering;
99
100        steering_->brakeRotate(rotate*10);
101        steering_->brakeLoop(loop);
102
103
104        mMouse->setEventCallback(this);
105        auMan_ = auMan;
106      }
107      bool frameStarted(const FrameEvent& evt)
108      {
109
110        auMan_->update();
111
112        mKeyboard->capture();
113        mMouse->capture();
114        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
115          steering_->moveForward(speed);
116        else
117          steering_->moveForward(0);
118        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
119          steering_->brakeForward(speed);
120        else
121          steering_->brakeForward(speed/10);
122        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
123          steering_->loopRight(loop);
124        else
125          steering_->loopRight(0);
126        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
127          steering_->loopLeft(loop);
128        else
129          steering_->loopLeft(0);
130
131        if(moved) {
132          if (mouseY<=0)
133            steering_->rotateUp(-mouseY*rotate);
134          if (mouseY>0)
135            steering_->rotateDown(mouseY*rotate);
136          if (mouseX>0)
137            steering_->rotateRight(mouseX*rotate);
138          if (mouseX<=0)
139            steering_->rotateLeft(-mouseX*rotate);
140          mouseY = 0;
141          mouseX = 0;
142          moved = false;
143        }
144        else {
145          steering_->rotateUp(0);
146          steering_->rotateDown(0);
147          steering_->rotateRight(0);
148          steering_->rotateLeft(0);
149        }
150
151                steering_->tick(evt.timeSinceLastFrame);
152
153
154
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      }
160
161      bool mouseMoved(const OIS::MouseEvent &e)
162      {
163        mouseX += e.state.X.rel;
164        mouseY += e.state.Y.rel;
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;
170      }
171
172      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
173      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
174
175    private:
176      float speed;
177      float rotate;
178      float loop;
179      float mouseY;
180      float mouseX;
181      float maxMouseX;
182      float minMouseX;
183      bool moved;
184      OIS::Keyboard *mKeyboard;
185      OIS::Mouse *mMouse;
186      audio::AudioManager*  auMan_;
187      SpaceshipSteering* steering_;
188  };
189  // init static singleton reference of Orxonox
190  Orxonox* Orxonox::singletonRef_ = NULL;
191
192  /**
193   * create a new instance of Orxonox
194   */
195  Orxonox::Orxonox()
196  {
197    ogre_ = new GraphicsEngine();
198  }
199
200  /**
201   * destruct Orxonox
202   */
203  Orxonox::~Orxonox()
204  {
205    // nothing to delete as for now
206  }
207
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
220
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);
228    }
229    else if(argc >=2 && strcmp(argv[1], "presentation")==0)
230    {
231      playableServer(path);
232    }
233    else
234      standalone(path);
235  }
236
237  /**
238   * start modules
239   */
240  void Orxonox::start()
241  {
242    //TODO: start modules
243    ogre_->startRender();
244    //TODO: run engine
245    createScene();
246    setupScene();
247    setupInputSystem();
248    createFrameListener();
249    Factory::createClassHierarchy();
250    startRenderLoop();
251  }
252
253  /**
254   * @return singleton object
255   */
256  Orxonox* Orxonox::getSingleton()
257  {
258    if (!singletonRef_)
259      singletonRef_ = new Orxonox();
260    return singletonRef_;
261  }
262
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  }
270
271  void Orxonox::standalone(std::string path)
272  {
273    ogre_->setConfigPath(path);
274    ogre_->setup();
275    root_ = ogre_->getRoot();
276    if(!ogre_->load()) die(/* unable to load */);
277
278    //defineResources();
279    //setupRenderSystem();
280    //createRenderWindow();
281    //initializeResourceGroups();
282    /*createScene();
283    setupScene();
284    setupInputSystem();
285    createFrameListener();
286    Factory::createClassHierarchy();
287    startRenderLoop();*/
288  }
289
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{
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    }
311    catch(exception &e)
312    {
313      std::cout << "There was a problem initialising the server :(" << std::endl;
314    }
315    startRenderLoop();
316  }
317
318  void Orxonox::serverInit(std::string path)
319  {
320    ogre_->setConfigPath(path);
321    ogre_->setup();
322    server_g = new network::Server(); // add some settings if wanted
323    if(!ogre_->load()) die(/* unable to load */);
324    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
325    ogre_->startRender();
326
327    createScene();
328    setupScene();
329  }
330
331  void Orxonox::clientInit(std::string path)
332  {
333    ogre_->setConfigPath(path);
334    ogre_->setup();
335    client_g = new network::Client(); // address here
336    if(!ogre_->load()) die(/* unable to load */);
337    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
338    ogre_->startRender();
339
340    createScene();
341    setupScene();
342    setupInputSystem();
343    createFrameListener();
344    startRenderLoop();
345  }
346
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
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  void Orxonox::setupScene()
417  {
418    SceneManager *mgr = ogre_->getSceneManager();
419
420
421    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
422
423
424    steering_ = new SpaceshipSteering(500, 200, 200, 200);
425    steering_->addNode(node);
426
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
435  }
436
437
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();
444
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);
449
450    try
451    {
452      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
453      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
454    }
455    catch (const OIS::Exception &e)
456    {
457      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
458    }
459  }
460
461  // we actually want to do this differently...
462  void Orxonox::createFrameListener()
463  {
464    TickFrameListener* TickFL = new TickFrameListener();
465    ogre_->getRoot()->addFrameListener(TickFL);
466
467    TimerFrameListener* TimerFL = new TimerFrameListener();
468    ogre_->getRoot()->addFrameListener(TimerFL);
469
470    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_);
471    ogre_->getRoot()->addFrameListener(frameListener_);
472  }
473
474  void Orxonox::startRenderLoop()
475  {
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.
479    unsigned int width, height, depth;
480    int left, top;
481    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
482
483    const OIS::MouseState &ms = mouse_->getMouseState();
484    ms.width = width;
485    ms.height = height;
486
487    ogre_->getRoot()->startRendering();
488  }
489}
Note: See TracBrowser for help on using the repository browser.