Added new osg::ApplicationUsage and osg::ArgumentParser to help streamline

application/example developement.
This commit is contained in:
Robert Osfield
2003-02-18 16:36:42 +00:00
parent 6184ecba3f
commit c10d5f2d6f
22 changed files with 1008 additions and 84 deletions

View File

@@ -0,0 +1,93 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_APPLICATIONUSAGE
#define OSG_APPLICATIONUSAGE 1
#include <osg/Export>
#include <map>
#include <string>
#include <iostream>
namespace osg {
class SG_EXPORT ApplicationUsage
{
public:
static ApplicationUsage* instance();
ApplicationUsage() {}
ApplicationUsage(const std::string& commandLineUsage);
typedef std::map<std::string,std::string> UsageMap;
enum Type
{
COMMAND_LINE_OPTION,
ENVIRONMENTAL_VARIABLE,
KEYBOARD_MOUSE_BINDING
};
void addUsageExplanation(Type type,const std::string& option,const std::string& explanation);
void setCommandLineUsage(const std::string& explanation) { _commandLineUsage=explanation; }
const std::string& getCommandLineUsage() const { return _commandLineUsage; }
void addCommandLineOption(const std::string& option,const std::string& explanation);
const UsageMap& getCommandLineOptions() const { return _commandLineOptions; }
void addEnvironmentalVariable(const std::string& option,const std::string& explanation);
const UsageMap& getEnvironmentalVariables() const { return _environmentalVariables; }
void addKeyboardMouseBinding(const std::string& option,const std::string& explanation);
const UsageMap& getKeyboardMouseBindings() const { return _keyboardMouse; }
void write(std::ostream& output,const UsageMap& um,unsigned int widthOfOutput=80);
void write(std::ostream& output,unsigned int widthOfOutput=80);
protected:
std::string _commandLineUsage;
UsageMap _commandLineOptions;
UsageMap _environmentalVariables;
UsageMap _keyboardMouse;
};
class ApplicationUsageProxy
{
public:
/** register an explanation of commandline/evironmentalvaraible/keyboard mouse usage.*/
ApplicationUsageProxy(ApplicationUsage::Type type,const std::string& option,const std::string& explanation)
{
ApplicationUsage::instance()->addUsageExplanation(type,option,explanation);
}
};
}
#endif

142
include/osg/ArgumentParser Normal file
View File

@@ -0,0 +1,142 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_ARGUMENTPARSER
#define OSG_ARGUMENTPARSER 1
#include <osg/Export>
#include <string>
#include <map>
namespace osg {
class SG_EXPORT ArgumentParser
{
public:
ArgumentParser(int* argc,char **argv):
_argc(argc),
_argv(argv) {}
/** return the argument count.*/
int& argc() { return *_argc; }
/** return the argument array.*/
char** argv() { return _argv; }
/** return char* argument at specificed position.*/
char* operator [] (int pos) { return _argv[pos]; }
/** return const char* argument at specificed position.*/
const char* operator [] (int pos) const { return _argv[pos]; }
/** return the program name, as specified by argv[0] */
std::string getProgramName() const;
/** return the position of an occurance of a string in the argument list.
* return -1 when no string is found.*/
int find(const std::string& str) const;
/** return true if specified argument matches string.*/
bool match(int pos, const std::string& str) const;
/** return return true if specified parameter is an option in the form of -option or --option .*/
bool isOption(int pos) const;
/** return return true if specified parameter is an string, which can be any other string apart from an option.*/
bool isString(int pos) const;
/** return return true if specified parameter is an number.*/
bool isNumber(int pos) const;
bool containsOptions() const;
/** remove one or more arguments from the argv argument list, and decrement the argc respectively.*/
void remove(int pos,int num=1);
/** search for an occurance of a string in the argument list, on sucess
* remove that occurance from the list and return true, otherwise return false.*/
bool read(const std::string& str);
/** search for an occurance of a string in the argument list followed by a string,
* on sucess set the value string with the second parameters and then
* remove the two entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,std::string& value1);
/** search for an occurance of a string in the argument list followed by a two strings,
* on sucess set the value strings with the second & third parameters and then
* remove the three entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,std::string& value1,std::string& value2);
/** search for an occurance of a string in the argument list followed by a three strings,
* on sucess set the value strings with the second & third & fourth parameters and then
* remove the four entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,std::string& value1,std::string& value2,std::string& value3);
/** search for an occurance of a string in the argument list followed by a numeric value,
* on sucess set the values with the second parameter and then
* remove the two entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,float& value);
/** search for an occurance of a string in the argument list followed by two numeric values,
* on sucess set the values with the second & third parameters and then
* remove the three entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,float& value1,float& value2);
/** search for an occurance of a string in the argument list followed by three numeric values,
* on sucess set the values with the second & third & fourth parameters and then
* remove the four entries from the list and return true, otherwise return false.*/
bool read(const std::string& str,float& value1,float& value2,float& value3);
enum ErrorSeverity
{
BENIGN = 0,
CRITICAL = 1
};
typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
/** return the error flag, true if an error has occured when reading arguments.*/
bool errors(ErrorSeverity severity=BENIGN) const;
/** report an error message by adding to the ErrorMessageMap.*/
void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
/** for each remaining option report it as an unrecongnized.*/
void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
/** return the error message, if any has occured.*/
ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
/** return the error message, if any has occured.*/
const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
/** write out error messages at an above specified .*/
void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
protected:
int* _argc;
char** _argv;
ErrorMessageMap _errorMessageMap;
};
}
#endif

