- added config file and commandline parsing
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 20 Sep 2016 05:46:50 +0000 (08:46 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 20 Sep 2016 05:46:50 +0000 (08:46 +0300)
- removed the weird CAFE mechanism now that we have a config file

src/cfgopt.c [new file with mode: 0644]
src/cfgopt.h [new file with mode: 0644]
src/demo.c
src/screen.c

diff --git a/src/cfgopt.c b/src/cfgopt.c
new file mode 100644 (file)
index 0000000..553a145
--- /dev/null
@@ -0,0 +1,108 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include "cfgopt.h"
+
+struct options opt;
+
+int parse_args(int argc, char **argv)
+{
+       int i;
+       char *scrname = 0;
+
+       for(i=1; i<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "-music") == 0) {
+                               opt.music = 1;
+                       } else if(strcmp(argv[i], "-nomusic") == 0) {
+                               opt.music = 0;
+                       } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
+                               scrname = argv[++i];
+                       } else {
+                               fprintf(stderr, "invalid option: %s\n", argv[i]);
+                               return -1;
+                       }
+               } else {
+                       if(scrname) {
+                               fprintf(stderr, "unexpected option: %s\n", argv[i]);
+                               return -1;
+                       }
+                       scrname = argv[i];
+               }
+       }
+
+       opt.start_scr = scrname;
+       return 0;
+}
+
+static char *strip_space(char *s)
+{
+       int len;
+       char *end;
+
+       while(*s && isspace(*s)) ++s;
+       if(!*s) return 0;
+
+       if((end = strrchr(s, '#'))) {
+               --end;
+       } else {
+               len = strlen(s);
+               end = s + len - 1;
+       }
+
+       while(end > s && isspace(*end)) *end-- = 0;
+       return end > s ? s : 0;
+}
+
+static int bool_value(char *s)
+{
+       char *ptr = s;
+       while(*ptr) {
+               *ptr = tolower(*ptr);
+               ++ptr;
+       }
+
+       return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
+}
+
+int load_config(const char *fname)
+{
+       FILE *fp;
+       char buf[256];
+       int nline = 0;
+
+       if(!(fp = fopen(fname, "rb"))) {
+               return 0;       /* just ignore missing config files */
+       }
+
+       while(fgets(buf, sizeof buf, fp)) {
+               char *line, *key, *value;
+
+               ++nline;
+               if(!(line = strip_space(buf))) {
+                       continue;
+               }
+
+               if(!(value = strchr(line, '='))) {
+                       fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
+                       return -1;
+               }
+               *value++ = 0;
+
+               if(!(key = strip_space(line)) || !(value = strip_space(value))) {
+                       fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
+                       return -1;
+               }
+
+               if(strcmp(line, "music") == 0) {
+                       opt.music = bool_value(value);
+               } else if(strcmp(line, "screen") == 0) {
+                       opt.start_scr = strdup(value);
+               } else {
+                       fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);
+                       return -1;
+               }
+       }
+       return 0;
+}
diff --git a/src/cfgopt.h b/src/cfgopt.h
new file mode 100644 (file)
index 0000000..7051501
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef CFGOPT_H_
+#define CFGOPT_H_
+
+struct options {
+       const char *start_scr;
+       int music;
+};
+
+extern struct options opt;
+
+int parse_args(int argc, char **argv);
+int load_config(const char *fname);
+
+#endif /* CFGOPT_H_ */
index 3a99314..472da32 100644 (file)
@@ -8,6 +8,7 @@
 #include "screen.h"
 #include "3dgfx.h"
 #include "music.h"
+#include "cfgopt.h"
 
 int fb_width = 320;
 int fb_height = 240;
@@ -18,15 +19,20 @@ int mouse_x, mouse_y;
 unsigned int mouse_bmask;
 
 static unsigned long nframes;
-static const char *start_scr_name;
 
 int demo_init(int argc, char **argv)
 {
        struct screen *scr;
+       char *env;
 
-       start_scr_name = getenv("START_SCR");
-       if(argv[1]) {
-               start_scr_name = argv[1];
+       if(load_config("demo.cfg") == -1) {
+               return -1;
+       }
+       if((env = getenv("START_SCR"))) {
+               opt.start_scr = env;
+       }
+       if(parse_args(argc, argv) == -1) {
+               return -1;
        }
 
        if(g3d_init() == -1) {
@@ -34,34 +40,40 @@ int demo_init(int argc, char **argv)
        }
        g3d_framebuffer(fb_width, fb_height, fb_pixels);
 
-       if(music_open("data/test.mod") == -1) {
-               return -1;
+       if(opt.music) {
+               if(music_open("data/test.mod") == -1) {
+                       return -1;
+               }
        }
 
        if(scr_init() == -1) {
                return -1;
        }
-       if(start_scr_name) {
-               scr = scr_lookup(start_scr_name);
+       if(opt.start_scr) {
+               scr = scr_lookup(opt.start_scr);
        } else {
                scr = scr_screen(0);
        }
 
        if(!scr || scr_change(scr, 4000) == -1) {
-               fprintf(stderr, "screen %s not found\n", start_scr_name ? start_scr_name : "0");
+               fprintf(stderr, "screen %s not found\n", opt.start_scr ? opt.start_scr : "0");
                return -1;
        }
 
        /* clear the framebuffer at least once */
        memset(fb_pixels, 0, fb_width * fb_height * fb_bpp / CHAR_BIT);
 
-       music_play();
+       if(opt.music) {
+               music_play();
+       }
        return 0;
 }
 
 void demo_cleanup(void)
 {
-       music_close();
+       if(opt.music) {
+               music_close();
+       }
        scr_shutdown();
        g3d_destroy();
 
@@ -73,7 +85,9 @@ void demo_cleanup(void)
 
 void demo_draw(void)
 {
-       music_update();
+       if(opt.music) {
+               music_update();
+       }
        scr_update();
        scr_draw();
 
index c91cb82..17491d5 100644 (file)
@@ -42,20 +42,9 @@ int scr_init(void)
        assert(num_screens <= NUM_SCR);
 
        for(i=0; i<num_screens; i++) {
-               int r;
-               r = scr[i]->init();
-               if(r == -1) {
+               if(scr[i]->init() == -1) {
                        return -1;
                }
-
-               /* Make the effect run first if it returns "CAFE" from ins init() */
-               if (r == 0xCAFE) {
-                       struct screen *tmp;
-                       tmp = scr[i];
-                       scr[i] = scr[0];
-                       scr[0] = tmp;
-                       printf("*** Screen %s displayed out of order ***\n", scr[0]->name);
-               }
        }
        return 0;
 }