Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 905 was 905, checked in by scheusso, 16 years ago

revided and checked gamestate handling

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