Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 533 was 531, checked in by scheusso, 16 years ago

started implementing presentation ;)

File size: 12.3 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    } else if(argc >=2 && strcmp(argv[1], "presentation")==0)
228    {
229      playableServer(path);
230    } else
231      standalone(path);
232  }
233
234  /**
235   * start modules
236   */
237  void Orxonox::start()
238  {
239    //TODO: start modules
240
241    //TODO: run engine
242  }
243
244  /**
245   * @return singleton object
246   */
247  Orxonox* Orxonox::getSingleton()
248  {
249    if (!singletonRef_)
250      singletonRef_ = new Orxonox();
251    return singletonRef_;
252  }
253
254  /**
255   * error kills orxonox
256   */
257  void Orxonox::die(/* some error code */)
258  {
259    //TODO: destroy and destruct everything and print nice error msg
260  }
261
262  void Orxonox::standalone(std::string path)
263  {
264    ogre_->setConfigPath(path);
265    ogre_->setup();
266    root_ = ogre_->getRoot();
267    //if(!ogre_->load()) die(/* unable to load */);
268
269    defineResources();
270    setupRenderSystem();
271    createRenderWindow();
272    initializeResourceGroups();
273    createScene();
274    setupScene();
275    setupInputSystem();
276    createFrameListener();
277    Factory::createClassHierarchy();
278    startRenderLoop();
279  }
280 
281  void Orxonox::playableServer(std::string path)
282  {
283    ogre_->setConfigPath(path);
284    ogre_->setup();
285    root_ = ogre_->getRoot();
286    defineResources();
287    setupRenderSystem();
288    createRenderWindow();
289    initializeResourceGroups();
290    createScene();
291    setupScene();
292    setupInputSystem();
293    Factory::createClassHierarchy();
294    createFrameListener();
295    try{
296    server_g = new network::Server(); // add port and bindadress
297    server_g->open(); // open server and create listener thread
298    if(ogre_ && ogre_->getRoot())
299      ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server
300    std::cout << "network framelistener added" << std::endl;
301    } 
302    catch(exception &e)
303    {
304      std::cout << "There was a problem initialising the server :(" << std::endl;
305    }
306    startRenderLoop();
307  }
308
309  void Orxonox::serverInit(std::string path)
310  {
311    ogre_->setConfigPath(path);
312    ogre_->setup();
313    server_g = new network::Server(); // add some settings if wanted
314    if(!ogre_->load()) die(/* unable to load */);
315    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
316    ogre_->startRender();
317
318    createScene();
319    setupScene();
320  }
321
322  void Orxonox::clientInit(std::string path)
323  {
324    ogre_->setConfigPath(path);
325    ogre_->setup();
326    client_g = new network::Client(); // address here
327    if(!ogre_->load()) die(/* unable to load */);
328    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
329    ogre_->startRender();
330
331    createScene();
332    setupScene();
333    setupInputSystem();
334    createFrameListener();
335    startRenderLoop();
336  }
337
338  void Orxonox::defineResources()
339  {
340    Ogre::String secName, typeName, archName;
341    Ogre::ConfigFile cf;
342#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
343    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
344#else
345    cf.load("resources.cfg");
346#endif
347
348    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
349    while (seci.hasMoreElements())
350    {
351      secName = seci.peekNextKey();
352      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
353      Ogre::ConfigFile::SettingsMultiMap::iterator i;
354      for (i = settings->begin(); i != settings->end(); ++i)
355      {
356        typeName = i->first;
357        archName = i->second;
358#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
359        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
360#else
361        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
362#endif
363      }
364    }
365  }
366
367  void Orxonox::setupRenderSystem()
368  {
369    if (!root_->restoreConfig() && !root_->showConfigDialog())
370      throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
371  }
372
373  void Orxonox::createRenderWindow()
374  {
375    root_->initialise(true, "OrxonoxV2");
376  }
377
378  void Orxonox::initializeResourceGroups()
379  {
380    TextureManager::getSingleton().setDefaultNumMipmaps(5);
381    ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
382  }
383
384  void Orxonox::createScene(void)
385  {
386        // Init audio
387    auMan_ = new audio::AudioManager();
388
389    // load this file from config
390    loader_ = new loader::LevelLoader("sample.oxw");
391    loader_->loadLevel();
392
393
394
395
396
397        /*
398    auMan_->ambientAdd("a1");
399    auMan_->ambientAdd("a2");
400    auMan_->ambientAdd("a3");
401                                //auMan->ambientAdd("ambient1");
402    auMan_->ambientStart();
403        */
404  }
405
406
407  void Orxonox::setupScene()
408  {
409    SceneManager *mgr = ogre_->getSceneManager();
410
411
412    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
413
414
415    steering_ = new SpaceshipSteering(500, 200, 200, 200);
416    steering_->addNode(node);
417
418  }
419
420
421  void Orxonox::setupInputSystem()
422  {
423    size_t windowHnd = 0;
424    std::ostringstream windowHndStr;
425    OIS::ParamList pl;
426    Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
427
428    win->getCustomAttribute("WINDOW", &windowHnd);
429    windowHndStr << windowHnd;
430    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
431    inputManager_ = OIS::InputManager::createInputSystem(pl);
432
433    try
434    {
435      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
436      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
437    }
438    catch (const OIS::Exception &e)
439    {
440      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
441    }
442  }
443
444  // we actually want to do this differently...
445  void Orxonox::createFrameListener()
446  {
447    TickFrameListener* TickFL = new TickFrameListener();
448    ogre_->getRoot()->addFrameListener(TickFL);
449
450    TimerFrameListener* TimerFL = new TimerFrameListener();
451    ogre_->getRoot()->addFrameListener(TimerFL);
452
453    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_);
454    ogre_->getRoot()->addFrameListener(frameListener_);
455  }
456
457  void Orxonox::startRenderLoop()
458  {
459    // this is a hack!!!
460    // the call to reset the mouse clipping size should probably be somewhere
461    // else, however this works for the moment.
462    unsigned int width, height, depth;
463    int left, top;
464    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
465
466    const OIS::MouseState &ms = mouse_->getMouseState();
467    ms.width = width;
468    ms.height = height;
469
470    ogre_->getRoot()->startRendering();
471  }
472}
Note: See TracBrowser for help on using the repository browser.