Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/Orxonox.cc @ 904

Last change on this file since 904 was 904, checked in by scheusso, 17 years ago

bugfix

File size: 11.7 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// Precompiled Headers
34#include "OrxonoxStableHeaders.h"
35
36//****** OGRE ******
37#include <OgreException.h>
38#include <OgreRoot.h>
39#include <OgreFrameListener.h>
40#include <OgreRenderWindow.h>
41#include <OgreTextureManager.h>
42#include <OgreResourceGroupManager.h>
43#include <OgreConfigFile.h>
44#include <OgreOverlay.h>
45#include <OgreOverlayManager.h>
46
47//****** OIS *******
48#include <OIS/OIS.h>
49
50//****** STD *******
51#include <iostream>
52#include <exception>
53
54//***** ORXONOX ****
55//misc
56#include "util/Sleep.h"
57
58// loader and audio
59//#include "loader/LevelLoader.h"
60#include "audio/AudioManager.h"
61
62// network
63#include "network/Server.h"
64#include "network/Client.h"
65#include "network/NetworkPrereqs.h"
66network::Client *client_g;
67network::Server *server_g;
68
69
70// objects
71#include "objects/Tickable.h"
72#include "tools/Timer.h"
73#include "objects/NPC.h"
74#include "core/ArgReader.h"
75#include "core/Factory.h"
76#include "core/Debug.h"
77#include "core/Loader.h"
78#include "hud/HUD.h"
79#include "objects/weapon/BulletManager.h"
80#include "GraphicsEngine.h"
81
82#include "Orxonox.h"
83
84namespace orxonox
85{
86   // put this in a seperate Class or solve the problem in another fashion
87  class OrxListener : public Ogre::FrameListener
88  {
89    public:
90      OrxListener(OIS::Keyboard *keyboard, audio::AudioManager*  auMan, gameMode mode)
91      {
92        mKeyboard = keyboard;
93        mode_=mode;
94        auMan_ = auMan;
95      }
96
97      bool frameStarted(const Ogre::FrameEvent& evt)
98      {
99        auMan_->update();
100        updateAI();
101
102//         if(mode_ == SERVER)
103//           server_g->tick(evt.timeSinceLastFrame);
104//         else if(mode_ == CLIENT)
105//           client_g->tick(evt.timeSinceLastFrame);
106
107        usleep(10);
108
109        mKeyboard->capture();
110        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
111      }
112
113      void updateAI()
114      {
115        for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it)
116        {
117          it->update();
118        }
119      }
120
121    private:
122      gameMode mode_;
123      OIS::Keyboard *mKeyboard;
124      audio::AudioManager*  auMan_;
125  };
126
127  // init static singleton reference of Orxonox
128  Orxonox* Orxonox::singletonRef_ = NULL;
129
130  /**
131   * create a new instance of Orxonox
132   */
133  Orxonox::Orxonox()
134  {
135    this->ogre_ = new GraphicsEngine();
136    this->dataPath_ = "";
137//    this->loader_ = 0;
138    this->auMan_ = 0;
139    this->singletonRef_ = 0;
140    this->keyboard_ = 0;
141    this->mouse_ = 0;
142    this->inputManager_ = 0;
143    this->frameListener_ = 0;
144    this->root_ = 0;
145  }
146
147  /**
148   * destruct Orxonox
149   */
150  Orxonox::~Orxonox()
151  {
152    // nothing to delete as for now
153  }
154
155  /**
156   * initialization of Orxonox object
157   * @param argc argument counter
158   * @param argv list of arguments
159   * @param path path to config (in home dir or something)
160   */
161  void Orxonox::init(int argc, char **argv, std::string path)
162  {
163    //TODO: find config file (assuming executable directory)
164    //TODO: read config file
165    //TODO: give config file to Ogre
166    std::string mode;
167   
168   
169    ArgReader ar = ArgReader(argc, argv);
170    ar.checkArgument("mode", mode, false);
171    ar.checkArgument("data", this->dataPath_, false);
172    ar.checkArgument("ip", serverIp_, false);
173    if(ar.errorHandling()) die();
174    if(mode == std::string("client"))
175    {
176      mode_ = CLIENT;
177      clientInit(path);
178    }
179    else if(mode== std::string("server")){
180      mode_ = SERVER;
181      serverInit(path);
182    }
183    else{
184      mode_ = STANDALONE;
185      standaloneInit(path);
186    }
187  }
188
189 
190  /**
191   * start modules
192   */
193  void Orxonox::start()
194  {
195    switch(mode_){
196    case CLIENT:
197      clientStart();
198      break;
199    case SERVER:
200      serverStart();
201      break;
202    default:
203      standaloneStart();
204    }
205  }
206 
207  void Orxonox::clientStart(){
208    ogre_->startRender();
209    Factory::createClassHierarchy();
210   
211   
212    auMan_ = new audio::AudioManager();
213
214    bulletMgr_ = new BulletManager();
215   
216    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
217    HUD* orxonoxHud;
218    orxonoxHud = new HUD();
219    orxonoxHud->setEnergyValue(20);
220    orxonoxHud->setEnergyDistr(20,20,60);
221    hudOverlay->show();
222   
223    client_g->establishConnection();
224    client_g->tick(0);
225   
226   
227    setupInputSystem();
228   
229    createFrameListener();
230   
231    startRenderLoop();
232  }
233 
234  void Orxonox::serverStart(){
235    //TODO: start modules
236    ogre_->startRender();
237    //TODO: run engine
238    Factory::createClassHierarchy();
239    createScene();
240    setupScene();
241    setupInputSystem();
242   
243    createFrameListener();
244    server_g->open();
245   
246    startRenderLoop();
247  }
248 
249  void Orxonox::standaloneStart(){
250    //TODO: start modules
251    ogre_->startRender();
252    //TODO: run engine
253    Factory::createClassHierarchy();
254    createScene();
255    setupScene();
256    setupInputSystem();
257   
258    createFrameListener();
259   
260    startRenderLoop();
261  }
262
263  /**
264   * @return singleton object
265   */
266  Orxonox* Orxonox::getSingleton()
267  {
268    if (!singletonRef_)
269      singletonRef_ = new Orxonox();
270    return singletonRef_;
271  }
272
273  /**
274   * error kills orxonox
275   */
276  void Orxonox::die(/* some error code */)
277  {
278    //TODO: destroy and destruct everything and print nice error msg
279    delete this;
280  }
281
282
283  void Orxonox::serverInit(std::string path)
284  {
285    COUT(2) << "initialising server" << std::endl;
286   
287    ogre_->setConfigPath(path);
288    ogre_->setup();
289    root_ = ogre_->getRoot();
290    if(!ogre_->load()) die(/* unable to load */);
291   
292    server_g = new network::Server();
293  }
294
295  void Orxonox::clientInit(std::string path)
296  {
297    COUT(2) << "initialising client" << std::endl;\
298   
299    ogre_->setConfigPath(path);
300    ogre_->setup();
301    if(serverIp_.compare("")==0)
302      client_g = new network::Client();
303    else
304      client_g = new network::Client(serverIp_, NETWORK_PORT);
305    if(!ogre_->load()) die(/* unable to load */);
306  }
307 
308  void Orxonox::standaloneInit(std::string path)
309  {
310    COUT(2) << "initialising standalone mode" << std::endl;
311   
312    ogre_->setConfigPath(path);
313    ogre_->setup();
314    root_ = ogre_->getRoot();
315    if(!ogre_->load()) die(/* unable to load */);
316  }
317
318  void Orxonox::defineResources()
319  {
320    std::string secName, typeName, archName;
321    Ogre::ConfigFile cf;
322#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
323    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
324#else
325    cf.load(dataPath_ + "resources.cfg");
326#endif
327
328    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
329    while (seci.hasMoreElements())
330    {
331      secName = seci.peekNextKey();
332      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
333      Ogre::ConfigFile::SettingsMultiMap::iterator i;
334      for (i = settings->begin(); i != settings->end(); ++i)
335      {
336        typeName = i->first;
337        archName = i->second;
338#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
339        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( std::string(macBundlePath() + "/" + archName), typeName, secName);
340#else
341        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
342#endif
343      }
344    }
345  }
346
347  void Orxonox::setupRenderSystem()
348  {
349    if (!root_->restoreConfig() && !root_->showConfigDialog())
350      throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
351  }
352
353  void Orxonox::createRenderWindow()
354  {
355    root_->initialise(true, "OrxonoxV2");
356  }
357
358  void Orxonox::initializeResourceGroups()
359  {
360    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
361    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
362  }
363
364  /**
365   *
366   * @param
367   */
368  void Orxonox::createScene(void)
369  {
370        // Init audio
371    auMan_ = new audio::AudioManager();
372
373    bulletMgr_ = new BulletManager();
374
375    // load this file from config
376//    loader_ = new loader::LevelLoader("sample.oxw");
377//    loader_->loadLevel();
378    Level* startlevel = new Level("levels/sample.oxw");
379    Loader::open(startlevel);
380
381    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
382    HUD* orxonoxHud;
383    orxonoxHud = new HUD();
384    orxonoxHud->setEnergyValue(20);
385    orxonoxHud->setEnergyDistr(20,20,60);
386    hudOverlay->show();
387
388        /*
389    auMan_->ambientAdd("a1");
390    auMan_->ambientAdd("a2");
391    auMan_->ambientAdd("a3");
392                                //auMan->ambientAdd("ambient1");
393    auMan_->ambientStart();*/
394  }
395
396
397  /**
398   *
399   */
400  void Orxonox::setupScene()
401  {
402//    SceneManager *mgr = ogre_->getSceneManager();
403
404
405//    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
406//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
407
408
409/*
410    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
411    e->particleSystem_->setParameter("local_space","true");
412    e->setPositionOfEmitter(0, Vector3(0,-10,0));
413    e->setDirection(Vector3(0,0,-1));
414    e->addToSceneNode(node);
415*/
416  }
417
418
419  void Orxonox::setupInputSystem()
420  {
421    size_t windowHnd = 0;
422    std::ostringstream windowHndStr;
423    OIS::ParamList pl;
424
425    // fixes auto repeat problem
426    #if defined OIS_LINUX_PLATFORM
427      pl.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
428    #endif
429
430      Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
431    win->getCustomAttribute("WINDOW", &windowHnd);
432    windowHndStr << windowHnd;
433    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
434    inputManager_ = OIS::InputManager::createInputSystem(pl);
435
436    try
437    {
438      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
439      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
440    }
441    catch (const OIS::Exception &e)
442    {
443      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
444    }
445  }
446
447  // FIXME we actually want to do this differently...
448  void Orxonox::createFrameListener()
449  {
450    TickFrameListener* TickFL = new TickFrameListener();
451    ogre_->getRoot()->addFrameListener(TickFL);
452
453    TimerFrameListener* TimerFL = new TimerFrameListener();
454    ogre_->getRoot()->addFrameListener(TimerFL);
455
456    //if(mode_!=CLIENT) // FIXME just a hack ------- remove this in future
457      frameListener_ = new OrxListener(keyboard_, auMan_, mode_);
458    ogre_->getRoot()->addFrameListener(frameListener_);
459  }
460
461  void Orxonox::startRenderLoop()
462  {
463    // FIXME
464    // this is a hack!!!
465    // the call to reset the mouse clipping size should probably be somewhere
466    // else, however this works for the moment.
467    unsigned int width, height, depth;
468    int left, top;
469    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
470
471    if(mode_!=CLIENT){
472      const OIS::MouseState &ms = mouse_->getMouseState();
473      ms.width = width;
474      ms.height = height;
475    }
476    ogre_->getRoot()->startRendering();
477  }
478}
Note: See TracBrowser for help on using the repository browser.