| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* inferno.c: Entry point of program (main procedure) |
| 23 |
* |
| 24 |
* After main initializes everything, most of the time is spent in the loop |
| 25 |
* while (window_get_front()) |
| 26 |
* In this loop, the main menu is brought up first. |
| 27 |
* |
| 28 |
* main() for Inferno |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
extern const char copyright[]; |
| 33 |
|
| 34 |
const |
| 35 |
#if DXX_BUILD_DESCENT == 1 |
| 36 |
char copyright[] = "DESCENT COPYRIGHT (C) 1994,1995 PARALLAX SOFTWARE CORPORATION"; |
| 37 |
#elif DXX_BUILD_DESCENT == 2 |
| 38 |
char copyright[] = "DESCENT II COPYRIGHT (C) 1994-1996 PARALLAX SOFTWARE CORPORATION"; |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "dxxsconf.h" |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <limits.h> |
| 46 |
#include <SDL.h> |
| 47 |
#if DXX_USE_SCREENSHOT_FORMAT_PNG |
| 48 |
#include <png.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef __unix__ |
| 52 |
#include <unistd.h> |
| 53 |
#include <sys/stat.h> |
| 54 |
#include <sys/types.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include <cctype> |
| 58 |
#include <locale> |
| 59 |
#include "pstypes.h" |
| 60 |
#include "strutil.h" |
| 61 |
#include "console.h" |
| 62 |
#include "gr.h" |
| 63 |
#include "key.h" |
| 64 |
#include "bm.h" |
| 65 |
#include "inferno.h" |
| 66 |
#include "dxxerror.h" |
| 67 |
#include "player.h" |
| 68 |
#include "game.h" |
| 69 |
#include "u_mem.h" |
| 70 |
#include "screens.h" |
| 71 |
#include "texmerge.h" |
| 72 |
#include "menu.h" |
| 73 |
#include "digi.h" |
| 74 |
#include "palette.h" |
| 75 |
#include "args.h" |
| 76 |
#include "titles.h" |
| 77 |
#include "text.h" |
| 78 |
#include "gamefont.h" |
| 79 |
#include "kconfig.h" |
| 80 |
#include "newmenu.h" |
| 81 |
#include "config.h" |
| 82 |
#include "multi.h" |
| 83 |
#include "gameseq.h" |
| 84 |
#if DXX_BUILD_DESCENT == 2 |
| 85 |
#include "gamepal.h" |
| 86 |
#include "movie.h" |
| 87 |
#endif |
| 88 |
#include "playsave.h" |
| 89 |
#include "newdemo.h" |
| 90 |
#include "joy.h" |
| 91 |
#if !DXX_USE_OGL |
| 92 |
#include "../texmap/scanline.h" //for select_tmap -MM |
| 93 |
#include "texmap.h" |
| 94 |
#endif |
| 95 |
#include "event.h" |
| 96 |
#include "rbaudio.h" |
| 97 |
#if DXX_WORDS_NEED_ALIGNMENT |
| 98 |
#include <sys/prctl.h> |
| 99 |
#endif |
| 100 |
#if DXX_USE_EDITOR |
| 101 |
#include "messagebox.h" |
| 102 |
#include "editor/editor.h" |
| 103 |
#include "ui.h" |
| 104 |
#endif |
| 105 |
#include "vers_id.h" |
| 106 |
#if DXX_USE_UDP |
| 107 |
#include "net_udp.h" |
| 108 |
#endif |
| 109 |
#include "dsx-ns.h" |
| 110 |
|
| 111 |
#if DXX_USE_SDLIMAGE |
| 112 |
#include <SDL_image.h> |
| 113 |
#endif |
| 114 |
#if DXX_USE_SDLMIXER |
| 115 |
#include <SDL_mixer.h> |
| 116 |
#endif |
| 117 |
|
| 118 |
namespace dsx { |
| 119 |
|
| 120 |
int Screen_mode=-1; //game screen or editor screen? |
| 121 |
|
| 122 |
#if DXX_BUILD_DESCENT == 1 |
| 123 |
uint8_t HiresGFXAvailable; |
| 124 |
int MacHog{0}; // using a Mac hogfile? |
| 125 |
#endif |
| 126 |
|
| 127 |
namespace { |
| 128 |
|
| 129 |
//read help from a file & print to screen |
| 130 |
static void print_commandline_help() |
| 131 |
{ |
| 132 |
#define DXX_COMMAND_LINE_HELP_FMT(FMT,...) FMT |
| 133 |
#define DXX_COMMAND_LINE_HELP_ARG(FMT,...) , ## __VA_ARGS__ |
| 134 |
|
| 135 |
#define DXX_if_defined_placeholder1 , |
| 136 |
#define DXX_if_defined_unwrap(A,...) A, ## __VA_ARGS__ |
| 137 |
/* If the parameter V, after macro expansion, evaluates to `1`, then |
| 138 |
* expand to the parameter F. Otherwise, expand to nothing. |
| 139 |
*/ |
| 140 |
#define DXX_if_defined(V,F) DXX_if_defined2(V,F) |
| 141 |
/* If the parameter V, after macro expansion, evaluates to anything |
| 142 |
* other than `1`, then expand to the parameter F. Otherwise, |
| 143 |
* expand to nothing. |
| 144 |
*/ |
| 145 |
#define DXX_if_not_defined_to_1(V,F) DXX_if_not_defined_to_1_2(V,F) |
| 146 |
#define DXX_if_defined2(V,F) DXX_if_defined3(DXX_if_defined_placeholder##V, F) |
| 147 |
#define DXX_if_not_defined_to_1_2(V,F) DXX_if_not_defined_to_1_3(DXX_if_defined_placeholder##V, F) |
| 148 |
#define DXX_if_defined3(V,F) DXX_if_defined4(F, V 1, 0) |
| 149 |
#define DXX_if_not_defined_to_1_3(V,F) DXX_if_defined4(F, V 0, 1) |
| 150 |
#define DXX_if_defined4(F,_,V,...) DXX_if_defined5_##V(F) |
| 151 |
#define DXX_if_defined5_0(F) |
| 152 |
#define DXX_if_defined5_1(F) DXX_if_defined_unwrap F |
| 153 |
#define DXX_if_defined_01(V,F) DXX_if_defined4(F,,V) |
| 154 |
|
| 155 |
#define DXX_COMMAND_LINE_HELP_unix(V) DXX_if_defined(__unix__, (V)) |
| 156 |
#define DXX_STRINGIZE2(X) #X |
| 157 |
#define DXX_STRINGIZE(X) DXX_STRINGIZE2(X) |
| 158 |
|
| 159 |
#if DXX_BUILD_DESCENT == 1 |
| 160 |
#define DXX_COMMAND_LINE_HELP_D1(V) V |
| 161 |
#define DXX_COMMAND_LINE_HELP_D2(V) |
| 162 |
#elif DXX_BUILD_DESCENT == 2 |
| 163 |
#define DXX_COMMAND_LINE_HELP_D1(V) |
| 164 |
#define DXX_COMMAND_LINE_HELP_D2(V) V |
| 165 |
#endif |
| 166 |
|
| 167 |
#if DXX_USE_OGL |
| 168 |
#define DXX_COMMAND_LINE_HELP_OGL(V) V |
| 169 |
#define DXX_COMMAND_LINE_HELP_SDL(V) |
| 170 |
#else |
| 171 |
#define DXX_COMMAND_LINE_HELP_OGL(V) |
| 172 |
#define DXX_COMMAND_LINE_HELP_SDL(V) V |
| 173 |
#endif |
| 174 |
|
| 175 |
#define DXX_COMMAND_LINE_HELP(VERB) \ |
| 176 |
VERB("\n System Options:\n\n") \ |
| 177 |
VERB(" -nonicefps Don't free CPU-cycles\n") \ |
| 178 |
VERB(" -maxfps <n> Set maximum framerate to <n>\n\t\t\t\t(default: " DXX_STRINGIZE(MAXIMUM_FPS) ", available: " DXX_STRINGIZE(MINIMUM_FPS) "-" DXX_STRINGIZE(MAXIMUM_FPS) ")\n") \ |
| 179 |
VERB(" -hogdir <s> set shared data directory to <s>\n") \ |
| 180 |
DXX_COMMAND_LINE_HELP_unix( \ |
| 181 |
VERB(" -nohogdir don't try to use shared data directory\n") \ |
| 182 |
) \ |
| 183 |
VERB(" -add-missions-dir <s> Add contents of location <s> to the missions directory\n") \ |
| 184 |
VERB(" -use_players_dir Put player files and saved games in Players subdirectory\n") \ |
| 185 |
VERB(" -lowmem Lowers animation detail for better performance with\n\t\t\t\tlow memory\n") \ |
| 186 |
VERB(" -pilot <s> Select pilot <s> automatically\n") \ |
| 187 |
VERB(" -auto-record-demo Start recording on level entry\n") \ |
| 188 |
VERB(" -record-demo-format Set demo name automatically\n") \ |
| 189 |
VERB(" -autodemo Start in demo mode\n") \ |
| 190 |
VERB(" -window Run the game in a window\n") \ |
| 191 |
VERB(" -noborders Don't show borders in window mode\n") \ |
| 192 |
DXX_COMMAND_LINE_HELP_D1( \ |
| 193 |
VERB(" -notitles Skip title screens\n") \ |
| 194 |
) \ |
| 195 |
DXX_COMMAND_LINE_HELP_D2( \ |
| 196 |
VERB(" -nomovies Don't play movies\n") \ |
| 197 |
) \ |
| 198 |
VERB("\n Controls:\n\n") \ |
| 199 |
VERB(" -nocursor Hide mouse cursor\n") \ |
| 200 |
VERB(" -nomouse Deactivate mouse\n") \ |
| 201 |
VERB(" -nojoystick Deactivate joystick\n") \ |
| 202 |
VERB(" -nostickykeys Make CapsLock and NumLock non-sticky\n") \ |
| 203 |
VERB("\n Sound:\n\n") \ |
| 204 |
VERB(" -nosound Disables sound output\n") \ |
| 205 |
VERB(" -nomusic Disables music output\n") \ |
| 206 |
DXX_COMMAND_LINE_HELP_D2( \ |
| 207 |
VERB(" -sound11k Use 11KHz sounds\n") \ |
| 208 |
) \ |
| 209 |
DXX_if_defined_01(DXX_USE_SDLMIXER, ( \ |
| 210 |
VERB(" -nosdlmixer Disable Sound output via SDL_mixer\n") \ |
| 211 |
)) \ |
| 212 |
VERB("\n Graphics:\n\n") \ |
| 213 |
VERB(" -lowresfont Force use of low resolution fonts\n") \ |
| 214 |
DXX_COMMAND_LINE_HELP_D2( \ |
| 215 |
VERB(" -lowresgraphics Force use of low resolution graphics\n") \ |
| 216 |
VERB(" -lowresmovies Play low resolution movies if available (for slow machines)\n") \ |
| 217 |
) \ |
| 218 |
DXX_COMMAND_LINE_HELP_OGL( \ |
| 219 |
VERB(" -gl_fixedfont Don't scale fonts to current resolution\n") \ |
| 220 |
VERB(" -gl_syncmethod <n> OpenGL sync method (default: %i)\n", OGL_SYNC_METHOD_DEFAULT) \ |
| 221 |
VERB(" 0: Disabled\n") \ |
| 222 |
VERB(" 1: Fence syncs, limit GPU latency to at most one frame\n") \ |
| 223 |
VERB(" 2: Like 1, but sleep during sync to reduce CPU load\n") \ |
| 224 |
VERB(" 3: Immediately sync after buffer swap\n") \ |
| 225 |
VERB(" 4: Immediately sync after buffer swap\n") \ |
| 226 |
VERB(" 5: Auto: if VSync is enabled and ARB_sync is supported, use mode 2, otherwise mode 0\n") \ |
| 227 |
VERB(" -gl_syncwait <n> Wait interval (ms) for sync mode 2 (default: " DXX_STRINGIZE(OGL_SYNC_WAIT_DEFAULT) ")\n") \ |
| 228 |
VERB(" -gl_darkedges Re-enable dark edges around filtered textures (as present in earlier versions of the engine)\n") \ |
| 229 |
DXX_if_defined_01(DXX_USE_STEREOSCOPIC_RENDER, ( \ |
| 230 |
VERB(" -gl_stereo Enable OpenGL stereo quad buffering, if available\n") \ |
| 231 |
VERB(" -gl_stereoview <n> Select OpenGL stereo viewport mode (experimental; incomplete)\n") \ |
| 232 |
VERB(" 1: above/below half-height format\n") \ |
| 233 |
VERB(" 2: side/by/side half-width format\n") \ |
| 234 |
VERB(" 3: side/by/side half-size format, normal aspect ratio\n") \ |
| 235 |
VERB(" 4: above/below format with external sync blank interval\n") \ |
| 236 |
)) \ |
| 237 |
) \ |
| 238 |
DXX_if_defined_01(DXX_USE_UDP, ( \ |
| 239 |
VERB("\n Multiplayer:\n\n") \ |
| 240 |
VERB(" -udp_hostaddr <s> Use IP address/Hostname <s> for manual game joining\n\t\t\t\t(default: %s)\n", UDP_MANUAL_ADDR_DEFAULT) \ |
| 241 |
VERB(" -udp_hostport <n> Use UDP port <n> for manual game joining (default: %hu)\n", UDP_PORT_DEFAULT) \ |
| 242 |
VERB(" -udp_myport <n> Set my own UDP port to <n> (default: %hu)\n", UDP_PORT_DEFAULT) \ |
| 243 |
DXX_if_defined_01(DXX_USE_TRACKER, ( \ |
| 244 |
VERB(" -no-tracker Disable tracker (unless overridden by later -tracker_hostaddr)\n") \ |
| 245 |
VERB(" -tracker_hostaddr <n> Address of tracker server to register/query games to/from\n\t\t\t\t(default: %s)\n", TRACKER_ADDR_DEFAULT) \ |
| 246 |
VERB(" -tracker_hostport <n> Port of tracker server to register/query games to/from\n\t\t\t\t(default: %hu)\n", TRACKER_PORT_DEFAULT) \ |
| 247 |
)) \ |
| 248 |
)) \ |
| 249 |
DXX_if_defined(EDITOR, ( \ |
| 250 |
VERB("\n Editor:\n\n") \ |
| 251 |
DXX_COMMAND_LINE_HELP_D1( \ |
| 252 |
VERB(" -nobm Don't load BITMAPS.TBL and BITMAPS.BIN - use internal data\n") \ |
| 253 |
) \ |
| 254 |
DXX_COMMAND_LINE_HELP_D2( \ |
| 255 |
VERB(" -autoload <s> Autoload level <s> in the editor\n") \ |
| 256 |
VERB(" -macdata Read and write Mac data files in editor (swap colors)\n") \ |
| 257 |
VERB(" -hoarddata Make the Hoard ham file from some files, then exit\n") \ |
| 258 |
) \ |
| 259 |
)) \ |
| 260 |
VERB("\n Debug (use only if you know what you're doing):\n\n") \ |
| 261 |
VERB(" -debug Enable debugging output.\n") \ |
| 262 |
VERB(" -verbose Enable verbose output.\n") \ |
| 263 |
VERB(" -safelog Write gamelog.txt unbuffered.\n\t\t\t\tUse to keep helpful output to trace program crashes.\n") \ |
| 264 |
VERB(" -norun Bail out after initialization\n") \ |
| 265 |
VERB(" -no-grab Never grab keyboard/mouse\n") \ |
| 266 |
VERB(" -renderstats Enable renderstats info by default\n") \ |
| 267 |
VERB(" -text <s> Specify alternate .tex file\n") \ |
| 268 |
VERB(" -showmeminfo Show memory statistics\n") \ |
| 269 |
VERB(" -nodoublebuffer Disable Doublebuffering\n") \ |
| 270 |
VERB(" -bigpig Use uncompressed RLE bitmaps\n") \ |
| 271 |
VERB(" -16bpp Use 16Bpp instead of 32Bpp\n") \ |
| 272 |
DXX_COMMAND_LINE_HELP_OGL( \ |
| 273 |
VERB(" -gl_oldtexmerge Use old texmerge, uses more ram, but might be faster\n") \ |
| 274 |
VERB(" -gl_intensity4_ok <n> Override DbgGlIntensity4Ok (default: 1)\n") \ |
| 275 |
VERB(" -gl_luminance4_alpha4_ok <n> Override DbgGlLuminance4Alpha4Ok (default: 1)\n") \ |
| 276 |
VERB(" -gl_rgba2_ok <n> Override DbgGlRGBA2Ok (default: 1)\n") \ |
| 277 |
VERB(" -gl_readpixels_ok <n> Override DbgGlReadPixelsOk (default: 1)\n") \ |
| 278 |
VERB(" -gl_gettexlevelparam_ok <n> Override DbgGlGetTexLevelParamOk (default: 1)\n") \ |
| 279 |
) \ |
| 280 |
DXX_COMMAND_LINE_HELP_SDL( \ |
| 281 |
VERB(" -tmap <s> Select texmapper <s> to use\n\t\t\t\t(default: c, available: c, fp, quad)\n") \ |
| 282 |
VERB(" -hwsurface Use SDL HW Surface\n") \ |
| 283 |
VERB(" -asyncblit Use queued blits over SDL. Can speed up rendering\n") \ |
| 284 |
) \ |
| 285 |
VERB("\n Help:\n\n") \ |
| 286 |
VERB(" -help, -h, -?, ? View this help screen\n") \ |
| 287 |
VERB("\n\n") \ |
| 288 |
|
| 289 |
printf(DXX_COMMAND_LINE_HELP(DXX_COMMAND_LINE_HELP_FMT) DXX_COMMAND_LINE_HELP(DXX_COMMAND_LINE_HELP_ARG)); |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
namespace dcx { |
| 297 |
|
| 298 |
// Default event handler for everything except the editor |
| 299 |
window_event_result standard_handler(const d_event &event) |
| 300 |
{ |
| 301 |
int key; |
| 302 |
|
| 303 |
switch (event.type) |
| 304 |
{ |
| 305 |
case event_type::mouse_button_down: |
| 306 |
case event_type::mouse_button_up: |
| 307 |
// No window selecting |
| 308 |
// We stay with the current one until it's closed/hidden or another one is made |
| 309 |
// Not the case for the editor |
| 310 |
break; |
| 311 |
|
| 312 |
case event_type::key_command: |
| 313 |
key = event_key_get(event); |
| 314 |
|
| 315 |
switch (key) |
| 316 |
{ |
| 317 |
#if DXX_USE_SCREENSHOT |
| 318 |
#ifdef macintosh |
| 319 |
case KEY_COMMAND + KEY_SHIFTED + KEY_3: |
| 320 |
#endif |
| 321 |
case KEY_PRINT_SCREEN: |
| 322 |
{ |
| 323 |
gr_set_default_canvas(); |
| 324 |
save_screen_shot(0); |
| 325 |
return window_event_result::handled; |
| 326 |
} |
| 327 |
#endif |
| 328 |
|
| 329 |
case KEY_ALTED+KEY_ENTER: |
| 330 |
case KEY_ALTED+KEY_PADENTER: |
| 331 |
if (Game_wind) |
| 332 |
if (window_get_front() == Game_wind) |
| 333 |
return window_event_result::ignored; |
| 334 |
gr_toggle_fullscreen(); |
| 335 |
#if SDL_MAJOR_VERSION == 2 |
| 336 |
{ |
| 337 |
/* Hack to force the canvas to adjust to the new |
| 338 |
* dimensions. Without this, the canvas |
| 339 |
* continues to use the old window size until |
| 340 |
* the hack of calling `init_cockpit` from |
| 341 |
* `game_handler` fixes the dimensions. If the |
| 342 |
* window became bigger, the game fails to draw |
| 343 |
* in the full new area. If the window became |
| 344 |
* smaller, part of the game is outside the |
| 345 |
* cropped area. |
| 346 |
* |
| 347 |
* If the automap is open, the view is still |
| 348 |
* wrong, since the automap uses its own private |
| 349 |
* canvas. That will need to be fixed |
| 350 |
* separately. Ideally, the whole window |
| 351 |
* system would be reworked to provide a general |
| 352 |
* notification to every interested canvas when |
| 353 |
* the top level window resizes. |
| 354 |
*/ |
| 355 |
auto sm = Screen_mode; |
| 356 |
Screen_mode = SCREEN_GAME; |
| 357 |
init_cockpit(); |
| 358 |
Screen_mode = sm; |
| 359 |
} |
| 360 |
#endif |
| 361 |
return window_event_result::handled; |
| 362 |
|
| 363 |
case KEY_SHIFTED + KEY_ESC: |
| 364 |
con_showup(Controls); |
| 365 |
return window_event_result::handled; |
| 366 |
} |
| 367 |
break; |
| 368 |
|
| 369 |
case event_type::window_draw: |
| 370 |
case event_type::idle: |
| 371 |
//see if redbook song needs to be restarted |
| 372 |
#if DXX_USE_SDL_REDBOOK_AUDIO |
| 373 |
RBACheckFinishedHook(); |
| 374 |
#endif |
| 375 |
return window_event_result::handled; |
| 376 |
|
| 377 |
case event_type::quit: |
| 378 |
#if DXX_USE_EDITOR |
| 379 |
if (!SafetyCheck()) |
| 380 |
return window_event_result::handled; |
| 381 |
#endif |
| 382 |
for (;;) |
| 383 |
{ |
| 384 |
const auto wind = window_get_front(); |
| 385 |
if (!wind) |
| 386 |
return window_event_result::handled; // finished quitting |
| 387 |
|
| 388 |
if (wind == Game_wind) |
| 389 |
{ |
| 390 |
const auto choice = nm_messagebox_str(menu_title{nullptr}, nm_messagebox_tie(TXT_YES, TXT_NO), menu_subtitle{TXT_ABORT_GAME}); |
| 391 |
if (choice != 0) |
| 392 |
return window_event_result::handled; // aborted quitting |
| 393 |
else |
| 394 |
CGameArg.SysAutoDemo = false; |
| 395 |
} |
| 396 |
|
| 397 |
// Close front window, let the code flow continue until all windows closed or quit cancelled |
| 398 |
if (!window_close(wind)) |
| 399 |
return window_event_result::handled; |
| 400 |
} |
| 401 |
default: |
| 402 |
break; |
| 403 |
} |
| 404 |
|
| 405 |
return window_event_result::ignored; |
| 406 |
} |
| 407 |
|
| 408 |
#if DXX_HAVE_POISON |
| 409 |
d_interface_unique_state::d_interface_unique_state() |
| 410 |
{ |
| 411 |
DXX_MAKE_VAR_UNDEFINED(PilotName); |
| 412 |
} |
| 413 |
#endif |
| 414 |
|
| 415 |
void d_interface_unique_state::update_window_title() |
| 416 |
{ |
| 417 |
#if SDL_MAJOR_VERSION == 1 |
| 418 |
if (!PilotName[0u]) |
| 419 |
SDL_WM_SetCaption(DESCENT_VERSION, DXX_SDL_WINDOW_CAPTION); |
| 420 |
else |
| 421 |
{ |
| 422 |
const char *const pilot = PilotName; |
| 423 |
std::array<char, 80> wm_caption_name, wm_caption_iconname; |
| 424 |
snprintf(wm_caption_name.data(), wm_caption_name.size(), "%s: %s", DESCENT_VERSION, pilot); |
| 425 |
snprintf(wm_caption_iconname.data(), wm_caption_iconname.size(), "%s: %s", DXX_SDL_WINDOW_CAPTION, pilot); |
| 426 |
SDL_WM_SetCaption(wm_caption_name.data(), wm_caption_iconname.data()); |
| 427 |
} |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
namespace dsx { |
| 434 |
|
| 435 |
#define PROGNAME argv[0] |
| 436 |
#define DXX_RENAME_IDENTIFIER2(I,N) I##$##N |
| 437 |
#define DXX_RENAME_IDENTIFIER(I,N) DXX_RENAME_IDENTIFIER2(I,N) |
| 438 |
#define argc DXX_RENAME_IDENTIFIER(argc_gc, DXX_git_commit) |
| 439 |
#define argv DXX_RENAME_IDENTIFIER(argv_gd$b32, DXX_git_describe) |
| 440 |
|
| 441 |
// DESCENT by Parallax Software |
| 442 |
// DESCENT II by Parallax Software |
| 443 |
// (varies based on preprocessor options) |
| 444 |
// Descent Main |
| 445 |
|
| 446 |
namespace { |
| 447 |
|
| 448 |
static int main(int argc, char *argv[]) |
| 449 |
{ |
| 450 |
if (!PHYSFSX_init(argc, argv)) |
| 451 |
return 1; |
| 452 |
con_init(); // Initialise the console |
| 453 |
|
| 454 |
setbuf(stdout, NULL); // unbuffered output via printf |
| 455 |
#ifdef _WIN32 |
| 456 |
freopen( "CON", "w", stdout ); |
| 457 |
freopen( "CON", "w", stderr ); |
| 458 |
#endif |
| 459 |
|
| 460 |
if (CGameArg.SysShowCmdHelp) { |
| 461 |
print_commandline_help(); |
| 462 |
|
| 463 |
return(0); |
| 464 |
} |
| 465 |
|
| 466 |
printf("\nType '%s -help' for a list of command-line options.\n\n", PROGNAME); |
| 467 |
|
| 468 |
PHYSFSX_listSearchPathContent(); |
| 469 |
|
| 470 |
if (!PHYSFSX_checkSupportedArchiveTypes()) |
| 471 |
return(0); |
| 472 |
|
| 473 |
#if DXX_BUILD_DESCENT == 1 |
| 474 |
const auto descent_hog = make_PHYSFSX_ComputedPathMount(descent_hog_basename, physfs_search_path::append); |
| 475 |
#define DXX_NAME_NUMBER "1" |
| 476 |
#define DXX_HOGFILE_NAMES "descent.hog" |
| 477 |
#elif DXX_BUILD_DESCENT == 2 |
| 478 |
const auto descent_hog = make_PHYSFSX_ComputedPathMount(descent2_hog_basename, d2demo_hog_basename, physfs_search_path::append); |
| 479 |
#define DXX_NAME_NUMBER "2" |
| 480 |
#define DXX_HOGFILE_NAMES "descent2.hog or d2demo.hog" |
| 481 |
#endif |
| 482 |
if (!descent_hog) |
| 483 |
{ |
| 484 |
#if defined(__unix__) && !defined(__APPLE__) |
| 485 |
#define DXX_HOGFILE_PROGRAM_DATA_DIRECTORY \ |
| 486 |
"\t$HOME/.d" DXX_NAME_NUMBER "x-rebirth\n" \ |
| 487 |
DXX_HOGFILE_SHAREPATH_INDENTED |
| 488 |
#if DXX_USE_SHAREPATH |
| 489 |
#define DXX_HOGFILE_SHAREPATH_INDENTED \ |
| 490 |
"\t" DXX_SHAREPATH "\n" |
| 491 |
#else |
| 492 |
#define DXX_HOGFILE_SHAREPATH_INDENTED |
| 493 |
#endif |
| 494 |
#elif (defined(__APPLE__) && defined(__MACH__)) |
| 495 |
#define DXX_HOGFILE_PROGRAM_DATA_DIRECTORY \ |
| 496 |
"\t~/Library/Preferences/D" DXX_NAME_NUMBER "X Rebirth\n" |
| 497 |
#else |
| 498 |
#define DXX_HOGFILE_PROGRAM_DATA_DIRECTORY \ |
| 499 |
"\tDirectory containing D" DXX_NAME_NUMBER "X\n" |
| 500 |
#endif |
| 501 |
#if (defined(__APPLE__) && defined(__MACH__)) || defined(macintosh) |
| 502 |
#define DXX_HOGFILE_APPLICATION_BUNDLE \ |
| 503 |
"\tIn 'Resources' inside the application bundle\n" |
| 504 |
#else |
| 505 |
#define DXX_HOGFILE_APPLICATION_BUNDLE "" |
| 506 |
#endif |
| 507 |
#define DXX_MISSING_HOGFILE_ERROR_TEXT \ |
| 508 |
"Could not find a valid hog file (" DXX_HOGFILE_NAMES ")\nPossible locations are:\n" \ |
| 509 |
DXX_HOGFILE_PROGRAM_DATA_DIRECTORY \ |
| 510 |
"\tIn a subdirectory called 'data'\n" \ |
| 511 |
DXX_HOGFILE_APPLICATION_BUNDLE \ |
| 512 |
"Or use the -hogdir option to specify an alternate location." |
| 513 |
UserError_puts(DXX_MISSING_HOGFILE_ERROR_TEXT); |
| 514 |
} |
| 515 |
|
| 516 |
#if DXX_BUILD_DESCENT == 1 |
| 517 |
switch (descent_hog_size{PHYSFSX_fsize(descent_hog_basename)}) |
| 518 |
{ |
| 519 |
case descent_hog_size::mac_shareware: |
| 520 |
case descent_hog_size::mac_retail: |
| 521 |
MacHog = 1; // used for fonts and the Automap |
| 522 |
break; |
| 523 |
default: |
| 524 |
break; |
| 525 |
} |
| 526 |
#endif |
| 527 |
|
| 528 |
load_text(); |
| 529 |
|
| 530 |
//print out the banner title |
| 531 |
#if DXX_BUILD_DESCENT == 1 |
| 532 |
con_printf(CON_NORMAL, "%s %s", DESCENT_VERSION, g_descent_build_datetime); // D1X version |
| 533 |
con_puts(CON_NORMAL, "This is a MODIFIED version of Descent, based on " BASED_VERSION "."); |
| 534 |
{ |
| 535 |
const auto &&m = TXT_COPYRIGHT; |
| 536 |
con_puts(CON_NORMAL, {m, strlen(m)}); |
| 537 |
} |
| 538 |
{ |
| 539 |
const auto &&m = TXT_TRADEMARK; |
| 540 |
con_puts(CON_NORMAL, {m, strlen(m)}); |
| 541 |
} |
| 542 |
con_puts(CON_NORMAL, "Copyright (C) 2005-2013 Christian Beckhaeuser, 2013-2017 Kp"); |
| 543 |
#elif DXX_BUILD_DESCENT == 2 |
| 544 |
static char d2x_hog_basename[]{MISSION_DIR "d2x.hog"}; |
| 545 |
con_printf(CON_NORMAL, "%s%s %s", DESCENT_VERSION, PHYSFSX_exists_ignorecase(d2x_hog_basename) ? " Vertigo Enhanced" : "", g_descent_build_datetime); // D2X version |
| 546 |
con_puts(CON_NORMAL, "This is a MODIFIED version of Descent 2, based on " BASED_VERSION "."); |
| 547 |
{ |
| 548 |
const auto &&m = TXT_COPYRIGHT; |
| 549 |
con_puts(CON_NORMAL, {m, strlen(m)}); |
| 550 |
} |
| 551 |
{ |
| 552 |
const auto &&m = TXT_TRADEMARK; |
| 553 |
con_puts(CON_NORMAL, {m, strlen(m)}); |
| 554 |
} |
| 555 |
con_puts(CON_NORMAL, "Copyright (C) 1999 Peter Hawkins, 2002 Bradley Bell, 2005-2013 Christian Beckhaeuser, 2013-2017 Kp"); |
| 556 |
#endif |
| 557 |
|
| 558 |
if (CGameArg.DbgVerbose) |
| 559 |
{ |
| 560 |
{ |
| 561 |
PHYSFS_Version vc, vl; |
| 562 |
PHYSFS_VERSION(&vc); |
| 563 |
PHYSFS_getLinkedVersion(&vl); |
| 564 |
con_printf(CON_VERBOSE, "D" DXX_NAME_NUMBER "X-Rebirth built with PhysFS %u.%u.%u; loaded with PhysFS %u.%u.%u", vc.major, vc.minor, vc.patch, vl.major, vl.minor, vl.patch); |
| 565 |
} |
| 566 |
{ |
| 567 |
SDL_version vc; |
| 568 |
SDL_VERSION(&vc); |
| 569 |
#if SDL_MAJOR_VERSION == 1 |
| 570 |
const auto vl = SDL_Linked_Version(); |
| 571 |
#else |
| 572 |
SDL_version vlv; |
| 573 |
const auto vl = &vlv; |
| 574 |
SDL_GetVersion(vl); |
| 575 |
#endif |
| 576 |
con_printf(CON_VERBOSE, "D" DXX_NAME_NUMBER "X-Rebirth built with libSDL %u.%u.%u; loaded with libSDL %u.%u.%u", vc.major, vc.minor, vc.patch, vl->major, vl->minor, vl->patch); |
| 577 |
} |
| 578 |
#if DXX_USE_SDLIMAGE |
| 579 |
{ |
| 580 |
SDL_version vc; |
| 581 |
SDL_IMAGE_VERSION(&vc); |
| 582 |
const auto vl = IMG_Linked_Version(); |
| 583 |
con_printf(CON_VERBOSE, "D" DXX_NAME_NUMBER "X-Rebirth built with SDL_image %u.%u.%u; loaded with SDL_image %u.%u.%u", vc.major, vc.minor, vc.patch, vl->major, vl->minor, vl->patch); |
| 584 |
} |
| 585 |
#endif |
| 586 |
#if DXX_USE_SDLMIXER |
| 587 |
{ |
| 588 |
SDL_version vc; |
| 589 |
SDL_MIXER_VERSION(&vc); |
| 590 |
const auto vl = Mix_Linked_Version(); |
| 591 |
con_printf(CON_VERBOSE, "D" DXX_NAME_NUMBER "X-Rebirth built with SDL_mixer %u.%u.%u; loaded with SDL_mixer %u.%u.%u", vc.major, vc.minor, vc.patch, vl->major, vl->minor, vl->patch); |
| 592 |
} |
| 593 |
#endif |
| 594 |
#if DXX_USE_SCREENSHOT_FORMAT_PNG |
| 595 |
con_printf(CON_VERBOSE, "D" DXX_NAME_NUMBER "X-Rebirth built with libpng version " PNG_LIBPNG_VER_STRING "; loaded with libpng version %s", png_get_libpng_ver(nullptr)); |
| 596 |
#endif |
| 597 |
{ |
| 598 |
const auto &&m = TXT_VERBOSE_1; |
| 599 |
con_puts(CON_VERBOSE, {m, strlen(m)}); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
ReadConfigFile(CGameCfg, GameCfg); |
| 604 |
|
| 605 |
PHYSFSX_addArchiveContent(); |
| 606 |
|
| 607 |
const auto &&arch_atexit_result = arch_init(); |
| 608 |
/* This variable exists for the side effects that occur when it is |
| 609 |
* destroyed. clang-9 fails to recognize those side effects as a |
| 610 |
* "use" and warns that the variable is unused. Cast it to void to |
| 611 |
* create a "use" to suppress the warning. |
| 612 |
*/ |
| 613 |
(void)arch_atexit_result; |
| 614 |
|
| 615 |
#if !DXX_USE_OGL |
| 616 |
select_tmap(CGameArg.DbgTexMap); |
| 617 |
|
| 618 |
#if DXX_BUILD_DESCENT == 2 |
| 619 |
Lighting_on = 1; |
| 620 |
#endif |
| 621 |
#endif |
| 622 |
|
| 623 |
con_puts(CON_VERBOSE, "Going into graphics mode..."); |
| 624 |
|
| 625 |
// Load the palette stuff. Returns non-zero if error. |
| 626 |
con_puts(CON_DEBUG, "Initializing palette system..."); |
| 627 |
#if DXX_BUILD_DESCENT == 1 |
| 628 |
gr_use_palette_table("palette.256"); |
| 629 |
#elif DXX_BUILD_DESCENT == 2 |
| 630 |
gr_use_palette_table(D2_DEFAULT_PALETTE ); |
| 631 |
#endif |
| 632 |
|
| 633 |
con_puts(CON_DEBUG, "Initializing font system..."); |
| 634 |
gamefont_init(); // must load after palette data loaded. |
| 635 |
|
| 636 |
#if DXX_USE_OGL |
| 637 |
gr_set_mode_from_window_size(); |
| 638 |
#else |
| 639 |
gr_set_mode(Game_screen_mode); |
| 640 |
#endif |
| 641 |
|
| 642 |
#if DXX_BUILD_DESCENT == 2 |
| 643 |
con_puts(CON_DEBUG, "Initializing movie libraries..."); |
| 644 |
auto &&loaded_builtin_movies = init_movies(); //init movie libraries |
| 645 |
/* clang does not recognize that creating the variable for the |
| 646 |
* purpose of extending the returned value's lifetime is a use. Add |
| 647 |
* an explicit dummy use to silence its bogus -Wunused-variable |
| 648 |
* warning. |
| 649 |
*/ |
| 650 |
(void)loaded_builtin_movies; |
| 651 |
#endif |
| 652 |
|
| 653 |
show_titles(); |
| 654 |
|
| 655 |
set_screen_mode(SCREEN_MENU); |
| 656 |
#if DXX_USE_DEBUG_MEMORY_ALLOCATOR |
| 657 |
/* Memdebug runs before global destructors, so it incorrectly |
| 658 |
* reports as leaked any allocations that would be freed by a global |
| 659 |
* destructor. This local will force the newmenu globals to be |
| 660 |
* reset before memdebug scans, which prevents memdebug falsely |
| 661 |
* reporting them as leaked. |
| 662 |
* |
| 663 |
* External tools, such as Valgrind, know to run global destructors |
| 664 |
* before checking for leaks, so this hack is only necessary when |
| 665 |
* memdebug is used. |
| 666 |
*/ |
| 667 |
struct hack_free_global_backgrounds |
| 668 |
{ |
| 669 |
~hack_free_global_backgrounds() |
| 670 |
{ |
| 671 |
newmenu_free_background(); |
| 672 |
} |
| 673 |
}; |
| 674 |
hack_free_global_backgrounds hack_free_global_background; |
| 675 |
#endif |
| 676 |
|
| 677 |
con_puts(CON_DEBUG, "Doing gamedata_init..."); |
| 678 |
gamedata_init(LevelSharedRobotInfoState); |
| 679 |
|
| 680 |
#if DXX_BUILD_DESCENT == 2 |
| 681 |
#if DXX_USE_EDITOR |
| 682 |
if (GameArg.EdiSaveHoardData) { |
| 683 |
save_hoard_data(); |
| 684 |
exit(1); |
| 685 |
} |
| 686 |
#endif |
| 687 |
#endif |
| 688 |
|
| 689 |
if (CGameArg.DbgNoRun) |
| 690 |
return(0); |
| 691 |
|
| 692 |
con_puts(CON_DEBUG, "Initializing texture caching system..."); |
| 693 |
texmerge_init(); // 10 cache bitmaps |
| 694 |
|
| 695 |
#if DXX_BUILD_DESCENT == 2 |
| 696 |
piggy_init_pigfile("groupa.pig"); //get correct pigfile |
| 697 |
#endif |
| 698 |
|
| 699 |
con_puts(CON_DEBUG, "Running game..."); |
| 700 |
init_game(); |
| 701 |
|
| 702 |
#if DXX_BUILD_DESCENT == 1 |
| 703 |
key_flush(); |
| 704 |
#endif |
| 705 |
{ |
| 706 |
InterfaceUniqueState.PilotName.fill(0); |
| 707 |
if (!CGameArg.SysPilot.empty()) |
| 708 |
{ |
| 709 |
char filename[sizeof(PLAYER_DIRECTORY_TEXT) + CALLSIGN_LEN + 4]; |
| 710 |
|
| 711 |
/* Step over the literal PLAYER_DIRECTORY_TEXT when it is |
| 712 |
* present. Point at &filename[0] when |
| 713 |
* PLAYER_DIRECTORY_TEXT is absent. |
| 714 |
*/ |
| 715 |
const auto b = &filename[-CGameArg.SysUsePlayersDir]; |
| 716 |
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.12s"), CGameArg.SysPilot.c_str()); |
| 717 |
/* The pilot name is never used after this. Clear it to |
| 718 |
* free the allocated memory, if any. |
| 719 |
*/ |
| 720 |
CGameArg.SysPilot.clear(); |
| 721 |
auto p = b; |
| 722 |
for (const auto &facet = std::use_facet<std::ctype<char>>(std::locale::classic()); char &c = *p; ++p) |
| 723 |
{ |
| 724 |
c = facet.tolower(static_cast<uint8_t>(c)); |
| 725 |
} |
| 726 |
auto j = p - filename; |
| 727 |
if (j < sizeof(filename) - 4 && (j <= 4 || strcmp(&filename[j - 4], ".plr"))) // if player hasn't specified .plr extension in argument, add it |
| 728 |
{ |
| 729 |
strcpy(&filename[j], ".plr"); |
| 730 |
j += 4; |
| 731 |
} |
| 732 |
if (PHYSFS_exists(filename)) |
| 733 |
{ |
| 734 |
InterfaceUniqueState.PilotName.copy(std::span<const char>(b, std::distance(b, &filename[j - 4]))); |
| 735 |
InterfaceUniqueState.update_window_title(); |
| 736 |
read_player_file(); |
| 737 |
WriteConfigFile(CGameCfg, GameCfg); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
#if DXX_BUILD_DESCENT == 2 |
| 743 |
#if DXX_USE_EDITOR |
| 744 |
if (!GameArg.EdiAutoLoad.empty()) { |
| 745 |
/* Any number >= FILENAME_LEN works */ |
| 746 |
Current_mission->level_names[0].copy_if(GameArg.EdiAutoLoad.c_str(), GameArg.EdiAutoLoad.size()); |
| 747 |
LoadLevel(1, 1); |
| 748 |
} |
| 749 |
else |
| 750 |
#endif |
| 751 |
#endif |
| 752 |
{ |
| 753 |
Game_mode = {}; |
| 754 |
DoMenu(); |
| 755 |
} |
| 756 |
|
| 757 |
while (window_get_front()) |
| 758 |
// Send events to windows and the default handler |
| 759 |
event_process(); |
| 760 |
|
| 761 |
// Tidy up - avoids a crash on exit |
| 762 |
{ |
| 763 |
window *wind; |
| 764 |
|
| 765 |
show_menus(); |
| 766 |
while ((wind = window_get_front())) |
| 767 |
window_close(wind); |
| 768 |
} |
| 769 |
|
| 770 |
WriteConfigFile(CGameCfg, GameCfg); |
| 771 |
|
| 772 |
con_puts(CON_DEBUG, "Cleanup..."); |
| 773 |
close_game(); |
| 774 |
texmerge_close(); |
| 775 |
gamedata_close(); |
| 776 |
Current_mission.reset(); |
| 777 |
PHYSFSX_removeArchiveContent(); |
| 778 |
|
| 779 |
return(0); //presumably successful exit |
| 780 |
} |
| 781 |
|
| 782 |
} |
| 783 |
|
| 784 |
} |
| 785 |
|
| 786 |
int main(int argc, char *argv[]) |
| 787 |
{ |
| 788 |
mem_init(); |
| 789 |
#if DXX_WORDS_NEED_ALIGNMENT |
| 790 |
prctl(PR_SET_UNALIGN, PR_UNALIGN_NOPRINT, 0, 0, 0); |
| 791 |
#endif |
| 792 |
#ifdef WIN32 |
| 793 |
void d_set_exception_handler(); |
| 794 |
d_set_exception_handler(); |
| 795 |
#endif |
| 796 |
return dsx::main(argc, argv); |
| 797 |
} |
| 798 |
|
| 799 |
#undef argv |
| 800 |
#undef argc |