Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/Orxonox.cc @ 658

Last change on this file since 658 was 637, checked in by rgrieder, 17 years ago
File size: 12.1 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//****** OGRE ******
34#include <OgreString.h>
35#include <OgreException.h>
36#include <OgreRoot.h>
37#include <OgreFrameListener.h>
38#include <OgreRenderWindow.h>
39#include <OgreTextureManager.h>
40#include <OgreResourceGroupManager.h>
41#include <OgreConfigFile.h>
42#include <OgreOverlay.h>
43#include <OgreOverlayManager.h>
44
45//****** OIS *******
46#include <OIS/OIS.h>
47
48//****** STD *******
49#include <string>
50#include <iostream>
51
52//***** ORXONOX ****
53// loader and audio
54#include "loader/LevelLoader.h"
55#include "audio/AudioManager.h"
56
57// network
58#include "network/Server.h"
59#include "network/Client.h"
60#include "network/NetworkFrameListener.h"
61
62// objects
63#include "objects/Tickable.h"
64#include "objects/Timer.h"
65#include "objects/NPC.h"
66#include "core/ArgReader.h"
67#include "core/Factory.h"
68#include "core/Debug.h"
69#include "hud/HUD.h"
70#include "objects/weapon_system/bullet_manager.h"
71
72#include "GraphicsEngine.h"
73#include "Orxonox.h"
74
75
76namespace orxonox
77{
78  using namespace Ogre;
79
80   // put this in a seperate Class or solve the problem in another fashion
81  class OrxListener : public FrameListener
82  {
83    public:
84      OrxListener(OIS::Keyboard *keyboard, audio::AudioManager*  auMan, gameMode mode)
85      {
86        mKeyboard = keyboard;
87        mode_=mode;
88        auMan_ = auMan;
89      }
90
91      bool frameStarted(const FrameEvent& evt)
92      {
93        auMan_->update();
94        updateAI();
95
96        if(mode_==PRESENTATION)
97          server_g->tick(evt.timeSinceLastFrame);
98        else if(mode_==CLIENT)
99          client_g->tick(evt.timeSinceLastFrame);
100
101        usleep(10);
102
103        mKeyboard->capture();
104        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
105      }
106
107      void updateAI()
108      {
109        for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it)
110        {
111          it->update();
112        }
113      }
114
115    private:
116      gameMode mode_;
117      OIS::Keyboard *mKeyboard;
118      audio::AudioManager*  auMan_;
119  };
120
121  // init static singleton reference of Orxonox
122  Orxonox* Orxonox::singletonRef_ = NULL;
123
124  /**
125   * create a new instance of Orxonox
126   */
127  Orxonox::Orxonox()
128  {
129    this->ogre_ = new GraphicsEngine();
130    this->dataPath_ = "";
131    this->loader_ = 0;
132    this->auMan_ = 0;
133    this->singletonRef_ = 0;
134    this->keyboard_ = 0;
135    this->mouse_ = 0;
136    this->inputManager_ = 0;
137    this->frameListener_ = 0;
138    this->root_ = 0;
139  }
140
141  /**
142   * destruct Orxonox
143   */
144  Orxonox::~Orxonox()
145  {
146    // nothing to delete as for now
147  }
148
149  /**
150   * initialization of Orxonox object
151   * @param argc argument counter
152   * @param argv list of arguments
153   * @param path path to config (in home dir or something)
154   */
155  void Orxonox::init(int argc, char **argv, std::string path)
156  {
157    //TODO: find config file (assuming executable directory)
158    //TODO: read config file
159    //TODO: give config file to Ogre
160    std::string mode;
161//     if(argc>=2)
162//       mode = std::string(argv[1]);
163//     else
164//       mode = "";
165    ArgReader ar = ArgReader(argc, argv);
166    ar.checkArgument("mode", mode, false);
167    ar.checkArgument("data", this->dataPath_, false);
168    ar.checkArgument("ip", serverIp_, false);
169    //mode = "presentation";
170    if(ar.errorHandling()) die();
171    if(mode == std::string("server"))
172    {
173      serverInit(path);
174      mode_ = SERVER;
175    }
176    else if(mode == std::string("client"))
177    {
178      clientInit(path);
179      mode_ = CLIENT;
180    }
181    else if(mode == std::string("presentation"))
182    {
183      serverInit(path);
184      mode_ = PRESENTATION;
185    }
186    else{
187      standaloneInit(path);
188      mode_ = STANDALONE;
189    }
190  }
191
192  /**
193   * start modules
194   */
195  void Orxonox::start()
196  {
197    //TODO: start modules
198    ogre_->startRender();
199    //TODO: run engine
200    createScene();
201    setupScene();
202    setupInputSystem();
203    if(mode_!=CLIENT){ // remove this in future ---- presentation hack
204    }
205    else
206      std::cout << "client here" << std::endl;
207    createFrameListener();
208    Factory::createClassHierarchy();
209    switch(mode_){
210    case PRESENTATION:
211      //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener());
212      //std::cout << "could not add framelistener" << std::endl;
213      server_g->open();
214      break;
215    case CLIENT:
216      client_g->establishConnection();
217      break;
218    case SERVER:
219    case STANDALONE:
220    default:
221      break;
222    }
223    startRenderLoop();
224  }
225
226  /**
227   * @return singleton object
228   */
229  Orxonox* Orxonox::getSingleton()
230  {
231    if (!singletonRef_)
232      singletonRef_ = new Orxonox();
233    return singletonRef_;
234  }
235
236  /**
237   * error kills orxonox
238   */
239  void Orxonox::die(/* some error code */)
240  {
241    //TODO: destroy and destruct everything and print nice error msg
242    delete this;
243  }
244
245  void Orxonox::standaloneInit(std::string path)
246  {
247    ogre_->setConfigPath(path);
248    ogre_->setup();
249    root_ = ogre_->getRoot();
250    if(!ogre_->load()) die(/* unable to load */);
251
252    //defineResources();
253    //setupRenderSystem();
254    //createRenderWindow();
255    //initializeResourceGroups();
256    /*createScene();
257    setupScene();
258    setupInputSystem();
259    createFrameListener();
260    Factory::createClassHierarchy();
261    startRenderLoop();*/
262  }
263
264  void Orxonox::playableServer(std::string path)
265  {
266    ogre_->setConfigPath(path);
267    ogre_->setup();
268    root_ = ogre_->getRoot();
269    defineResources();
270    setupRenderSystem();
271    createRenderWindow();
272    initializeResourceGroups();
273    setupInputSystem();
274    Factory::createClassHierarchy();
275    createScene();
276    setupScene();
277    createFrameListener();
278    try{
279      server_g = new network::Server(); // add port and bindadress
280      server_g->open(); // open server and create listener thread
281      if(ogre_ && ogre_->getRoot())
282        ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server
283      COUT(3) << "Info: network framelistener added" << std::endl;
284    }
285    catch(exception &e)
286    {
287      COUT(1) << "Error: There was a problem initialising the server :(" << std::endl;
288    }
289    startRenderLoop();
290  }
291
292  void Orxonox::standalone(){
293
294
295
296  }
297
298  void Orxonox::serverInit(std::string path)
299  {
300    COUT(2) << "initialising server" << std::endl;
301    ogre_->setConfigPath(path);
302    ogre_->setup();
303    server_g = new network::Server(); // add some settings if wanted
304    if(!ogre_->load()) die(/* unable to load */);
305    // add network framelistener
306  }
307
308  void Orxonox::clientInit(std::string path)
309  {
310    COUT(2) << "initialising client" << std::endl;
311    ogre_->setConfigPath(path);
312    ogre_->setup();
313    if(serverIp_.compare("")==0)
314      client_g = new network::Client();
315    else
316      client_g = new network::Client(serverIp_, 55556);
317    if(!ogre_->load()) die(/* unable to load */);
318    ogre_->getRoot()->addFrameListener(new network::ClientFrameListener());
319  }
320
321  void Orxonox::defineResources()
322  {
323    Ogre::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        ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
343#else
344        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 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    TextureManager::getSingleton().setDefaultNumMipmaps(5);
364    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
382    Overlay* hudOverlay = OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
383    hud::HUD* orxonoxHud;
384    orxonoxHud = new hud::HUD();
385    orxonoxHud->setEnergyValue(20);
386    orxonoxHud->setEnergyDistr(20,20,60);
387    hudOverlay->show();
388
389        /*
390    auMan_->ambientAdd("a1");
391    auMan_->ambientAdd("a2");
392    auMan_->ambientAdd("a3");
393                                //auMan->ambientAdd("ambient1");
394    auMan_->ambientStart();*/
395  }
396
397
398  /**
399   *
400   */
401  void Orxonox::setupScene()
402  {
403//    SceneManager *mgr = ogre_->getSceneManager();
404
405
406//    SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode");
407//     SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
408
409
410/*
411    particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl");
412    e->particleSystem_->setParameter("local_space","true");
413    e->setPositionOfEmitter(0, Vector3(0,-10,0));
414    e->setDirection(Vector3(0,0,-1));
415    e->addToSceneNode(node);
416*/
417  }
418
419
420  void Orxonox::setupInputSystem()
421  {
422    size_t windowHnd = 0;
423    std::ostringstream windowHndStr;
424    OIS::ParamList pl;
425    RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow();
426
427    win->getCustomAttribute("WINDOW", &windowHnd);
428    windowHndStr << windowHnd;
429    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
430    inputManager_ = OIS::InputManager::createInputSystem(pl);
431
432    try
433    {
434      keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false));
435      mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true));
436    }
437    catch (const OIS::Exception &e)
438    {
439      throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
440    }
441  }
442
443  // we actually want to do this differently...
444  void Orxonox::createFrameListener()
445  {
446    TickFrameListener* TickFL = new TickFrameListener();
447    ogre_->getRoot()->addFrameListener(TickFL);
448
449    TimerFrameListener* TimerFL = new TimerFrameListener();
450    ogre_->getRoot()->addFrameListener(TimerFL);
451
452    //if(mode_!=CLIENT) // just a hack ------- remove this in future
453      frameListener_ = new OrxListener(keyboard_, auMan_, mode_);
454    ogre_->getRoot()->addFrameListener(frameListener_);
455  }
456
457  void Orxonox::startRenderLoop()
458  {
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.