Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 515 was 515, checked in by nicolape, 16 years ago

Added some objects for the level loader

File size: 12.7 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)
83      : mKeyboard(keyboard), mMouse(mouse)
84      {
85       
86        SpaceshipSteering* steering = orxonox::Orxonox::getSingleton()->getSteeringPointer();
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        steering->brakeRotate(rotate*10);
97        steering->brakeLoop(loop);
98        mMouse->setEventCallback(this);
99        auMan_ = auMan;
100      }
101      bool frameStarted(const FrameEvent& evt)
102      {
103
104        auMan_->update();
105
106        SpaceshipSteering* steering = orxonox::Orxonox::getSingleton()->getSteeringPointer();
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  };
184  // init static singleton reference of Orxonox
185  Orxonox* Orxonox::singletonRef_ = NULL;
186
187  /**
188   * create a new instance of Orxonox
189   */
190  Orxonox::Orxonox()
191  {
192    ogre_ = new GraphicsEngine();
193  }
194
195  /**
196   * destruct Orxonox
197   */
198  Orxonox::~Orxonox()
199  {
200    // nothing to delete as for now
201  }
202
203  /**
204   * initialization of Orxonox object
205   * @param argc argument counter
206   * @param argv list of arguments
207   * @param path path to config (in home dir or something)
208   */
209  void Orxonox::init(int argc, char **argv, std::string path)
210  {
211    //TODO: initialization with command line parameters
212    //TODO: find config file (assuming executable directory)
213    //TODO: read config file
214    //TODO: give config file to Ogre
215    if(argc >=2 && strcmp(argv[1], "server")==0)
216    {
217      serverInit(path);
218    }
219    else if(argc >=2 && strcmp(argv[1], "client")==0)
220    {
221      clientInit(path);
222    }
223    standalone(path);
224  }
225
226  /**
227   * start modules
228   */
229  void Orxonox::start()
230  {
231    //TODO: start modules
232
233    //TODO: run engine
234  }
235
236  /**
237   * @return singleton object
238   */
239  Orxonox* Orxonox::getSingleton()
240  {
241    if (!singletonRef_)
242      singletonRef_ = new Orxonox();
243    return singletonRef_;
244  }
245
246  /**
247   * error kills orxonox
248   */
249  void Orxonox::die(/* some error code */)
250  {
251    //TODO: destroy and destruct everything and print nice error msg
252  }
253
254  void Orxonox::standalone(std::string path)
255  {
256    ogre_->setConfigPath(path);
257    ogre_->setup();
258    root_ = ogre_->getRoot();
259    //if(!ogre_->load()) die(/* unable to load */);
260
261    defineResources();
262    setupRenderSystem();
263    createRenderWindow();
264    initializeResourceGroups();
265    createScene();
266    setupScene();
267    setupInputSystem();
268    createFrameListener();
269    Factory::createClassHierarchy();
270    startRenderLoop();
271  }
272
273  void Orxonox::serverInit(std::string path)
274  {
275    ogre_->setConfigPath(path);
276    ogre_->setup();
277    server_g = new network::Server(); // add some settings if wanted
278    if(!ogre_->load()) die(/* unable to load */);
279    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
280    ogre_->startRender();
281
282    createScene();
283    setupScene();
284  }
285
286  void Orxonox::clientInit(std::string path)
287  {
288    ogre_->setConfigPath(path);
289    ogre_->setup();
290    client_g = new network::Client(); // address here
291    if(!ogre_->load()) die(/* unable to load */);
292    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
293    ogre_->startRender();
294
295    createScene();
296    setupScene();
297    setupInputSystem();
298    createFrameListener();
299    startRenderLoop();
300  }
301
302  void Orxonox::defineResources()
303  {
304    Ogre::String secName, typeName, archName;
305    Ogre::ConfigFile cf;
306#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
307    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
308#else
309    cf.load("resources.cfg");
310#endif
311
312    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
313    while (seci.hasMoreElements())
314    {
315      secName = seci.peekNextKey();
316      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
317      Ogre::ConfigFile::SettingsMultiMap::iterator i;
318      for (i = settings->begin(); i != settings->end(); ++i)
319      {
320        typeName = i->first;
321        archName = i->second;
322#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
323        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
324#else
325        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
326#endif
327      }
328    }
329  }
330
331  void Orxonox::setupRenderSystem()
332  {
333    if (!root_->restoreConfig() && !root_->showConfigDialog())
334      throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
335  }
336
337  void Orxonox::createRenderWindow()
338  {
339    root_->initialise(true, "OrxonoxV2");
340  }
341
342  void Orxonox::initializeResourceGroups()
343  {
344    TextureManager::getSingleton().setDefaultNumMipmaps(5);
345    ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
346  }
347
348  void Orxonox::createScene(void)
349  {
350        // Init audio
351    auMan_ = new audio::AudioManager();
352
353    // load this file from config
354    loader_ = new loader::LevelLoader("sample.oxw");
355    loader_->loadLevel();
356
357
358
359
360
361        /*
362    auMan_->ambientAdd("a1");
363    auMan_->ambientAdd("a2");
364    auMan_->ambientAdd("a3");
365                                //auMan->ambientAdd("ambient1");
366    auMan_->ambientStart();
367        */
368  }
369
370
371  void Orxonox::setupScene()
372  {
373    SceneManager *mgr = ogre_->getSceneManager();
374
375        /*
376    Camera *cam = mgr->createCamera("Camera");
377    cam->setPosition(Vector3(0,0,-250));
378    cam->lookAt(Vector3(0,0,0));
379
380
381    Viewport *vp = ogre_->getRoot()->getAutoCreatedWindow()->addViewport(cam);
382
383/*
384    Entity* head = mgr->createEntity("ASSF", "assf2.mesh");
385    SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
386    node->attachObject(head);
387    node->attachObject(cam);
388
389
390
391    Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh");
392    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0));
393    node1->attachObject(head1);
394    Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh");
395    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100));
396    node2->attachObject(head2);
397    Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh");
398    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200));
399    node3->attachObject(head3);
400    Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh");
401    SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600));
402    node4->attachObject(head4);
403    Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh");
404    SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400));
405    node5->attachObject(head5);
406
407    steering = new SpaceshipSteering(500, 200, 200, 200);
408    steering->addNode(node);
409    */
410
411  }
412
413
414  void Orxonox::setupInputSystem()
415  {
416    size_t windowHnd = 0;
417    std::ostringstream windowHndStr;
418    OIS::ParamList pl;
419    Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
420
421    win->getCustomAttribute("WINDOW", &windowHnd);
422    windowHndStr << windowHnd;
423    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
424    inputManager_ = OIS::InputManager::createInputSystem(pl);
425
426    try
427    {
428      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
429      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
430    }
431    catch (const OIS::Exception &e)
432    {
433      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
434    }
435  }
436
437  // we actually want to do this differently...
438  void Orxonox::createFrameListener()
439  {
440    TickFrameListener* TickFL = new TickFrameListener();
441    ogre_->getRoot()->addFrameListener(TickFL);
442
443    TimerFrameListener* TimerFL = new TimerFrameListener();
444    ogre_->getRoot()->addFrameListener(TimerFL);
445
446    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_);
447    ogre_->getRoot()->addFrameListener(frameListener_);
448  }
449
450  void Orxonox::startRenderLoop()
451  {
452    // this is a hack!!!
453    // the call to reset the mouse clipping size should probably be somewhere
454    // else, however this works for the moment.
455                unsigned int width, height, depth;
456                int left, top;
457                ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
458
459                const OIS::MouseState &ms = mouse_->getMouseState();
460                ms.width = width;
461                ms.height = height;
462
463    ogre_->getRoot()->startRendering();
464  }
465}
Note: See TracBrowser for help on using the repository browser.