Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

suggestion for solution of WinMain problem

File size: 13.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
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.
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
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.
20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29 @file  orxonox.cc
30 @brief Orxonox Main Class
31 */
32
33#include "orxonox.h"
34#include "graphicsEngine.h"
35
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
47#include <OIS/OIS.h>
48//#include <CEGUI/CEGUI.h>
49//#include <OgreCEGUIRenderer.h>
50
51#include <string>
52#include <iostream>
53
54#include "objects/Tickable.h"
55#include "objects/Timer.h"
56#include "core/ArgReader.h"
57#include "core/Factory.h"
58
59#include "../loader/LevelLoader.h"
60#include "../audio/AudioManager.h"
61
62#include "spaceship_steering.h"
63
64#include "particle/ParticleInterface.h"
65
66//network stuff
67#include "../network/Server.h"
68#include "../network/Client.h"
69#include "../network/NetworkFrameListener.h"
70
71
72namespace orxonox
73{
74  using namespace Ogre;
75
76   // put this in seperate Class or solve the problem in another fashion
77  class OrxListener : public FrameListener, public OIS::MouseListener
78  {
79    public:
80      OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager*  auMan, SpaceshipSteering* steering)
81      : mKeyboard(keyboard), mMouse(mouse)
82      {
83
84
85
86        speed = 250;
87        loop = 100;
88        rotate = 10;
89        mouseX = 0;
90        mouseY = 0;
91        maxMouseX = 0;
92        minMouseX = 0;
93        moved = false;
94
95        steering_ = steering;
96
97        steering_->brakeRotate(rotate*10);
98        steering_->brakeLoop(loop);
99
100
101        mMouse->setEventCallback(this);
102        auMan_ = auMan;
103      }
104      bool frameStarted(const FrameEvent& evt)
105      {
106
107        auMan_->update();
108
109        mKeyboard->capture();
110        mMouse->capture();
111        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
112          steering_->moveForward(speed);
113        else
114          steering_->moveForward(0);
115        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
116          steering_->brakeForward(speed);
117        else
118          steering_->brakeForward(speed/10);
119        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
120          steering_->loopRight(loop);
121        else
122          steering_->loopRight(0);
123        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
124          steering_->loopLeft(loop);
125        else
126          steering_->loopLeft(0);
127
128        if(moved) {
129          if (mouseY<=0)
130            steering_->rotateUp(-mouseY*rotate);
131          if (mouseY>0)
132            steering_->rotateDown(mouseY*rotate);
133          if (mouseX>0)
134            steering_->rotateRight(mouseX*rotate);
135          if (mouseX<=0)
136            steering_->rotateLeft(-mouseX*rotate);
137          mouseY = 0;
138          mouseX = 0;
139          moved = false;
140        }
141        else {
142          steering_->rotateUp(0);
143          steering_->rotateDown(0);
144          steering_->rotateRight(0);
145          steering_->rotateLeft(0);
146        }
147
148                steering_->tick(evt.timeSinceLastFrame);
149
150
151
152//      scenemanager->spacehip->tick(evt.timesincelastframe);
153        //if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
154          //cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl;
155        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
156      }
157
158      bool mouseMoved(const OIS::MouseEvent &e)
159      {
160        mouseX += e.state.X.rel;
161        mouseY += e.state.Y.rel;
162        if(mouseX>maxMouseX) maxMouseX = mouseX;
163        if(mouseX<minMouseX) minMouseX = mouseX;
164        //cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
165        moved = true;
166        return true;
167      }
168
169      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
170      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
171
172    private:
173      float speed;
174      float rotate;
175      float loop;
176      float mouseY;
177      float mouseX;
178      float maxMouseX;
179      float minMouseX;
180      bool moved;
181      OIS::Keyboard *mKeyboard;
182      OIS::Mouse *mMouse;
183      audio::AudioManager*  auMan_;
184      SpaceshipSteering* steering_;
185  };
186  // init static singleton reference of Orxonox
187  Orxonox* Orxonox::singletonRef_ = NULL;
188
189  /**
190   * create a new instance of Orxonox
191   */
192  Orxonox::Orxonox()
193  {
194    ogre_ = new GraphicsEngine();
195    dataPath_ = "";
196  }
197
198  /**
199   * destruct Orxonox
200   */
201  Orxonox::~Orxonox()
202  {
203    // nothing to delete as for now
204  }
205
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
217    std::string mode = "";
218    ArgReader ar = ArgReader(argc, argv);
219    ar.checkArgument("mode", mode, false);
220    ar.checkArgument("data", this->dataPath_, false);
221    if(ar.errorHandling()) die();
222
223    if(mode == std::string("server"))
224    {
225      serverInit(path);
226    }
227    else if(mode == std::string("client"))
228    {
229      clientInit(path);
230    }
231    else if(mode == std::string("presentation"))
232    {
233      playableServer(path);
234    }
235    else
236      standalone(path);
237  }
238
239  /**
240   * start modules
241   */
242  void Orxonox::start()
243  {
244    //TODO: start modules
245    ogre_->startRender();
246    //TODO: run engine
247    createScene();
248    setupScene();
249    setupInputSystem();
250    createFrameListener();
251    Factory::createClassHierarchy();
252    startRenderLoop();
253  }
254
255  /**
256   * @return singleton object
257   */
258  Orxonox* Orxonox::getSingleton()
259  {
260    if (!singletonRef_)
261      singletonRef_ = new Orxonox();
262    return singletonRef_;
263  }
264
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
271    delete this;
272  }
273
274  void Orxonox::standalone(std::string path)
275  {
276    ogre_->setConfigPath(path);
277    ogre_->setup();
278    root_ = ogre_->getRoot();
279    if(!ogre_->load()) die(/* unable to load */);
280
281    //defineResources();
282    //setupRenderSystem();
283    //createRenderWindow();
284    //initializeResourceGroups();
285    /*createScene();
286    setupScene();
287    setupInputSystem();
288    createFrameListener();
289    Factory::createClassHierarchy();
290    startRenderLoop();*/
291  }
292
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{
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    }
314    catch(exception &e)
315    {
316      std::cout << "There was a problem initialising the server :(" << std::endl;
317    }
318    startRenderLoop();
319  }
320
321  void Orxonox::serverInit(std::string path)
322  {
323    ogre_->setConfigPath(path);
324    ogre_->setup();
325    server_g = new network::Server(); // add some settings if wanted
326    if(!ogre_->load()) die(/* unable to load */);
327    ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
328    ogre_->startRender();
329
330    createScene();
331    setupScene();
332  }
333
334  void Orxonox::clientInit(std::string path)
335  {
336    ogre_->setConfigPath(path);
337    ogre_->setup();
338    client_g = new network::Client(); // address here
339    if(!ogre_->load()) die(/* unable to load */);
340    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
341    ogre_->startRender();
342
343    createScene();
344    setupScene();
345    setupInputSystem();
346    createFrameListener();
347    startRenderLoop();
348  }
349
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
357    cf.load(dataPath_ + "resources.cfg");
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  {
381    if (/*!root_->restoreConfig() &&*/ !root_->showConfigDialog())
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
396  /**
397   *
398   * @param
399   */
400  void Orxonox::createScene(void)
401  {
402        // Init audio
403    auMan_ = new audio::AudioManager();
404
405    // load this file from config
406    loader_ = new loader::LevelLoader("sample.oxw");
407    loader_->loadLevel();
408
409        /*
410    auMan_->ambientAdd("a1");
411    auMan_->ambientAdd("a2");
412    auMan_->ambientAdd("a3");
413                                //auMan->ambientAdd("ambient1");
414    auMan_->ambientStart();
415        */
416  }
417
418
419  /**
420   *
421   */
422  void Orxonox::setupScene()
423  {
424    SceneManager *mgr = ogre_->getSceneManager();
425
426
427    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
428//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
429
430
431    steering_ = new SpaceshipSteering(500, 200, 200, 200);
432    steering_->addNode(node);
433
434
435    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
436    e->particleSystem_->setParameter("local_space","true");
437    e->setPositionOfEmitter(0, Vector3(0,-10,200));
438    e->setDirection(Vector3(0,0,-1));
439    e->addToSceneNode(node);
440
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);
448  }
449
450
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();
457
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);
462
463    try
464    {
465      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
466      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
467    }
468    catch (const OIS::Exception &e)
469    {
470      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
471    }
472  }
473
474  // we actually want to do this differently...
475  void Orxonox::createFrameListener()
476  {
477    TickFrameListener* TickFL = new TickFrameListener();
478    ogre_->getRoot()->addFrameListener(TickFL);
479
480    TimerFrameListener* TimerFL = new TimerFrameListener();
481    ogre_->getRoot()->addFrameListener(TimerFL);
482
483    frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_);
484    ogre_->getRoot()->addFrameListener(frameListener_);
485  }
486
487  void Orxonox::startRenderLoop()
488  {
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.
492    unsigned int width, height, depth;
493    int left, top;
494    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
495
496    const OIS::MouseState &ms = mouse_->getMouseState();
497    ms.width = width;
498    ms.height = height;
499
500    ogre_->getRoot()->startRendering();
501  }
502}
Note: See TracBrowser for help on using the repository browser.