14 #define DEF_INVMOUSEY 0
15 #define DEF_MOUSE_SPEED 50
18 struct options opt = {
22 DEF_VOL, DEF_VOL, DEF_VOL,
28 int parse_options(int argc, char **argv)
31 static const char *usage_fmt = "Usage: %s [options]\n"
33 " -screen <name>: select starting screen\n"
34 " -h,-help: print usage information and exit\n\n";
36 for(i=1; i<argc; i++) {
37 if(argv[i][0] == '-') {
38 if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
40 fprintf(stderr, "%s must be followed by a screen name\n", argv[i - 1]);
43 } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
44 printf(usage_fmt, argv[0]);
47 fprintf(stderr, "invalid option: %s\n", argv[i]);
51 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
59 int load_options(const char *fname)
63 if(!(cfg = ts_load(fname))) {
66 printf("loaded config: %s\n", fname);
68 opt.xres = ts_lookup_int(cfg, "options.video.xres", DEF_XRES);
69 opt.yres = ts_lookup_int(cfg, "options.video.yres", DEF_YRES);
70 opt.vsync = ts_lookup_int(cfg, "options.video.vsync", DEF_VSYNC);
71 opt.fullscreen = ts_lookup_int(cfg, "options.video.fullscreen", DEF_FULLSCR);
73 opt.vol_master = ts_lookup_int(cfg, "options.audio.volmaster", DEF_VOL);
74 opt.vol_mus = ts_lookup_int(cfg, "options.audio.volmusic", DEF_VOL);
75 opt.vol_sfx = ts_lookup_int(cfg, "options.audio.volsfx", DEF_VOL);
76 opt.music = ts_lookup_int(cfg, "options.audio.music", DEF_MUS);
78 opt.inv_mouse_y = ts_lookup_int(cfg, "options.controls.invmousey", DEF_INVMOUSEY);
79 opt.mouse_speed = ts_lookup_int(cfg, "options.controls.mousespeed", DEF_MOUSE_SPEED);
85 #define WROPT(lvl, fmt, val, defval) \
88 for(i=0; i<lvl; i++) fputc('\t', fp); \
89 if((val) == (defval)) { \
90 fprintf(fp, fmt "\t# default\n", val); \
92 fprintf(fp, fmt "\n", val); \
96 int save_options(const char *fname)
100 printf("writing config: %s\n", fname);
102 if(!(fp = fopen(fname, "wb"))) {
103 fprintf(stderr, "failed to save options (%s): %s\n", fname, strerror(errno));
105 fprintf(fp, "options {\n");
106 fprintf(fp, "\tvideo {\n");
107 WROPT(2, "xres = %d", opt.xres, DEF_XRES);
108 WROPT(2, "yres = %d", opt.yres, DEF_YRES);
109 WROPT(2, "vsync = %d", opt.vsync, DEF_VSYNC);
110 WROPT(2, "fullscreen = %d", opt.fullscreen, DEF_FULLSCR);
111 fprintf(fp, "\t}\n");
113 fprintf(fp, "\taudio {\n");
114 WROPT(2, "volmaster = %d", opt.vol_master, DEF_VOL);
115 WROPT(2, "volmusic = %d", opt.vol_mus, DEF_VOL);
116 WROPT(2, "volsfx = %d", opt.vol_sfx, DEF_VOL);
117 WROPT(2, "music = %d", opt.music ? 1 : 0, DEF_MUS);
118 fprintf(fp, "\t}\n");
120 fprintf(fp, "\tcontrols {\n");
121 WROPT(2, "invmousey = %d", opt.inv_mouse_y, DEF_INVMOUSEY);
122 WROPT(2, "mousespeed = %d", opt.mouse_speed, DEF_MOUSE_SPEED);
123 fprintf(fp, "\t}\n");
126 fprintf(fp, "# v" "i:ts=4 sts=4 sw=4 noexpandtab:\n");