Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 534 was 534, checked in by bknecht, 16 years ago

some updates

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