From 493e590908a39324651cd2c81323e6969c0b4c4f Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Tue, 20 Sep 2016 08:46:50 +0300 Subject: [PATCH] - added config file and commandline parsing - removed the weird CAFE mechanism now that we have a config file --- src/cfgopt.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cfgopt.h | 14 ++++++++ src/demo.c | 38 ++++++++++++++------- src/screen.c | 13 +------ 4 files changed, 149 insertions(+), 24 deletions(-) create mode 100644 src/cfgopt.c create mode 100644 src/cfgopt.h diff --git a/src/cfgopt.c b/src/cfgopt.c new file mode 100644 index 0000000..553a145 --- /dev/null +++ b/src/cfgopt.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include "cfgopt.h" + +struct options opt; + +int parse_args(int argc, char **argv) +{ + int i; + char *scrname = 0; + + for(i=1; i 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 index 0000000..7051501 --- /dev/null +++ b/src/cfgopt.h @@ -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_ */ diff --git a/src/demo.c b/src/demo.c index 3a99314..472da32 100644 --- a/src/demo.c +++ b/src/demo.c @@ -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(); diff --git a/src/screen.c b/src/screen.c index c91cb82..17491d5 100644 --- a/src/screen.c +++ b/src/screen.c @@ -42,20 +42,9 @@ int scr_init(void) assert(num_screens <= NUM_SCR); for(i=0; iinit(); - 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; } -- 1.7.10.4