DEADSOFTWARE

game: disable gibs 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,
46 {$IFDEF USE_SDLMIXER}
47 SDL_mixer,
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 g_items in 'g_items.pas',
133 g_map in 'g_map.pas',
134 g_monsters in 'g_monsters.pas',
135 g_options in 'g_options.pas',
136 g_phys in 'g_phys.pas',
137 g_player in 'g_player.pas',
138 g_playermodel in 'g_playermodel.pas',
139 g_saveload in 'g_saveload.pas',
140 g_sound in 'g_sound.pas',
141 g_textures in 'g_textures.pas',
142 g_triggers in 'g_triggers.pas',
143 g_weapons in 'g_weapons.pas',
144 g_window in 'g_window.pas',
145 {$IFNDEF HEADLESS}
146 {$IFDEF USE_SYSSTUB}
147 g_system in 'stub/g_system.pas',
148 {$ENDIF}
149 {$IFDEF USE_SDL}
150 g_system in 'sdl/g_system.pas',
151 {$ENDIF}
152 {$IFDEF USE_SDL2}
153 g_system in 'sdl2/g_system.pas',
154 {$ENDIF}
155 {$ENDIF}
157 {$IFNDEF HEADLESS}
158 {$I ../shared/vampimg.inc}
159 r_animations in 'opengl/r_animations.pas',
160 r_console in 'opengl/r_console.pas',
161 r_game in 'opengl/r_game.pas',
162 {$IFDEF ENABLE_GFX}
163 r_gfx in 'opengl/r_gfx.pas',
164 {$ENDIF}
165 r_graphics in 'opengl/r_graphics.pas',
166 r_items in 'opengl/r_items.pas',
167 r_map in 'opengl/r_map.pas',
168 r_monsters in 'opengl/r_monsters.pas',
169 r_netmaster in 'opengl/r_netmaster.pas',
170 r_player in 'opengl/r_player.pas',
171 r_playermodel in 'opengl/r_playermodel.pas',
172 r_render in 'opengl/r_render.pas',
173 r_texture in 'opengl/r_texture.pas',
174 r_textures in 'opengl/r_textures.pas',
175 r_weapons in 'opengl/r_weapons.pas',
176 r_window in 'opengl/r_window.pas',
177 {$IFDEF ENABLE_TOUCH}
178 r_touch in 'opengl/r_touch.pas',
179 {$ENDIF}
180 {$IFDEF ENABLE_MENU}
181 g_gui in 'g_gui.pas',
182 g_menu in 'g_menu.pas',
183 {$ENDIF}
184 {$ENDIF}
186 {$IFDEF USE_FMOD}
187 fmod in '../lib/FMOD/fmod.pas',
188 fmoderrors in '../lib/FMOD/fmoderrors.pas',
189 fmodpresets in '../lib/FMOD/fmodpresets.pas',
190 fmodtypes in '../lib/FMOD/fmodtypes.pas',
191 {$ENDIF}
192 xprofiler in '../shared/xprofiler.pas',
193 binheap in '../shared/binheap.pas',
194 hashtable in '../shared/hashtable.pas',
195 fhashdb in '../shared/fhashdb.pas',
196 idpool in '../shared/idpool.pas',
197 xparser in '../shared/xparser.pas',
198 xdynrec in '../shared/xdynrec.pas',
199 exoma in '../shared/exoma.pas',
200 envvars in '../shared/envvars.pas',
201 g_panel in 'g_panel.pas',
202 g_language in 'g_language.pas',
204 {$IFDEF ENABLE_HOLMES}
205 g_holmes in 'g_holmes.pas',
207 sdlcarcass in '../flexui/sdlcarcass.pas',
208 //sdlstandalone in '../flexui/sdlstandalone.pas',
210 fui_wadread in '../flexui/fui_wadread.pas',
211 fui_common in '../flexui/fui_common.pas',
212 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
213 fui_events in '../flexui/fui_events.pas',
214 fui_style in '../flexui/fui_style.pas',
215 fui_flexlay in '../flexui/fui_flexlay.pas',
216 fui_ctls in '../flexui/fui_ctls.pas',
217 {$ENDIF}
219 SysUtils, Classes;
221 {$IFDEF WINDOWS}
222 {$R *.res}
223 {$ENDIF}
225 const
226 autoexecScript = 'autoexec.cfg';
228 var
229 noct: Boolean = False;
230 binPath: AnsiString = '';
231 forceBinDir: Boolean = False;
233 Time_Old: Int64;
234 NoSound: Boolean;
236 procedure Update ();
237 begin
238 // remember old mobj positions, prepare for update
239 g_Game_PreUpdate();
240 // server: receive client commands for new frame
241 // client: receive game state changes from server
242 if (NetMode = NET_SERVER) then g_Net_Host_Update()
243 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
244 // think
245 {$IFNDEF HEADLESS}
246 r_Render_Update;
247 {$ENDIF}
248 g_Game_Update();
249 // server: send any accumulated outgoing data to clients
250 if NetMode = NET_SERVER then g_Net_Flush();
251 end;
253 function ProcessMessage (): Boolean;
254 var
255 i, t: Integer;
256 flag: Boolean;
257 Time, Time_Delta: Int64;
258 Frame: Int64;
259 begin
260 {$IFDEF HEADLESS}
261 Result := False;
262 {$ELSE}
263 Result := sys_HandleInput();
264 {$ENDIF}
266 Time := GetTickCount64();
267 Time_Delta := Time-Time_Old;
269 flag := false;
271 if wNeedTimeReset then
272 begin
273 Frame := 0;
274 Time_Delta := 28;
275 wNeedTimeReset := false;
276 end;
278 g_Map_ProfilersBegin();
279 g_Mons_ProfilersBegin();
281 t := Time_Delta div 28;
282 if (t > 0) then
283 begin
284 flag := true;
285 for i := 1 to t do
286 Update();
287 end;
289 g_Map_ProfilersEnd();
290 g_Mons_ProfilersEnd();
292 if (gExit = EXIT_QUIT) then
293 begin
294 result := true;
295 exit;
296 end;
298 // Время предыдущего обновления
299 if flag then
300 Time_Old := Time - (Time_Delta mod 28);
302 // don't wait if VSync is on, GL already probably waits enough
303 if gLerpActors then
304 flag := (Time - Frame >= gFrameTime) or gVSync;
306 if flag then
307 begin
308 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
309 gLerpFactor := 1.0
310 else
311 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
312 {$IFNDEF HEADLESS}
313 r_Render_Draw;
314 sys_Repaint;
315 {$ENDIF}
316 Frame := Time
317 end
318 else
319 Sleep(1);
321 e_SoundUpdate();
322 end;
324 procedure DebugOptions;
325 var
326 idx: Integer;
327 arg: AnsiString;
328 mdfo: TStream;
329 {$IFDEF ENABLE_HOLMES}
330 itmp: Integer;
331 valres: Word;
332 {$ENDIF}
333 begin
334 idx := 1;
335 while (idx <= ParamCount) do
336 begin
337 arg := ParamStr(idx);
338 Inc(idx);
339 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
340 if arg = '--jah' then g_profile_history_size := 100;
341 if arg = '--no-los' then gmon_dbg_los_enabled := false;
343 if arg = '--profile-render' then g_profile_frame_draw := true;
344 if arg = '--profile-coldet' then g_profile_collision := true;
345 if arg = '--profile-los' then g_profile_los := true;
347 {$IFDEF ENABLE_GFX}
348 if arg = '--no-particles' then gpart_dbg_enabled := false;
349 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
350 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
351 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
352 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
353 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
354 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
355 {$ENDIF}
357 if arg = '--debug-input' then g_dbg_input := True;
359 {.$IF DEFINED(D2F_DEBUG)}
360 if arg = '--aimline' then g_dbg_aimline_on := true;
361 {.$ENDIF}
363 {$IFDEF ENABLE_HOLMES}
364 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
366 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
367 begin
368 if (idx <= ParamCount) then
369 begin
370 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
371 Inc(idx);
372 end;
373 end;
375 if (arg = '--holmes-font') or (arg = '-holmes-font') then
376 begin
377 if (idx <= ParamCount) then
378 begin
379 itmp := 0;
380 val(ParamStr(idx), itmp, valres);
381 {$IFNDEF HEADLESS}
382 if (valres = 0) and (not g_holmes_imfunctional) then
383 begin
384 case itmp of
385 8: uiContext.font := 'win8';
386 14: uiContext.font := 'win14';
387 16: uiContext.font := 'win16';
388 end;
389 end;
390 {$ELSE}
391 // fuck off, fpc!
392 itmp := itmp;
393 valres := valres;
394 {$ENDIF}
395 Inc(idx);
396 end;
397 end;
398 {$ENDIF}
400 if (arg = '--game-scale') or (arg = '-game-scale') then
401 begin
402 if (idx <= ParamCount) then
403 begin
404 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
405 Inc(idx);
406 end;
407 end;
409 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
410 begin
411 mdfo := createDiskFile('mapdef.txt');
412 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
413 mdfo.Free();
414 Halt(0);
415 end;
417 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
418 begin
419 if (idx <= ParamCount) then
420 begin
421 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
422 Inc(idx);
423 end;
424 end;
425 end;
426 end;
428 function GetBinaryPath (): AnsiString;
429 {$IFDEF LINUX}
430 var sl: AnsiString;
431 {$ENDIF}
432 begin
433 result := ExtractFilePath(ParamStr(0));
434 {$IFDEF LINUX}
435 // it may be a symlink; do some guesswork here
436 sl := fpReadLink(ExtractFileName(ParamStr(0)));
437 if (sl = ParamStr(0)) then
438 begin
439 // use current directory, as we don't have anything better
440 //result := '.';
441 GetDir(0, result);
442 end;
443 {$ENDIF}
444 result := fixSlashes(result);
445 if (length(result) > 0) and (result[length(result)] <> '/') then
446 result := result + '/';
447 end;
449 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
450 var dir: AnsiString;
451 begin
452 e_LogWriteln(msg + ':');
453 for dir in dirs do
454 e_LogWriteln(' ' + dir);
455 end;
457 {$IFDEF DARWIN}
458 function NSStringToAnsiString (s: NSString): AnsiString;
459 var i: Integer;
460 begin
461 result := '';
462 for i := 0 to s.length - 1 do
463 result := result + AnsiChar(s.characterAtIndex(i));
464 end;
466 function GetBundlePath (): AnsiString;
467 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
468 begin
469 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
470 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
471 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
472 CFRelease(pathRef);
473 CFRelease(pathCFStr);
474 Result := pathStr;
475 end;
476 {$ENDIF}
478 procedure InitPath;
479 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
481 procedure AddDir (var dirs: SSArray; append: AnsiString);
482 begin
483 SetLength(dirs, Length(dirs) + 1);
484 dirs[High(dirs)] := ExpandFileName(append)
485 end;
487 function IsSep (ch: Char): Boolean;
488 begin
489 {$IFDEF WINDOWS}
490 result := (ch = '/') or (ch = '\');
491 {$ELSE}
492 result := (ch = '/');
493 {$ENDIF}
494 end;
496 function OptimizePath (dir: AnsiString): AnsiString;
497 var i, len: Integer; s: AnsiString;
498 begin
499 i := 1; len := Length(dir); s := '';
500 while i <= len do
501 begin
502 if IsSep(dir[i]) then
503 begin
504 s := s + DirectorySeparator;
505 Inc(i);
506 while (i <= len) and IsSep(dir[i]) do Inc(i);
507 if (i <= len) and (dir[i] = '.') then
508 begin
509 if (i = len) or IsSep(dir[i + 1]) then
510 begin
511 Inc(i)
512 end
513 else if (i + 1 <= len) and (dir[i + 1] = '.') then
514 begin
515 if (i + 1 = len) or IsSep(dir[i + 2]) then
516 begin
517 s := e_UpperDir(s);
518 Inc(i, 2)
519 end
520 end
521 end
522 end
523 else
524 begin
525 s := s + dir[i];
526 Inc(i)
527 end
528 end;
529 result := s
530 end;
532 procedure OptimizeDirs (var dirs: SSArray);
533 var i, j, k: Integer;
534 begin
535 for i := 0 to High(dirs) do
536 dirs[i] := OptimizePath(dirs[i]);
537 // deduplicate
538 i := High(dirs);
539 while i >= 0 do
540 begin
541 j := 0;
542 while j < i do
543 begin
544 if dirs[j] = dirs[i] then
545 begin
546 for k := j + 1 to High(dirs) do
547 dirs[k - 1] := dirs[k];
548 Dec(i);
549 SetLength(dirs, High(dirs))
550 end
551 else
552 begin
553 Inc(j)
554 end
555 end;
556 Dec(i)
557 end
558 end;
560 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
561 var s: AnsiString;
562 begin
563 if Length(dirs) = 0 then
564 for s in base do
565 AddDir(dirs, e_CatPath(s, append));
566 OptimizeDirs(dirs)
567 end;
569 function GetDefaultRODirs (): SSArray;
570 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
571 var home: AnsiString;
572 {$ENDIF}
573 {$IFDEF WINDOWS}
574 var appdata: AnsiString;
575 {$ENDIF}
576 {$IFDEF DARWIN}
577 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
578 {$ENDIF}
579 begin
580 result := nil;
581 {$IFDEF DARWIN}
582 bundle := GetBundlePath();
583 if ExtractFileExt(bundle) <> '.app' then
584 AddDir(result, binpath);
585 {$ELSE}
586 AddDir(result, binPath);
587 {$ENDIF}
588 if forceBinDir = false then
589 begin
590 {$IFDEF USE_SDL2}
591 AddDir(result, SDL_GetBasePath());
592 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
593 {$ENDIF}
594 {$IFDEF WINDOWS}
595 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
596 if appdata <> '' then
597 AddDir(result, appdata);
598 {$ENDIF}
599 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
600 AddDir(result, '/usr/share/doom2df');
601 AddDir(result, '/usr/local/share/doom2df');
602 home := GetEnvironmentVariable('HOME');
603 if home <> '' then
604 AddDir(result, e_CatPath(home, '.doom2df'));
605 {$ENDIF}
606 {$IFDEF DARWIN}
607 bundle := GetBundlePath();
608 if bundle <> '' then
609 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
610 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
611 for i := 0 to dirArr.count - 1 do
612 begin
613 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
614 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
615 end;
616 {$ENDIF}
617 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
618 AddDir(result, SDL_AndroidGetInternalStoragePath());
619 if SDL_AndroidGetExternalStorageState() <> 0 then
620 AddDir(result, SDL_AndroidGetExternalStoragePath());
621 {$ENDIF}
622 end
623 end;
625 function GetDefaultRWDirs (): SSArray;
626 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
627 var home: AnsiString;
628 {$ENDIF}
629 {$IFDEF WINDOWS}
630 var appdata: AnsiString;
631 {$ENDIF}
632 {$IFDEF DARWIN}
633 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
634 {$ENDIF}
635 begin
636 result := nil;
637 {$IFDEF DARWIN}
638 bundle := GetBundlePath();
639 if ExtractFileExt(bundle) <> '.app' then
640 AddDir(result, binPath);
641 {$ELSE}
642 AddDir(result, binPath);
643 {$ENDIF}
644 if forceBinDir = false then
645 begin
646 {$IFDEF USE_SDL2}
647 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
648 {$ENDIF}
649 {$IFDEF WINDOWS}
650 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
651 if appdata <> '' then
652 AddDir(result, appdata);
653 {$ENDIF}
654 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
655 home := GetEnvironmentVariable('HOME');
656 if home <> '' then
657 AddDir(result, e_CatPath(home, '.doom2df'));
658 {$ENDIF}
659 {$IFDEF DARWIN}
660 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
661 for i := 0 to dirArr.count - 1 do
662 begin
663 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
664 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
665 end;
666 {$ENDIF}
667 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
668 if SDL_AndroidGetExternalStorageState() <> 0 then
669 AddDir(result, SDL_AndroidGetExternalStoragePath());
670 {$ENDIF}
671 end
672 end;
674 begin
675 forceBinDir := false;
676 binPath := GetBinaryPath();
678 i := 1;
679 while i < ParamCount do
680 begin
681 case ParamStr(i) of
682 '--like-windoze': forceBinDir := true;
683 '--rw-dir':
684 begin
685 Inc(i);
686 rwdir := ParamStr(i);
687 (* RW *)
688 AddDir(LogDirs, e_CatPath(rwdir, ''));
689 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
690 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
691 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
692 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
693 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
694 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
695 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
696 (* RO *)
697 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
698 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
699 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
700 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
701 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
702 end;
703 '--ro-dir':
704 begin
705 Inc(i);
706 rodir := ParamStr(i);
707 (* RO *)
708 AddDir(DataDirs, e_CatPath(rodir, 'data'));
709 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
710 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
711 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
712 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
713 end;
714 '--game-wad':
715 begin
716 Inc(i);
717 GameWADName := ParamStr(i);
718 end;
719 '--config':
720 begin
721 Inc(i);
722 gConfigScript := ParamStr(i);
723 end;
724 end;
725 Inc(i)
726 end;
728 // prefer bin dir if it writable and contains game.wad
729 if forceBinDir = false then
730 begin
731 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
732 if e_CanCreateFilesAt(binPath) then
733 forceBinDir := true
734 end;
736 (* RO *)
737 rodirs := GetDefaultRODirs();
738 AddDef(DataDirs, rodirs, 'data');
739 AddDef(ModelDirs, rodirs, 'data/models');
740 AddDef(MegawadDirs, rodirs, 'maps/megawads');
741 AddDef(MapDirs, rodirs, 'maps');
742 AddDef(WadDirs, rodirs, 'wads');
744 (* RW *)
745 rwdirs := GetDefaultRWDirs();
746 AddDef(LogDirs, rwdirs, '');
747 AddDef(SaveDirs, rwdirs, 'data');
748 AddDef(CacheDirs, rwdirs, 'data/cache');
749 AddDef(ConfigDirs, rwdirs, '');
750 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
751 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
752 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
753 AddDef(StatsDirs, rwdirs, 'stats');
755 for i := 0 to High(MapDirs) do
756 AddDir(AllMapDirs, MapDirs[i]);
757 for i := 0 to High(MegawadDirs) do
758 AddDir(AllMapDirs, MegawadDirs[i]);
759 OptimizeDirs(AllMapDirs);
761 // HACK: ensure the screenshots folder also has a stats subfolder in it
762 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
763 if rwdir <> '' then CreateDir(rwdir + '/stats');
764 end;
766 procedure EntryParams;
767 var i: Integer;
768 begin
769 i := 1;
770 while i <= ParamCount do
771 begin
772 case ParamStr(i) of
773 '--gdb': noct := true;
774 '--log', '--con-stdout': conbufDumpToStdOut := true;
775 '--safe-log': e_SetSafeSlowLog(true);
776 '--log-file':
777 if i + 1 <= ParamCount then
778 begin
779 Inc(i);
780 LogFileName := ParamStr(i)
781 end;
782 '--no-fbo': glRenderToFBO := false;
783 end;
784 Inc(i)
785 end
786 end;
788 procedure InitLog;
789 var rwdir: AnsiString;
790 begin
791 if LogFileName = '' then
792 begin
793 rwdir := e_GetWriteableDir(LogDirs, false);
794 if rwdir <> '' then
795 begin
796 {$IFDEF HEADLESS}
797 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
798 {$ELSE}
799 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
800 {$ENDIF}
801 end
802 end;
803 if LogFileName <> '' then
804 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
805 e_InitWritelnDriver
806 end;
808 procedure InitPrep;
809 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
810 var timiditycfg: AnsiString;
811 {$ENDIF}
812 begin
813 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
814 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
815 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
816 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
818 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
819 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
821 PrintDirs('DataDirs', DataDirs);
822 PrintDirs('ModelDirs', ModelDirs);
823 PrintDirs('MegawadDirs', MegawadDirs);
824 PrintDirs('MapDirs', MapDirs);
825 PrintDirs('WadDirs', WadDirs);
827 PrintDirs('LogDirs', LogDirs);
828 PrintDirs('SaveDirs', SaveDirs);
829 PrintDirs('CacheDirs', CacheDirs);
830 PrintDirs('ConfigDirs', ConfigDirs);
831 PrintDirs('ScreenshotDirs', ScreenshotDirs);
832 PrintDirs('StatsDirs', StatsDirs);
833 PrintDirs('MapDownloadDirs', MapDownloadDirs);
834 PrintDirs('WadDownloadDirs', WadDownloadDirs);
836 {$IFDEF HEADLESS}
837 {$IFDEF USE_SDLMIXER}
838 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
839 {$ELSE}
840 NoSound := True; // FMOD backend will sort it out
841 {$ENDIF}
842 {$ELSE}
843 NoSound := False;
844 {$ENDIF}
846 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
847 timiditycfg := 'timidity.cfg';
848 if e_FindResource(ConfigDirs, timiditycfg) = true then
849 begin
850 timiditycfg := ExpandFileName(timiditycfg);
851 SetEnvVar('TIMIDITY_CFG', timiditycfg);
852 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
853 end;
854 {$ENDIF}
856 GameWAD := e_FindWad(DataDirs, GameWADName);
857 if GameWad = '' then
858 begin
859 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
860 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
861 if forceBinDir = false then
862 SDL_ShowSimpleMessageBox(
863 SDL_MESSAGEBOX_ERROR,
864 'Doom 2D Forever',
865 PChar('WAD ' + GameWADName + ' not found in data directories.'),
866 nil
867 );
868 {$ENDIF}
869 e_DeinitLog;
870 Halt(1);
871 end
872 end;
874 {$IFDEF ENABLE_HOLMES}
875 procedure InitHolmes;
876 var flexloaded: Boolean;
877 begin
878 flexloaded := true;
879 if not fuiAddWad('flexui.wad') then
880 begin
881 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
882 end;
883 try
884 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
885 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
886 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
887 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
888 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
889 except on e: Exception do
890 begin
891 writeln('ERROR loading FlexUI fonts');
892 flexloaded := false;
893 //raise;
894 end;
895 else
896 begin
897 flexloaded := false;
898 //raise;
899 end;
900 end;
901 if flexloaded then
902 begin
903 try
904 e_LogWriteln('FlexUI: loading stylesheet...');
905 uiLoadStyles('flexui/widgets.wgs');
906 except on e: TParserException do
907 begin
908 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
909 //raise;
910 flexloaded := false;
911 end;
912 else
913 begin
914 //raise;
915 flexloaded := false;
916 end;
917 end;
918 end;
919 g_holmes_imfunctional := not flexloaded;
920 if not g_holmes_imfunctional then
921 begin
922 uiInitialize();
923 uiContext.font := 'win14';
924 end;
925 if assigned(oglInitCB) then oglInitCB;
926 end;
928 procedure FreeHolmes;
929 begin
930 if assigned(oglDeinitCB) then
931 oglDeinitCB
932 end;
933 {$ENDIF}
935 procedure ScreenResize (w, h: Integer);
936 begin
937 {$IFNDEF HEADLESS}
938 r_Render_Resize(w, h);
939 {$IFDEF ENABLE_HOLMES}
940 fuiScrWdt := w;
941 fuiScrHgt := h;
942 {$ENDIF}
943 {$IFNDEF ANDROID}
944 (* This will fix menu reset on keyboard showing *)
945 {$IFDEF ENABLE_MENU}
946 g_Menu_Reset;
947 {$ENDIF}
948 {$ENDIF}
949 //g_Game_ClearLoading;
950 {$IFDEF ENABLE_HOLMES}
951 if assigned(oglInitCB) then oglInitCB;
952 {$ENDIF}
953 {$ENDIF}
954 end;
956 procedure Startup;
957 begin
958 Randomize;
959 InitPath;
960 InitLog;
961 InitPrep;
962 e_Input_Initialize;
963 e_InitSoundSystem(NoSound);
964 {$IFNDEF HEADLESS}
965 sys_Init;
966 sys_CharPress := @CharPress; (* install hook *)
967 sys_ScreenResize := @ScreenResize; (* install hook *)
968 {$ENDIF}
969 g_Options_SetDefault;
970 g_Options_SetDefaultVideo;
971 g_Console_Initialize;
972 // TODO move load configs here
973 g_Language_Set(gLanguage);
974 {$IFNDEF HEADLESS}
975 r_Render_Initialize;
976 {$ENDIF}
977 DebugOptions;
978 g_Net_InitLowLevel;
979 // TODO init serverlist
980 {$IFDEF ENABLE_HOLMES}
981 InitHolmes;
982 {$ENDIF}
983 {$IFDEF HEADLESS}
984 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
985 {$ELSE}
986 g_PlayerModel_LoadAll;
987 r_Render_Load;
988 {$ENDIF}
989 g_Game_Init;
990 {$IFDEF ENABLE_MENU}
991 g_Menu_Init;
992 g_GUI_Init;
993 {$ENDIF}
994 g_Game_Process_Params;
995 // TODO reload GAME textures
996 g_Console_Init; // welcome message
997 {$IFDEF ENABLE_MENU}
998 if (not gGameOn) and gAskLanguage then
999 g_Menu_AskLanguage;
1000 {$ENDIF}
1001 Time_Old := GetTickCount64();
1002 while not ProcessMessage() do begin end;
1003 g_Console_WriteGameConfig;
1004 {$IFDEF ENABLE_MENU}
1005 g_GUI_Destroy;
1006 g_Menu_Free;
1007 {$ENDIF}
1008 {$IFNDEF HEADLESS}
1009 r_Render_Free;
1010 {$ENDIF}
1011 {$IFDEF ENABLE_HOLMES}
1012 FreeHolmes;
1013 {$ENDIF}
1014 g_Net_Slist_ShutdownAll;
1015 g_Net_DeinitLowLevel;
1016 (* g_Touch_Finalize; *)
1017 {$IFNDEF HEADLESS}
1018 r_Render_Finalize;
1019 sys_Final;
1020 {$ENDIF}
1021 g_Console_Finalize;
1022 e_ReleaseSoundSystem;
1023 e_Input_Finalize;
1024 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1025 end;
1027 procedure EntryPoint;
1028 begin
1029 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1030 EntryParams;
1031 e_Log_Initialize;
1032 {$IFDEF HEADLESS}
1033 conbufDumpToStdOut := true;
1034 {$ENDIF}
1035 if noct then
1036 Startup
1037 else
1038 try
1039 Startup
1040 except on e: Exception do
1041 e_WriteStackTrace(e.message)
1042 else
1043 e_WriteStackTrace('FATAL ERROR')
1044 end;
1045 e_Log_Finalize
1046 end;
1048 {$IFDEF ANDROID}
1049 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1050 begin
1051 // TODO pass argc+argv to rtl
1052 EntryPoint;
1053 result := 0
1054 end;
1056 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1057 begin
1058 result:= JNI_VERSION_1_6;
1059 end;
1061 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1062 begin
1063 end;
1065 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1066 exports SDL_main name 'SDL_main';
1067 exports JNI_OnLoad name 'JNI_OnLoad';
1068 exports JNI_OnUnload name 'JNI_Unload';
1069 {$ELSE}
1070 begin
1071 EntryPoint
1072 {$ENDIF}
1074 end.