Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/orxonox.cc @ 609

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