Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 621 was 616, checked in by nicolasc, 17 years ago

changed include names to new file names…<

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