|
From: <de...@us...> - 2003-02-19 23:03:02
|
Update of /cvsroot/csp/APPLICATIONS/CSPSim/Source In directory sc8-pr-cvs1:/tmp/cvs-serv18243 Added Files: ScreenInfoManager.cpp ScreenInfo.cpp Log Message: New display code --- NEW FILE: ScreenInfoManager.cpp --- // Combat Simulator Project - FlightSim Demo // Copyright (C) 2002 The Combat Simulator Project // http://csp.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** * @file ScreenInfoManager.cpp * **/ #include <osg/Depth> #include <osg/MatrixTransform> #include "ScreenInfoManager.h" ScreenInfoManager::ScreenInfoManager(int ScreenWidth, int ScreenHeight) { setMatrix(osg::Matrix::ortho2D(0,ScreenWidth,0,ScreenHeight)); m_modelview_abs = new osg::MatrixTransform; m_modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE); m_modelview_abs->setMatrix(osg::Matrix::identity()); set2dScene(m_modelview_abs,ScreenWidth,ScreenHeight); addChild(m_modelview_abs); setCullingActive(true); } void ScreenInfoManager::set2dScene(osg::Group* rootNode, int ScreenWidth, int ScreenHeight) { osg::ref_ptr<Framerate> framerate = new Framerate(0,ScreenHeight); //float textWidth = bitmapFont->getWidth(pause->getEncodedText()); osg::ref_ptr<ScreenInfo> pause = new ScreenInfo(ScreenWidth - 5 * 8, ScreenHeight,"PAUSE", "PAUSE"); pause->setStatus(false); osg::ref_ptr<GeneralStats> generalStats = new GeneralStats(0, ScreenHeight / 3); osg::ref_ptr<ObjectStats> objectStats = new ObjectStats(0, 2 * ScreenHeight / 3); rootNode->addChild(framerate.get()); rootNode->addChild(pause.get()); rootNode->addChild(generalStats.get()); rootNode->addChild(objectStats.get()); // now add a depth attribute to the scene to force it to draw on top. osg::Depth* depth = new osg::Depth; depth->setRange(0.0,0.0); osg::StateSet* rootState = new osg::StateSet(); rootState->setAttribute(depth); rootState->setMode(GL_LIGHTING,osg::StateAttribute::OFF); rootNode->setStateSet(rootState); } class FindNamedNodeVisitor : public osg::NodeVisitor { public : FindNamedNodeVisitor() : osg::NodeVisitor( TRAVERSE_ALL_CHILDREN ) { setNodeMaskOverride(1); _found_node = 0L; } virtual void apply( osg::Node &node ) { if( node.getName() == _name_to_find ) _found_node = &node; traverse(node); } void setNameToFind( std::string const & name ) { _name_to_find = name; } osg::Node *foundNode() { return _found_node; } private : std::string _name_to_find; osg::Node *_found_node; }; ScreenInfo * ScreenInfoManager::getScreenInfo(std::string const & name) { FindNamedNodeVisitor nv; nv.setNameToFind(name); nv.apply( *m_modelview_abs ); return dynamic_cast<ScreenInfo *>(nv.foundNode()); } void ScreenInfoManager::setStatus(std::string const & name, bool bvisible) { ScreenInfo * sci = getScreenInfo(name); if (sci) sci->setStatus(bvisible); } bool ScreenInfoManager::getStatus(std::string const & name) { ScreenInfo * sci = getScreenInfo(name); if (sci) return sci->getStatus(); else return false; } --- NEW FILE: ScreenInfo.cpp --- // Combat Simulator Project - FlightSim Demo // Copyright (C) 2002 The Combat Simulator Project // http://csp.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** * @file ScreenInfo.cpp * **/ #include <iomanip> #include <sstream> #include "CSPSim.h" #include "ScreenInfo.h" #include "VirtualBattlefield.h" class UpdateCallback : public osg::NodeCallback { virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { ScreenInfo * aScreenInfo = dynamic_cast<ScreenInfo*>(node); if (aScreenInfo && aScreenInfo->getStatus()) aScreenInfo->update(); traverse(node,nv); } }; ScreenInfo::ScreenInfo(int posx,int posy, std::string const & name, std::string const & text): m_TTFPath("../Data/Fonts/ltype.ttf"),m_FontSize(11), m_BitmapFont(new osgText::BitmapFont(m_TTFPath,m_FontSize)), m_Text(new osgText::Text(m_BitmapFont)) { int heightFont = m_BitmapFont->getHeight(); //uh, an osg fix is needed here heightFont = m_FontSize; setName(name); m_Text->setPosition(osg::Vec3(posx,posy - heightFont,0)); setUpdateCallback(new UpdateCallback()); m_Text->setText(text); addDrawable( m_Text ); } Framerate::Framerate(int posx, int posy):ScreenInfo(posx,posy,"FRAMERATE"),m_minFps(60),m_maxFps(25) { int heightFont = m_BitmapFont->getHeight(); //uh, an osg fix is needed here m_Date = new osgText::Text(m_BitmapFont); m_Date->setPosition(osg::Vec3(posx,posy - 2 * m_FontSize,0)); addDrawable( m_Date ); } void Framerate::update() { float fps = CSPSim::theSim->getFrameRate(); m_minFps = min(m_minFps,fps); m_maxFps = max(m_maxFps,fps); std::ostringstream osstr; osstr << setprecision(1) << fixed << fps << " FPS min: " << m_minFps << " max: " << m_maxFps; m_Text->setText(osstr.str()); simdata::SimDate artificial_time = CSPSim::theSim->getCurrentTime(); artificial_time.addTime(CSPSim::theSim->getBattlefield()->getSpin()); m_Date->setText("Date: " + artificial_time.asString()); } GeneralStats::GeneralStats(int posx,int posy):ScreenInfo(posx,posy,"GENERAL STATS") { unsigned int yOffset; yOffset = 50 - m_FontSize; m_LocalPosition = new osgText::Text(m_BitmapFont); m_LocalPosition->setPosition(osg::Vec3(posx,yOffset,0)); addDrawable( m_LocalPosition ); yOffset -= m_FontSize; m_GlobalPosition = new osgText::Text(m_BitmapFont); m_GlobalPosition->setPosition(osg::Vec3(posx,yOffset,0)); addDrawable( m_GlobalPosition ); yOffset -= m_FontSize; m_Velocity = new osgText::Text(m_BitmapFont); m_Velocity->setPosition(osg::Vec3(posx,yOffset,0)); addDrawable( m_Velocity ); } void GeneralStats::update() { unsigned short const precision = 2; std::ostringstream osstr; osstr << "Terrain Polygons: " << CSPSim::theSim->getBattlefield()->getTerrainPolygonsRendered(); m_Text->setText(osstr.str()); simdata::Pointer<DynamicObject> activeObject = CSPSim::theSim->getActiveObject(); if (!activeObject.isNull()) { simdata::Vector3 pos = activeObject->getLocalPosition(); osstr.str(""); osstr << "LocalPosition: (" << setprecision(precision) << fixed << setw(6 + precision) << setfill('0') << pos.x << ", "; osstr << setw(6 + precision) << setfill('0') << pos.y << ", "; osstr << setw(6 + precision) << setfill('0') << pos.z << ")"; m_LocalPosition->setText(osstr.str()); pos = activeObject->getGlobalPosition(); osstr.str(""); osstr << "GlobalPosition: (" << setprecision(precision) << fixed << setw(7 + precision) << setfill('0') << pos.x << ", "; osstr << setw(7 + precision) << setfill('0') << pos.y << ", "; osstr << setw(7 + precision) << setfill('0') << pos.z << ")"; m_GlobalPosition->setText(osstr.str()); simdata::Vector3 vel = activeObject->getVelocity(); osstr.str(""); osstr << "Velocity: (" << setprecision(2) << fixed << vel.x << ", " << vel.y << ", " << vel.z << ")"; m_Velocity->setText(osstr.str()); } } ObjectStats::ObjectStats(int posx,int posy):ScreenInfo(posx,posy,"OBJECT STATS") { simdata::Pointer<DynamicObject> activeObject = CSPSim::theSim->getActiveObject(); if (!activeObject.isNull()) { std::vector<std::string> stringStats; activeObject->getStats(stringStats); for (unsigned short i = 0; i < stringStats.size(); ++i) { posy -= m_FontSize; osgText::Text* aStat = new osgText::Text(m_BitmapFont); aStat->setPosition(osg::Vec3(posx,posy,0)); m_ObjectStats.push_back(aStat); addDrawable( aStat ); } removeDrawable(m_Text); } }; void ObjectStats::update() { simdata::Pointer<DynamicObject> activeObject = CSPSim::theSim->getActiveObject(); if (!activeObject.isNull()) { std::vector<std::string> stringStats; activeObject->getStats(stringStats); for (unsigned short i = 0; i < m_ObjectStats.size(); ++i) m_ObjectStats[i]->setText(stringStats[i]); } } |