Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1244 was 982, checked in by chaiy, 17 years ago

hallo

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