From: John Tsiombikas Date: Wed, 27 Jul 2016 01:32:04 +0000 (+0300) Subject: better option/cfgfile parsing X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=vrfileman;a=commitdiff_plain;h=b7ea5b1ae3f0f0a5eba13b49e303b49876ee209e better option/cfgfile parsing --- diff --git a/Makefile b/Makefile index f8a955d..c3de574 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ warn = -pedantic -Wall CFLAGS = $(warn) $(opt) $(dbg) $(inc) `pkg-config --cflags sdl2` CXXFLAGS = -std=c++11 $(warn) $(opt) $(dbg) $(inc) `pkg-config --cflags sdl2` -LDFLAGS = $(libgl) -lgmath -limago -lgoatvr `pkg-config --libs sdl2` +LDFLAGS = $(libgl) -lgmath -limago -lgoatvr -loptcfg `pkg-config --libs sdl2` ifeq ($(shell uname -s), Darwin) diff --git a/src/app.cc b/src/app.cc index 0dc88f0..e9e4cbb 100644 --- a/src/app.cc +++ b/src/app.cc @@ -7,15 +7,13 @@ #include "meshgen.h" #include "backdrop.h" #include "goatvr.h" - -static bool parse_args(int argc, char **argv); +#include "opt.h" int win_width, win_height; float win_aspect; long time_msec; Mat4 view_matrix; -static bool use_vr; static bool should_swap; static float cam_theta, cam_phi; @@ -27,7 +25,7 @@ static int prev_x, prev_y; bool app_init(int argc, char **argv) { - if(!parse_args(argc, argv)) { + if(!init_options(argc, argv, 0)) { return false; } if(init_opengl() == -1) { @@ -48,10 +46,11 @@ bool app_init(int argc, char **argv) glEnable(GL_LIGHT0); if(GLEW_ARB_framebuffer_sRGB) { + printf("enabling sRGB framebuffer\n"); glEnable(GL_FRAMEBUFFER_SRGB); } - if(use_vr) { + if(opt.vr) { if(goatvr_init() == -1) { return false; } @@ -76,7 +75,7 @@ bool app_init(int argc, char **argv) void app_cleanup() { - if(use_vr) { + if(opt.vr) { goatvr_shutdown(); } delete mesh_torus; @@ -85,7 +84,7 @@ void app_cleanup() void app_draw() { - if(use_vr) { + if(opt.vr) { // VR mode goatvr_draw_start(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -152,7 +151,7 @@ void app_keyboard(int key, bool pressed) break; case ' ': - if(use_vr) { + if(opt.vr) { goatvr_recenter(); } break; @@ -179,7 +178,7 @@ void app_mouse_motion(int x, int y) if(bnstate[0]) { cam_theta += dx * 0.5; - if(!use_vr || !goatvr_have_headtracking()) { + if(!opt.vr || !goatvr_have_headtracking()) { cam_phi += dy * 0.5; if(cam_phi < -90) cam_phi = -90; @@ -188,30 +187,3 @@ void app_mouse_motion(int x, int y) } app_redraw(); } - -static bool parse_args(int argc, char **argv) -{ - for(int i=1; i +#include +#include +#include +#include "opt.h" + +Options opt; + +enum { + OPT_VR, + OPT_SRGB, + OPT_FULLSCREEN, + OPT_WINDOWED, + OPT_HELP +}; + +static optcfg_option options[] = { + // short, long, id, desc + {0, "vr", OPT_VR, "enable VR mode"}, + {0, "srgb", OPT_SRGB, "use linear color space"}, + {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"}, + {'w', "windowed", OPT_WINDOWED, "run in windowed mode"}, + {'h', "help", OPT_HELP, "print usage and exit"}, + OPTCFG_OPTIONS_END +}; + +static int opt_handler(optcfg *oc, int opt, void *cls); + +bool init_options(int argc, char **argv, const char *cfgfile) +{ + // default options + memset(&opt, 0, sizeof opt); + opt.srgb = true; + + optcfg *oc = optcfg_init(options); + optcfg_set_opt_callback(oc, opt_handler, 0); + + if(cfgfile && optcfg_parse_config_file(oc, cfgfile) == -1) { + optcfg_destroy(oc); + return false; + } + + if(argv && optcfg_parse_args(oc, argc, argv) == -1) { + fprintf(stderr, "invalid option\n"); + optcfg_destroy(oc); + return false; + } + + optcfg_destroy(oc); + return true; +} + +static bool is_enabled(optcfg *oc) +{ + int res; + optcfg_enabled_value(oc, &res); + return res != 0; +} + +static int opt_handler(optcfg *oc, int optid, void *cls) +{ + switch(optid) { + case OPT_VR: + opt.vr = is_enabled(oc); + break; + + case OPT_SRGB: + opt.srgb = is_enabled(oc); + break; + + case OPT_FULLSCREEN: + opt.fullscreen = is_enabled(oc); + break; + + case OPT_WINDOWED: + opt.fullscreen = !is_enabled(oc); + break; + + case OPT_HELP: + printf("Usage: vrfileman [options]\nOptions:\n"); + optcfg_print_options(oc); + exit(0); + } + return true; +} diff --git a/src/opt.h b/src/opt.h new file mode 100644 index 0000000..c0bb5f6 --- /dev/null +++ b/src/opt.h @@ -0,0 +1,14 @@ +#ifndef OPT_H_ +#define OPT_H_ + +struct Options { + bool vr; + bool srgb; + bool fullscreen; +}; + +extern Options opt; + +bool init_options(int argc, char **argv, const char *cfgfile); + +#endif // OPT_H_