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 | * Yuning Chai |
---|
23 | * Co-authors: |
---|
24 | * Felix Schulthess |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "OrxonoxStableHeaders.h" |
---|
29 | #include "HUD.h" |
---|
30 | |
---|
31 | #include <string> |
---|
32 | #include <OgreOverlay.h> |
---|
33 | #include <OgreOverlayContainer.h> |
---|
34 | #include <OgreOverlayManager.h> |
---|
35 | #include <OgreStringConverter.h> |
---|
36 | |
---|
37 | #include "core/Debug.h" |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "objects/SpaceShip.h" |
---|
40 | #include "GraphicsEngine.h" |
---|
41 | #include "BarOverlayElement.h" |
---|
42 | #include "RadarObject.h" |
---|
43 | #include "RadarOverlayElement.h" |
---|
44 | #include "Navigation.h" |
---|
45 | #include "OverlayElementFactories.h" |
---|
46 | |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | ConsoleCommandShortcut(HUD, cycleNavigationFocus, AccessLevel::User); |
---|
50 | |
---|
51 | using namespace Ogre; |
---|
52 | |
---|
53 | HUD::HUD(){ |
---|
54 | om = &Ogre::OverlayManager::getSingleton(); |
---|
55 | firstRadarObject = NULL; |
---|
56 | lastRadarObject = NULL; |
---|
57 | |
---|
58 | // create Factories |
---|
59 | BarOverlayElementFactory *barOverlayElementFactory = new BarOverlayElementFactory(); |
---|
60 | om->addOverlayElementFactory(barOverlayElementFactory); |
---|
61 | RadarOverlayElementFactory *radarOverlayElementFactory = new RadarOverlayElementFactory(); |
---|
62 | om->addOverlayElementFactory(radarOverlayElementFactory); |
---|
63 | |
---|
64 | orxonoxHUD = om->create("Orxonox/HUD"); |
---|
65 | container = static_cast<Ogre::OverlayContainer*>(om->createOverlayElement("Panel", "Orxonox/HUD/container")); |
---|
66 | |
---|
67 | // creating text to display fps |
---|
68 | fpsText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "fpsText")); |
---|
69 | fpsText->show(); |
---|
70 | fpsText->setMetricsMode(Ogre::GMM_PIXELS); |
---|
71 | fpsText->setDimensions(0.001, 0.001); |
---|
72 | fpsText->setPosition(10, 10); |
---|
73 | fpsText->setFontName("Console"); |
---|
74 | fpsText->setCharHeight(20); |
---|
75 | fpsText->setCaption("init"); |
---|
76 | |
---|
77 | // creating text to display render time ratio |
---|
78 | rTRText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "rTRText")); |
---|
79 | rTRText->show(); |
---|
80 | rTRText->setMetricsMode(Ogre::GMM_PIXELS); |
---|
81 | rTRText->setDimensions(0.001, 0.001); |
---|
82 | rTRText->setPosition(10, 30); |
---|
83 | rTRText->setFontName("Console"); |
---|
84 | rTRText->setCharHeight(20); |
---|
85 | rTRText->setCaption("init"); |
---|
86 | |
---|
87 | // create energy bar |
---|
88 | energyBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "energyBar")); |
---|
89 | energyBar->show(); |
---|
90 | // create speedo bar |
---|
91 | speedoBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "speedoBar")); |
---|
92 | speedoBar->show(); |
---|
93 | // create radar |
---|
94 | radar = static_cast<RadarOverlayElement*>(om->createOverlayElement("Radar", "radar")); |
---|
95 | radar->show(); |
---|
96 | |
---|
97 | // create Navigation |
---|
98 | nav = new Navigation(container); |
---|
99 | |
---|
100 | // set up screen-wide container |
---|
101 | container->show(); |
---|
102 | |
---|
103 | orxonoxHUD->add2D(container); |
---|
104 | orxonoxHUD->show(); |
---|
105 | container->setLeft(0.0); |
---|
106 | container->setTop(0.0); |
---|
107 | container->setWidth(1.0); |
---|
108 | container->setHeight(1.0); |
---|
109 | container->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
110 | container->addChild(fpsText); |
---|
111 | container->addChild(rTRText); |
---|
112 | |
---|
113 | energyBar->init(0.01, 0.94, 0.4, container); |
---|
114 | energyBar->setValue(1); |
---|
115 | |
---|
116 | speedoBar->init(0.01, 0.90, 0.4, container); |
---|
117 | |
---|
118 | radar->init(0.5, 0.9, 0.2, container); |
---|
119 | addRadarObject(Vector3(2000.0, 0.0, 0.0)); |
---|
120 | addRadarObject(Vector3(0.0, 2000.0, 0.0)); |
---|
121 | addRadarObject(Vector3(0.0, 0.0, 2000.0)); |
---|
122 | } |
---|
123 | |
---|
124 | HUD::~HUD(){ |
---|
125 | //todo: clean up objects |
---|
126 | } |
---|
127 | |
---|
128 | void HUD::tick(float dt) |
---|
129 | { |
---|
130 | energyBar->resize(); |
---|
131 | |
---|
132 | if(!SpaceShip::getLocalShip()) |
---|
133 | return; |
---|
134 | float v = SpaceShip::getLocalShip()->getVelocity().length(); |
---|
135 | float vmax = SpaceShip::getLocalShip()->getMaxSpeed(); |
---|
136 | speedoBar->setValue(v/vmax); |
---|
137 | speedoBar->resize(); |
---|
138 | |
---|
139 | radar->resize(); |
---|
140 | radar->update(); |
---|
141 | |
---|
142 | nav->update(); |
---|
143 | |
---|
144 | float fps = GraphicsEngine::getSingleton().getAverageFPS(); |
---|
145 | fpsText->setCaption("FPS: " + Ogre::StringConverter::toString(fps)); |
---|
146 | } |
---|
147 | |
---|
148 | void HUD::setRenderTimeRatio(float ratio) |
---|
149 | { |
---|
150 | rTRText->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio)); |
---|
151 | } |
---|
152 | |
---|
153 | void HUD::addRadarObject(Vector3 pos){ |
---|
154 | // check if this is the first RadarObject to create |
---|
155 | if(firstRadarObject == NULL){ |
---|
156 | firstRadarObject = new RadarObject(container, pos); |
---|
157 | lastRadarObject = firstRadarObject; |
---|
158 | } |
---|
159 | else{ // if not, append to list |
---|
160 | lastRadarObject->next = new RadarObject(container, pos); |
---|
161 | lastRadarObject = lastRadarObject->next; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | RadarObject* HUD::getFirstRadarObject(){ |
---|
166 | return firstRadarObject; |
---|
167 | } |
---|
168 | |
---|
169 | /*static*/ HUD& HUD::getSingleton(){ |
---|
170 | static HUD theInstance; |
---|
171 | return theInstance; |
---|
172 | } |
---|
173 | |
---|
174 | /*static*/ void HUD::setEnergy(float value){ |
---|
175 | HUD::getSingleton().energyBar->setValue(value); |
---|
176 | } |
---|
177 | |
---|
178 | /*static*/ void HUD::cycleNavigationFocus(){ |
---|
179 | HUD::getSingleton().nav->cycleFocus(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|