Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 497 was 496, checked in by landauf, 17 years ago

added files from objecthierarchy, changed includes

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