Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/hud/HUD.cc @ 1705

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

started another attempt to fix the occasionally focus-cycling crash

  • Property svn:eol-style set to native
File size: 9.4 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 *      Yuning Chai
24 *   Co-authors:
25 *      Felix Schulthess
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "HUD.h"
31
32#include <string>
33#include <set>
34#include <OgreOverlay.h>
35#include <OgreOverlayContainer.h>
36#include <OgreOverlayManager.h>
37#include <OgreStringConverter.h>
38
39#include "core/Debug.h"
40#include "core/ConsoleCommand.h"
41#include "objects/SpaceShip.h"
42#include "objects/WorldEntity.h"
43#include "GraphicsEngine.h"
44#include "BarOverlayElement.h"
45#include "RadarObject.h"
46#include "RadarOverlayElement.h"
47#include "Navigation.h"
48
49namespace orxonox
50{
51    SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User);
52    SetConsoleCommandShortcut(HUD, releaseNavigationFocus).setAccessLevel(AccessLevel::User);
53    SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User);
54    SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User);
55
56    using namespace Ogre;
57
58    HUD::HUD()
59    {
60        orxonoxHUD_ = 0;
61        container_ = 0;
62        fpsText_ = 0;
63        rTRText_ = 0;
64        energyBar_ = 0;
65        speedoBar_ = 0;
66        radar_ = 0;
67        nav_ = 0;
68        showFPS_ = true;
69        showRenderTime_ = true;
70    }
71
72    HUD::~HUD()
73    {
74        this->destroy();
75    }
76
77    void HUD::initialise()
78    {
79        showFPS_ = true;
80        showRenderTime_ = true;
81
82        // create Factories
83        Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&barOverlayElementFactory_);
84        Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_);
85
86        // creating text to display fps
87        fpsText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "fpsText"));
88        fpsText_->setMetricsMode(Ogre::GMM_PIXELS);
89        fpsText_->setDimensions(0.001, 0.001);
90        fpsText_->setPosition(10, 10);
91        fpsText_->setFontName("Console");
92        fpsText_->setCharHeight(20);
93        fpsText_->setCaption("init");
94        fpsText_->show();
95
96        // creating text to display render time ratio
97        rTRText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "rTRText"));
98        rTRText_->setMetricsMode(Ogre::GMM_PIXELS);
99        rTRText_->setDimensions(0.001, 0.001);
100        rTRText_->setPosition(10, 30);
101        rTRText_->setFontName("Console");
102        rTRText_->setCharHeight(20);
103        rTRText_->setCaption("init");
104        rTRText_->show();
105
106        // set up screen-wide container
107        container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container"));
108        container_->setLeft(0.0);
109        container_->setTop(0.0);
110        container_->setWidth(1.0);
111        container_->setHeight(1.0);
112        container_->setMetricsMode(Ogre::GMM_RELATIVE);
113        container_->addChild(fpsText_);
114        container_->addChild(rTRText_);
115        container_->show();
116
117        orxonoxHUD_ = Ogre::OverlayManager::getSingleton().create("Orxonox/HUD");
118        orxonoxHUD_->add2D(container_);
119        orxonoxHUD_->show();
120
121        // create energy bar
122        energyBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "energyBar"));
123        energyBar_->init(0.01, 0.94, 0.4, container_);
124        energyBar_->setValue(1);
125        energyBar_->show();
126
127        // create speedo bar
128        speedoBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "speedoBar"));
129        speedoBar_->init(0.01, 0.90, 0.4, container_);
130        speedoBar_->addColour(0.7, ColourValue(0.2, 0.7, 0.2));
131        speedoBar_->addColour(0.4, ColourValue(0.7, 0.5, 0.2));
132        speedoBar_->addColour(0.1, ColourValue(0.7, 0.2, 0.2));
133        speedoBar_->show();
134
135        // create radar
136        radar_ = static_cast<RadarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Radar", "radar"));
137        radar_->init(0.5, 0.9, 0.2, container_);
138        radar_->show();
139
140        // create Navigation
141        nav_ = new Navigation(container_);
142
143        WorldEntity* object;
144        object = new WorldEntity();
145        object->setPosition(2000.0, 0.0, 0.0);
146        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
147        object = new WorldEntity();
148        object->setPosition(0.0, 2000.0, 0.0);
149        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
150        object = new WorldEntity();
151        object->setPosition(0.0, 0.0, 2000.0);
152        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
153        object = new WorldEntity();
154        object->setPosition(10000.0,16000.0,0.0);
155        addRadarObject(object);
156    }
157
158    void HUD::destroy()
159    {
160        if (this->container_)
161          Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->container_);
162        this->container_ = 0;
163        if (this->fpsText_)
164            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->fpsText_);
165        this->fpsText_ = 0;
166        if (this->rTRText_)
167            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->rTRText_);
168        this->rTRText_ = 0;
169        if (this->energyBar_)
170            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->energyBar_);
171        this->energyBar_ = 0;
172        if (this->speedoBar_)
173            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->speedoBar_);
174        this->speedoBar_ = 0;
175        if (this->radar_)
176            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->radar_);
177        this->radar_ = 0;
178        if (this->orxonoxHUD_)
179            Ogre::OverlayManager::getSingleton().destroy(this->orxonoxHUD_);
180        this->orxonoxHUD_ = 0;
181
182        if (this->nav_)
183            delete this->nav_;
184        this->nav_ = 0;
185    }
186
187    void HUD::tick(float dt)
188    {
189        if(!SpaceShip::getLocalShip())
190          return;
191
192        float v = SpaceShip::getLocalShip()->getVelocity().length();
193        float vmax = SpaceShip::getLocalShip()->getMaxSpeed();
194        speedoBar_->setValue(v/vmax);
195
196        radar_->update();
197        nav_->update();
198
199        setFPS();
200    }
201
202    void HUD::resize()
203    {
204        this->speedoBar_->resize();
205        this->energyBar_->resize();
206        this->radar_->resize();
207    }
208
209    void HUD::setRenderTimeRatio(float ratio)
210    {
211        if(showRenderTime_){
212            rTRText_->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio));
213        }
214        else{
215            rTRText_->setCaption("");
216            return;
217        }
218    }
219
220    void HUD::setFPS(){
221        if(showFPS_){
222            float fps = GraphicsEngine::getSingleton().getAverageFPS();
223            fpsText_->setCaption("FPS: " + Ogre::StringConverter::toString(fps));
224        }
225        else{
226            fpsText_->setCaption("");
227            return;
228        }
229    }
230
231    void HUD::addRadarObject(WorldEntity* object, const ColourValue& colour){
232        RadarObject* obj = new RadarObject(container_, object, colour);
233        roSet_.insert(roSet_.end(), obj);
234//        // check if this is the first RadarObject to create
235//        if(firstRadarObject == NULL){
236//            firstRadarObject = new RadarObject(container_, object, colour);
237//            lastRadarObject = firstRadarObject;
238//        }
239//        else{ // if not, append to list
240//            lastRadarObject->next = new RadarObject(container_, object, colour);
241//            lastRadarObject = lastRadarObject->next;
242//        }
243    }
244
245    void HUD::removeRadarObject(WorldEntity* object){
246        for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){
247            if ((*it)->getObject() == object)
248            {
249                if (this->nav_ && this->nav_->getFocus() == (*it))
250                    this->nav_->releaseFocus();
251
252                delete (*it);
253                roSet_.erase(it);
254                return;
255            }
256        }
257    }
258
259    /*static*/ HUD& HUD::getSingleton(){
260        static HUD theInstance;
261        return theInstance;
262    }
263
264    /*static*/ void HUD::setEnergy(float value){
265        HUD::getSingleton().energyBar_->setValue(value);
266    }
267
268    /*static*/ void HUD::cycleNavigationFocus(){
269        HUD::getSingleton().nav_->cycleFocus();
270    }
271
272    /*static*/ void HUD::releaseNavigationFocus(){
273        HUD::getSingleton().nav_->releaseFocus();
274    }
275
276    /*static*/ void HUD::toggleFPS(){
277        HUD::getSingleton().showFPS_ = !HUD::getSingleton().showFPS_;
278    }
279
280    /*static*/ void HUD::toggleRenderTime(){
281        HUD::getSingleton().showRenderTime_ = !HUD::getSingleton().showRenderTime_;
282    }
283}
Note: See TracBrowser for help on using the repository browser.