8 struct options def_opt = { 1024, 768, 0, "game" };
21 static struct optcfg_option options[] = {
22 // short, long, id, desc
23 {'s', "size", OPTCFG_SIZE, "window size (WxH)"},
25 {0, "vr", OPTCFG_VR, "enable VR mode"},
27 {'f', "fullscreen", OPTCFG_FULLSCREEN, "run in fullscreen mode"},
28 {'w', "windowed", OPTCFG_WINDOWED, "run in windowed mode"},
29 {0, "screen", OPTCFG_SCREEN, "select starting screen"},
30 {'h', "help", OPTCFG_HELP, "print usage and exit"},
34 static int opt_handler(struct optcfg *oc, int opt, void *cls);
35 static int arg_handler(struct optcfg *oc, const char *arg, void *cls);
39 int init_options(int argc, char **argv, const char *cfgfile)
45 if(!(opt.start_scr = malloc(strlen(def_opt.start_scr) + 1))) {
46 perror("failed to allocate memory");
49 strcpy(opt.start_scr, def_opt.start_scr);
53 oc = optcfg_init(options);
54 optcfg_set_opt_callback(oc, opt_handler, 0);
55 optcfg_set_arg_callback(oc, arg_handler, 0);
58 optcfg_parse_config_file(oc, cfgfile);
61 if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
62 fprintf(stderr, "invalid option\n");
71 static int is_enabled(struct optcfg *oc)
74 optcfg_enabled_value(oc, &res);
78 static int opt_handler(struct optcfg *oc, int optid, void *cls)
84 valstr = optcfg_next_value(oc);
85 if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
86 fprintf(stderr, "size must be of the form: WIDTHxHEIGHT\n");
101 case OPTCFG_FULLSCREEN:
103 opt.flags |= OPT_FULLSCREEN;
105 opt.flags &= ~OPT_FULLSCREEN;
109 case OPTCFG_WINDOWED:
111 opt.flags &= ~OPT_FULLSCREEN;
113 opt.flags |= OPT_FULLSCREEN;
118 if(!(valstr = optcfg_next_value(oc))) {
119 fprintf(stderr, "screen name missing\n");
123 if(!(opt.start_scr = malloc(strlen(valstr) + 1))) {
124 perror("failed to allocate memory");
127 strcpy(opt.start_scr, valstr);
131 printf("Usage: %s [options]\nOptions:\n", argv0);
132 optcfg_print_options(oc);
138 static int arg_handler(struct optcfg *oc, const char *arg, void *cls)
140 fprintf(stderr, "unexpected argument: %s\n", arg);