Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

created client start routine

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    startRenderLoop();
227  }
228 
229  void Orxonox::serverStart(){
230    //TODO: start modules
231    ogre_->startRender();
232    //TODO: run engine
233    Factory::createClassHierarchy();
234    createScene();
235    setupScene();
236    setupInputSystem();
237   
238    createFrameListener();
239    server_g->open();
240   
241    startRenderLoop();
242  }
243 
244  void Orxonox::standaloneStart(){
245    //TODO: start modules
246    ogre_->startRender();
247    //TODO: run engine
248    Factory::createClassHierarchy();
249    createScene();
250    setupScene();
251    setupInputSystem();
252   
253    createFrameListener();
254   
255    startRenderLoop();
256  }
257
258  /**
259   * @return singleton object
260   */
261  Orxonox* Orxonox::getSingleton()
262  {
263    if (!singletonRef_)
264      singletonRef_ = new Orxonox();
265    return singletonRef_;
266  }
267
268  /**
269   * error kills orxonox
270   */
271  void Orxonox::die(/* some error code */)
272  {
273    //TODO: destroy and destruct everything and print nice error msg
274    delete this;
275  }
276
277
278  void Orxonox::serverInit(std::string path)
279  {
280    COUT(2) << "initialising server" << std::endl;
281   
282    ogre_->setConfigPath(path);
283    ogre_->setup();
284    root_ = ogre_->getRoot();
285    if(!ogre_->load()) die(/* unable to load */);
286   
287    server_g = new network::Server();
288  }
289
290  void Orxonox::clientInit(std::string path)
291  {
292    COUT(2) << "initialising client" << std::endl;\
293   
294    ogre_->setConfigPath(path);
295    ogre_->setup();
296    if(serverIp_.compare("")==0)
297      client_g = new network::Client();
298    else
299      client_g = new network::Client(serverIp_, NETWORK_PORT);
300    if(!ogre_->load()) die(/* unable to load */);
301  }
302 
303  void Orxonox::standaloneInit(std::string path)
304  {
305    COUT(2) << "initialising standalone mode" << std::endl;
306   
307    ogre_->setConfigPath(path);
308    ogre_->setup();
309    root_ = ogre_->getRoot();
310    if(!ogre_->load()) die(/* unable to load */);
311  }
312
313  void Orxonox::defineResources()
314  {
315    std::string secName, typeName, archName;
316    Ogre::ConfigFile cf;
317#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
318    cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
319#else
320    cf.load(dataPath_ + "resources.cfg");
321#endif
322
323    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
324    while (seci.hasMoreElements())
325    {
326      secName = seci.peekNextKey();
327      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
328      Ogre::ConfigFile::SettingsMultiMap::iterator i;
329      for (i = settings->begin(); i != settings->end(); ++i)
330      {
331        typeName = i->first;
332        archName = i->second;
333#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
334        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( std::string(macBundlePath() + "/" + archName), typeName, secName);
335#else
336        Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
337#endif
338      }
339    }
340  }
341
342  void Orxonox::setupRenderSystem()
343  {
344    if (!root_->restoreConfig() && !root_->showConfigDialog())
345      throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
346  }
347
348  void Orxonox::createRenderWindow()
349  {
350    root_->initialise(true, "OrxonoxV2");
351  }
352
353  void Orxonox::initializeResourceGroups()
354  {
355    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
356    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
357  }
358
359  /**
360   *
361   * @param
362   */
363  void Orxonox::createScene(void)
364  {
365        // Init audio
366    auMan_ = new audio::AudioManager();
367
368    bulletMgr_ = new BulletManager();
369
370    // load this file from config
371//    loader_ = new loader::LevelLoader("sample.oxw");
372//    loader_->loadLevel();
373    Level* startlevel = new Level("levels/sample.oxw");
374    Loader::open(startlevel);
375
376    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
377    HUD* orxonoxHud;
378    orxonoxHud = new HUD();
379    orxonoxHud->setEnergyValue(20);
380    orxonoxHud->setEnergyDistr(20,20,60);
381    hudOverlay->show();
382
383        /*
384    auMan_->ambientAdd("a1");
385    auMan_->ambientAdd("a2");
386    auMan_->ambientAdd("a3");
387                                //auMan->ambientAdd("ambient1");
388    auMan_->ambientStart();*/
389  }
390
391
392  /**
393   *
394   */
395  void Orxonox::setupScene()
396  {
397//    SceneManager *mgr = ogre_->getSceneManager();
398
399
400//    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
401//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
402
403
404/*
405    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
406    e->particleSystem_->setParameter("local_space","true");
407    e->setPositionOfEmitter(0, Vector3(0,-10,0));
408    e->setDirection(Vector3(0,0,-1));
409    e->addToSceneNode(node);
410*/
411  }
412
413
414  void Orxonox::setupInputSystem()
415  {
416    size_t windowHnd = 0;
417    std::ostringstream windowHndStr;
418    OIS::ParamList pl;
419
420    // fixes auto repeat problem
421    #if defined OIS_LINUX_PLATFORM
422      pl.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
423    #endif
424
425      Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
426    win->getCustomAttribute("WINDOW", &windowHnd);
427    windowHndStr << windowHnd;
428    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
429    inputManager_ = OIS::InputManager::createInputSystem(pl);
430
431    try
432    {
433      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
434      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
435    }
436    catch (const OIS::Exception &e)
437    {
438      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
439    }
440  }
441
442  // FIXME we actually want to do this differently...
443  void Orxonox::createFrameListener()
444  {
445    TickFrameListener* TickFL = new TickFrameListener();
446    ogre_->getRoot()->addFrameListener(TickFL);
447
448    TimerFrameListener* TimerFL = new TimerFrameListener();
449    ogre_->getRoot()->addFrameListener(TimerFL);
450
451    //if(mode_!=CLIENT) // FIXME just a hack ------- remove this in future
452      frameListener_ = new OrxListener(keyboard_, auMan_, mode_);
453    ogre_->getRoot()->addFrameListener(frameListener_);
454  }
455
456  void Orxonox::startRenderLoop()
457  {
458    // FIXME
459    // this is a hack!!!
460    // the call to reset the mouse clipping size should probably be somewhere
461    // else, however this works for the moment.
462    unsigned int width, height, depth;
463    int left, top;
464    ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top);
465
466    if(mode_!=CLIENT){
467      const OIS::MouseState &ms = mouse_->getMouseState();
468      ms.width = width;
469      ms.height = height;
470    }
471    ogre_->getRoot()->startRendering();
472  }
473}
Note: See TracBrowser for help on using the repository browser.