View File

@@ -15,6 +15,7 @@
#define OSG_DisplaySettings 1
#include <osg/Referenced>
#include <osg/ArgumentParser>
#include <string>
#include <vector>
@@ -59,6 +60,9 @@ class SG_EXPORT DisplaySettings : public osg::Referenced
/** read the command line string list, removing any matched control sequences.*/
void readCommandLine(std::vector<std::string>& commandLine);
/** read the commandline arguments.*/
void readCommandLine(ArgumentParser& parser);

View File

@@ -16,6 +16,7 @@
#include <osg/Node>
#include <osg/Image>
#include <osg/ArgumentParser>
#include <osgDB/Export>
@@ -51,6 +52,10 @@ extern OSGDB_EXPORT osg::Node* readNodeFile(const std::string& filename);
* than one subgraph has been loaded.*/
extern OSGDB_EXPORT osg::Node* readNodeFiles(std::vector<std::string>& commandLine);
/** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
* than one subgraph has been loaded.*/
extern OSGDB_EXPORT osg::Node* readNodeFiles(osg::ArgumentParser& parser);
}
#endif

View File

@@ -15,6 +15,7 @@
#define OSGDB_REGISTRY 1
#include <osg/ref_ptr>
#include <osg/ArgumentParser>
#include <osgDB/DynamicLibrary>
#include <osgDB/ReaderWriter>
@@ -53,6 +54,9 @@ class OSGDB_EXPORT Registry : public osg::Referenced
/** read the command line string list, removing any matched control sequences.*/
void readCommandLine(std::vector<std::string>& commandLine);
/** read the command line arguments.*/
void readCommandLine(osg::ArgumentParser& commandLine);
/** register an .fileextension alias to mapExt toExt, the later
* should the the extension name of the readerwriter plugin library.
* For example to map .tif files to the tiff loader, use
@@ -203,6 +207,12 @@ inline void readCommandLine(std::vector<std::string>& commandLine)
Registry::instance()->readCommandLine(commandLine);
}
/** read the command line arguments.*/
inline void readCommandLine(osg::ArgumentParser& parser)
{
Registry::instance()->readCommandLine(parser);
}
/** Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
class RegisterDotOsgWrapperProxy
{

View File

@@ -17,6 +17,7 @@
#include <Producer/CameraGroup>
#include <osg/ArgumentParser>
#include <osg/Group>
#include <osg/StateSet>
#include <osg/FrameStamp>
@@ -38,6 +39,8 @@ class OSGPRODUCER_EXPORT CameraGroup : public Producer::CameraGroup
CameraGroup(const std::string& configFile);
CameraGroup(osg::ArgumentParser& arguments);
virtual ~CameraGroup() {}

View File

@@ -1,10 +1,27 @@
#ifndef FRAME_STATS_HANDLER
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGPRODUCER_FRAME_STATS_HANDLER
#define OSGPRODUCER_FRAME_STATS_HANDLER 1
#include <Producer/CameraGroup>
#include <stdio.h>
#include <GL/gl.h>
namespace Producer {
namespace osgProducer {
class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callback
class FrameStatsHandler : public Producer::CameraGroup::StatsHandler, public Producer::Camera::Callback
{
public:
FrameStatsHandler()
@@ -14,15 +31,16 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
}
void setArraySize(unsigned int size) { _fs.resize(size); }
unsigned int getArraySize() { return _fs.size(); }
void operator() (const CameraGroup &cg )
void operator() (const Producer::CameraGroup &cg )
{
_index = (_index + 1) % _fs.size();
_fs[_index] = cg.getFrameStats();
}
void operator() (const Camera &camera)
void operator() (const Producer::Camera &camera)
{
if (!camera.getInstrumentationMode()) return;
@@ -43,10 +61,10 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
glLoadIdentity();
unsigned int lindex = (_index + 1) % _fs.size();
Camera::TimeStamp zero = _fs[lindex]._startOfFrame;
int i;
double x1, x2, y1, y2;
for( int frame = 0; frame < _fs.size(); frame++ )
Producer::Camera::TimeStamp zero = _fs[lindex]._startOfFrame;
unsigned int i;
double x1=0.0, x2=0.0, y1=0.0, y2=0.0;
for(unsigned int frame = 0; frame < _fs.size(); frame++ )
{
CameraGroup::FrameStats fs = _fs[(lindex + frame) % _fs.size()];
y1 = 0.0;
@@ -65,11 +83,11 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
for( i = 0; i < fs.getNumFrameTimeStampSets(); i++ )
{
Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i);
Producer::Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i);
y1 += 13.0;
y2 = y1 + 10.0;
x1 = fts[Camera::BeginCull] - zero;
x2 = fts[Camera::EndCull] - zero;
x1 = fts[Producer::Camera::BeginCull] - zero;
x2 = fts[Producer::Camera::EndCull] - zero;
glColor4f( 0.0, 0.0, 1.0, 0.5 );
glVertex2d( x1, y1);
@@ -77,8 +95,8 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
glVertex2d( x2, y2);
glVertex2d( x1, y2);
x1 = fts[Camera::BeginDraw] - zero;
x2 = fts[Camera::EndDraw] - zero;
x1 = fts[Producer::Camera::BeginDraw] - zero;
x2 = fts[Producer::Camera::EndDraw] - zero;
glColor4f( 1.0, 0.0, 0.0, 0.5 );
glVertex2d( x1, y1);
@@ -99,8 +117,8 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
for( i = 0; i < fs.getNumFrameTimeStampSets(); i++ )
{
y2 = y1 + 11;
Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i);
Camera::TimeStamp vsync = fts[Camera::Vsync];
Producer::Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i);
Producer::Camera::TimeStamp vsync = fts[Producer::Camera::Vsync];
double x1 = vsync - zero;
glColor4f( 1.0, 1.0, 0.0, 0.5 );
glVertex2d( x1, y1 );
@@ -136,8 +154,10 @@ class FrameStatsHandler : public CameraGroup::StatsHandler, public Camera::Callb
}
private:
std::vector <CameraGroup::FrameStats> _fs;
std::vector <Producer::CameraGroup::FrameStats> _fs;
unsigned int _index;
};
}
#endif

View File

@@ -8,7 +8,7 @@ class StatsEventHandler : public osgGA::GUIEventHandler
StatsEventHandler(osgProducer::Viewer* cg):_cg(cg) {}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
if(!_cg) return false;

View File

@@ -16,6 +16,7 @@
#include <osg/Timer>
#include <osg/NodeVisitor>
#include <osg/ArgumentParser>
#include <osgGA/GUIActionAdapter>
#include <osgGA/GUIEventHandler>
@@ -38,6 +39,8 @@ class OSGPRODUCER_EXPORT Viewer : public CameraGroup, public osgGA::GUIActionAda
Viewer(const std::string& configFile);
Viewer(osg::ArgumentParser& arguments);
virtual ~Viewer() {}