From: Christian Heller Date: Sun, 1 Sep 2013 02:05:13 +0000 (+0200) Subject: Heavily re-structured initialization of windows and how individual windows are identi... X-Git-Tag: tce~983 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/template?a=commitdiff_plain;h=db346802082f53faf4d49f234c0ba2cf86221d1e;p=plomrogue Heavily re-structured initialization of windows and how individual windows are identified to facilitate future plans for editing window configurations. --- diff --git a/config/windows/Info b/config/windows/Info deleted file mode 100644 index a8759ad..0000000 --- a/config/windows/Info +++ /dev/null @@ -1,3 +0,0 @@ -Info -3 -20 diff --git a/config/windows/Keys b/config/windows/Keys deleted file mode 100644 index a719bf3..0000000 --- a/config/windows/Keys +++ /dev/null @@ -1,3 +0,0 @@ -Keys -0 -29 diff --git a/config/windows/Log b/config/windows/Log deleted file mode 100644 index feef14f..0000000 --- a/config/windows/Log +++ /dev/null @@ -1,3 +0,0 @@ -Log --4 -20 diff --git a/config/windows/Map b/config/windows/Map deleted file mode 100644 index a403ed8..0000000 --- a/config/windows/Map +++ /dev/null @@ -1,3 +0,0 @@ -Map -0 --51 diff --git a/config/windows/Win_i b/config/windows/Win_i new file mode 100644 index 0000000..a8759ad --- /dev/null +++ b/config/windows/Win_i @@ -0,0 +1,3 @@ +Info +3 +20 diff --git a/config/windows/Win_k b/config/windows/Win_k new file mode 100644 index 0000000..a719bf3 --- /dev/null +++ b/config/windows/Win_k @@ -0,0 +1,3 @@ +Keys +0 +29 diff --git a/config/windows/Win_l b/config/windows/Win_l new file mode 100644 index 0000000..feef14f --- /dev/null +++ b/config/windows/Win_l @@ -0,0 +1,3 @@ +Log +-4 +20 diff --git a/config/windows/Win_m b/config/windows/Win_m new file mode 100644 index 0000000..a403ed8 --- /dev/null +++ b/config/windows/Win_m @@ -0,0 +1,3 @@ +Map +0 +-51 diff --git a/src/control.c b/src/control.c index b9ebbe1..0b601fe 100644 --- a/src/control.c +++ b/src/control.c @@ -11,7 +11,7 @@ #include "map.h" /* for map_scroll(), map_center_player(), dir enum */ #include "main.h" /* for World struct */ #include "rexit.h" /* for exit_err() */ -#include "wincontrol.h" /* for scroll_pad(), toggle_window(), +#include "wincontrol.h" /* for scroll_pad(), toggle_window(), get_win_by_id(), * growshrink_active_window(), reload_win_config() */ #include "map_object_actions.h" /* for player_wait(), move_player() */ @@ -78,11 +78,11 @@ extern uint8_t player_control(int key, struct World * world) extern uint8_t meta_control(int key, struct World * world) { - struct WinMeta * win_meta = world->wins.meta; - struct Win * win_keys = world->wins.keys; - struct Win * win_map = world->wins.map; - struct Win * win_info = world->wins.info; - struct Win * win_log = world->wins.log; + struct WinMeta * win_meta = world->wmeta; + struct Win * win_keys = get_win_by_id(world, 'k'); + struct Win * win_map = get_win_by_id(world, 'm'); + struct Win * win_info = get_win_by_id(world, 'i'); + struct Win * win_log = get_win_by_id(world, 'l'); char * err_toggle = "Trouble with toggle_window() in meta_keys()."; char * err_shift = "Trouble with shift_active_win() in meta_keys()."; char * err_resize = "Trouble with growshrink_active_window() in " diff --git a/src/main.c b/src/main.c index 0041b1e..bf0caa9 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,9 @@ */ #include "map.h" /* for struct Map, init_map() */ #include "misc.h" /* for update_log(), find_passable_pos(), save_game() */ -#include "wincontrol.h" /* for toggle_window() */ +#include "wincontrol.h" /* for create_winconfs(), init_winconfs(), init_wins(), + * sorted_wintoggle() + */ #include "rrand.h" /* for rrand(), rrand_seed() */ #include "rexit.h" /* for exit_game() */ #include "control.h" /* for meta_control() */ @@ -203,16 +205,13 @@ int main(int argc, char *argv[]) init_keybindings(&world); set_cleanup_flag(CLEANUP_KEYBINDINGS); char * err_winmem = "Trouble with init_win_meta() in main ()."; - exit_err(init_win_meta(screen, &world.wins.meta), &world, err_winmem); + exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem); set_cleanup_flag(CLEANUP_WIN_META); - world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win); - set_cleanup_flag(CLEANUP_WIN_KEYS); - world.wins.info = init_win_from_file(&world, "Info", draw_info_win); - set_cleanup_flag(CLEANUP_WIN_INFO); - world.wins.log = init_win_from_file(&world, "Log", draw_log_win); - set_cleanup_flag(CLEANUP_WIN_LOG); - world.wins.map = init_win_from_file(&world, "Map", draw_map_win); - set_cleanup_flag(CLEANUP_WIN_MAP); + create_winconfs(&world); + init_winconfs(&world); + set_cleanup_flag(CLEANUP_WINCONFS); + init_wins(&world); + set_cleanup_flag(CLEANUP_WINS); sorted_wintoggle(&world); err_winmem = "Trouble with draw_all_wins() in main()."; @@ -235,7 +234,7 @@ int main(int argc, char *argv[]) } while (1) { - draw_all_wins(world.wins.meta); + draw_all_wins(world.wmeta); key = getch(); if ( EOF != action && key == get_action_key(world.keybindings, "wait")) @@ -261,7 +260,7 @@ int main(int argc, char *argv[]) while (1) { save_game(&world); - draw_all_wins(world.wins.meta); + draw_all_wins(world.wmeta); key = getch(); if (0 != player.hitpoints && 0 == player_control(key, &world)) { diff --git a/src/main.h b/src/main.h index 0bd2cd8..ee9fc90 100644 --- a/src/main.h +++ b/src/main.h @@ -10,6 +10,7 @@ #include /* for uint32_t*/ struct WinMeta; +struct WinConf; struct Win; struct KeyBinding; struct KeysWinData; @@ -19,17 +20,6 @@ struct MonsterDef; -struct Wins -{ - struct WinMeta * meta; - struct Win * keys; - struct Win * info; - struct Win * log; - struct Win * map; -}; - - - struct World { char interactive; /* 1: playing; 0: record playback. */ @@ -46,7 +36,8 @@ struct World struct Monster * monster; /* Pointer to the monsters' data. */ struct Player * player; /* Pointer to the player data. */ struct CommandDB * cmd_db; /* Pointer to the command database. */ - struct Wins wins; /* Pointers to WinMeta, individual Wins.*/ + struct WinMeta * wmeta; /* Pointer to window manager's WinMeta. */ + struct WinConf * winconfs; /* Pointer to windows' configurations. */ }; diff --git a/src/rexit.c b/src/rexit.c index 5640d89..e39a9fe 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -12,7 +12,7 @@ #include "command_db.h" /* for free_command_db() */ #include "windows.h" /* for Win struct, free_win(), free_winmeta() */ #include "map_objects.h" /* for free_item_defs(), free_monster_defs() */ - +#include "wincontrol.h" /* for get_win_by_id(), free_winconfs(), free_wins() */ /* The clean-up routine and the flag resource by which it decides what to do. */ @@ -49,35 +49,27 @@ static void cleanup(struct World * world) { free_command_db(world); } - if (cleanup_flags & CLEANUP_WIN_INFO) + if (cleanup_flags & CLEANUP_MAP_OBJECTS) { - free_win(world->wins.info); + free_items(world->item); + free_monsters(world->monster); } - if (cleanup_flags & CLEANUP_WIN_MAP) + if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS) { - free_win(world->wins.map); + free_item_defs(world->item_def); + free_monster_defs(world->monster_def); } - if (cleanup_flags & CLEANUP_WIN_LOG) + if (cleanup_flags & CLEANUP_WINS) { - free_win(world->wins.log); + free_wins(world); } - if (cleanup_flags & CLEANUP_WIN_KEYS) + if (cleanup_flags & CLEANUP_WINCONFS) { - free_win(world->wins.keys); + free_winconfs(world); } if (cleanup_flags & CLEANUP_WIN_META) { - free_winmeta(world->wins.meta); - } - if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS) - { - free_item_defs(world->item_def); - free_monster_defs(world->monster_def); - } - if (cleanup_flags & CLEANUP_MAP_OBJECTS) - { - free_items(world->item); - free_monsters(world->monster); + free_winmeta(world->wmeta); } } diff --git a/src/rexit.h b/src/rexit.h index a6b48f9..459fd29 100644 --- a/src/rexit.h +++ b/src/rexit.h @@ -28,11 +28,9 @@ enum cleanup_flag CLEANUP_COMMAND_DB = 0x0010, CLEANUP_MAP_OBJECTS = 0x0020, CLEANUP_MAP_OBJECT_DEFS = 0x0040, - CLEANUP_WIN_INFO = 0x0080, - CLEANUP_WIN_LOG = 0x0100, - CLEANUP_WIN_MAP = 0x0200, - CLEANUP_WIN_KEYS = 0x0400, - CLEANUP_WIN_META = 0x0800 + CLEANUP_WIN_META = 0x0080, + CLEANUP_WINCONFS = 0x0100, + CLEANUP_WINS = 0x0200 }; extern void set_cleanup_flag(enum cleanup_flag flag); diff --git a/src/wincontrol.c b/src/wincontrol.c index 0417852..f20f37a 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -19,62 +19,166 @@ -extern void reload_win_config(struct World * world) -{ - while (0 != world->wins.meta->active) - { - suspend_win(world->wins.meta, world->wins.meta->active); - } - free_win(world->wins.info); - free_win(world->wins.keys); - free_win(world->wins.map); - free_win(world->wins.log); - world->wins.keys = init_win_from_file(world, "Keys", draw_keys_win); - world->wins.info = init_win_from_file(world, "Info", draw_info_win); - world->wins.log = init_win_from_file(world, "Log", draw_log_win); - world->wins.map = init_win_from_file(world, "Map", draw_map_win); - sorted_wintoggle(world); -} +/* Initialize Winconf of "id" from appropriate config file.*/ +static void init_winconf_from_file(struct World * world, char id); + +/* Wrapper around init_win() called with values from appropriate Winconf. */ +static void init_win_from_winconf(struct World * world, char id); -extern struct Win * init_win_from_file(struct World * world, char * w_name, - void (* f) (struct Win *)) +static void init_winconf_from_file(struct World * world, char id) { char * err_m = "Trouble in init_win_from_file() with malloc()."; - char * prefix = "config/windows/"; - uint8_t size = strlen(prefix) + strlen(w_name) + 1; + char * prefix = "config/windows/Win_"; + uint8_t size = strlen(prefix) + 2; char * path = malloc(size); exit_err(NULL == path, world, err_m); - sprintf(path, "%s%s", prefix, w_name); + sprintf(path, "%s_", prefix); + path[size - 2] = id; char * err = "Trouble in init_win_from_file() with fopen()."; FILE * file = fopen(path, "r"); - free(path); exit_err(NULL == file, world, err); + free(path); + + struct WinConf * winconf = get_winconf_by_id(world, id); uint16_t linemax; err = "Trouble in init_win_from_file() with textfile_sizes()."; exit_err(textfile_sizes(file, &linemax, NULL), world, err); char * line = malloc(linemax); exit_err(NULL == line, world, err_m); err = "Trouble in init_win_from_file() with fgets()."; + exit_err(NULL == fgets(line, linemax, file), world, err); - char * title = malloc(strlen(line)); - exit_err(NULL == title, world, err_m); - memcpy(title, line, strlen(line) - 1); /* Eliminate newline char at end */ - title[strlen(line) - 1] = '\0'; /* of string. */ + winconf->title = malloc(strlen(line)); + exit_err(NULL == winconf->title, world, err_m); + memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */ + winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */ exit_err(NULL == fgets(line, linemax, file), world, err); - int16_t height = atoi(line); + winconf->height = atoi(line); exit_err(NULL == fgets(line, linemax, file), world, err); - int16_t width = atoi(line); + winconf->width = atoi(line); + free(line); err = "Trouble in init_win_from_file() with fclose()."; exit_err(fclose(file), world, err); - struct WinMeta * wmeta = world->wins.meta; - struct Win * wp; - err = "Trouble in init_win_from_file() with init_win()."; - exit_err(init_win(wmeta, &wp, title, height, width, world, f), world, err); - free(title); - return wp; +} + + + +static void init_win_from_winconf(struct World * world, char id) +{ + struct WinConf * winconf = get_winconf_by_id(world, id); + char * err = "Trouble in init_win_from_file() with init_win()."; + exit_err(init_win(world->wmeta, &winconf->win, winconf->title, + winconf->height, winconf->width, world, winconf->draw), + world, err); +} + + + +extern void create_winconfs(struct World * world) +{ + char * err = "Trouble with malloc() in init_winconfs()."; + struct WinConf * winconfs = malloc(4 * sizeof(struct WinConf)); + exit_err(NULL == winconfs, world, err); + winconfs[0].id = 'i'; + winconfs[0].draw = draw_info_win; + winconfs[1].id = 'k'; + winconfs[1].draw = draw_keys_win; + winconfs[2].id = 'l'; + winconfs[2].draw = draw_log_win; + winconfs[3].id = 'm'; + winconfs[3].draw = draw_map_win; + world->winconfs = winconfs; +} + + + +extern void init_winconfs(struct World * world) +{ + init_winconf_from_file(world, 'i'); + init_winconf_from_file(world, 'k'); + init_winconf_from_file(world, 'l'); + init_winconf_from_file(world, 'm'); +} + + + +extern void free_winconf(struct World * world, char id) +{ + struct WinConf * wc = get_winconf_by_id(world, id); + free(wc->title); +} + + + +extern void free_winconfs(struct World * world) +{ + free_winconf(world, 'i'); + free_winconf(world, 'k'); + free_winconf(world, 'l'); + free_winconf(world, 'm'); + free(world->winconfs); +} + + + +extern void init_wins(struct World * world) +{ + init_win_from_winconf(world, 'i'); + init_win_from_winconf(world, 'k'); + init_win_from_winconf(world, 'l'); + init_win_from_winconf(world, 'm'); +} + + + +extern void free_wins(struct World * world) +{ + free_win(get_win_by_id(world, 'i')); + free_win(get_win_by_id(world, 'k')); + free_win(get_win_by_id(world, 'l')); + free_win(get_win_by_id(world, 'm')); +} + + + +extern void reload_win_config(struct World * world) +{ + while (0 != world->wmeta->active) + { + suspend_win(world->wmeta, world->wmeta->active); + } + free_wins(world); + free_winconfs(world); + create_winconfs(world); + init_winconfs(world); + init_wins(world); + sorted_wintoggle(world); +} + + + +extern struct WinConf * get_winconf_by_id(struct World * world, char id) +{ + uint8_t i = 0; + while (1) + { + if (id == world->winconfs[i].id) + { + return &world->winconfs[i]; + } + i++; + } +} + + + +extern struct Win * get_win_by_id(struct World * world, char id) +{ + struct WinConf * wc = get_winconf_by_id(world, id); + return wc->win; } @@ -92,28 +196,10 @@ extern void sorted_wintoggle(struct World * world) exit_err(NULL == fgets(win_order, linemax, file), world, err); err = "Trouble in sorted_wintoggle() with fclose()."; exit_err(fclose(file), world, err); - - char c; uint8_t i = 0; - for (; i < linemax; i++) + for (; i < linemax - 2; i++) { - c = win_order[i]; - if ('k' == c) - { - toggle_window(world->wins.meta, world->wins.keys); - } - else if ('m' == c) - { - toggle_window(world->wins.meta, world->wins.map); - } - else if ('i' == c) - { - toggle_window(world->wins.meta, world->wins.info); - } - else if ('l' == c) - { - toggle_window(world->wins.meta, world->wins.log); - } + toggle_window(world->wmeta, get_win_by_id(world, win_order[i])); } } diff --git a/src/wincontrol.h b/src/wincontrol.h index 404ae1b..5c9c4b5 100644 --- a/src/wincontrol.h +++ b/src/wincontrol.h @@ -16,18 +16,36 @@ struct World; +/* Puts an identifier on each window, interfaces to its config file data. */ +struct WinConf +{ + char id; + struct Win * win; + char * title; + int16_t height; + int16_t width; + void (* draw) (struct Win *); +}; + + + +/* Create/initialize (from config files)/free Winconf / Win structs. */ +extern void create_winconfs(struct World * world); +extern void init_winconfs(struct World * world); +extern void free_winconfs(struct World * world); +extern void init_wins(struct World * world); +extern void free_wins(struct World * world); + + + /* Reload windows in order and sizes defined in win config. */ extern void reload_win_config(struct World * world); -/* Wrapper around init_win() that reads the desired window size and title from a - * file at the path prefixing the provided win name "w_name" with - * "config/windows/". "f"() is the window drawing function (Win._draw()). - */ -extern struct Win * init_win_from_file(struct World * world, char * w_name, - void (* f) (struct Win *)); - +/* Get WinConf identified by winconf->id == "id", or that winconf's ->win. */ +extern struct WinConf * get_winconf_by_id(struct World * world, char id); +extern struct Win * get_win_by_id(struct World * world, char id);