*/
#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() */
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().";
}
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"))
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))
{
#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. */
{
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);
}
}
-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;
}
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]));
}
}