8 struct options def_opt = { 1024, 768, 0, "game" };
19 static struct optcfg_option options[] = {
20 // short, long, id, desc
21 {'s', "size", OPTCFG_SIZE, "window size (WxH)"},
22 {0, "vr", OPTCFG_VR, "enable VR mode"},
23 {'f', "fullscreen", OPTCFG_FULLSCREEN, "run in fullscreen mode"},
24 {'w', "windowed", OPTCFG_WINDOWED, "run in windowed mode"},
25 {0, "screen", OPTCFG_SCREEN, "select starting screen"},
26 {'h', "help", OPTCFG_HELP, "print usage and exit"},
30 static int opt_handler(struct optcfg *oc, int opt, void *cls);
31 static int arg_handler(struct optcfg *oc, const char *arg, void *cls);
35 int init_options(int argc, char **argv, const char *cfgfile)
41 if(!(opt.start_scr = malloc(strlen(def_opt.start_scr) + 1))) {
42 perror("failed to allocate memory");
45 strcpy(opt.start_scr, def_opt.start_scr);
49 oc = optcfg_init(options);
50 optcfg_set_opt_callback(oc, opt_handler, 0);
51 optcfg_set_arg_callback(oc, arg_handler, 0);
54 optcfg_parse_config_file(oc, cfgfile);
57 if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
58 fprintf(stderr, "invalid option\n");
67 static int is_enabled(struct optcfg *oc)
70 optcfg_enabled_value(oc, &res);
74 static int opt_handler(struct optcfg *oc, int optid, void *cls)
80 valstr = optcfg_next_value(oc);
81 if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
82 fprintf(stderr, "size must be of the form: WIDTHxHEIGHT\n");
95 case OPTCFG_FULLSCREEN:
97 opt.flags |= OPT_FULLSCREEN;
99 opt.flags &= ~OPT_FULLSCREEN;
103 case OPTCFG_WINDOWED:
105 opt.flags &= ~OPT_FULLSCREEN;
107 opt.flags |= OPT_FULLSCREEN;
112 if(!(valstr = optcfg_next_value(oc))) {
113 fprintf(stderr, "screen name missing\n");
117 if(!(opt.start_scr = malloc(strlen(valstr) + 1))) {
118 perror("failed to allocate memory");
121 strcpy(opt.start_scr, valstr);
125 printf("Usage: %s [options]\nOptions:\n", argv0);
126 optcfg_print_options(oc);
132 static int arg_handler(struct optcfg *oc, const char *arg, void *cls)
134 fprintf(stderr, "unexpected argument: %s\n", arg);