forked from CleverRaven/Cataclysm-DDA
/
main.cpp
144 lines (126 loc) · 3.01 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Main Loop for cataclysm
* Linux only I guess
* But maybe not
* Who knows
*/
#include "cursesdef.h"
#include <ctime>
#include "game.h"
#include "color.h"
#include "options.h"
#include "mapbuffer.h"
#include "debug.h"
#include <sys/stat.h>
#include <cstdlib>
#include <signal.h>
#include "translations.h"
#if (defined OSX_SDL_FW)
#include "SDL.h"
#elif (defined OSX_SDL_LIBS)
#include "SDL/SDL.h"
#endif
void exit_handler(int s);
#ifdef USE_WINMAIN
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int argc = __argc;
char **argv = __argv;
#else
int main(int argc, char *argv[])
{
#endif
#ifdef ENABLE_LOGGING
setupDebug();
#endif
int seed = time(NULL);
// set locale to system default
setlocale(LC_ALL, "");
bindtextdomain("cataclysm-dda", "lang/mo");
bind_textdomain_codeset("cataclysm-dda", "UTF-8");
textdomain("cataclysm-dda");
//args: world seeding only.
argc--; argv++;
while (argc){
if(std::string(argv[0]) == "--seed"){
argc--; argv++;
if(argc){
seed = djb2_hash((unsigned char*)argv[0]);
argc--; argv++;
}
}
else // ignore unknown args.
argc--; argv++;
}
// ncurses stuff
load_options(); // For getting size options
initscr(); // Initialize ncurses
noecho(); // Don't echo keypresses
cbreak(); // C-style breaks (e.g. ^C to SIGINT)
keypad(stdscr, true); // Numpad is numbers
init_colors(); // See color.cpp
// curs_set(0); // Invisible cursor
set_escdelay(10); // Make escape actually responsive
std::srand(seed);
bool quit_game = false;
bool delete_world = false;
g = new game;
if(g->game_error())
exit_handler(-999);
g->init_ui();
MAPBUFFER.set_game(g);
g->load_artifacts(); //artifacts have to be loaded before any items are created
if(g->game_error())
exit_handler(-999);
MAPBUFFER.load();
curs_set(0); // Invisible cursor here, because MAPBUFFER.load() is crash-prone
#if (!(defined _WIN32 || defined WINDOWS))
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = exit_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
#endif
do {
g->setup();
while (!g->do_turn()) ;
if (g->uquit == QUIT_DELETE_WORLD)
delete_world = true;
if (g->game_quit() || g->game_error())
quit_game = true;
} while (!quit_game);
if (delete_world)
{
g->delete_save();
} else {
MAPBUFFER.save_if_dirty();
}
exit_handler(-999);
return 0;
}
void exit_handler(int s) {
bool bExit = false;
if (s == 2) {
if (query_yn(_("Really Quit? All unsaved changes will be lost."))) {
bExit = true;
}
} else if (s == -999) {
bExit = true;
} else {
//query_yn(g, "Signal received: %d", s);
bExit = true;
}
if (bExit) {
erase(); // Clear screen
endwin(); // End ncurses
#if (defined _WIN32 || defined WINDOWS)
system("cls"); // Tell the terminal to clear itself
system("color 07");
#else
system("clear"); // Tell the terminal to clear itself
#endif
if(g != NULL)
if(g->game_error())
exit(1);
exit(0);
}
}