Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 555 was 546, checked in by bknecht, 17 years ago

suggestion for solution of WinMain problem

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