Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 608 was 608, checked in by landauf, 16 years ago

added SpaceShip class

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