| 1 |
/** |
| 2 |
* System initialization, common to FreeDink and FreeDinkEdit |
| 3 |
|
| 4 |
* Copyright (C) 2007, 2008, 2009, 2010, 2014, 2015 Sylvain Beucler |
| 5 |
|
| 6 |
* This file is part of GNU FreeDink |
| 7 |
|
| 8 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License as |
| 10 |
* published by the Free Software Foundation; either version 3 of the |
| 11 |
* License, or (at your option) any later version. |
| 12 |
|
| 13 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
|
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see |
| 20 |
* <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <math.h> |
| 30 |
#include <getopt.h> |
| 31 |
#include <unistd.h> /* chdir */ |
| 32 |
#include <xalloc.h> |
| 33 |
|
| 34 |
#include <locale.h> |
| 35 |
#include "gettext.h" |
| 36 |
#define _(String) gettext (String) |
| 37 |
|
| 38 |
#include "app.h" |
| 39 |
#include "SDL.h" |
| 40 |
#include "dinkini.h" |
| 41 |
#include "DMod.h" |
| 42 |
#include "hardness_tiles.h" |
| 43 |
#include "live_screen.h" |
| 44 |
#include "gfx.h" |
| 45 |
#include "sfx.h" |
| 46 |
#include "input.h" |
| 47 |
#include "paths.h" |
| 48 |
#include "log.h" |
| 49 |
|
| 50 |
#ifdef __ANDROID__ |
| 51 |
#include <string.h> /* strerror */ |
| 52 |
#include <errno.h> |
| 53 |
#include <jni.h> |
| 54 |
extern char **environ; |
| 55 |
|
| 56 |
static void log_environment() { |
| 57 |
for (char **pair = environ; *pair; pair++) |
| 58 |
log_info("%s", *pair); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
// No default locale on Android, need to get it manually (T-T) |
| 63 |
// TODO: submit me for inclusion in SDL2 |
| 64 |
// TODO: ScummVM uses a simpler technique, combine %s_%s: |
| 65 |
// __system_property_get("persist.sys.language", buf1) |
| 66 |
// __system_property_get("persist.sys.country", buf2) |
| 67 |
static void android_set_LANG() { |
| 68 |
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); |
| 69 |
jmethodID mid; |
| 70 |
|
| 71 |
// locale = Locale.getDefault() |
| 72 |
jclass localeClass = env->FindClass("java/util/Locale"); |
| 73 |
mid = env->GetStaticMethodID(localeClass, "getDefault", |
| 74 |
"()Ljava/util/Locale;"); |
| 75 |
jobject locale = env->CallStaticObjectMethod(localeClass, mid); |
| 76 |
|
| 77 |
// locale.toString() |
| 78 |
mid = env->GetMethodID(localeClass, "toString", |
| 79 |
"()Ljava/lang/String;"); |
| 80 |
jstring toString = (jstring)env->CallObjectMethod(locale, mid); |
| 81 |
const char* lang = env->GetStringUTFChars(toString, NULL); |
| 82 |
|
| 83 |
SDL_setenv("LANG", lang, 1); |
| 84 |
env->ReleaseStringUTFChars(toString, lang); |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
/** |
| 89 |
* What to do if running out of memory |
| 90 |
*/ |
| 91 |
void xalloc_die (void) { |
| 92 |
fprintf(stderr, "Memory exhausted!"); |
| 93 |
abort(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* chdir to ease locating resources |
| 98 |
*/ |
| 99 |
static void app_chdir() { |
| 100 |
#ifdef __ANDROID__ |
| 101 |
/* SD Card - SDL_AndroidGetExternalStoragePath() |
| 102 |
== /storage/sdcard0/Android/data/org.freedink/files */ |
| 103 |
log_info("Android external storage: %s\n", SDL_AndroidGetExternalStoragePath()); |
| 104 |
if (SDL_AndroidGetExternalStoragePath() == NULL) |
| 105 |
log_error("Could not get external storage path '%s': %s'", |
| 106 |
SDL_AndroidGetExternalStoragePath(), |
| 107 |
SDL_GetError()); |
| 108 |
int state = SDL_AndroidGetExternalStorageState(); |
| 109 |
log_info("- read : %s\n", (state&SDL_ANDROID_EXTERNAL_STORAGE_READ) ? "yes":"no"); |
| 110 |
log_info("- write: %s\n", (state&SDL_ANDROID_EXTERNAL_STORAGE_WRITE) ? "yes":"no"); |
| 111 |
|
| 112 |
/* SDL_AndroidGetInternalStoragePath() == /data/data/org.freedink/files/ */ |
| 113 |
log_info("Android internal storage: %s\n", SDL_AndroidGetInternalStoragePath()); |
| 114 |
|
| 115 |
if (chdir(SDL_AndroidGetExternalStoragePath()) < 0) |
| 116 |
log_error("Could not chdir to '%s': %s'", |
| 117 |
SDL_AndroidGetExternalStoragePath(), |
| 118 |
strerror(errno)); |
| 119 |
|
| 120 |
android_set_LANG(); |
| 121 |
log_environment(); |
| 122 |
#elif defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ |
| 123 |
/* .exe's directory */ |
| 124 |
log_info("Woe exe dir: %s\n", SDL_GetBasePath()); |
| 125 |
if (chdir(SDL_GetBasePath()) < 0) |
| 126 |
log_error("Could not chdir to '%s': %s'", |
| 127 |
SDL_GetBasePath(), |
| 128 |
strerror(errno)); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Prints the version on the standard ouput. Based on the homonymous |
| 134 |
* function from ratpoison |
| 135 |
*/ |
| 136 |
void App::print_version() { |
| 137 |
printf("%s %s\n", PACKAGE_NAME, VERSION); |
| 138 |
printf("FreeDink is free software, and you are welcome to redistribute it\n"); |
| 139 |
printf("under certain conditions; see the GNU GPL for details.\n"); |
| 140 |
printf("http://gnu.org/licenses/gpl.html\n"); |
| 141 |
printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
| 142 |
exit(EXIT_SUCCESS); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Prints the version on the standard ouput. Based on the homonymous |
| 148 |
* function from ratpoison |
| 149 |
*/ |
| 150 |
void App::print_help(int argc, char *argv[]) |
| 151 |
{ |
| 152 |
printf(_("Usage: %s [OPTIONS]...\n"), argv[0]); |
| 153 |
printf("%s\n", description.c_str()); |
| 154 |
printf("\n"); |
| 155 |
/* TODO: Display the default configuration here */ |
| 156 |
printf(_(" -h, --help Display this help screen\n")); |
| 157 |
printf(_(" -v, --version Display the version\n")); |
| 158 |
printf("\n"); |
| 159 |
printf(_(" -g, --game <dir> Specify a DMod directory\n")); |
| 160 |
printf(_(" -r, --refdir <dir> Specify base directory for dink/graphics, D-Mods, etc.\n")); |
| 161 |
printf("\n"); |
| 162 |
printf(_(" -d, --debug Explain what is being done\n")); |
| 163 |
printf(_(" -i, --noini Do not attempt to write dinksmallwood.ini\n")); |
| 164 |
printf(_(" -j, --nojoy Do not attempt to use joystick\n")); |
| 165 |
printf(_(" -s, --nosound Do not play sound\n")); |
| 166 |
printf(_(" -t, --truecolor Allow more colours (for recent D-Mod graphics)\n")); |
| 167 |
printf(_(" -w, --window Use windowed mode instead of screen mode\n")); |
| 168 |
printf(_(" -7, --v1.07 Enable v1.07 compatibility mode\n")); |
| 169 |
printf(_(" -S, --software-rendering Don't use OpenGL\n")); |
| 170 |
printf("\n"); |
| 171 |
// Tentative option names: |
| 172 |
//printf(_(" --dinkgl Full OpenGL acceleration\n")); |
| 173 |
//printf(_(" --soft Semi-accelerated rendering with software surfaces\n")); |
| 174 |
|
| 175 |
/* printf ("Type 'info freedink' for more information\n"); */ |
| 176 |
|
| 177 |
/* TRANSLATORS: The placeholder indicates the bug-reporting address |
| 178 |
for this package. Please add _another line_ saying "Report |
| 179 |
translation bugs to <...>\n" with the address for translation |
| 180 |
bugs (typically your translation team's web or email |
| 181 |
address). */ |
| 182 |
printf(_("Report bugs to %s.\n"), PACKAGE_BUGREPORT); |
| 183 |
|
| 184 |
exit(EXIT_SUCCESS); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Initialize gettext as early as possible |
| 190 |
*/ |
| 191 |
App::App() : |
| 192 |
splash_path(NULL), |
| 193 |
g_b_no_write_ini(0), |
| 194 |
opt_version(108), |
| 195 |
dinkini_playmidi(false), |
| 196 |
dinkgl(true), windowed(false) { |
| 197 |
/* chdir to resource paths under woe&android */ |
| 198 |
app_chdir(); |
| 199 |
|
| 200 |
/* Reset debug levels */ |
| 201 |
log_debug_off(); |
| 202 |
|
| 203 |
/** i18n **/ |
| 204 |
/* Only using LC_MESSAGES because LC_CTYPE (part of LC_ALL) may |
| 205 |
bring locale-specific behavior in the DinkC parsers. If that's a |
| 206 |
problem we may use some gnulib modules |
| 207 |
(cf. (gettext.info.gz)Triggering) */ |
| 208 |
/* Ex. with scanf("%f"...): |
| 209 |
LANG=C 1.1 -> 1.100000 |
| 210 |
LANG=C 1,1 -> 1.000000 |
| 211 |
LANG=fr_FR.UTF-8 1.1 -> 1,000000 |
| 212 |
LANG=fr_FR.UTF-8 1,1 -> 1,100000 */ |
| 213 |
/* setlocale (LC_ALL, ""); */ |
| 214 |
// log_info("LANG=%s", getenv("LANG")); // no debug log yet |
| 215 |
setlocale(LC_MESSAGES, ""); |
| 216 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 217 |
bindtextdomain(PACKAGE "-gnulib", LOCALEDIR); |
| 218 |
textdomain(PACKAGE); |
| 219 |
|
| 220 |
/* SDL can display messages in either ASCII or UTF-8. Thus we need |
| 221 |
gettext to output translations in UTF-8. */ |
| 222 |
/* That's a problem for console messages in locales that are not |
| 223 |
UTF-8-encoded. If this is really a problem, we'll have to perform |
| 224 |
some character conversion directly. We can't create a separate |
| 225 |
message catalog for those, because several console messages are |
| 226 |
also displayed on the SDL screen and need a separate conversion |
| 227 |
anyway. */ |
| 228 |
bind_textdomain_codeset(PACKAGE, "UTF-8"); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Check the command line arguments and initialize the required global |
| 233 |
* variables |
| 234 |
*/ |
| 235 |
bool App::check_arg(int argc, char *argv[]) { |
| 236 |
int c; |
| 237 |
char *refdir_opt = NULL; |
| 238 |
char *dmoddir_opt = NULL; |
| 239 |
int debug_p = 0; |
| 240 |
joystick = 1; |
| 241 |
|
| 242 |
/* Options '-debug', '-game', '-noini', '-nojoy', '-nosound' and |
| 243 |
'-window' (with one dash '-' only) are required to maintain |
| 244 |
backward compatibility with the original game */ |
| 245 |
struct option long_options[] = |
| 246 |
{ |
| 247 |
{"debug", no_argument, NULL, 'd'}, |
| 248 |
{"refdir", required_argument, NULL, 'r'}, |
| 249 |
{"game", required_argument, NULL, 'g'}, |
| 250 |
{"help", no_argument, NULL, 'h'}, |
| 251 |
{"noini", no_argument, NULL, 'i'}, |
| 252 |
{"nojoy", no_argument, NULL, 'j'}, |
| 253 |
{"nosound", no_argument, NULL, 's'}, |
| 254 |
{"version", no_argument, NULL, 'v'}, |
| 255 |
{"window", no_argument, NULL, 'w'}, |
| 256 |
{"v1.07", no_argument, NULL, '7'}, |
| 257 |
{"truecolor", no_argument, NULL, 't'}, |
| 258 |
{"nomovie" , no_argument, NULL, ','}, |
| 259 |
{"software-rendering", no_argument, NULL, 'S'}, |
| 260 |
{0, 0, 0, 0} |
| 261 |
}; |
| 262 |
|
| 263 |
char short_options[] = "dr:g:hijsvw7tS"; |
| 264 |
|
| 265 |
/* Allow parsing options multiple times, e.g. in EMSCRIPTEN */ |
| 266 |
optind = 0; |
| 267 |
truecolor = 0; |
| 268 |
|
| 269 |
/* Loop through each argument */ |
| 270 |
while ((c = getopt_long_only (argc, argv, short_options, long_options, NULL)) != EOF) |
| 271 |
{ |
| 272 |
switch (c) { |
| 273 |
case 'd': |
| 274 |
debug_p = 1; |
| 275 |
/* Enable early debugging, before we can locate DEBUG.txt */ |
| 276 |
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); |
| 277 |
break; |
| 278 |
case 'r': |
| 279 |
refdir_opt = strdup(optarg); |
| 280 |
break; |
| 281 |
case 'g': |
| 282 |
dmoddir_opt = strdup(optarg); |
| 283 |
break; |
| 284 |
case 'h': |
| 285 |
print_help(argc, argv); |
| 286 |
break; |
| 287 |
case 'j': |
| 288 |
joystick = 0; |
| 289 |
break; |
| 290 |
case 'i': |
| 291 |
g_b_no_write_ini = 1; |
| 292 |
break; |
| 293 |
case 's': |
| 294 |
sound_on = 0; |
| 295 |
break; |
| 296 |
case 'v': |
| 297 |
print_version(); |
| 298 |
break; |
| 299 |
case 'w': |
| 300 |
windowed = true; |
| 301 |
break; |
| 302 |
case '7': |
| 303 |
opt_version = 107; |
| 304 |
break; |
| 305 |
case 't': |
| 306 |
truecolor = 1; |
| 307 |
break; |
| 308 |
case 'S': |
| 309 |
dinkgl = false; |
| 310 |
break; |
| 311 |
case ',': |
| 312 |
printf(_("Note: -nomovie is accepted for compatibility, but has no effect.\n")); |
| 313 |
break; |
| 314 |
default: |
| 315 |
return false; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (optind < argc) { |
| 320 |
fprintf(stderr, "Invalid additional argument: "); |
| 321 |
while (optind < argc) |
| 322 |
fprintf(stderr, "%s ", argv[optind++]); |
| 323 |
printf(" (did you forget '--game'?)\n"); |
| 324 |
return false; |
| 325 |
} |
| 326 |
|
| 327 |
log_init(); |
| 328 |
|
| 329 |
if (!paths_init(argv[0], refdir_opt, dmoddir_opt)) { |
| 330 |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, PACKAGE_STRING, |
| 331 |
log_getLastLog(), NULL); |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
free(refdir_opt); |
| 336 |
free(dmoddir_opt); |
| 337 |
|
| 338 |
if (debug_p == 1) |
| 339 |
{ |
| 340 |
/* Remove DEBUG.TXT when starting Dink (but not when toggling debug) */ |
| 341 |
char* fullpath = paths_dmodfile("DEBUG.TXT"); |
| 342 |
remove(fullpath); |
| 343 |
log_set_output_file(fullpath); |
| 344 |
free(fullpath); |
| 345 |
|
| 346 |
log_debug_on(); |
| 347 |
} |
| 348 |
|
| 349 |
log_info("Game directory is '%s'.", paths_getdmoddir()); |
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
/* freedink and freedinkedit's common init procedure. This procedure |
| 354 |
will also initialize each subsystem as needed (eg InitSound) */ |
| 355 |
int App::main(int argc, char *argv[]) { |
| 356 |
if (!check_arg(argc, argv)) |
| 357 |
return EXIT_FAILURE; |
| 358 |
|
| 359 |
#ifdef __ANDROID__ |
| 360 |
//log_debug_on(); |
| 361 |
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); |
| 362 |
#endif |
| 363 |
|
| 364 |
/* SDL */ |
| 365 |
/* Init timer subsystem */ |
| 366 |
#ifndef __EMSCRIPTEN__ |
| 367 |
if (SDL_Init(SDL_INIT_TIMER) == -1) { |
| 368 |
log_error("Timer initialization error: %s\n", SDL_GetError()); |
| 369 |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, PACKAGE_STRING, |
| 370 |
log_getLastLog(), NULL); |
| 371 |
return EXIT_FAILURE; |
| 372 |
} |
| 373 |
#endif |
| 374 |
|
| 375 |
/* Quits in case we couldn't do it properly first (i.e. attempt to |
| 376 |
avoid stucking the user in 640x480 when crashing) */ |
| 377 |
//atexit(app_quit); |
| 378 |
|
| 379 |
/* GFX */ |
| 380 |
if (gfx_init(dinkgl, windowed, splash_path) < 0) { |
| 381 |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, PACKAGE_STRING, |
| 382 |
log_getLastLog(), NULL); |
| 383 |
return EXIT_FAILURE; |
| 384 |
} |
| 385 |
g_dmod.load(paths_getdmoddir()); |
| 386 |
|
| 387 |
/* Joystick */ |
| 388 |
input_init(); |
| 389 |
|
| 390 |
/* SFX & BGM */ |
| 391 |
sfx_init(); |
| 392 |
|
| 393 |
// D-Mod loading |
| 394 |
// TODO: move to a separate D-Mod object |
| 395 |
{ |
| 396 |
/** localization (l10n) for this D-Mod's .mo (after options are parsed) */ |
| 397 |
char* dmod_localedir = paths_dmodfile("l10n"); |
| 398 |
log_info("localedir: %s", LOCALEDIR); |
| 399 |
log_info("localedir for dmod: %s", dmod_localedir); |
| 400 |
// get translations in the D-Mod's "l10n" folder; for Dink |
| 401 |
// itself, it would be worth adding a fallback to LOCALEDIR, in |
| 402 |
// case we try to fix AppStream's 'gettext-data-not-found' |
| 403 |
bindtextdomain(paths_getdmodname(), dmod_localedir); |
| 404 |
bind_textdomain_codeset(paths_getdmodname(), "UTF-8"); |
| 405 |
free(dmod_localedir); |
| 406 |
|
| 407 |
int nb_idata = (opt_version >= 108) ? 1000 : 600; |
| 408 |
dinkini_init(nb_idata); |
| 409 |
|
| 410 |
live_screen_init(); |
| 411 |
|
| 412 |
//dinks normal walk |
| 413 |
log_info("Loading batch..."); |
| 414 |
load_batch(dinkini_playmidi); |
| 415 |
log_info(" done!"); |
| 416 |
|
| 417 |
log_info("Loading hard..."); |
| 418 |
load_hard(); |
| 419 |
log_info(" done!"); |
| 420 |
|
| 421 |
log_info("World data...."); |
| 422 |
g_dmod.map.load(); |
| 423 |
log_info(" done!"); |
| 424 |
} |
| 425 |
|
| 426 |
init(); |
| 427 |
|
| 428 |
loop(); |
| 429 |
|
| 430 |
return EXIT_SUCCESS; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
#ifdef __EMSCRIPTEN__ |
| 435 |
#include <emscripten.h> |
| 436 |
void myemscripten_loop(void* arg) { |
| 437 |
App* app = (App*)arg; |
| 438 |
app->one_iter(); |
| 439 |
} |
| 440 |
#endif |
| 441 |
|
| 442 |
void App::loop() { |
| 443 |
/* Main loop */ |
| 444 |
#ifdef __EMSCRIPTEN__ |
| 445 |
emscripten_set_main_loop_arg(myemscripten_loop, this, 0, 1); |
| 446 |
#else |
| 447 |
while(run) { |
| 448 |
one_iter(); |
| 449 |
} |
| 450 |
#endif |
| 451 |
} |
| 452 |
|
| 453 |
void App::one_iter() { |
| 454 |
/* Controller: dispatch events */ |
| 455 |
SDL_Event event; |
| 456 |
SDL_Event* ev = &event; |
| 457 |
input_reset(); |
| 458 |
while (SDL_PollEvent(ev)) { |
| 459 |
switch(ev->type) |
| 460 |
{ |
| 461 |
case SDL_QUIT: |
| 462 |
run = 0; |
| 463 |
break; |
| 464 |
default: |
| 465 |
input(ev); |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/* Main app logic */ |
| 471 |
logic(); |
| 472 |
|
| 473 |
/* Clean-up finished sounds: normally this is done by |
| 474 |
SDL_mixer but since we're using effects tricks to |
| 475 |
stream&resample sounds, we need to do this manually. */ |
| 476 |
sfx_cleanup_finished_channels(); |
| 477 |
|
| 478 |
#ifdef __EMSCRIPTEN__ |
| 479 |
if (!run) { |
| 480 |
delete this; |
| 481 |
} |
| 482 |
#endif |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Release all objects we use |
| 487 |
*/ |
| 488 |
App::~App() { |
| 489 |
SDL_Event ev; |
| 490 |
ev.type = SDL_QUIT; |
| 491 |
SDL_PushEvent(&ev); |
| 492 |
|
| 493 |
dinkini_quit(); |
| 494 |
|
| 495 |
sfx_quit(); |
| 496 |
|
| 497 |
input_quit(); |
| 498 |
|
| 499 |
g_dmod.unload(); |
| 500 |
gfx_quit(); |
| 501 |
|
| 502 |
SDL_QuitSubSystem(SDL_INIT_EVENTS); |
| 503 |
SDL_QuitSubSystem(SDL_INIT_TIMER); |
| 504 |
|
| 505 |
SDL_Quit(); |
| 506 |
|
| 507 |
paths_quit(); |
| 508 |
|
| 509 |
log_quit(); |
| 510 |
|
| 511 |
#ifdef __EMSCRIPTEN__ |
| 512 |
emscripten_cancel_main_loop(); |
| 513 |
EM_ASM( Module.onExit() ); |
| 514 |
#endif |
| 515 |
} |