Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1456 was 1456, checked in by FelixSchulthess, 16 years ago

using a std::set for the radar objects, which still has a the small problem, that the first radar object is selected twice when cycling through radar objects

File size: 7.3 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 "GraphicsEngine.h"
43#include "BarOverlayElement.h"
44#include "RadarObject.h"
45#include "RadarOverlayElement.h"
46#include "Navigation.h"
47#include "OverlayElementFactories.h"
48
49namespace orxonox
50{
51    SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User);
52    SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User);
53    SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User);
54
55    using namespace Ogre;
56
57    HUD::HUD(){
58        om = &Ogre::OverlayManager::getSingleton();
59        sm = GraphicsEngine::getSingleton().getSceneManager();
60        showFPS = true;
61        showRenderTime = true;
62
63        // create Factories
64        BarOverlayElementFactory *barOverlayElementFactory = new BarOverlayElementFactory();
65        om->addOverlayElementFactory(barOverlayElementFactory);
66        RadarOverlayElementFactory *radarOverlayElementFactory = new RadarOverlayElementFactory();
67        om->addOverlayElementFactory(radarOverlayElementFactory);
68
69        orxonoxHUD = om->create("Orxonox/HUD");
70        container = static_cast<Ogre::OverlayContainer*>(om->createOverlayElement("Panel", "Orxonox/HUD/container"));
71
72        // creating text to display fps
73        fpsText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "fpsText"));
74        fpsText->show();
75        fpsText->setMetricsMode(Ogre::GMM_PIXELS);
76        fpsText->setDimensions(0.001, 0.001);
77        fpsText->setPosition(10, 10);
78        fpsText->setFontName("Console");
79        fpsText->setCharHeight(20);
80        fpsText->setCaption("init");
81
82        // creating text to display render time ratio
83        rTRText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "rTRText"));
84        rTRText->show();
85        rTRText->setMetricsMode(Ogre::GMM_PIXELS);
86        rTRText->setDimensions(0.001, 0.001);
87        rTRText->setPosition(10, 30);
88        rTRText->setFontName("Console");
89        rTRText->setCharHeight(20);
90        rTRText->setCaption("init");
91
92        // create energy bar
93        energyBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "energyBar"));
94        energyBar->show();
95        // create speedo bar
96        speedoBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "speedoBar"));
97        speedoBar->show();
98        // create radar
99        radar = static_cast<RadarOverlayElement*>(om->createOverlayElement("Radar", "radar"));
100        radar->show();
101
102        // create Navigation
103        nav = new Navigation(container);
104
105        // set up screen-wide container
106        container->show();
107
108        orxonoxHUD->add2D(container);
109        orxonoxHUD->show();
110        container->setLeft(0.0);
111        container->setTop(0.0);
112        container->setWidth(1.0);
113        container->setHeight(1.0);
114        container->setMetricsMode(Ogre::GMM_RELATIVE);
115        container->addChild(fpsText);
116        container->addChild(rTRText);
117
118        energyBar->init(0.01, 0.94, 0.4, container);
119        energyBar->setValue(1);
120
121        speedoBar->init(0.01, 0.90, 0.4, container);
122
123        radar->init(0.5, 0.9, 0.2, container);
124        SceneNode* node;
125        node = sm->getRootSceneNode()->createChildSceneNode("tomato1", Vector3(2000.0, 0.0, 0.0));
126        addRadarObject(node);
127        node = sm->getRootSceneNode()->createChildSceneNode("tomato2", Vector3(0.0, 2000.0, 0.0));
128        addRadarObject(node);
129        node = sm->getRootSceneNode()->createChildSceneNode("tomato3", Vector3(0.0, 0.0, 2000.0));
130        addRadarObject(node);
131    }
132
133    HUD::~HUD(){
134        //todo: clean up objects
135    }
136
137    void HUD::tick(float dt)
138    {
139        energyBar->resize();
140
141        if(!SpaceShip::getLocalShip())
142          return;
143        float v = SpaceShip::getLocalShip()->getVelocity().length();
144        float vmax = SpaceShip::getLocalShip()->getMaxSpeed();
145        speedoBar->setValue(v/vmax);
146        speedoBar->resize();
147
148        radar->resize();
149        radar->update();
150
151        nav->update();
152
153        setFPS();
154    }
155
156    void HUD::setRenderTimeRatio(float ratio)
157    {
158        if(showRenderTime){
159            rTRText->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio));
160        }
161        else{
162            rTRText->setCaption("");
163            return;
164        }
165    }
166
167    void HUD::setFPS(){
168        if(showFPS){
169            float fps = GraphicsEngine::getSingleton().getAverageFPS();
170            fpsText->setCaption("FPS: " + Ogre::StringConverter::toString(fps));
171        }
172        else{
173            fpsText->setCaption("");
174            return;
175        }
176    }
177
178    void HUD::addRadarObject(SceneNode* node, int colour){
179        RadarObject* obj = new RadarObject(container, node, colour);
180        roSet.insert(obj);
181//        // check if this is the first RadarObject to create
182//        if(firstRadarObject == NULL){
183//            firstRadarObject = new RadarObject(container, node, colour);
184//            lastRadarObject = firstRadarObject;
185//        }
186//        else{ // if not, append to list
187//            lastRadarObject->next = new RadarObject(container, node, colour);
188//            lastRadarObject = lastRadarObject->next;
189//        }
190    }
191
192    void HUD::removeRadarObject(SceneNode* node){
193        for(std::set<RadarObject*>::iterator it=roSet.begin(); it!=roSet.end(); it++){
194            if((*it)->getNode() == node) {
195                delete (*it);
196                roSet.erase(it);
197            }
198        }
199    }
200
201    /*static*/ HUD& HUD::getSingleton(){
202        static HUD theInstance;
203        return theInstance;
204    }
205
206    /*static*/ void HUD::setEnergy(float value){
207        HUD::getSingleton().energyBar->setValue(value);
208    }
209
210    /*static*/ void HUD::cycleNavigationFocus(){
211        HUD::getSingleton().nav->cycleFocus();
212    }
213
214    /*static*/ void HUD::toggleFPS(){
215        if(HUD::getSingleton().showFPS) HUD::getSingleton().showFPS = false;
216        else HUD::getSingleton().showFPS = true;
217    }
218
219    /*static*/ void HUD::toggleRenderTime(){
220        if(HUD::getSingleton().showRenderTime) HUD::getSingleton().showRenderTime = false;
221        else HUD::getSingleton().showRenderTime = true;
222    }
223}
224
225
226
227
228
229
Note: See TracBrowser for help on using the repository browser.