DEADSOFTWARE

game: disable corpses for server
[d2df-sdl.git] / src / game / Doom2DF.lpr
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$IFDEF ANDROID}library{$ELSE}program{$ENDIF} Doom2DF;
18 {$IFNDEF HEADLESS}
19 {$IFDEF WINDOWS}
20 {$APPTYPE GUI}
21 {$ENDIF}
22 {$ENDIF}
23 {$HINTS OFF}
25 uses
26 {$IFDEF ANDROID}
27 ctypes, jni,
28 {$ENDIF}
29 {$IFDEF UNIX}
30 cthreads, BaseUnix,
31 {$ENDIF}
32 {$IFDEF DARWIN}
33 MacOSAll, CocoaAll,
34 {$ENDIF}
35 mempool in '../shared/mempool.pas',
36 conbuf in '../shared/conbuf.pas',
37 geom in '../shared/geom.pas',
38 math,
40 {$IFDEF USE_MINIUPNPC}
41 miniupnpc in '../lib/miniupnpc/miniupnpc.pas',
42 {$ENDIF}
44 {$IFDEF USE_SDL}
45 SDL in '../lib/sdl/sdl.pas',
46 {$IFDEF USE_SDLMIXER}
47 SDL_mixer in '../lib/sdl/sdl_mixer.pas',
48 {$ENDIF}
49 {$ENDIF}
50 {$IFDEF USE_SDL2}
51 SDL2 in '../lib/sdl2/sdl2.pas',
52 {$IFDEF USE_SDLMIXER}
53 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
54 {$ENDIF}
55 {$ENDIF}
56 {$IFDEF USE_SYSSTUB}
57 {$IFDEF USE_SDLMIXER}
58 SDL2 in '../lib/sdl2/sdl2.pas',
59 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
60 {$ENDIF}
61 {$ENDIF}
63 {$IFDEF USE_OPENAL}
64 AL in '../lib/openal/al.pas',
65 e_soundfile in '../engine/e_soundfile.pas',
66 {$IF DEFINED(USE_SDL) OR DEFINED(USE_SDL2)}
67 e_soundfile_wav in '../engine/e_soundfile_wav.pas',
68 {$ENDIF}
69 {$IFDEF USE_VORBIS}
70 vorbis in '../lib/vorbis/vorbis.pas',
71 e_soundfile_vorbis in '../engine/e_soundfile_vorbis.pas',
72 {$ENDIF}
73 {$IFDEF USE_FLUIDSYNTH}
74 fluidsynth in '../lib/fluidsynth/fluidsynth.pas',
75 e_soundfile_fluid in '../engine/e_soundfile_fluid.pas',
76 {$ENDIF}
77 {$IFDEF USE_MODPLUG}
78 modplug in '../lib/modplug/modplug.pas',
79 e_soundfile_modplug in '../engine/e_soundfile_modplug.pas',
80 {$ENDIF}
81 {$IFDEF USE_XMP}
82 xmp in '../lib/xmp/xmp.pas',
83 e_soundfile_xmp in '../engine/e_soundfile_xmp.pas',
84 {$ENDIF}
85 {$IFDEF USE_GME}
86 gme in '../lib/gme/gme.pas',
87 e_soundfile_gme in '../engine/e_soundfile_gme.pas',
88 {$ENDIF}
89 {$IFDEF USE_MPG123}
90 mpg123 in '../lib/mpg123/mpg123.pas',
91 e_soundfile_mp3 in '../engine/e_soundfile_mp3.pas',
92 {$ENDIF}
93 {$IFDEF USE_OPUS}
94 opus in '../lib/opus/opus.pas',
95 e_soundfile_opus in '../engine/e_soundfile_opus.pas',
96 {$ENDIF}
97 {$IF DEFINED(USE_VORBIS) OR DEFINED(USE_OPUS)}
98 ogg in '../lib/vorbis/ogg.pas', // this has to come last because link order
99 {$ENDIF}
100 {$ENDIF}
102 ENet in '../lib/enet/enet.pp',
103 e_input in '../engine/e_input.pas',
104 e_log in '../engine/e_log.pas',
105 e_sound in '../engine/e_sound.pas',
106 e_msg in '../engine/e_msg.pas',
107 e_res in '../engine/e_res.pas',
108 utils in '../shared/utils.pas',
109 xstreams in '../shared/xstreams.pas',
110 sfs in '../sfs/sfs.pas',
111 sfsPlainFS in '../sfs/sfsPlainFS.pas',
112 sfsZipFS in '../sfs/sfsZipFS.pas',
113 wadreader in '../shared/wadreader.pas',
114 MAPDEF in '../shared/MAPDEF.pas',
115 CONFIG in '../shared/CONFIG.pas',
116 g_base in 'g_base.pas',
117 g_basic in 'g_basic.pas',
118 g_console in 'g_console.pas',
119 g_net in 'g_net.pas',
120 g_netmsg in 'g_netmsg.pas',
121 g_nethandler in 'g_nethandler.pas',
122 g_netmaster in 'g_netmaster.pas',
123 g_res_downloader in 'g_res_downloader.pas',
124 g_grid in 'g_grid.pas',
125 g_game in 'g_game.pas',
126 {$IFDEF ENABLE_GFX}
127 g_gfx in 'g_gfx.pas',
128 {$ENDIF}
129 {$IFDEF ENABLE_GIBS}
130 g_gibs in 'g_gibs.pas',
131 {$ENDIF}
132 {$IFDEF ENABLE_SHELLS}
133 g_shells in 'g_shells.pas',
134 {$ENDIF}
135 {$IFDEF ENABLE_CORPSES}
136 g_corpses in 'g_corpses.pas',
137 {$ENDIF}
138 g_items in 'g_items.pas',
139 g_map in 'g_map.pas',
140 g_monsters in 'g_monsters.pas',
141 g_options in 'g_options.pas',
142 g_phys in 'g_phys.pas',
143 g_player in 'g_player.pas',
144 g_playermodel in 'g_playermodel.pas',
145 g_saveload in 'g_saveload.pas',
146 g_sound in 'g_sound.pas',
147 g_textures in 'g_textures.pas',
148 g_triggers in 'g_triggers.pas',
149 g_weapons in 'g_weapons.pas',
150 g_window in 'g_window.pas',
151 {$IFNDEF HEADLESS}
152 {$IFDEF USE_SYSSTUB}
153 g_system in 'stub/g_system.pas',
154 {$ENDIF}
155 {$IFDEF USE_SDL}
156 g_system in 'sdl/g_system.pas',
157 {$ENDIF}
158 {$IFDEF USE_SDL2}
159 g_system in 'sdl2/g_system.pas',
160 {$ENDIF}
161 {$ENDIF}
163 {$IFNDEF HEADLESS}
164 {$I ../shared/vampimg.inc}
165 r_animations in 'opengl/r_animations.pas',
166 r_console in 'opengl/r_console.pas',
167 r_game in 'opengl/r_game.pas',
168 {$IFDEF ENABLE_GFX}
169 r_gfx in 'opengl/r_gfx.pas',
170 {$ENDIF}
171 r_graphics in 'opengl/r_graphics.pas',
172 r_items in 'opengl/r_items.pas',
173 r_map in 'opengl/r_map.pas',
174 r_monsters in 'opengl/r_monsters.pas',
175 r_netmaster in 'opengl/r_netmaster.pas',
176 r_player in 'opengl/r_player.pas',
177 r_playermodel in 'opengl/r_playermodel.pas',
178 r_render in 'opengl/r_render.pas',
179 r_texture in 'opengl/r_texture.pas',
180 r_textures in 'opengl/r_textures.pas',
181 r_weapons in 'opengl/r_weapons.pas',
182 r_window in 'opengl/r_window.pas',
183 {$IFDEF ENABLE_TOUCH}
184 r_touch in 'opengl/r_touch.pas',
185 {$ENDIF}
186 {$IFDEF ENABLE_MENU}
187 g_gui in 'g_gui.pas',
188 g_menu in 'g_menu.pas',
189 {$ENDIF}
190 {$ENDIF}
192 {$IFDEF USE_FMOD}
193 fmod in '../lib/FMOD/fmod.pas',
194 fmoderrors in '../lib/FMOD/fmoderrors.pas',
195 fmodpresets in '../lib/FMOD/fmodpresets.pas',
196 fmodtypes in '../lib/FMOD/fmodtypes.pas',
197 {$ENDIF}
198 xprofiler in '../shared/xprofiler.pas',
199 binheap in '../shared/binheap.pas',
200 hashtable in '../shared/hashtable.pas',
201 fhashdb in '../shared/fhashdb.pas',
202 idpool in '../shared/idpool.pas',
203 xparser in '../shared/xparser.pas',
204 xdynrec in '../shared/xdynrec.pas',
205 exoma in '../shared/exoma.pas',
206 envvars in '../shared/envvars.pas',
207 g_panel in 'g_panel.pas',
208 g_language in 'g_language.pas',
210 {$IFDEF ENABLE_HOLMES}
211 g_holmes in 'g_holmes.pas',
213 sdlcarcass in '../flexui/sdlcarcass.pas',
214 //sdlstandalone in '../flexui/sdlstandalone.pas',
216 fui_wadread in '../flexui/fui_wadread.pas',
217 fui_common in '../flexui/fui_common.pas',
218 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
219 fui_events in '../flexui/fui_events.pas',
220 fui_style in '../flexui/fui_style.pas',
221 fui_flexlay in '../flexui/fui_flexlay.pas',
222 fui_ctls in '../flexui/fui_ctls.pas',
223 {$ENDIF}
225 SysUtils, Classes;
227 {$IFDEF WINDOWS}
228 {$R *.res}
229 {$ENDIF}
231 const
232 autoexecScript = 'autoexec.cfg';
234 var
235 noct: Boolean = False;
236 binPath: AnsiString = '';
237 forceBinDir: Boolean = False;
238 {$IFDEF USE_SDLMIXER}
239 UseNativeMusic: Boolean;
240 {$ENDIF}
242 Time_Old: Int64;
243 NoSound: Boolean;
245 procedure Update ();
246 begin
247 // remember old mobj positions, prepare for update
248 g_Game_PreUpdate();
249 // server: receive client commands for new frame
250 // client: receive game state changes from server
251 if (NetMode = NET_SERVER) then g_Net_Host_Update()
252 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
253 // think
254 {$IFNDEF HEADLESS}
255 r_Render_Update;
256 {$ENDIF}
257 g_Game_Update();
258 // server: send any accumulated outgoing data to clients
259 if NetMode = NET_SERVER then g_Net_Flush();
260 end;
262 function ProcessMessage (): Boolean;
263 var
264 i, t: Integer;
265 flag: Boolean;
266 Time, Time_Delta: Int64;
267 Frame: Int64;
268 begin
269 {$IFDEF HEADLESS}
270 Result := False;
271 {$ELSE}
272 Result := sys_HandleInput();
273 {$ENDIF}
275 Time := GetTickCount64();
276 Time_Delta := Time-Time_Old;
278 flag := false;
280 if wNeedTimeReset then
281 begin
282 Frame := 0;
283 Time_Delta := 28;
284 wNeedTimeReset := false;
285 end;
287 g_Map_ProfilersBegin();
288 g_Mons_ProfilersBegin();
290 t := Time_Delta div 28;
291 if (t > 0) then
292 begin
293 flag := true;
294 for i := 1 to t do
295 Update();
296 end;
298 g_Map_ProfilersEnd();
299 g_Mons_ProfilersEnd();
301 if (gExit = EXIT_QUIT) then
302 begin
303 result := true;
304 exit;
305 end;
307 // Время предыдущего обновления
308 if flag then
309 Time_Old := Time - (Time_Delta mod 28);
311 // don't wait if VSync is on, GL already probably waits enough
312 if gLerpActors then
313 flag := (Time - Frame >= gFrameTime) or gVSync;
315 if flag then
316 begin
317 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
318 gLerpFactor := 1.0
319 else
320 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
321 {$IFNDEF HEADLESS}
322 r_Render_Draw;
323 sys_Repaint;
324 {$ENDIF}
325 Frame := Time
326 end
327 else
328 Sleep(1);
330 e_SoundUpdate();
331 end;
333 procedure DebugOptions;
334 var
335 idx: Integer;
336 arg: AnsiString;
337 mdfo: TStream;
338 {$IFDEF ENABLE_HOLMES}
339 itmp: Integer;
340 valres: Word;
341 {$ENDIF}
342 begin
343 idx := 1;
344 while (idx <= ParamCount) do
345 begin
346 arg := ParamStr(idx);
347 Inc(idx);
348 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
349 if arg = '--jah' then g_profile_history_size := 100;
350 if arg = '--no-los' then gmon_dbg_los_enabled := false;
352 if arg = '--profile-render' then g_profile_frame_draw := true;
353 if arg = '--profile-coldet' then g_profile_collision := true;
354 if arg = '--profile-los' then g_profile_los := true;
356 {$IFDEF ENABLE_GFX}
357 if arg = '--no-particles' then gpart_dbg_enabled := false;
358 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
359 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
360 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
361 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
362 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
363 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
364 {$ENDIF}
366 if arg = '--debug-input' then g_dbg_input := True;
368 {.$IF DEFINED(D2F_DEBUG)}
369 if arg = '--aimline' then g_dbg_aimline_on := true;
370 {.$ENDIF}
372 {$IFDEF ENABLE_HOLMES}
373 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
375 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
376 begin
377 if (idx <= ParamCount) then
378 begin
379 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
380 Inc(idx);
381 end;
382 end;
384 if (arg = '--holmes-font') or (arg = '-holmes-font') then
385 begin
386 if (idx <= ParamCount) then
387 begin
388 itmp := 0;
389 val(ParamStr(idx), itmp, valres);
390 {$IFNDEF HEADLESS}
391 if (valres = 0) and (not g_holmes_imfunctional) then
392 begin
393 case itmp of
394 8: uiContext.font := 'win8';
395 14: uiContext.font := 'win14';
396 16: uiContext.font := 'win16';
397 end;
398 end;
399 {$ELSE}
400 // fuck off, fpc!
401 itmp := itmp;
402 valres := valres;
403 {$ENDIF}
404 Inc(idx);
405 end;
406 end;
407 {$ENDIF}
409 if (arg = '--game-scale') or (arg = '-game-scale') then
410 begin
411 if (idx <= ParamCount) then
412 begin
413 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
414 Inc(idx);
415 end;
416 end;
418 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
419 begin
420 mdfo := createDiskFile('mapdef.txt');
421 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
422 mdfo.Free();
423 Halt(0);
424 end;
426 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
427 begin
428 if (idx <= ParamCount) then
429 begin
430 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
431 Inc(idx);
432 end;
433 end;
434 end;
435 end;
437 function GetBinaryPath (): AnsiString;
438 {$IFDEF LINUX}
439 var sl: AnsiString;
440 {$ENDIF}
441 begin
442 result := ExtractFilePath(ParamStr(0));
443 {$IFDEF LINUX}
444 // it may be a symlink; do some guesswork here
445 sl := fpReadLink(ExtractFileName(ParamStr(0)));
446 if (sl = ParamStr(0)) then
447 begin
448 // use current directory, as we don't have anything better
449 //result := '.';
450 GetDir(0, result);
451 end;
452 {$ENDIF}
453 result := fixSlashes(result);
454 if (length(result) > 0) and (result[length(result)] <> '/') then
455 result := result + '/';
456 end;
458 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
459 var dir: AnsiString;
460 begin
461 e_LogWriteln(msg + ':');
462 for dir in dirs do
463 e_LogWriteln(' ' + dir);
464 end;
466 {$IFDEF DARWIN}
467 function NSStringToAnsiString (s: NSString): AnsiString;
468 var i: Integer;
469 begin
470 result := '';
471 for i := 0 to s.length - 1 do
472 result := result + AnsiChar(s.characterAtIndex(i));
473 end;
475 function GetBundlePath (): AnsiString;
476 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
477 begin
478 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
479 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
480 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
481 CFRelease(pathRef);
482 CFRelease(pathCFStr);
483 Result := pathStr;
484 end;
485 {$ENDIF}
487 procedure InitPath;
488 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
490 procedure AddDir (var dirs: SSArray; append: AnsiString);
491 begin
492 SetLength(dirs, Length(dirs) + 1);
493 dirs[High(dirs)] := ExpandFileName(append)
494 end;
496 function IsSep (ch: Char): Boolean;
497 begin
498 {$IFDEF WINDOWS}
499 result := (ch = '/') or (ch = '\');
500 {$ELSE}
501 result := (ch = '/');
502 {$ENDIF}
503 end;
505 function OptimizePath (dir: AnsiString): AnsiString;
506 var i, len: Integer; s: AnsiString;
507 begin
508 i := 1; len := Length(dir); s := '';
509 while i <= len do
510 begin
511 if IsSep(dir[i]) then
512 begin
513 s := s + DirectorySeparator;
514 Inc(i);
515 while (i <= len) and IsSep(dir[i]) do Inc(i);
516 if (i <= len) and (dir[i] = '.') then
517 begin
518 if (i = len) or IsSep(dir[i + 1]) then
519 begin
520 Inc(i)
521 end
522 else if (i + 1 <= len) and (dir[i + 1] = '.') then
523 begin
524 if (i + 1 = len) or IsSep(dir[i + 2]) then
525 begin
526 s := e_UpperDir(s);
527 Inc(i, 2)
528 end
529 end
530 end
531 end
532 else
533 begin
534 s := s + dir[i];
535 Inc(i)
536 end
537 end;
538 result := s
539 end;
541 procedure OptimizeDirs (var dirs: SSArray);
542 var i, j, k: Integer;
543 begin
544 for i := 0 to High(dirs) do
545 dirs[i] := OptimizePath(dirs[i]);
546 // deduplicate
547 i := High(dirs);
548 while i >= 0 do
549 begin
550 j := 0;
551 while j < i do
552 begin
553 if dirs[j] = dirs[i] then
554 begin
555 for k := j + 1 to High(dirs) do
556 dirs[k - 1] := dirs[k];
557 Dec(i);
558 SetLength(dirs, High(dirs))
559 end
560 else
561 begin
562 Inc(j)
563 end
564 end;
565 Dec(i)
566 end
567 end;
569 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
570 var s: AnsiString;
571 begin
572 if Length(dirs) = 0 then
573 for s in base do
574 AddDir(dirs, e_CatPath(s, append));
575 OptimizeDirs(dirs)
576 end;
578 function GetDefaultRODirs (): SSArray;
579 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
580 var home: AnsiString;
581 {$ENDIF}
582 {$IFDEF WINDOWS}
583 var appdata: AnsiString;
584 {$ENDIF}
585 {$IFDEF DARWIN}
586 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
587 {$ENDIF}
588 begin
589 result := nil;
590 {$IFDEF DARWIN}
591 bundle := GetBundlePath();
592 if ExtractFileExt(bundle) <> '.app' then
593 AddDir(result, binpath);
594 {$ELSE}
595 AddDir(result, binPath);
596 {$ENDIF}
597 if forceBinDir = false then
598 begin
599 {$IFDEF USE_SDL2}
600 AddDir(result, SDL_GetBasePath());
601 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
602 {$ENDIF}
603 {$IFDEF WINDOWS}
604 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
605 if appdata <> '' then
606 AddDir(result, appdata);
607 {$ENDIF}
608 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
609 AddDir(result, '/usr/share/doom2df');
610 AddDir(result, '/usr/local/share/doom2df');
611 home := GetEnvironmentVariable('HOME');
612 if home <> '' then
613 AddDir(result, e_CatPath(home, '.doom2df'));
614 {$ENDIF}
615 {$IFDEF DARWIN}
616 bundle := GetBundlePath();
617 if bundle <> '' then
618 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
619 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
620 for i := 0 to dirArr.count - 1 do
621 begin
622 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
623 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
624 end;
625 {$ENDIF}
626 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
627 AddDir(result, SDL_AndroidGetInternalStoragePath());
628 if SDL_AndroidGetExternalStorageState() <> 0 then
629 AddDir(result, SDL_AndroidGetExternalStoragePath());
630 {$ENDIF}
631 end
632 end;
634 function GetDefaultRWDirs (): SSArray;
635 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
636 var home: AnsiString;
637 {$ENDIF}
638 {$IFDEF WINDOWS}
639 var appdata: AnsiString;
640 {$ENDIF}
641 {$IFDEF DARWIN}
642 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
643 {$ENDIF}
644 begin
645 result := nil;
646 {$IFDEF DARWIN}
647 bundle := GetBundlePath();
648 if ExtractFileExt(bundle) <> '.app' then
649 AddDir(result, binPath);
650 {$ELSE}
651 AddDir(result, binPath);
652 {$ENDIF}
653 if forceBinDir = false then
654 begin
655 {$IFDEF USE_SDL2}
656 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
657 {$ENDIF}
658 {$IFDEF WINDOWS}
659 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
660 if appdata <> '' then
661 AddDir(result, appdata);
662 {$ENDIF}
663 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
664 home := GetEnvironmentVariable('HOME');
665 if home <> '' then
666 AddDir(result, e_CatPath(home, '.doom2df'));
667 {$ENDIF}
668 {$IFDEF DARWIN}
669 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
670 for i := 0 to dirArr.count - 1 do
671 begin
672 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
673 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
674 end;
675 {$ENDIF}
676 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
677 if SDL_AndroidGetExternalStorageState() <> 0 then
678 AddDir(result, SDL_AndroidGetExternalStoragePath());
679 {$ENDIF}
680 end
681 end;
683 begin
684 forceBinDir := false;
685 binPath := GetBinaryPath();
687 i := 1;
688 while i < ParamCount do
689 begin
690 case ParamStr(i) of
691 '--like-windoze': forceBinDir := true;
692 '--rw-dir':
693 begin
694 Inc(i);
695 rwdir := ParamStr(i);
696 (* RW *)
697 AddDir(LogDirs, e_CatPath(rwdir, ''));
698 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
699 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
700 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
701 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
702 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
703 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
704 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
705 (* RO *)
706 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
707 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
708 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
709 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
710 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
711 end;
712 '--ro-dir':
713 begin
714 Inc(i);
715 rodir := ParamStr(i);
716 (* RO *)
717 AddDir(DataDirs, e_CatPath(rodir, 'data'));
718 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
719 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
720 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
721 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
722 end;
723 '--game-wad':
724 begin
725 Inc(i);
726 GameWADName := ParamStr(i);
727 end;
728 '--config':
729 begin
730 Inc(i);
731 gConfigScript := ParamStr(i);
732 end;
733 end;
734 Inc(i)
735 end;
737 // prefer bin dir if it writable and contains game.wad
738 if forceBinDir = false then
739 begin
740 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
741 if e_CanCreateFilesAt(binPath) then
742 forceBinDir := true
743 end;
745 (* RO *)
746 rodirs := GetDefaultRODirs();
747 AddDef(DataDirs, rodirs, 'data');
748 AddDef(ModelDirs, rodirs, 'data/models');
749 AddDef(MegawadDirs, rodirs, 'maps/megawads');
750 AddDef(MapDirs, rodirs, 'maps');
751 AddDef(WadDirs, rodirs, 'wads');
753 (* RW *)
754 rwdirs := GetDefaultRWDirs();
755 AddDef(LogDirs, rwdirs, '');
756 AddDef(SaveDirs, rwdirs, 'data');
757 AddDef(CacheDirs, rwdirs, 'data/cache');
758 AddDef(ConfigDirs, rwdirs, '');
759 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
760 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
761 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
762 AddDef(StatsDirs, rwdirs, 'stats');
764 for i := 0 to High(MapDirs) do
765 AddDir(AllMapDirs, MapDirs[i]);
766 for i := 0 to High(MegawadDirs) do
767 AddDir(AllMapDirs, MegawadDirs[i]);
768 OptimizeDirs(AllMapDirs);
770 // HACK: ensure the screenshots folder also has a stats subfolder in it
771 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
772 if rwdir <> '' then CreateDir(rwdir + '/stats');
773 end;
775 procedure EntryParams;
776 var i: Integer;
777 begin
778 i := 1;
779 while i <= ParamCount do
780 begin
781 case ParamStr(i) of
782 '--gdb': noct := true;
783 '--log', '--con-stdout': conbufDumpToStdOut := true;
784 '--safe-log': e_SetSafeSlowLog(true);
785 '--log-file':
786 if i + 1 <= ParamCount then
787 begin
788 Inc(i);
789 LogFileName := ParamStr(i)
790 end;
791 '--no-fbo': glRenderToFBO := false;
792 end;
793 Inc(i)
794 end
795 end;
797 procedure InitLog;
798 var rwdir: AnsiString;
799 begin
800 if LogFileName = '' then
801 begin
802 rwdir := e_GetWriteableDir(LogDirs, false);
803 if rwdir <> '' then
804 begin
805 {$IFDEF HEADLESS}
806 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
807 {$ELSE}
808 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
809 {$ENDIF}
810 end
811 end;
812 if LogFileName <> '' then
813 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
814 e_InitWritelnDriver
815 end;
817 procedure InitPrep;
818 begin
819 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
820 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
821 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
822 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
823 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
825 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
826 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
828 PrintDirs('DataDirs', DataDirs);
829 PrintDirs('ModelDirs', ModelDirs);
830 PrintDirs('MegawadDirs', MegawadDirs);
831 PrintDirs('MapDirs', MapDirs);
832 PrintDirs('WadDirs', WadDirs);
834 PrintDirs('LogDirs', LogDirs);
835 PrintDirs('SaveDirs', SaveDirs);
836 PrintDirs('CacheDirs', CacheDirs);
837 PrintDirs('ConfigDirs', ConfigDirs);
838 PrintDirs('ScreenshotDirs', ScreenshotDirs);
839 PrintDirs('StatsDirs', StatsDirs);
840 PrintDirs('MapDownloadDirs', MapDownloadDirs);
841 PrintDirs('WadDownloadDirs', WadDownloadDirs);
843 {$IFDEF HEADLESS}
844 {$IFDEF USE_SDLMIXER}
845 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
846 {$ELSE}
847 NoSound := True; // FMOD backend will sort it out
848 {$ENDIF}
849 {$ELSE}
850 NoSound := False;
851 {$ENDIF}
853 GameWAD := e_FindWad(DataDirs, GameWADName);
854 if GameWad = '' then
855 begin
856 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
857 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
858 if forceBinDir = false then
859 SDL_ShowSimpleMessageBox(
860 SDL_MESSAGEBOX_ERROR,
861 'Doom 2D Forever',
862 PChar('WAD ' + GameWADName + ' not found in data directories.'),
863 nil
864 );
865 {$ENDIF}
866 e_DeinitLog;
867 Halt(1);
868 end
869 end;
871 {$IFDEF ENABLE_HOLMES}
872 procedure InitHolmes;
873 var flexloaded: Boolean;
874 begin
875 flexloaded := true;
876 if not fuiAddWad('flexui.wad') then
877 begin
878 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
879 end;
880 try
881 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
882 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
883 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
884 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
885 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
886 except on e: Exception do
887 begin
888 writeln('ERROR loading FlexUI fonts');
889 flexloaded := false;
890 //raise;
891 end;
892 else
893 begin
894 flexloaded := false;
895 //raise;
896 end;
897 end;
898 if flexloaded then
899 begin
900 try
901 e_LogWriteln('FlexUI: loading stylesheet...');
902 uiLoadStyles('flexui/widgets.wgs');
903 except on e: TParserException do
904 begin
905 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
906 //raise;
907 flexloaded := false;
908 end;
909 else
910 begin
911 //raise;
912 flexloaded := false;
913 end;
914 end;
915 end;
916 g_holmes_imfunctional := not flexloaded;
917 if not g_holmes_imfunctional then
918 begin
919 uiInitialize();
920 uiContext.font := 'win14';
921 end;
922 if assigned(oglInitCB) then oglInitCB;
923 end;
925 procedure FreeHolmes;
926 begin
927 if assigned(oglDeinitCB) then
928 oglDeinitCB
929 end;
930 {$ENDIF}
932 procedure InitSound;
933 {$IFDEF USE_SDLMIXER}
934 var timiditycfg: AnsiString;
935 var oldcwd, newcwd: RawByteString;
936 {$ENDIF}
937 begin
938 if NoSound = false then
939 begin
940 e_WriteLog('Initializing sound system', TMsgType.Notify);
941 {$IFDEF USE_SDLMIXER}
942 newcwd := '';
943 if UseNativeMusic then
944 SetEnvVar('SDL_NATIVE_MUSIC', '1');
945 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
946 if timiditycfg = '' then
947 begin
948 timiditycfg := 'timidity.cfg';
949 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
950 begin
951 timiditycfg := ExpandFileName(timiditycfg);
952 newcwd := ExtractFileDir(timiditycfg);
953 SetEnvVar('TIMIDITY_CFG', timiditycfg);
954 end
955 else
956 timiditycfg := '';
957 end;
958 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
959 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
960 {$ENDIF}
961 e_InitSoundSystem(NoSound);
962 {$IFDEF USE_SDLMIXER}
963 if e_TimidityDecoder and (newcwd <> '') then
964 begin
965 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
966 (* CWD not restored after sound init because timidity *)
967 (* store relative pathes internally and load patches *)
968 (* later. I hope game never relies on CWD. *)
969 oldcwd := '';
970 GetDir(0, oldcwd);
971 ChDir(newcwd);
972 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
973 end;
974 {$ENDIF}
975 end;
976 end;
978 procedure ScreenResize (w, h: Integer);
979 begin
980 {$IFNDEF HEADLESS}
981 r_Render_Resize(w, h);
982 {$IFDEF ENABLE_HOLMES}
983 fuiScrWdt := w;
984 fuiScrHgt := h;
985 {$ENDIF}
986 {$IFNDEF ANDROID}
987 (* This will fix menu reset on keyboard showing *)
988 {$IFDEF ENABLE_MENU}
989 g_Menu_Reset;
990 {$ENDIF}
991 {$ENDIF}
992 //g_Game_ClearLoading;
993 {$IFDEF ENABLE_HOLMES}
994 if assigned(oglInitCB) then oglInitCB;
995 {$ENDIF}
996 {$ENDIF}
997 end;
999 procedure Startup;
1000 begin
1001 Randomize;
1002 InitPath;
1003 InitLog;
1004 InitPrep;
1005 e_Input_Initialize;
1006 InitSound;
1007 {$IFNDEF HEADLESS}
1008 sys_Init;
1009 sys_CharPress := @CharPress; (* install hook *)
1010 sys_ScreenResize := @ScreenResize; (* install hook *)
1011 {$ENDIF}
1012 g_Options_SetDefault;
1013 g_Options_SetDefaultVideo;
1014 g_Console_Initialize;
1015 // TODO move load configs here
1016 g_Language_Set(gLanguage);
1017 {$IFNDEF HEADLESS}
1018 r_Render_Initialize;
1019 {$ENDIF}
1020 DebugOptions;
1021 g_Net_InitLowLevel;
1022 // TODO init serverlist
1023 {$IFDEF ENABLE_HOLMES}
1024 InitHolmes;
1025 {$ENDIF}
1026 {$IFDEF HEADLESS}
1027 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1028 {$ELSE}
1029 g_PlayerModel_LoadAll;
1030 r_Render_Load;
1031 {$ENDIF}
1032 g_Game_Init;
1033 {$IFDEF ENABLE_MENU}
1034 g_Menu_Init;
1035 g_GUI_Init;
1036 {$ENDIF}
1037 g_Game_Process_Params;
1038 // TODO reload GAME textures
1039 g_Console_Init; // welcome message
1040 {$IFDEF ENABLE_MENU}
1041 if (not gGameOn) and gAskLanguage then
1042 g_Menu_AskLanguage;
1043 {$ENDIF}
1044 Time_Old := GetTickCount64();
1045 while not ProcessMessage() do begin end;
1046 g_Console_WriteGameConfig;
1047 {$IFDEF ENABLE_MENU}
1048 g_GUI_Destroy;
1049 g_Menu_Free;
1050 {$ENDIF}
1051 {$IFNDEF HEADLESS}
1052 r_Render_Free;
1053 {$ENDIF}
1054 {$IFDEF ENABLE_HOLMES}
1055 FreeHolmes;
1056 {$ENDIF}
1057 g_Net_Slist_ShutdownAll;
1058 g_Net_DeinitLowLevel;
1059 (* g_Touch_Finalize; *)
1060 {$IFNDEF HEADLESS}
1061 r_Render_Finalize;
1062 sys_Final;
1063 {$ENDIF}
1064 g_Console_Finalize;
1065 e_ReleaseSoundSystem;
1066 e_Input_Finalize;
1067 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1068 end;
1070 procedure InitCVars;
1071 begin
1072 {$IFDEF USE_SDLMIXER}
1073 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1074 {$IFDEF DARWIN}
1075 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1076 {$ELSE}
1077 UseNativeMusic := false;
1078 {$ENDIF}
1079 {$ENDIF}
1080 end;
1082 procedure EntryPoint;
1083 begin
1084 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1085 InitCVars;
1086 EntryParams;
1087 e_Log_Initialize;
1088 {$IFDEF HEADLESS}
1089 conbufDumpToStdOut := true;
1090 {$ENDIF}
1091 if noct then
1092 Startup
1093 else
1094 try
1095 Startup
1096 except on e: Exception do
1097 e_WriteStackTrace(e.message)
1098 else
1099 e_WriteStackTrace('FATAL ERROR')
1100 end;
1101 e_Log_Finalize
1102 end;
1104 {$IFDEF ANDROID}
1105 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1106 begin
1107 {$IFDEF ANDROID}
1108 System.argc := argc;
1109 System.argv := argv;
1110 {$ENDIF}
1111 EntryPoint;
1112 result := 0
1113 end;
1115 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1116 begin
1117 result:= JNI_VERSION_1_6;
1118 end;
1120 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1121 begin
1122 end;
1124 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1125 exports SDL_main name 'SDL_main';
1126 exports JNI_OnLoad name 'JNI_OnLoad';
1127 exports JNI_OnUnload name 'JNI_Unload';
1128 {$ELSE}
1129 begin
1130 EntryPoint
1131 {$ENDIF}
1133 end.