VR mode
[laserbrain_demo] / src / opt.cc
diff --git a/src/opt.cc b/src/opt.cc
new file mode 100644 (file)
index 0000000..9226785
--- /dev/null
@@ -0,0 +1,104 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <optcfg.h>
+#include "opt.h"
+
+Options opt;
+
+Options def_opt = {
+       1280, 800,
+       false,  // vr
+       false   // fullscreen
+};
+
+enum {
+       OPT_SIZE,
+       OPT_VR,
+       OPT_SRGB,
+       OPT_FULLSCREEN,
+       OPT_WINDOWED,
+       OPT_HELP
+};
+
+static optcfg_option options[] = {
+       // short, long, id, desc
+       {'s', "size", OPT_SIZE, "window size (WxH)"},
+       {0, "vr", OPT_VR, "enable VR mode"},
+       {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"},
+       {'w', "windowed", OPT_WINDOWED, "run in windowed mode"},
+       {'h', "help", OPT_HELP, "print usage and exit"},
+       OPTCFG_OPTIONS_END
+};
+
+static int opt_handler(optcfg *oc, int opt, void *cls);
+static int arg_handler(optcfg *oc, const char *arg, void *cls);
+
+bool init_options(int argc, char **argv, const char *cfgfile)
+{
+       // default options
+       opt = def_opt;
+
+       optcfg *oc = optcfg_init(options);
+       optcfg_set_opt_callback(oc, opt_handler, 0);
+       optcfg_set_arg_callback(oc, arg_handler, 0);
+
+       if(cfgfile) {
+               optcfg_parse_config_file(oc, cfgfile);
+       }
+
+       if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
+               fprintf(stderr, "invalid option\n");
+               optcfg_destroy(oc);
+               return false;
+       }
+
+       optcfg_destroy(oc);
+       return true;
+}
+
+static bool is_enabled(optcfg *oc)
+{
+       int res;
+       optcfg_enabled_value(oc, &res);
+       return res != 0;
+}
+
+static int opt_handler(optcfg *oc, int optid, void *cls)
+{
+       switch(optid) {
+       case OPT_SIZE:
+               {
+                       char *valstr = optcfg_next_value(oc);
+                       if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
+                               fprintf(stderr, "size must be in the form: WIDTHxHEIGHT\n");
+                               return -1;
+                       }
+               }
+               break;
+
+       case OPT_VR:
+               opt.vr = is_enabled(oc);
+               break;
+
+       case OPT_FULLSCREEN:
+               opt.fullscreen = is_enabled(oc);
+               break;
+
+       case OPT_WINDOWED:
+               opt.fullscreen = !is_enabled(oc);
+               break;
+
+       case OPT_HELP:
+               printf("Usage: vrfileman [options]\nOptions:\n");
+               optcfg_print_options(oc);
+               exit(0);
+       }
+       return 0;
+}
+
+static int arg_handler(optcfg *oc, const char *arg, void *cls)
+{
+       fprintf(stderr, "unexpected argument: %s\n", arg);
+       return -1;
+}