[1454] | 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 | */ |
---|
[1362] | 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
[1410] | 30 | #include "HUD.h" |
---|
| 31 | |
---|
| 32 | #include <string> |
---|
[1456] | 33 | #include <set> |
---|
[1362] | 34 | #include <OgreOverlay.h> |
---|
| 35 | #include <OgreOverlayContainer.h> |
---|
| 36 | #include <OgreOverlayManager.h> |
---|
| 37 | #include <OgreStringConverter.h> |
---|
[1406] | 38 | |
---|
[1362] | 39 | #include "core/Debug.h" |
---|
[1406] | 40 | #include "core/ConsoleCommand.h" |
---|
[1362] | 41 | #include "objects/SpaceShip.h" |
---|
[1414] | 42 | #include "GraphicsEngine.h" |
---|
[1362] | 43 | #include "BarOverlayElement.h" |
---|
[1410] | 44 | #include "RadarObject.h" |
---|
[1362] | 45 | #include "RadarOverlayElement.h" |
---|
[1410] | 46 | #include "Navigation.h" |
---|
[1362] | 47 | #include "OverlayElementFactories.h" |
---|
| 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[1446] | 51 | SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User); |
---|
[1451] | 52 | SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User); |
---|
| 53 | SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User); |
---|
[1406] | 54 | |
---|
[1362] | 55 | using namespace Ogre; |
---|
| 56 | |
---|
[1406] | 57 | HUD::HUD(){ |
---|
[1362] | 58 | om = &Ogre::OverlayManager::getSingleton(); |
---|
[1450] | 59 | sm = GraphicsEngine::getSingleton().getSceneManager(); |
---|
[1451] | 60 | showFPS = true; |
---|
| 61 | showRenderTime = true; |
---|
[1362] | 62 | |
---|
[1410] | 63 | // create Factories |
---|
[1362] | 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 | |
---|
[1406] | 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 | |
---|
[1422] | 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 | |
---|
[1362] | 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 | |
---|
[1406] | 102 | // create Navigation |
---|
| 103 | nav = new Navigation(container); |
---|
| 104 | |
---|
[1410] | 105 | // set up screen-wide container |
---|
[1362] | 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); |
---|
[1406] | 115 | container->addChild(fpsText); |
---|
[1422] | 116 | container->addChild(rTRText); |
---|
[1406] | 117 | |
---|
[1362] | 118 | energyBar->init(0.01, 0.94, 0.4, container); |
---|
| 119 | energyBar->setValue(1); |
---|
[1406] | 120 | |
---|
[1362] | 121 | speedoBar->init(0.01, 0.90, 0.4, container); |
---|
[1406] | 122 | |
---|
[1362] | 123 | radar->init(0.5, 0.9, 0.2, container); |
---|
[1450] | 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); |
---|
[1362] | 131 | } |
---|
| 132 | |
---|
[1406] | 133 | HUD::~HUD(){ |
---|
| 134 | //todo: clean up objects |
---|
| 135 | } |
---|
| 136 | |
---|
[1362] | 137 | void HUD::tick(float dt) |
---|
| 138 | { |
---|
| 139 | energyBar->resize(); |
---|
| 140 | |
---|
[1385] | 141 | if(!SpaceShip::getLocalShip()) |
---|
| 142 | return; |
---|
[1384] | 143 | float v = SpaceShip::getLocalShip()->getVelocity().length(); |
---|
| 144 | float vmax = SpaceShip::getLocalShip()->getMaxSpeed(); |
---|
[1362] | 145 | speedoBar->setValue(v/vmax); |
---|
| 146 | speedoBar->resize(); |
---|
| 147 | |
---|
| 148 | radar->resize(); |
---|
| 149 | radar->update(); |
---|
[1406] | 150 | |
---|
| 151 | nav->update(); |
---|
[1362] | 152 | |
---|
[1451] | 153 | setFPS(); |
---|
[1411] | 154 | } |
---|
| 155 | |
---|
[1422] | 156 | void HUD::setRenderTimeRatio(float ratio) |
---|
| 157 | { |
---|
[1451] | 158 | if(showRenderTime){ |
---|
| 159 | rTRText->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio)); |
---|
| 160 | } |
---|
| 161 | else{ |
---|
| 162 | rTRText->setCaption(""); |
---|
| 163 | return; |
---|
| 164 | } |
---|
[1422] | 165 | } |
---|
| 166 | |
---|
[1451] | 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 | |
---|
[1450] | 178 | void HUD::addRadarObject(SceneNode* node, int colour){ |
---|
[1456] | 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 | // } |
---|
[1362] | 190 | } |
---|
[1406] | 191 | |
---|
[1456] | 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 | } |
---|
[1406] | 199 | } |
---|
| 200 | |
---|
[1410] | 201 | /*static*/ HUD& HUD::getSingleton(){ |
---|
[1406] | 202 | static HUD theInstance; |
---|
| 203 | return theInstance; |
---|
| 204 | } |
---|
| 205 | |
---|
[1410] | 206 | /*static*/ void HUD::setEnergy(float value){ |
---|
[1406] | 207 | HUD::getSingleton().energyBar->setValue(value); |
---|
| 208 | } |
---|
| 209 | |
---|
[1410] | 210 | /*static*/ void HUD::cycleNavigationFocus(){ |
---|
[1406] | 211 | HUD::getSingleton().nav->cycleFocus(); |
---|
| 212 | } |
---|
[1451] | 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 | } |
---|
[1362] | 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | |
---|