]> git.tdb.fi Git - libs/gl.git/commitdiff
Use an Options struct to process command line options in viewer
authorMikko Rasa <tdb@tdb.fi>
Tue, 13 Apr 2021 18:39:00 +0000 (21:39 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 13 Apr 2021 18:39:00 +0000 (21:39 +0300)
tools/viewer.cpp

index a1853e922f19485097ee644e80900b76c9913b1f..05f85de2430c5f2854ac40c9fc6e2015d6d48a64 100644 (file)
@@ -34,6 +34,15 @@ using namespace Msp;
 class Viewer: public RegisteredApplication<Viewer>
 {
 private:
+       struct Options
+       {
+               list<string> 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<string> 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<string>::const_iterator i=resource_locations.begin(); i!=resource_locations.end(); ++i)
+       for(list<string>::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<GL::Mesh>(renderable_name);
+                       mesh = &resources.get<GL::Mesh>(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<GL::Object>(renderable_name);
+               renderable = load<GL::Object>(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<GL::Animation>(animation_name);
+               GL::Animation *anim = load<GL::Animation>(opts.animation_name);
                anim_player = new GL::AnimationPlayer;
                anim_object = new GL::AnimatedObject(*object);
                anim_player->play(*anim_object, *anim);