| 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 | SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User); |
|---|
| 50 | SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User); |
|---|
| 51 | SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User); |
|---|
| 52 | |
|---|
| 53 | using namespace Ogre; |
|---|
| 54 | |
|---|
| 55 | HUD::HUD(){ |
|---|
| 56 | om = &Ogre::OverlayManager::getSingleton(); |
|---|
| 57 | sm = GraphicsEngine::getSingleton().getSceneManager(); |
|---|
| 58 | firstRadarObject = NULL; |
|---|
| 59 | lastRadarObject = NULL; |
|---|
| 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 | // check if this is the first RadarObject to create |
|---|
| 180 | if(firstRadarObject == NULL){ |
|---|
| 181 | firstRadarObject = new RadarObject(container, node, colour); |
|---|
| 182 | lastRadarObject = firstRadarObject; |
|---|
| 183 | } |
|---|
| 184 | else{ // if not, append to list |
|---|
| 185 | lastRadarObject->next = new RadarObject(container, node, colour); |
|---|
| 186 | lastRadarObject = lastRadarObject->next; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | RadarObject* HUD::getFirstRadarObject(){ |
|---|
| 191 | return firstRadarObject; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /*static*/ HUD& HUD::getSingleton(){ |
|---|
| 195 | static HUD theInstance; |
|---|
| 196 | return theInstance; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /*static*/ void HUD::setEnergy(float value){ |
|---|
| 200 | HUD::getSingleton().energyBar->setValue(value); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /*static*/ void HUD::cycleNavigationFocus(){ |
|---|
| 204 | HUD::getSingleton().nav->cycleFocus(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /*static*/ void HUD::toggleFPS(){ |
|---|
| 208 | if(HUD::getSingleton().showFPS) HUD::getSingleton().showFPS = false; |
|---|
| 209 | else HUD::getSingleton().showFPS = true; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /*static*/ void HUD::toggleRenderTime(){ |
|---|
| 213 | if(HUD::getSingleton().showRenderTime) HUD::getSingleton().showRenderTime = false; |
|---|
| 214 | else HUD::getSingleton().showRenderTime = true; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|