Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1613 was 1613, checked in by rgrieder, 16 years ago
  • Radar now working like before
  • but more of a svn save..

There is class called Radar which takes care of the focus and all objects that can be displayed on a Radar.
The actual visual implementation is in HUDRadar.
To make a object radar viewable, simply implement RadarViewable and set the WorldEntitiy pointer in the constructor (assertation will fail otherwise!).
You can also set a camouflage value between 0 and 1 that tells how good a radar can see an object.
The HUDRadar (or another one) on the other side has a sensitivity between 0 and 1. So only if the sensitivity is higher than the camouflage value, the object gets displayed.

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
26 *
27 */
28
29/**
30 @file
31 @brief Orxonox Main Class
32 */
33
34// Precompiled Headers
35#include "OrxonoxStableHeaders.h"
36#include "Orxonox.h"
37
38//****** STD *******
39#include <deque>
40
41//****** OGRE ******
42#include <OgreFrameListener.h>
43#include <OgreOverlay.h>
44#include <OgreOverlayManager.h>
45#include <OgreRoot.h>
46#include <OgreTimer.h>
47#include <OgreWindowEventUtilities.h>
48
49//***** ORXONOX ****
50// util
51//#include "util/Sleep.h"
52#include "util/ArgReader.h"
53
54// core
55#include "core/ConfigFileManager.h"
56#include "core/ConsoleCommand.h"
57#include "core/Debug.h"
58#include "core/Loader.h"
59#include "core/input/InputManager.h"
60#include "core/TclBind.h"
61#include "core/Core.h"
62
63// audio
64#include "audio/AudioManager.h"
65
66// network
67#include "network/Server.h"
68#include "network/Client.h"
69
70// objects and tools
71#include "overlays/OverlayGroup.h"
72#include "overlays/console/InGameConsole.h"
73#include "objects/Tickable.h"
74#include "tools/ParticleInterface.h"
75
76#include "GraphicsEngine.h"
77#include "Settings.h"
78#include "Radar.h"
79
80// globals for the server or client
81static network::Client *client_g = 0;
82static network::Server *server_g = 0;
83
84namespace orxonox
85{
86  SetConsoleCommandShortcut(Orxonox, exit).setKeybindMode(KeybindMode::OnPress);
87  SetConsoleCommandShortcut(Orxonox, slomo).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0).setAxisParamIndex(0).setIsAxisRelative(false);
88  SetConsoleCommandShortcut(Orxonox, setTimeFactor).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0);
89
90  /**
91    @brief Reference to the only instance of the class.
92  */
93  Orxonox *Orxonox::singletonRef_s = 0;
94
95  /**
96   * Create a new instance of Orxonox. Avoid doing any actual work here.
97   */
98  Orxonox::Orxonox()
99    : ogre_(0)
100    , startLevel_(0)
101    , hud_(0)
102    , radar_(0)
103    //, auMan_(0)
104    , timer_(0)
105    // turn on frame smoothing by setting a value different from 0
106    , frameSmoothingTime_(0.0f)
107    //orxonoxHUD_(0)
108    , bAbort_(false)
109    , timefactor_(1.0f)
110    , mode_(STANDALONE)
111    , serverIp_("")
112    , serverPort_(NETWORK_PORT)
113  {
114  }
115
116  /**
117   * Destruct Orxonox.
118   */
119  Orxonox::~Orxonox()
120  {
121    // keep in mind: the order of deletion is very important!
122    Loader::unload(startLevel_);
123    if (this->startLevel_)
124      delete this->startLevel_;
125
126    Loader::unload(hud_);
127    if (this->hud_)
128      delete this->hud_;
129
130    if (this->radar_)
131      delete this->radar_;
132
133    Loader::close();
134    //if (this->auMan_)
135    //  delete this->auMan_;
136    InGameConsole::getInstance().destroy();
137    if (this->timer_)
138      delete this->timer_;
139    InputManager::destroy();
140    GraphicsEngine::getSingleton().destroy();
141
142    if (network::Client::getSingleton())
143      network::Client::destroySingleton();
144    if (server_g)
145      delete network::Server::getSingleton();
146  }
147
148
149  /**
150    Asks the mainloop nicely to abort.
151  */
152  void Orxonox::abortRequest()
153  {
154    COUT(3) << "Orxonox: Abort requested." << std::endl;
155    bAbort_ = true;
156  }
157
158  /**
159   * @return singleton reference
160   */
161  Orxonox* Orxonox::getSingleton()
162  {
163    if (!singletonRef_s)
164      singletonRef_s = new Orxonox();
165    return singletonRef_s;
166  }
167
168  /**
169    @brief Destroys the Orxonox singleton.
170  */
171  void Orxonox::destroySingleton()
172  {
173    if (singletonRef_s)
174      delete singletonRef_s;
175    singletonRef_s = 0;
176  }
177
178  /**
179    @brief Changes the speed of Orxonox
180  */
181  void Orxonox::setTimeFactor(float factor)
182  {
183    float change = factor / Orxonox::getSingleton()->getTimeFactor();
184    Orxonox::getSingleton()->timefactor_ = factor;
185
186    for (Iterator<ParticleInterface> it = ObjectList<ParticleInterface>::begin(); it; ++it)
187        it->setSpeedFactor(it->getSpeedFactor() * change);
188  }
189
190  /**
191   * initialization of Orxonox object
192   * @param argc argument counter
193   * @param argv list of argumenst
194   * @param path path to config (in home dir or something)
195   */
196  bool Orxonox::init(int argc, char **argv)
197  {
198#ifdef _DEBUG
199    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox_d.ini");
200#else
201    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
202#endif
203    Factory::createClassHierarchy();
204
205    std::string mode;
206    std::string tempDataPath;
207
208    ArgReader ar(argc, argv);
209    ar.checkArgument("mode", &mode, false);
210    ar.checkArgument("data", &tempDataPath, false);
211    ar.checkArgument("ip",   &serverIp_, false);
212    ar.checkArgument("port", &serverPort_, false);
213    if(ar.errorHandling())
214    {
215      COUT(1) << "Error while parsing command line arguments" << std::endl;
216      COUT(1) << ar.getErrorString();
217      COUT(0) << "Usage:" << std::endl << "orxonox [mode client|server|dedicated|standalone] "
218        << "[--data PATH] [--ip IP] [--port PORT]" << std::endl;
219      return false;
220    }
221
222    if (mode == "client")
223      mode_ = CLIENT;
224    else if (mode == "server")
225      mode_ = SERVER;
226    else if (mode == "dedicated")
227      mode_ = DEDICATED;
228    else
229    {
230      if (mode == "")
231        mode = "standalone";
232      if (mode != "standalone")
233      {
234        COUT(2) << "Warning: mode \"" << mode << "\" doesn't exist. "
235          << "Defaulting to standalone" << std::endl;
236        mode = "standalone";
237      }
238      mode_ = STANDALONE;
239    }
240    COUT(3) << "Orxonox: Mode is " << mode << "." << std::endl;
241
242    if (tempDataPath != "")
243    {
244      if (tempDataPath[tempDataPath.size() - 1] != '/')
245        tempDataPath += "/";
246      Settings::tsetDataPath(tempDataPath);
247    }
248
249    // initialise TCL
250    TclBind::getInstance().setDataPath(Settings::getDataPath());
251
252    //if (mode_ == DEDICATED)
253      // TODO: decide what to do here
254    //else
255
256    // for playable server, client and standalone, the startup
257    // procedure until the GUI is identical
258
259    ogre_ = &GraphicsEngine::getSingleton();
260    if (!ogre_->setup())       // creates ogre root and other essentials
261      return false;
262
263    return true;
264  }
265
266  /**
267   * start modules
268   */
269  bool Orxonox::start()
270  {
271    if (mode_ == DEDICATED)
272    {
273      // do something else
274    }
275    else
276    { // not dedicated server
277      if (!ogre_->loadRenderer())    // creates the render window
278        return false;
279
280      // TODO: Spread this so that this call only initialises things needed for the Console
281      if (!ogre_->initialiseResources())
282        return false;
283
284      // Load the InGameConsole
285      InGameConsole::getInstance().initialise();
286
287      // Calls the InputManager which sets up the input devices.
288      // The render window width and height are used to set up the mouse movement.
289      if (!InputManager::initialise(ogre_->getWindowHandle(),
290            ogre_->getWindowWidth(), ogre_->getWindowHeight(), true, true, true))
291        return false;
292
293      // TOOD: load the GUI here
294      // set InputManager to GUI mode
295      InputManager::setInputState(InputManager::IS_GUI);
296      // TODO: run GUI here
297
298      // The following lines depend very much on the GUI output, so they're probably misplaced here..
299
300      InputManager::setInputState(InputManager::IS_NONE);
301
302      // create Ogre SceneManager
303      ogre_->createNewScene();
304
305      if (!loadPlayground())
306        return false;
307    }
308
309    switch (mode_)
310    {
311    case SERVER:
312      if (!serverLoad())
313        return false;
314      break;
315    case CLIENT:
316      if (!clientLoad())
317        return false;
318      break;
319    case DEDICATED:
320      if (!serverLoad())
321        return false;
322      break;
323    default:
324      if (!standaloneLoad())
325        return false;
326    }
327
328    InputManager::setInputState(InputManager::IS_NORMAL);
329
330    return startRenderLoop();
331  }
332
333  /**
334   * Loads everything in the scene except for the actual objects.
335   * This includes HUD, audio..
336   */
337  bool Orxonox::loadPlayground()
338  {
339    // Init audio
340    //auMan_ = new audio::AudioManager();
341    //auMan_->ambientAdd("a1");
342    //auMan_->ambientAdd("a2");
343    //auMan_->ambientAdd("a3");
344    //auMan->ambientAdd("ambient1");
345    //auMan_->ambientStart();
346
347    // Load the HUD
348    COUT(3) << "Orxonox: Loading HUD" << std::endl;
349    hud_ = new Level(Settings::getDataPath() + "overlay/hud.oxo");
350    Loader::load(hud_);
351
352    // Start the Radar
353    this->radar_ = new Radar();
354
355    return true;
356  }
357
358  /**
359   * Level loading method for server mode.
360   */
361  bool Orxonox::serverLoad()
362  {
363    COUT(0) << "Loading level in server mode" << std::endl;
364
365    //server_g = new network::Server(serverPort_);
366    server_g = network::Server::createSingleton(serverPort_);
367
368    if (!loadScene())
369      return false;
370
371    server_g->open();
372
373    return true;
374  }
375
376  /**
377   * Level loading method for client mode.
378   */
379  bool Orxonox::clientLoad()
380  {
381    COUT(0) << "Loading level in client mode" << std::endl;\
382
383    if (serverIp_.compare("") == 0)
384      client_g = network::Client::createSingleton();
385    else
386
387      client_g = network::Client::createSingleton(serverIp_, serverPort_);
388
389    if(!client_g->establishConnection())
390      return false;
391    client_g->tick(0);
392
393    return true;
394  }
395
396  /**
397   * Level loading method for standalone mode.
398   */
399  bool Orxonox::standaloneLoad()
400  {
401    COUT(0) << "Loading level in standalone mode" << std::endl;
402
403    if (!loadScene())
404      return false;
405
406    return true;
407  }
408
409  /**
410   * Helper method to load a level.
411   */
412  bool Orxonox::loadScene()
413  {
414    startLevel_ = new Level("levels/sample.oxw");
415    Loader::open(startLevel_);
416
417    return true;
418  }
419
420
421  /**
422    Main loop of the orxonox game.
423    About the loop: The design is almost exactly like the one in ogre, so that
424    if any part of ogre registers a framelisteners, it will still behave
425    correctly. Furthermore the time smoothing feature from ogre has been
426    implemented too. If turned on (see orxonox constructor), it will calculate
427    the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
428  */
429  bool Orxonox::startRenderLoop()
430  {
431    // first check whether ogre root object has been created
432    if (Ogre::Root::getSingletonPtr() == 0)
433    {
434      COUT(2) << "Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;
435      return false;
436    }
437    Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
438
439
440    // Contains the times of recently fired events
441    // eventTimes[4] is the list for the times required for the fps counter
442    std::deque<unsigned long> eventTimes[3];
443    // Clear event times
444    for (int i = 0; i < 3; ++i)
445      eventTimes[i].clear();
446
447    // use the ogre timer class to measure time.
448    if (!timer_)
449      timer_ = new Ogre::Timer();
450    timer_->reset();
451
452    float renderTime = 0.0f;
453    float frameTime = 0.0f;
454//    clock_t time = 0;
455
456    COUT(3) << "Orxonox: Starting the main loop." << std::endl;
457    while (!bAbort_)
458    {
459      // get current time
460      unsigned long now = timer_->getMilliseconds();
461
462      // create an event to pass to the frameStarted method in ogre
463      Ogre::FrameEvent evt;
464      evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]);
465      evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]);
466      frameTime += evt.timeSinceLastFrame;
467
468      // OverlayGroup::getHUD().setTime(now);
469      if (mode_ != DEDICATED && frameTime > 0.4f)
470      {
471        GraphicsEngine::getSingleton().setAverageRTR(renderTime / frameTime);
472        frameTime = 0.0f;
473        renderTime = 0.0f;
474      }
475
476      // tick the core
477      Core::tick((float)evt.timeSinceLastFrame);
478      // Call those objects that need the real time
479      for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it)
480        it->tick((float)evt.timeSinceLastFrame);
481      // Call the scene objects
482      for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it)
483        it->tick((float)evt.timeSinceLastFrame * this->timefactor_);
484      //AudioManager::tick();
485      if (client_g)
486        client_g->tick((float)evt.timeSinceLastFrame);
487      if (server_g)
488        server_g->tick((float)evt.timeSinceLastFrame);
489
490      // don't forget to call _fireFrameStarted in ogre to make sure
491      // everything goes smoothly
492      ogreRoot._fireFrameStarted(evt);
493
494      // get current time
495      now = timer_->getMilliseconds();
496      calculateEventTime(now, eventTimes[2]);
497
498      if (mode_ != DEDICATED)
499      {
500        // Pump messages in all registered RenderWindows
501        // This calls the WindowEventListener objects.
502        Ogre::WindowEventUtilities::messagePump();
503
504        // render
505        ogreRoot._updateAllRenderTargets();
506      }
507
508      // get current time
509      now = timer_->getMilliseconds();
510
511      // create an event to pass to the frameEnded method in ogre
512      evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]);
513      renderTime += calculateEventTime(now, eventTimes[2]);
514
515      // again, just to be sure ogre works fine
516      ogreRoot._fireFrameEnded(evt);
517      //msleep(500);
518    }
519
520    if (mode_ == CLIENT)
521      network::Client::getSingleton()->closeConnection();
522    else if (mode_ == SERVER)
523      server_g->close();
524
525    return true;
526  }
527
528  /**
529    Method for calculating the average time between recently fired events.
530    Code directly taken from OgreRoot.cc
531    @param now The current time in ms.
532    @param type The type of event to be considered.
533  */
534  float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> &times)
535  {
536    // Calculate the average time passed between events of the given type
537    // during the last frameSmoothingTime_ seconds.
538
539    times.push_back(now);
540
541    if(times.size() == 1)
542      return 0;
543
544    // Times up to frameSmoothingTime_ seconds old should be kept
545    unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f);
546
547    // Find the oldest time to keep
548    std::deque<unsigned long>::iterator it  = times.begin();
549    // We need at least two times
550    std::deque<unsigned long>::iterator end = times.end() - 2;
551
552    while(it != end)
553    {
554      if (now - *it > discardThreshold)
555        ++it;
556      else
557        break;
558    }
559
560    // Remove old times
561    times.erase(times.begin(), it);
562
563    return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000);
564  }
565}
Note: See TracBrowser for help on using the repository browser.