home · contact · privacy
Made game exiting and cleaning up more flexible. Provided so far unused exit-on-error...
[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() */
6 #include <ncurses.h> /* for endwin() */
7 #include "main.h" /* for World struct */
8 #include "map.h" /* for Map struct */
9 #include "keybindings.h" /* for KeysWinData, KeyBinding structs */
10
11
12
13 /* The clean-up routine and the flag resource by which it decides what to do. */
14 static unsigned char cleanup_flags = 0x00;
15 static void cleanup(struct World * world);
16
17
18
19 static void cleanup(struct World * world)
20 {
21     if (cleanup_flags & CLEANUP_NCURSES)
22     {
23         endwin();
24     }
25     if (cleanup_flags & CLEANUP_MAP)
26     {
27         free(world->map->cells);
28     }
29     if (cleanup_flags & CLEANUP_KEYBINDINGS)
30     {
31         uint16_t key;
32         for (key = 0; key <= world->keyswindata->max; key++)
33         {
34             free(world->keybindings[key].name);
35         }
36         free(world->keybindings);
37         free(world->keyswindata);
38     }
39     if (cleanup_flags & CLEANUP_LOG)
40     {
41         free(world->log);
42     }
43 }
44
45
46
47 extern void set_cleanup_flag(enum cleanup_flag flag)
48 {
49     cleanup_flags = cleanup_flags | flag;
50 }
51
52
53
54 extern void exit_game(struct World * world)
55 {
56     cleanup(world);
57     exit(EXIT_SUCCESS);
58 }
59
60
61
62 extern void exit_err(struct World * world, char * msg)
63 {
64     cleanup(world);
65     printf(msg);
66     exit(EXIT_FAILURE);
67 }