Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added some objects for the level loader

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