From: DeaDDooMER Date: Fri, 17 Apr 2020 11:15:11 +0000 (+0400) Subject: config: read config and args by renders X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=9f3ca6bf02300e3dc72e3c2085a8e35aa9242678;p=flatwaifu.git config: read config and args by renders --- diff --git a/src/args.c b/src/args.c new file mode 100644 index 0000000..ac3fb67 --- /dev/null +++ b/src/args.c @@ -0,0 +1,38 @@ +#include "args.h" +#include "system.h" +#include "error.h" +#include "config.h" +#include + +void ARG_parse (int argc, char **argv, int n, const cfg_t **list) { + assert(argc >= 0); + assert(argv != NULL); + assert(n >= 0); + assert(list != NULL); + int i, j; + char *key; + const cfg_t *c; + for (i = 1; i < argc; i++) { + if (argv[i][0] == '-' && argv[i][1] != 0) { + j = 0; + c = NULL; + key = &argv[i][1]; + while (j < n && c == NULL){ + c = CFG_find_entry(key, list[j]); + j++; + } + if (c == NULL) { + ERR_failinit("%s: unknown parameter %s\n", argv[0], argv[i]); + } else if (c->t == Y_SW_ON || c->t == Y_SW_OFF) { + CFG_update_key(key, "on", c); + } else if (i + 1 < argc) { + CFG_update_key(key, argv[i + 1], c); + i += 1; + } else { + ERR_failinit("%s: missing argument for parameter %s\n", argv[0], argv[i]); + } + } else { + ERR_failinit("%s: something wrong here: %s\n", argv[0], argv[i]); + } + } +} diff --git a/src/args.h b/src/args.h new file mode 100644 index 0000000..1eafb27 --- /dev/null +++ b/src/args.h @@ -0,0 +1,8 @@ +#ifndef ARGS_H_INCLUDED +#define ARGS_H_INCLUDED + +#include "system.h" + +void ARG_parse (int argc, char **argv, int n, const cfg_t **list); + +#endif /* ARGS_H_INCLUDED */ diff --git a/src/config.c b/src/config.c index ed611c3..80c94bf 100644 --- a/src/config.c +++ b/src/config.c @@ -53,7 +53,7 @@ int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) { case Y_DWORD: *(dword*)p = atoi(value); break; case Y_STRING: strcpy(p, value); break; // TODO fix this security problem case Y_SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break; - case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 0 : 1; break; + case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 1 : 0; break; case Y_FILES: F_addwad(value); break; case Y_KEY: *(int*)p = I_string_to_key(value); break; default: assert(0); // unknown type -> something broken @@ -140,13 +140,20 @@ void CFG_close_iterator (void) { /* --- reader --- */ -int CFG_read_config (const char *name, const cfg_t *cfg) { +int CFG_read_config (const char *name, int n, const cfg_t **cfg) { + assert(name != NULL); + assert(n >= 0); + assert(cfg != NULL); + int i; char key[64]; char value[64]; assert(name != NULL); if (CFG_open_iterator(name)) { while (CFG_scan_iterator(key, 64, value, 64)) { - CFG_update_key(key, value, cfg); + i = 0; + while (i < n && CFG_update_key(key, value, cfg[i]) == 0) { + i++; + } } CFG_close_iterator(); return 1; @@ -172,21 +179,21 @@ static void CFG_write_key_value (FILE *f, const char *key, const char *value) { static int CFG_write_entry (FILE *f, const cfg_t *entry) { assert(f != NULL); assert(entry != NULL); - char buf[64]; + char buf[16]; const char *str; const char *key = entry->cfg; if (key != NULL) { switch (entry->t) { case Y_BYTE: - snprintf(buf, 64, "%i", *(byte*)entry->p); + snprintf(buf, 16, "%i", *(byte*)entry->p); CFG_write_key_value(f, key, buf); break; case Y_WORD: - snprintf(buf, 64, "%i", *(word*)entry->p); + snprintf(buf, 16, "%i", *(word*)entry->p); CFG_write_key_value(f, key, buf); break; case Y_DWORD: - snprintf(buf, 64, "%i", *(dword*)entry->p); + snprintf(buf, 16, "%i", *(dword*)entry->p); CFG_write_key_value(f, key, buf); break; case Y_STRING: diff --git a/src/config.h b/src/config.h index c666722..4290768 100644 --- a/src/config.h +++ b/src/config.h @@ -32,7 +32,7 @@ int CFG_open_iterator (const char *name); int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen); void CFG_close_iterator (void); -int CFG_read_config (const char *name, const cfg_t *cfg); +int CFG_read_config (const char *name, int n, const cfg_t **cfg); int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg); #endif /* CONFIG_H_INCLUDED */ diff --git a/src/gl/render.c b/src/gl/render.c index f0619ba..f68327a 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -80,11 +80,15 @@ typedef struct image { } image; /* Render Specific */ -static int SCRW = 320; -static int SCRH = 200; +static int SCRW; +static int SCRH; +static float screen_scale; static int screen_width = 320; static int screen_height = 200; -static float screen_scale = 1.0; +static byte screen_full = 0; +static int init_screen_width = 0; +static int init_screen_height = 0; +static byte init_screen_full = 0xFF; static rgb playpal[256]; static byte bright[256]; static GLuint lastTexture; @@ -153,6 +157,7 @@ static const char *anm[ANIT - 1][5] = { {"W73A_1", "W73A_2", NULL, NULL, NULL}, {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL} }; +static byte w_horiz = 1; static int max_wall_width; static int max_wall_height; static int max_textures; @@ -1862,6 +1867,7 @@ void R_set_videomode (int w, int h, int fullscreen) { } } Y_get_videomode(&screen_width, &screen_height); + screen_full = Y_get_fullscreen(); screen_scale = max(1, screen_width / 320); root = R_cache_new(); assert(root); @@ -1938,10 +1944,35 @@ const menu_t *R_menu (void) { return &video_menu; } +const cfg_t *R_args (void) { + static const cfg_t args[] = { + { "fullscr", &init_screen_full, Y_SW_ON }, + { "window", &init_screen_full, Y_SW_OFF }, + { "width", &init_screen_width, Y_DWORD }, + { "height", &init_screen_height, Y_DWORD }, + { NULL, NULL, 0 } // end + }; + return args; +} + +const cfg_t *R_conf (void) { + static const cfg_t conf[] = { + { "sky", &w_horiz, Y_SW_ON }, + { "fullscreen", &screen_full, Y_SW_ON }, + { "screen_width", &screen_width, Y_DWORD }, + { "screen_height", &screen_height, Y_DWORD }, + { NULL, NULL, 0 } // end + }; + return conf; +} + void R_init (void) { logo("R_init: intialize opengl render\n"); R_init_playpal(); - R_set_videomode(SCRW, SCRH, 0); + init_screen_width = init_screen_width > 0 ? init_screen_width : screen_width; + init_screen_height = init_screen_height > 0 ? init_screen_height : screen_height; + init_screen_full = init_screen_full != 0xFF ? init_screen_full : screen_full; + R_set_videomode(init_screen_width, init_screen_height, init_screen_full); } void R_done (void) { diff --git a/src/render.h b/src/render.h index 443b98f..0b6319c 100644 --- a/src/render.h +++ b/src/render.h @@ -1,9 +1,13 @@ #ifndef RENDER_H_INCLUDED #define RENDER_H_INCLUDED +#include "system.h" #include "menu.h" +const cfg_t *R_args (void); +const cfg_t *R_conf (void); const menu_t *R_menu (void); + void R_init (void); void R_draw (void); void R_done (void); diff --git a/src/sdl/main.c b/src/sdl/main.c index a440844..1c16dc3 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -40,6 +40,7 @@ #include "files.h" // F_startup F_addwad F_initwads F_allocres #include "config.h" // CFG_args CFG_load CFG_save +#include "args.h" // ARG_parse #include "memory.h" // M_startup #include "game.h" // G_init G_act #include "sound.h" // S_init S_done @@ -63,13 +64,8 @@ static const cfg_t arg[] = { // {"vga", &shot_vga, Y_SW_ON}, {"sndvol", &snd_vol, Y_WORD}, {"musvol", &mus_vol, Y_WORD}, -// {"fullscr", &fullscreen, Y_SW_ON}, -// {"window", &fullscreen, Y_SW_OFF}, {"mon", &nomon, Y_SW_OFF}, -// {"gamma", &gammaa, Y_DWORD}, {"warp", &_warp, Y_BYTE}, -// {"width", &SCRW, Y_DWORD}, -// {"height", &SCRH, Y_DWORD}, // {"config", NULL, cfg_file, Y_STRING}, {NULL, NULL, 0} // end }; @@ -78,11 +74,6 @@ static const cfg_t cfg[] = { // {"screenshot", &shot_vga, Y_SW_ON}, {"sound_volume", &snd_vol, Y_WORD}, {"music_volume", &mus_vol, Y_WORD}, -// {"fullscreen", &fullscreen, Y_SW_ON}, - {"sky", &w_horiz, Y_SW_ON}, -// {"gamma", &gammaa, Y_DWORD}, -// {"screen_width", &SCRW, Y_DWORD}, -// {"screen_height", &SCRH, Y_DWORD}, {"music_random", &music_random, Y_SW_ON}, {"music_time", &music_time, Y_DWORD}, {"music_fade", &music_fade, Y_DWORD}, @@ -108,34 +99,19 @@ static const cfg_t cfg[] = { }; static void CFG_args (int argc, char **argv) { - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] != 0) { - if (i + 1 >= argc) { - ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv[i]); - } else { - if (CFG_update_key(&argv[i][1], argv[i + 1], arg) != 0) { - ERR_failinit("CFG_args: unknown parameter %s\n", argv[i]); - } - i += 1; - } - } else { - ERR_failinit("CFG_args: something wrong here: %s\n", argv[i]); - } - } + const cfg_t *list[] = { arg, R_args() }; + ARG_parse(argc, argv, 2, list); } static void CFG_load (void) { - CFG_read_config("default.cfg", cfg); - CFG_read_config("doom2d.cfg", cfg); + const cfg_t *list[] = { cfg, R_conf() }; + CFG_read_config("default.cfg", 2, list); + CFG_read_config("doom2d.cfg", 2, list); } static void CFG_save (void) { - const cfg_t *list[] = { &cfg, NULL }; - CFG_update_config("doom2d.cfg", "doom2d.cfg", 1, list, "generated by doom2d, do not modify"); - //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file"); - //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); - //remove("doom2d.tmp"); + const cfg_t *list[] = { cfg, R_conf() }; + CFG_update_config("doom2d.cfg", "doom2d.cfg", 2, list, "generated by doom2d, do not modify"); } /* --- error.h --- */ diff --git a/src/sdl2/main.c b/src/sdl2/main.c index 37b4c26..f9eb159 100644 --- a/src/sdl2/main.c +++ b/src/sdl2/main.c @@ -21,6 +21,7 @@ #include "files.h" // F_startup F_addwad F_initwads F_allocres #include "config.h" // CFG_args CFG_load CFG_save +#include "args.h" // ARG_parse #include "memory.h" // M_startup #include "game.h" // G_init G_act #include "sound.h" // S_init S_done @@ -42,13 +43,8 @@ static const cfg_t arg[] = { // {"vga", &shot_vga, Y_SW_ON}, {"sndvol", &snd_vol, Y_WORD}, {"musvol", &mus_vol, Y_WORD}, -// {"fullscr", &fullscreen, Y_SW_ON}, -// {"window", &fullscreen, Y_SW_OFF}, {"mon", &nomon, Y_SW_OFF}, -// {"gamma", &gammaa, Y_DWORD}, {"warp", &_warp, Y_BYTE}, -// {"width", &SCRW, Y_DWORD}, -// {"height", &SCRH, Y_DWORD}, // {"config", NULL, cfg_file, Y_STRING}, {NULL, NULL, 0} // end }; @@ -57,11 +53,6 @@ static const cfg_t cfg[] = { // {"screenshot", &shot_vga, Y_SW_ON}, {"sound_volume", &snd_vol, Y_WORD}, {"music_volume", &mus_vol, Y_WORD}, -// {"fullscreen", &fullscreen, Y_SW_ON}, - {"sky", &w_horiz, Y_SW_ON}, -// {"gamma", &gammaa, Y_DWORD}, -// {"screen_width", &SCRW, Y_DWORD}, -// {"screen_height", &SCRH, Y_DWORD}, {"music_random", &music_random, Y_SW_ON}, {"music_time", &music_time, Y_DWORD}, {"music_fade", &music_fade, Y_DWORD}, @@ -87,34 +78,19 @@ static const cfg_t cfg[] = { }; static void CFG_args (int argc, char **argv) { - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] != 0) { - if (i + 1 >= argc) { - ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv[i]); - } else { - if (CFG_update_key(&argv[i][1], argv[i + 1], arg) != 0) { - ERR_failinit("CFG_args: unknown parameter %s\n", argv[i]); - } - i += 1; - } - } else { - ERR_failinit("CFG_args: something wrong here: %s\n", argv[i]); - } - } + const cfg_t *list[] = { arg, R_args() }; + ARG_parse(argc, argv, 2, list); } static void CFG_load (void) { - CFG_read_config("default.cfg", cfg); - CFG_read_config("doom2d.cfg", cfg); + const cfg_t *list[] = { cfg, R_conf() }; + CFG_read_config("default.cfg", 2, list); + CFG_read_config("doom2d.cfg", 2, list); } static void CFG_save (void) { - const cfg_t *list[] = { &cfg, NULL }; - CFG_update_config("doom2d.cfg", "doom2d.cfg", 1, list, "generated by doom2d, do not modify"); - //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file"); - //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); - //remove("doom2d.tmp"); + const cfg_t *list[] = { cfg, R_conf() }; + CFG_update_config("doom2d.cfg", "doom2d.cfg", 2, list, "generated by doom2d, do not modify"); } /* --- error.h --- */ @@ -580,6 +556,7 @@ static void step (void) { int main (int argc, char **argv) { char *pw; + CFG_args(argc, argv); logo("system: initialize SDL2\n"); if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) { logo("system: failed to init SDL2: %s\n", SDL_GetError()); @@ -607,6 +584,7 @@ int main (int argc, char **argv) { pl2.kp = KEY_E; srand(SDL_GetTicks()); F_startup(); + CFG_load(); #ifndef WIN32 pw = "/usr/share/doom2d-rembo/doom2d.wad"; #else @@ -617,8 +595,6 @@ int main (int argc, char **argv) { } else { F_addwad("doom2d.wad"); } - CFG_args(argc, argv); - CFG_load(); F_initwads(); M_startup(); F_allocres(); diff --git a/src/soft/render.c b/src/soft/render.c index 363a058..38c3ba7 100644 --- a/src/soft/render.c +++ b/src/soft/render.c @@ -75,8 +75,14 @@ static byte walani[256]; static int anih[ANIT][5]; static byte anic[ANIT]; static int max_textures; +static byte w_horiz = 1; static vgaimg *horiz; +static int init_screen_width = 0; +static int init_screen_height = 0; +static byte init_screen_full = 0xFF; +static int init_screen_gammaa = -1; + /* --- misc --- */ static void *Z_getspr (char n[4], int s, int d, char *dir) { @@ -1572,6 +1578,30 @@ const menu_t *R_menu (void) { return &video_menu; } +const cfg_t *R_args (void) { + static const cfg_t args[] = { + { "fullscr", &init_screen_full, Y_SW_ON }, + { "window", &init_screen_full, Y_SW_OFF }, + { "width", &init_screen_width, Y_DWORD }, + { "height", &init_screen_height, Y_DWORD }, + { "gamma", &init_screen_gammaa, Y_DWORD }, + { NULL, NULL, 0 } // end + }; + return args; +} + +const cfg_t *R_conf (void) { + static const cfg_t conf[] = { + { "sky", &w_horiz, Y_SW_ON }, + { "fullscreen", &fullscreen, Y_SW_ON }, + { "screen_width", &SCRW, Y_DWORD }, + { "screen_height", &SCRH, Y_DWORD }, + { "gamma", &gammaa, Y_DWORD }, + { NULL, NULL, 0 } // end + }; + return conf; +} + void R_init () { int i; logo("R_init: initialize software render\n"); @@ -1581,6 +1611,10 @@ void R_init () { } F_loadres(F_getresid("MIXMAP"), mixmap, 0, 0x10000); F_loadres(F_getresid("COLORMAP"), clrmap, 0, 256*12); + SCRW = init_screen_width > 0 ? init_screen_width : SCRW; + SCRH = init_screen_height > 0 ? init_screen_height : SCRH; + fullscreen = init_screen_full != 0xFF ? init_screen_full : fullscreen; + gammaa = init_screen_gammaa >= 0 ? init_screen_gammaa : gammaa; R_set_videomode(SCRW, SCRH, fullscreen); V_setrect(0, SCRW, 0, SCRH); V_clr(0, SCRW, 0, SCRH, 0); diff --git a/src/view.c b/src/view.c index 77985e1..08fde9f 100644 --- a/src/view.c +++ b/src/view.c @@ -41,7 +41,6 @@ #include "my.h" #include "render.h" -byte w_horiz=ON; int sky_type=1; dword walf[256]; byte walswp[256]; diff --git a/src/view.h b/src/view.h index 6b70fdd..200bf02 100644 --- a/src/view.h +++ b/src/view.h @@ -59,7 +59,6 @@ typedef struct { char t; } wall_t; -extern byte w_horiz; extern int sky_type; extern dword walf[256]; extern byte walswp[256];