forked from beefviper/hode-vs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
322 lines (302 loc) · 7.5 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Heart of Darkness engine rewrite
* Copyright (C) 2009-2011 Gregory Montoir ([email protected])
*/
#if !defined(PSP) && !defined(WII)
#include <SDL.h>
#endif
#include <ctype.h>
#include <getopt.h>
#include <sys/stat.h>
#include "game.h"
#include "menu.h"
#include "mixer.h"
#include "paf.h"
#include "util.h"
#include "resource.h"
#include "system.h"
#include "video.h"
#include "compat.h"
#ifdef __SWITCH__
#include <switch.h>
#endif
static const char *_title = "Heart of Darkness";
#ifdef __vita__
static const char *_configIni = "ux0:data/hode/hode.ini";
#else
static const char *_configIni = "hode.ini";
#endif
static const char *_usage =
"hode - Heart of Darkness Interpreter\n"
"Usage: %s [OPTIONS]...\n"
" --datapath=PATH Path to data files (default '.')\n"
" --savepath=PATH Path to save files (default '.')\n"
" --level=NUM Start at level NUM\n"
" --checkpoint=NUM Start at checkpoint NUM\n"
;
static bool _fullscreen = false;
static bool _widescreen = false;
static const bool _runBenchmark = false;
static bool _runMenu = true;
static bool _displayLoadingScreen = true;
static void lockAudio(int flag) {
if (flag) {
g_system->lockAudio();
} else {
g_system->unlockAudio();
}
}
static void mixAudio(void *userdata, int16_t *buf, int len) {
((Game *)userdata)->mixAudio(buf, len);
}
static void setupAudio(Game *g) {
g->_mix._lock = lockAudio;
AudioCallback cb;
cb.proc = mixAudio;
cb.userdata = g;
g_system->startAudio(cb);
}
static const char *_defaultDataPath = ".";
static const char *_defaultSavePath = ".";
static const char *_levelNames[] = {
"rock",
"fort",
"pwr1",
"isld",
"lava",
"pwr2",
"lar1",
"lar2",
"dark",
0
};
static bool configBool(const char *value) {
return strcasecmp(value, "true") == 0 || (strlen(value) == 2 && (value[0] == 't' || value[0] == '1'));
}
static void handleConfigIni(Game *g, const char *section, const char *name, const char *value) {
// fprintf(stdout, "config.ini: section '%s' name '%s' value '%s'\n", section, name, value);
if (strcmp(section, "engine") == 0) {
if (strcmp(name, "disable_paf") == 0) {
if (!g->_paf->_skipCutscenes) { // .paf file not found
g->_paf->_skipCutscenes = configBool(value);
}
} else if (strcmp(name, "disable_mst") == 0) {
g->_mstDisabled = configBool(value);
} else if (strcmp(name, "disable_sss") == 0) {
g->_sssDisabled = configBool(value);
} else if (strcmp(name, "disable_menu") == 0) {
_runMenu = !configBool(value);
} else if (strcmp(name, "max_active_sounds") == 0) {
g->_playingSssObjectsMax = atoi(value);
} else if (strcmp(name, "difficulty") == 0) {
g->_difficulty = atoi(value);
} else if (strcmp(name, "frame_duration") == 0) {
g->_frameMs = g->_paf->_frameMs = atoi(value);
} else if (strcmp(name, "loading_screen") == 0) {
_displayLoadingScreen = configBool(value);
}
} else if (strcmp(section, "display") == 0) {
if (strcmp(name, "scale_factor") == 0) {
const int scale = atoi(value);
g_system->setScaler(0, scale);
} else if (strcmp(name, "scale_algorithm") == 0) {
g_system->setScaler(value, 0);
} else if (strcmp(name, "gamma") == 0) {
g_system->setGamma(atof(value));
} else if (strcmp(name, "fullscreen") == 0) {
_fullscreen = configBool(value);
} else if (strcmp(name, "widescreen") == 0) {
_widescreen = configBool(value);
}
}
}
static void readConfigIni(const char *filename, Game *g) {
FILE *fp = fopen(filename, "rb");
if (fp) {
char *section = 0;
char buf[256];
while (fgets(buf, sizeof(buf), fp)) {
if (buf[0] == '#') {
continue;
}
if (buf[0] == '[') {
char *p = strchr(&buf[1], ']');
if (p) {
*p = 0;
free(section);
section = strdup(&buf[1]);
}
continue;
}
char *p = strchr(buf, '=');
if (!p) {
continue;
}
*p++ = 0;
while (*p && isspace(*p)) {
++p;
}
if (*p) {
char *q = p + strlen(p) - 1;
while (q > p && isspace(*q)) {
*q-- = 0;
}
handleConfigIni(g, section, buf, p);
}
}
free(section);
fclose(fp);
}
}
int main(int argc, char *argv[]) {
#ifdef __SWITCH__
socketInitializeDefault();
nxlinkStdio();
#endif
#ifdef __vita__
const char *dataPath = "ux0:data/hode";
const char *savePath = "ux0:data/hode";
#else
char *dataPath = 0;
char *savePath = 0;
#endif
int level = 0;
int checkpoint = 0;
bool resume = true; // resume game from 'setup.cfg'
g_debugMask = 0; //kDebug_GAME | kDebug_RESOURCE | kDebug_SOUND | kDebug_MONSTER;
int cheats = 0;
#ifdef WII
System_earlyInit();
static const char *pathsWII[] = {
"sd:/hode",
"usb:/hode",
0
};
for (int i = 0; pathsWII[i]; ++i) {
struct stat st;
if (stat(pathsWII[i], &st) == 0 && S_ISDIR(st.st_mode)) {
dataPath = strdup(pathsWII[i]);
savePath = strdup(pathsWII[i]);
break;
}
}
#endif
if (System_hasCommandLine()) {
if (argc == 2) {
// data path as the only command line argument
struct stat st;
if (stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode)) {
dataPath = strdup(argv[1]);
}
}
while (1) {
static struct option options[] = {
{ "datapath", required_argument, 0, 1 },
{ "savepath", required_argument, 0, 2 },
{ "level", required_argument, 0, 3 },
{ "checkpoint", required_argument, 0, 4 },
{ "debug", required_argument, 0, 5 },
{ "cheats", required_argument, 0, 6 },
{ 0, 0, 0, 0 },
};
int index;
const int c = getopt_long(argc, argv, "", options, &index);
if (c == -1) {
break;
}
switch (c) {
case 1:
dataPath = strdup(optarg);
break;
case 2:
savePath = strdup(optarg);
break;
case 3:
if (optarg[0] >= '0' && optarg[0] <= '9') {
level = atoi(optarg);
} else {
for (int i = 0; _levelNames[i]; ++i) {
if (strcmp(_levelNames[i], optarg) == 0) {
level = i;
break;
}
}
}
resume = false;
break;
case 4:
checkpoint = atoi(optarg);
resume = false;
break;
case 5:
g_debugMask |= atoi(optarg);
break;
case 6:
cheats |= atoi(optarg);
break;
default:
fprintf(stdout, "%s\n", _usage);
return -1;
}
}
}
Game *g = new Game(dataPath ? dataPath : _defaultDataPath, savePath ? savePath : _defaultSavePath, cheats);
readConfigIni(_configIni, g);
if (_runBenchmark) {
g->benchmarkCpu();
}
// load setup.dat (PC) or setup.dax (PSX)
g->_res->loadSetupDat();
const bool isPsx = g->_res->_isPsx;
g_system->init(_title, Video::W, Video::H, _fullscreen, _widescreen, isPsx);
setupAudio(g);
if (isPsx) {
g->_video->initPsx();
}
if (_displayLoadingScreen) {
g->displayLoadingScreen();
}
do {
g->loadSetupCfg(resume);
if (_runMenu && resume) {
Menu *m = new Menu(g, g->_paf, g->_res, g->_video);
const bool runGame = m->mainLoop();
delete m;
if (!runGame) {
break;
}
}
bool levelChanged = false;
while (!g_system->inp.quit && level < kLvl_test) {
if (_displayLoadingScreen) {
g->displayLoadingScreen();
}
g->mainLoop(level, checkpoint, levelChanged);
// do not save progress when starting from a specific level checkpoint
if (resume) {
g->saveSetupCfg();
}
if (g->_res->_isDemo) {
break;
}
level = g->_currentLevel + 1;
checkpoint = 0;
levelChanged = true;
}
} while (!g_system->inp.quit && resume && !isPsx); // do not return to menu when starting from a specific level checkpoint
g_system->stopAudio();
g_system->destroy();
delete g;
#ifndef __vita__
free(dataPath);
free(savePath);
#endif
#ifdef WII
fatUnmount("sd:/");
fatUnmount("usb:/");
#endif
#ifdef __SWITCH__
socketExit();
#endif
return 0;
}