Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 511 was 511, checked in by nicolape, 17 years ago

Reto added steering bug fix

File size: 12.5 KB
RevLine 
[74]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
[462]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.
[74]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
[462]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.
[74]20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
[142]28/**
29 @file  orxonox.cc
[462]30 @brief Orxonox Main Class
[142]31 */
32
[462]33#include "orxonox.h"
34#include "graphicsEngine.h"
35
[379]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
[137]47#include <OIS/OIS.h>
[346]48//#include <CEGUI/CEGUI.h>
49//#include <OgreCEGUIRenderer.h>
[74]50
[164]51#include <string>
52#include <iostream>
53
[496]54#include "objects/Tickable.h"
55#include "objects/Timer.h"
56#include "core/Factory.h"
57
[462]58#include "../loader/LevelLoader.h"
59#include "../audio/AudioManager.h"
[164]60
[337]61#include "spaceship_steering.h"
62SpaceshipSteering* steering;
[164]63
[337]64
[459]65//network stuff
[462]66#include "../network/Server.h"
67#include "../network/Client.h"
[473]68#include "../network/NetworkFrameListener.h"
[459]69
[151]70// some tests to see if enet works without includsion
71//#include <enet/enet.h>
72//#include <enet/protocol.h>
[142]73
[74]74
[337]75namespace orxonox
76{
[462]77  using namespace Ogre;
[74]78
[462]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;
[337]93        moved = false;
[462]94        steering->brakeRotate(rotate*10);
95        steering->brakeLoop(loop);
96        mMouse->setEventCallback(this);
97        auMan_ = auMan;
[265]98      }
[462]99      bool frameStarted(const FrameEvent& evt)
100      {
[135]101
[462]102        auMan_->update();
[74]103
[462]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);
[337]122
[462]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);
[511]132          mouseY = 0;
133          mouseX = 0;
[462]134          moved = false;
135        }
136        else {
137          steering->rotateUp(0);
138          steering->rotateDown(0);
139          steering->rotateRight(0);
140          steering->rotateLeft(0);
141        }
[337]142
[462]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      }
[337]149
[462]150      bool mouseMoved(const OIS::MouseEvent &e)
[265]151      {
[511]152        mouseX += e.state.X.rel;
153        mouseY += e.state.Y.rel;
[462]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;
[265]159      }
[74]160
[462]161      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
162      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
[137]163
[265]164    private:
[462]165      float speed;
166      float rotate;
167      float loop;
168      float mouseY;
169      float mouseX;
170      float maxMouseX;
171      float minMouseX;
172      bool moved;
[265]173      OIS::Keyboard *mKeyboard;
174      OIS::Mouse *mMouse;
[462]175      audio::AudioManager*  auMan_;
176  };
177  // init static singleton reference of Orxonox
178  Orxonox* Orxonox::singletonRef_ = NULL;
[137]179
[462]180  /**
181   * create a new instance of Orxonox
182   */
183  Orxonox::Orxonox()
184  {
185    ogre_ = new GraphicsEngine();
186  }
[74]187
[462]188  /**
189   * destruct Orxonox
190   */
191  Orxonox::~Orxonox()
192  {
193    // nothing to delete as for now
194  }
[265]195
[462]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  }
[263]218
[462]219  /**
220   * start modules
221   */
222  void Orxonox::start()
223  {
224    //TODO: start modules
[480]225
[462]226    //TODO: run engine
227  }
[74]228
[462]229  /**
230   * @return singleton object
231   */
232  Orxonox* Orxonox::getSingleton()
233  {
234    if (!singletonRef_)
235      singletonRef_ = new Orxonox();
236    return singletonRef_;
237  }
[74]238
[462]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  }
[74]246
[462]247  void Orxonox::standalone(std::string path)
248  {
249    ogre_->setConfigPath(path);
250    ogre_->setup();
[473]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();
[496]262    Factory::createClassHierarchy();
[473]263    startRenderLoop();
[462]264  }
[74]265
[462]266  void Orxonox::serverInit(std::string path)
267  {
268    ogre_->setConfigPath(path);
269    ogre_->setup();
[473]270    server_g = new network::Server(); // add some settings if wanted
[462]271    if(!ogre_->load()) die(/* unable to load */);
[505]272    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
[462]273    ogre_->startRender();
[389]274
[462]275    createScene();
276    setupScene();
277  }
[164]278
[462]279  void Orxonox::clientInit(std::string path)
280  {
281    ogre_->setConfigPath(path);
282    ogre_->setup();
[473]283    client_g = new network::Client(); // address here
[462]284    if(!ogre_->load()) die(/* unable to load */);
[505]285    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
[462]286    ogre_->startRender();
[389]287
[462]288    createScene();
289    setupScene();
290    setupInputSystem();
291    createFrameListener();
292    startRenderLoop();
293  }
[258]294
[473]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
[462]341  void Orxonox::createScene(void)
342  {
[480]343        // Init audio
[462]344    auMan_ = new audio::AudioManager();
[487]345
[480]346    // load this file from config
347    loader_ = new loader::LevelLoader("sample.oxw");
348    loader_->loadLevel();
[337]349
[480]350        /*
[462]351    auMan_->ambientAdd("a1");
352    auMan_->ambientAdd("a2");
353    auMan_->ambientAdd("a3");
354                                //auMan->ambientAdd("ambient1");
355    auMan_->ambientStart();
[480]356        */
[462]357  }
[337]358
359
[462]360  void Orxonox::setupScene()
361  {
362    SceneManager *mgr = ogre_->getSceneManager();
[507]363   
[462]364    Camera *cam = mgr->createCamera("Camera");
[511]365    cam->setPosition(Vector3(0,0,-250));
[462]366    cam->lookAt(Vector3(0,0,0));
367    Viewport *vp = ogre_->getRoot()->getAutoCreatedWindow()->addViewport(cam);
[337]368
[487]369
370    Entity* head = mgr->createEntity("ASSF", "assf2.mesh");
[462]371    SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
372    node->attachObject(head);
373    node->attachObject(cam);
[507]374   
375   
[74]376
[462]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);
[74]392
[462]393    steering = new SpaceshipSteering(500, 200, 200, 200);
394    steering->addNode(node);
[74]395
[482]396
[462]397  }
[137]398
399
[462]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();
[137]406
[462]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);
[265]411
[462]412    try
[337]413    {
[462]414      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
415      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
[337]416    }
[462]417    catch (const OIS::Exception &e)
[459]418    {
[462]419      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
[459]420    }
[462]421  }
[137]422
[462]423  // we actually want to do this differently...
424  void Orxonox::createFrameListener()
[137]425  {
[496]426    TickFrameListener* TickFL = new TickFrameListener();
427    ogre_->getRoot()->addFrameListener(TickFL);
428
429    TimerFrameListener* TimerFL = new TimerFrameListener();
430    ogre_->getRoot()->addFrameListener(TimerFL);
431
[462]432    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_);
433    ogre_->getRoot()->addFrameListener(frameListener_);
[137]434  }
[462]435
436  void Orxonox::startRenderLoop()
[137]437  {
[511]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
[462]449    ogre_->getRoot()->startRendering();
[74]450  }
451}
Note: See TracBrowser for help on using the repository browser.