options/config
[demo_prior] / src / opt.c
diff --git a/src/opt.c b/src/opt.c
new file mode 100644 (file)
index 0000000..796722c
--- /dev/null
+++ b/src/opt.c
@@ -0,0 +1,83 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "opt.h"
+#include "treestore.h"
+
+static void print_usage(const char *argv0);
+
+struct options opt = {
+       1280, 800,
+       1,                      /* fullscreen */
+       1,                      /* music */
+       1,                      /* sRGB */
+       1                       /* anti-aliasing */
+};
+
+int parse_args(int argc, char **argv)
+{
+       int i;
+
+       for(i=1; i<argc; i++) {
+               if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "-size") == 0) {
+                       if(sscanf(argv[++i], "%dx%d", &opt.width, &opt.height) != 2) {
+                               fprintf(stderr, "%s must be followed by <width>x<height>\n", argv[-1]);
+                               return -1;
+                       }
+               } else if(strcmp(argv[i], "-fs") == 0) {
+                       opt.fullscr = 1;
+               } else if(strcmp(argv[i], "-win") == 0) {
+                       opt.fullscr = 0;
+               } else if(strcmp(argv[i], "-srgb") == 0) {
+                       opt.srgb = 1;
+               } else if(strcmp(argv[i], "-nosrgb") == 0) {
+                       opt.srgb = 0;
+               } else if(strcmp(argv[i], "-aa") == 0) {
+                       opt.msaa = 1;
+               } else if(strcmp(argv[i], "-noaa") == 0) {
+                       opt.msaa = 0;
+               } else if(strcmp(argv[i], "-music") == 0) {
+                       opt.music = 1;
+               } else if(strcmp(argv[i], "-nomusic") == 0) {
+                       opt.music = 0;
+               } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
+                       print_usage(argv[0]);
+                       exit(0);
+               } else {
+                       fprintf(stderr, "invalid argument: %s\n", argv[i]);
+                       print_usage(argv[0]);
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+static void print_usage(const char *argv0)
+{
+       printf("Usage: %s [options]\n", argv0);
+       printf("  -fs: fullscr\n");
+       printf("  -win: windowed\n");
+       printf("  -s,-size <WxH>: windowed resolution\n");
+       printf("  -srgb/-nosrgb: enable/disable sRGB framebuffer\n");
+       printf("  -aa/-noaa: enable/disable multisample anti-aliasing\n");
+       printf("  -music/-nomusic: enable/disable music playback\n");
+       printf("  -h,-help: print usage and exit\n");
+}
+
+int read_cfg(const char *fname)
+{
+       struct ts_node *ts;
+
+       if(!(ts = ts_load(fname))) {
+               return -1;
+       }
+       opt.width = ts_lookup_int(ts, "demo.width", opt.width);
+       opt.height = ts_lookup_int(ts, "demo.height", opt.height);
+       opt.fullscr = ts_lookup_int(ts, "demo.fullscreen", opt.fullscr);
+       opt.music = ts_lookup_int(ts, "demo.music", opt.music);
+       opt.srgb = ts_lookup_int(ts, "demo.srgb", opt.srgb);
+       opt.msaa = ts_lookup_int(ts, "demo.aa", opt.msaa);
+
+       ts_free_tree(ts);
+       return 0;
+}