Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

sdfsdf

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