f9bcf53df04d42544dabe42e9de3f275efba1e7e
[laserbrain_demo] / src / opt.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <optcfg.h>
5 #include "opt.h"
6
7 Options opt;
8
9 Options def_opt = {
10         1280, 800,
11         false,  // vr
12         false,  // fullscreen
13         0,              // scene file
14         true    // music
15 };
16
17 enum {
18         OPT_SIZE,
19         OPT_VR,
20         OPT_FULLSCREEN,
21         OPT_WINDOWED,
22         OPT_SCENEFILE,
23         OPT_MUSIC,
24         OPT_HELP
25 };
26
27 static optcfg_option options[] = {
28         // short, long, id, desc
29         {'s', "size", OPT_SIZE, "window size (WxH)"},
30         {0, "vr", OPT_VR, "enable VR mode"},
31         {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"},
32         {'w', "windowed", OPT_WINDOWED, "run in windowed mode"},
33         {0, "scene", OPT_SCENEFILE, "scene file to open"},
34         {'m', "music", OPT_MUSIC, "play background audio"},
35         {'h', "help", OPT_HELP, "print usage and exit"},
36         OPTCFG_OPTIONS_END
37 };
38
39 static int opt_handler(optcfg *oc, int opt, void *cls);
40 static int arg_handler(optcfg *oc, const char *arg, void *cls);
41
42 bool init_options(int argc, char **argv, const char *cfgfile)
43 {
44         // default options
45         opt = def_opt;
46
47         optcfg *oc = optcfg_init(options);
48         optcfg_set_opt_callback(oc, opt_handler, 0);
49         optcfg_set_arg_callback(oc, arg_handler, 0);
50
51         if(cfgfile) {
52                 optcfg_parse_config_file(oc, cfgfile);
53         }
54
55         if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
56                 fprintf(stderr, "invalid option\n");
57                 optcfg_destroy(oc);
58                 return false;
59         }
60
61         optcfg_destroy(oc);
62         return true;
63 }
64
65 static bool is_enabled(optcfg *oc)
66 {
67         int res;
68         optcfg_enabled_value(oc, &res);
69         return res != 0;
70 }
71
72 static int opt_handler(optcfg *oc, int optid, void *cls)
73 {
74         switch(optid) {
75         case OPT_SIZE:
76                 {
77                         char *valstr = optcfg_next_value(oc);
78                         if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
79                                 fprintf(stderr, "size must be in the form: WIDTHxHEIGHT\n");
80                                 return -1;
81                         }
82                 }
83                 break;
84
85         case OPT_VR:
86                 opt.vr = is_enabled(oc);
87                 break;
88
89         case OPT_FULLSCREEN:
90                 opt.fullscreen = is_enabled(oc);
91                 break;
92
93         case OPT_WINDOWED:
94                 opt.fullscreen = !is_enabled(oc);
95                 break;
96
97         case OPT_SCENEFILE:
98                 opt.scenefile = strdup(optcfg_next_value(oc));
99                 break;
100
101         case OPT_MUSIC:
102                 opt.music = is_enabled(oc);
103                 break;
104
105         case OPT_HELP:
106                 printf("Usage: demo [options]\nOptions:\n");
107                 optcfg_print_options(oc);
108                 exit(0);
109         }
110         return 0;
111 }
112
113 static int arg_handler(optcfg *oc, const char *arg, void *cls)
114 {
115         if(opt.scenefile) {
116                 fprintf(stderr, "unexpected argument: %s\n", arg);
117                 return -1;
118         }
119         opt.scenefile = arg;
120         return 0;
121 }