home · contact · privacy
Improved windows cleaning up: free win_meta, ncurses windows/pads.
[plomrogue] / src / rexit.c
1 /* rexit.c */
2
3 #include "rexit.h"
4 #include <stdlib.h> /* for exit(), free(), defines EXIT_SUCESS, EXIT_FAILURE */
5 #include <stdio.h> /* for printf(), perror() */
6 #include <stdint.h> /* for uint8_t */
7 #include <ncurses.h> /* for endwin() */
8 #include <errno.h> /* for errno */
9 #include "main.h" /* for World struct */
10 #include "map.h" /* for Map struct */
11 #include "keybindings.h" /* for KeysWinData, KeyBinding structs */
12 #include "command_db.h" /* for free_command_db() */
13 #include "windows.h" /* for Win struct, free_win() */
14 #include "map_objects.h" /* for free_item_defs(), free_monster_defs() */
15
16
17
18 /* The clean-up routine and the flag resource by which it decides what to do. */
19 static uint32_t cleanup_flags = 0x0000;
20 static void cleanup(struct World * world);
21
22
23
24 static void cleanup(struct World * world)
25 {
26     if (cleanup_flags & CLEANUP_NCURSES)
27     {
28         endwin();
29     }
30     if (cleanup_flags & CLEANUP_MAP)
31     {
32         free(world->map->cells);
33     }
34     if (cleanup_flags & CLEANUP_KEYBINDINGS)
35     {
36         uint16_t key;
37         for (key = 0; key <= world->keyswindata->max; key++)
38         {
39             free(world->keybindings[key].name);
40         }
41         free(world->keybindings);
42         free(world->keyswindata);
43     }
44     if (cleanup_flags & CLEANUP_LOG)
45     {
46         free(world->log);
47     }
48     if (cleanup_flags & CLEANUP_COMMAND_DB)
49     {
50         free_command_db(world);
51     }
52     if (cleanup_flags & CLEANUP_WIN_INFO)
53     {
54         free_win(world->wins.info);
55     }
56     if (cleanup_flags & CLEANUP_WIN_MAP)
57     {
58         free_win(world->wins.map);
59     }
60     if (cleanup_flags & CLEANUP_WIN_LOG)
61     {
62         free_win(world->wins.log);
63     }
64     if (cleanup_flags & CLEANUP_WIN_KEYS)
65     {
66         free_win(world->wins.keys);
67     }
68     if (cleanup_flags & CLEANUP_WIN_META)
69     {
70         delwin(world->wins.meta->padframe.curses_win);
71         free(world->wins.meta);
72     }
73     if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
74     {
75         free_item_defs(world->item_def);
76         free_monster_defs(world->monster_def);
77     }
78     if (cleanup_flags & CLEANUP_MAP_OBJECTS)
79     {
80         free_items(world->item);
81         free_monsters(world->monster);
82     }
83 }
84
85
86
87 extern void set_cleanup_flag(enum cleanup_flag flag)
88 {
89     cleanup_flags = cleanup_flags | flag;
90 }
91
92
93
94 extern void exit_game(struct World * world)
95 {
96     cleanup(world);
97     exit(EXIT_SUCCESS);
98 }
99
100
101
102 extern void exit_err(uint8_t err, struct World * world, char * msg)
103 {
104     if (0 == err)
105     {
106         return;
107     }
108     cleanup(world);
109     if (NULL == msg)
110     {
111         msg = "Details unknown.";
112     }
113     printf("Aborted PlomRogue due to error. %s\nInternal error code: %d\n",
114            msg, err);
115     if (0 != errno)
116     {
117         perror("errno states");
118     }
119     exit(EXIT_FAILURE);
120 }