Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 530 was 530, checked in by nicolasc, 16 years ago

test upload

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