Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 534 was 534, checked in by bknecht, 16 years ago

some updates

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