Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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