From: Mikko Rasa Date: Tue, 13 Apr 2021 18:39:00 +0000 (+0300) Subject: Use an Options struct to process command line options in viewer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4bc50a3c977af89ab4a79aa777bb99322cda053d;p=libs%2Fgl.git Use an Options struct to process command line options in viewer --- diff --git a/tools/viewer.cpp b/tools/viewer.cpp index a1853e92..05f85de2 100644 --- a/tools/viewer.cpp +++ b/tools/viewer.cpp @@ -34,6 +34,15 @@ using namespace Msp; class Viewer: public RegisteredApplication { private: + struct Options + { + list resource_locations; + string animation_name; + string renderable_name; + + Options(int, char **); + }; + class Resources: public GL::Resources { private: @@ -47,6 +56,7 @@ private: void add_pack(const string &); }; + Options opts; Graphics::SimpleGLWindow window; Input::Mouse mouse; Resources resources; @@ -87,7 +97,17 @@ private: }; +Viewer::Options::Options(int argc, char **argv) +{ + GetOpt getopt; + getopt.add_option('r', "resources", resource_locations, GetOpt::REQUIRED_ARG); + getopt.add_option('a', "animation", animation_name, GetOpt::REQUIRED_ARG); + getopt.add_argument("renderable", renderable_name); + getopt(argc, argv); +} + Viewer::Viewer(int argc, char **argv): + opts(argc, argv), window(1024, 768, false), mouse(window), view(window, window.get_gl_context()), @@ -102,16 +122,7 @@ Viewer::Viewer(int argc, char **argv): light_pitch(0), dragging(0) { - list resource_locations; - string animation_name; - string renderable_name; - GetOpt getopt; - getopt.add_option('r', "resources", resource_locations, GetOpt::REQUIRED_ARG); - getopt.add_option('a', "animation", animation_name, GetOpt::REQUIRED_ARG); - getopt.add_argument("renderable", renderable_name); - getopt(argc, argv); - - for(list::const_iterator i=resource_locations.begin(); i!=resource_locations.end(); ++i) + for(list::const_iterator i=opts.resource_locations.begin(); i!=opts.resource_locations.end(); ++i) { if(FS::is_dir(*i)) resources.add_directory(*i); @@ -127,18 +138,18 @@ Viewer::Viewer(int argc, char **argv): GL::Object *object = 0; - string ext = FS::extpart(renderable_name); + string ext = FS::extpart(opts.renderable_name); if(ext==".mesh") { GL::Mesh *mesh = 0; - if(FS::exists(renderable_name)) + if(FS::exists(opts.renderable_name)) { mesh = new GL::Mesh; - DataFile::load(*mesh, renderable_name); - resources.add("__"+renderable_name, mesh); + DataFile::load(*mesh, opts.renderable_name); + resources.add("__"+opts.renderable_name, mesh); } else - mesh = &resources.get(renderable_name); + mesh = &resources.get(opts.renderable_name); object = new GL::Object; GL::Technique *tech = new GL::Technique; @@ -151,22 +162,22 @@ Viewer::Viewer(int argc, char **argv): resources.add("__.object", object); } else if(ext==".object") - renderable = load(renderable_name); + renderable = load(opts.renderable_name); else if(ext==".scene") { GL::SimpleScene *scene = new GL::SimpleScene; - DataFile::load(*scene, renderable_name, resources); + DataFile::load(*scene, opts.renderable_name, resources); renderable = scene; } else throw usage_error("Unknown renderable type"); - if(!animation_name.empty()) + if(!opts.animation_name.empty()) { if(!object) throw usage_error("Must have an object to animate"); - GL::Animation *anim = load(animation_name); + GL::Animation *anim = load(opts.animation_name); anim_player = new GL::AnimationPlayer; anim_object = new GL::AnimatedObject(*object); anim_player->play(*anim_object, *anim);