Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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