Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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