Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 633 was 627, checked in by bknecht, 17 years ago

working AI added (put them aside if you want)

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