Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed tcl initialisation bug

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