Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/Orxonox.cc @ 825

Last change on this file since 825 was 825, checked in by nicolasc, 16 years ago

created error output, up to chai to fix it

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