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 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 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;
232 {$IFDEF USE_SDLMIXER}
233 UseNativeMusic: Boolean;
234 {$ENDIF}
236 Time_Old: Int64;
237 NoSound: Boolean;
239 procedure Update ();
240 begin
241 // remember old mobj positions, prepare for update
242 g_Game_PreUpdate();
243 // server: receive client commands for new frame
244 // client: receive game state changes from server
245 if (NetMode = NET_SERVER) then g_Net_Host_Update()
246 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
247 // think
248 {$IFNDEF HEADLESS}
249 r_Render_Update;
250 {$ENDIF}
251 g_Game_Update();
252 // server: send any accumulated outgoing data to clients
253 if NetMode = NET_SERVER then g_Net_Flush();
254 end;
256 function ProcessMessage (): Boolean;
257 var
258 i, t: Integer;
259 flag: Boolean;
260 Time, Time_Delta: Int64;
261 Frame: Int64;
262 begin
263 {$IFDEF HEADLESS}
264 Result := False;
265 {$ELSE}
266 Result := sys_HandleInput();
267 {$ENDIF}
269 Time := GetTickCount64();
270 Time_Delta := Time-Time_Old;
272 flag := false;
274 if wNeedTimeReset then
275 begin
276 Frame := 0;
277 Time_Delta := 28;
278 wNeedTimeReset := false;
279 end;
281 g_Map_ProfilersBegin();
282 g_Mons_ProfilersBegin();
284 t := Time_Delta div 28;
285 if (t > 0) then
286 begin
287 flag := true;
288 for i := 1 to t do
289 Update();
290 end;
292 g_Map_ProfilersEnd();
293 g_Mons_ProfilersEnd();
295 if (gExit = EXIT_QUIT) then
296 begin
297 result := true;
298 exit;
299 end;
301 // Время предыдущего обновления
302 if flag then
303 Time_Old := Time - (Time_Delta mod 28);
305 // don't wait if VSync is on, GL already probably waits enough
306 if gLerpActors then
307 flag := (Time - Frame >= gFrameTime) or gVSync;
309 if flag then
310 begin
311 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
312 gLerpFactor := 1.0
313 else
314 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
315 {$IFNDEF HEADLESS}
316 r_Render_Draw;
317 sys_Repaint;
318 {$ENDIF}
319 Frame := Time
320 end
321 else
322 Sleep(1);
324 e_SoundUpdate();
325 end;
327 procedure DebugOptions;
328 var
329 idx: Integer;
330 arg: AnsiString;
331 mdfo: TStream;
332 {$IFDEF ENABLE_HOLMES}
333 itmp: Integer;
334 valres: Word;
335 {$ENDIF}
336 begin
337 idx := 1;
338 while (idx <= ParamCount) do
339 begin
340 arg := ParamStr(idx);
341 Inc(idx);
342 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
343 if arg = '--jah' then g_profile_history_size := 100;
344 if arg = '--no-los' then gmon_dbg_los_enabled := false;
346 if arg = '--profile-render' then g_profile_frame_draw := true;
347 if arg = '--profile-coldet' then g_profile_collision := true;
348 if arg = '--profile-los' then g_profile_los := true;
350 {$IFDEF ENABLE_GFX}
351 if arg = '--no-particles' then gpart_dbg_enabled := false;
352 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
353 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
354 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
355 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
356 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
357 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
358 {$ENDIF}
360 if arg = '--debug-input' then g_dbg_input := True;
362 {.$IF DEFINED(D2F_DEBUG)}
363 if arg = '--aimline' then g_dbg_aimline_on := true;
364 {.$ENDIF}
366 {$IFDEF ENABLE_HOLMES}
367 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
369 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
370 begin
371 if (idx <= ParamCount) then
372 begin
373 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
374 Inc(idx);
375 end;
376 end;
378 if (arg = '--holmes-font') or (arg = '-holmes-font') then
379 begin
380 if (idx <= ParamCount) then
381 begin
382 itmp := 0;
383 val(ParamStr(idx), itmp, valres);
384 {$IFNDEF HEADLESS}
385 if (valres = 0) and (not g_holmes_imfunctional) then
386 begin
387 case itmp of
388 8: uiContext.font := 'win8';
389 14: uiContext.font := 'win14';
390 16: uiContext.font := 'win16';
391 end;
392 end;
393 {$ELSE}
394 // fuck off, fpc!
395 itmp := itmp;
396 valres := valres;
397 {$ENDIF}
398 Inc(idx);
399 end;
400 end;
401 {$ENDIF}
403 if (arg = '--game-scale') or (arg = '-game-scale') then
404 begin
405 if (idx <= ParamCount) then
406 begin
407 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
408 Inc(idx);
409 end;
410 end;
412 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
413 begin
414 mdfo := createDiskFile('mapdef.txt');
415 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
416 mdfo.Free();
417 Halt(0);
418 end;
420 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
421 begin
422 if (idx <= ParamCount) then
423 begin
424 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
425 Inc(idx);
426 end;
427 end;
428 end;
429 end;
431 function GetBinaryPath (): AnsiString;
432 {$IFDEF LINUX}
433 var sl: AnsiString;
434 {$ENDIF}
435 begin
436 result := ExtractFilePath(ParamStr(0));
437 {$IFDEF LINUX}
438 // it may be a symlink; do some guesswork here
439 sl := fpReadLink(ExtractFileName(ParamStr(0)));
440 if (sl = ParamStr(0)) then
441 begin
442 // use current directory, as we don't have anything better
443 //result := '.';
444 GetDir(0, result);
445 end;
446 {$ENDIF}
447 result := fixSlashes(result);
448 if (length(result) > 0) and (result[length(result)] <> '/') then
449 result := result + '/';
450 end;
452 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
453 var dir: AnsiString;
454 begin
455 e_LogWriteln(msg + ':');
456 for dir in dirs do
457 e_LogWriteln(' ' + dir);
458 end;
460 {$IFDEF DARWIN}
461 function NSStringToAnsiString (s: NSString): AnsiString;
462 var i: Integer;
463 begin
464 result := '';
465 for i := 0 to s.length - 1 do
466 result := result + AnsiChar(s.characterAtIndex(i));
467 end;
469 function GetBundlePath (): AnsiString;
470 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
471 begin
472 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
473 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
474 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
475 CFRelease(pathRef);
476 CFRelease(pathCFStr);
477 Result := pathStr;
478 end;
479 {$ENDIF}
481 procedure InitPath;
482 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
484 procedure AddDir (var dirs: SSArray; append: AnsiString);
485 begin
486 SetLength(dirs, Length(dirs) + 1);
487 dirs[High(dirs)] := ExpandFileName(append)
488 end;
490 function IsSep (ch: Char): Boolean;
491 begin
492 {$IFDEF WINDOWS}
493 result := (ch = '/') or (ch = '\');
494 {$ELSE}
495 result := (ch = '/');
496 {$ENDIF}
497 end;
499 function OptimizePath (dir: AnsiString): AnsiString;
500 var i, len: Integer; s: AnsiString;
501 begin
502 i := 1; len := Length(dir); s := '';
503 while i <= len do
504 begin
505 if IsSep(dir[i]) then
506 begin
507 s := s + DirectorySeparator;
508 Inc(i);
509 while (i <= len) and IsSep(dir[i]) do Inc(i);
510 if (i <= len) and (dir[i] = '.') then
511 begin
512 if (i = len) or IsSep(dir[i + 1]) then
513 begin
514 Inc(i)
515 end
516 else if (i + 1 <= len) and (dir[i + 1] = '.') then
517 begin
518 if (i + 1 = len) or IsSep(dir[i + 2]) then
519 begin
520 s := e_UpperDir(s);
521 Inc(i, 2)
522 end
523 end
524 end
525 end
526 else
527 begin
528 s := s + dir[i];
529 Inc(i)
530 end
531 end;
532 result := s
533 end;
535 procedure OptimizeDirs (var dirs: SSArray);
536 var i, j, k: Integer;
537 begin
538 for i := 0 to High(dirs) do
539 dirs[i] := OptimizePath(dirs[i]);
540 // deduplicate
541 i := High(dirs);
542 while i >= 0 do
543 begin
544 j := 0;
545 while j < i do
546 begin
547 if dirs[j] = dirs[i] then
548 begin
549 for k := j + 1 to High(dirs) do
550 dirs[k - 1] := dirs[k];
551 Dec(i);
552 SetLength(dirs, High(dirs))
553 end
554 else
555 begin
556 Inc(j)
557 end
558 end;
559 Dec(i)
560 end
561 end;
563 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
564 var s: AnsiString;
565 begin
566 if Length(dirs) = 0 then
567 for s in base do
568 AddDir(dirs, e_CatPath(s, append));
569 OptimizeDirs(dirs)
570 end;
572 function GetDefaultRODirs (): SSArray;
573 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
574 var home: AnsiString;
575 {$ENDIF}
576 {$IFDEF WINDOWS}
577 var appdata: AnsiString;
578 {$ENDIF}
579 {$IFDEF DARWIN}
580 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
581 {$ENDIF}
582 begin
583 result := nil;
584 {$IFDEF DARWIN}
585 bundle := GetBundlePath();
586 if ExtractFileExt(bundle) <> '.app' then
587 AddDir(result, binpath);
588 {$ELSE}
589 AddDir(result, binPath);
590 {$ENDIF}
591 if forceBinDir = false then
592 begin
593 {$IFDEF USE_SDL2}
594 AddDir(result, SDL_GetBasePath());
595 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
596 {$ENDIF}
597 {$IFDEF WINDOWS}
598 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
599 if appdata <> '' then
600 AddDir(result, appdata);
601 {$ENDIF}
602 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
603 AddDir(result, '/usr/share/doom2df');
604 AddDir(result, '/usr/local/share/doom2df');
605 home := GetEnvironmentVariable('HOME');
606 if home <> '' then
607 AddDir(result, e_CatPath(home, '.doom2df'));
608 {$ENDIF}
609 {$IFDEF DARWIN}
610 bundle := GetBundlePath();
611 if bundle <> '' then
612 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
613 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
614 for i := 0 to dirArr.count - 1 do
615 begin
616 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
617 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
618 end;
619 {$ENDIF}
620 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
621 AddDir(result, SDL_AndroidGetInternalStoragePath());
622 if SDL_AndroidGetExternalStorageState() <> 0 then
623 AddDir(result, SDL_AndroidGetExternalStoragePath());
624 {$ENDIF}
625 end
626 end;
628 function GetDefaultRWDirs (): SSArray;
629 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
630 var home: AnsiString;
631 {$ENDIF}
632 {$IFDEF WINDOWS}
633 var appdata: AnsiString;
634 {$ENDIF}
635 {$IFDEF DARWIN}
636 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
637 {$ENDIF}
638 begin
639 result := nil;
640 {$IFDEF DARWIN}
641 bundle := GetBundlePath();
642 if ExtractFileExt(bundle) <> '.app' then
643 AddDir(result, binPath);
644 {$ELSE}
645 AddDir(result, binPath);
646 {$ENDIF}
647 if forceBinDir = false then
648 begin
649 {$IFDEF USE_SDL2}
650 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
651 {$ENDIF}
652 {$IFDEF WINDOWS}
653 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
654 if appdata <> '' then
655 AddDir(result, appdata);
656 {$ENDIF}
657 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
658 home := GetEnvironmentVariable('HOME');
659 if home <> '' then
660 AddDir(result, e_CatPath(home, '.doom2df'));
661 {$ENDIF}
662 {$IFDEF DARWIN}
663 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
664 for i := 0 to dirArr.count - 1 do
665 begin
666 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
667 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
668 end;
669 {$ENDIF}
670 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
671 if SDL_AndroidGetExternalStorageState() <> 0 then
672 AddDir(result, SDL_AndroidGetExternalStoragePath());
673 {$ENDIF}
674 end
675 end;
677 begin
678 forceBinDir := false;
679 binPath := GetBinaryPath();
681 i := 1;
682 while i < ParamCount do
683 begin
684 case ParamStr(i) of
685 '--like-windoze': forceBinDir := true;
686 '--rw-dir':
687 begin
688 Inc(i);
689 rwdir := ParamStr(i);
690 (* RW *)
691 AddDir(LogDirs, e_CatPath(rwdir, ''));
692 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
693 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
694 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
695 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
696 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
697 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
698 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
699 (* RO *)
700 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
701 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
702 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
703 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
704 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
705 end;
706 '--ro-dir':
707 begin
708 Inc(i);
709 rodir := ParamStr(i);
710 (* RO *)
711 AddDir(DataDirs, e_CatPath(rodir, 'data'));
712 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
713 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
714 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
715 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
716 end;
717 '--game-wad':
718 begin
719 Inc(i);
720 GameWADName := ParamStr(i);
721 end;
722 '--config':
723 begin
724 Inc(i);
725 gConfigScript := ParamStr(i);
726 end;
727 end;
728 Inc(i)
729 end;
731 // prefer bin dir if it writable and contains game.wad
732 if forceBinDir = false then
733 begin
734 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
735 if e_CanCreateFilesAt(binPath) then
736 forceBinDir := true
737 end;
739 (* RO *)
740 rodirs := GetDefaultRODirs();
741 AddDef(DataDirs, rodirs, 'data');
742 AddDef(ModelDirs, rodirs, 'data/models');
743 AddDef(MegawadDirs, rodirs, 'maps/megawads');
744 AddDef(MapDirs, rodirs, 'maps');
745 AddDef(WadDirs, rodirs, 'wads');
747 (* RW *)
748 rwdirs := GetDefaultRWDirs();
749 AddDef(LogDirs, rwdirs, '');
750 AddDef(SaveDirs, rwdirs, 'data');
751 AddDef(CacheDirs, rwdirs, 'data/cache');
752 AddDef(ConfigDirs, rwdirs, '');
753 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
754 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
755 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
756 AddDef(StatsDirs, rwdirs, 'stats');
758 for i := 0 to High(MapDirs) do
759 AddDir(AllMapDirs, MapDirs[i]);
760 for i := 0 to High(MegawadDirs) do
761 AddDir(AllMapDirs, MegawadDirs[i]);
762 OptimizeDirs(AllMapDirs);
764 // HACK: ensure the screenshots folder also has a stats subfolder in it
765 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
766 if rwdir <> '' then CreateDir(rwdir + '/stats');
767 end;
769 procedure EntryParams;
770 var i: Integer;
771 begin
772 i := 1;
773 while i <= ParamCount do
774 begin
775 case ParamStr(i) of
776 '--gdb': noct := true;
777 '--log', '--con-stdout': conbufDumpToStdOut := true;
778 '--safe-log': e_SetSafeSlowLog(true);
779 '--log-file':
780 if i + 1 <= ParamCount then
781 begin
782 Inc(i);
783 LogFileName := ParamStr(i)
784 end;
785 '--no-fbo': glRenderToFBO := false;
786 end;
787 Inc(i)
788 end
789 end;
791 procedure InitLog;
792 var rwdir: AnsiString;
793 begin
794 if LogFileName = '' then
795 begin
796 rwdir := e_GetWriteableDir(LogDirs, false);
797 if rwdir <> '' then
798 begin
799 {$IFDEF HEADLESS}
800 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
801 {$ELSE}
802 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
803 {$ENDIF}
804 end
805 end;
806 if LogFileName <> '' then
807 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
808 e_InitWritelnDriver
809 end;
811 procedure InitPrep;
812 begin
813 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
814 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
815 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
816 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
817 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
819 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
820 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
822 PrintDirs('DataDirs', DataDirs);
823 PrintDirs('ModelDirs', ModelDirs);
824 PrintDirs('MegawadDirs', MegawadDirs);
825 PrintDirs('MapDirs', MapDirs);
826 PrintDirs('WadDirs', WadDirs);
828 PrintDirs('LogDirs', LogDirs);
829 PrintDirs('SaveDirs', SaveDirs);
830 PrintDirs('CacheDirs', CacheDirs);
831 PrintDirs('ConfigDirs', ConfigDirs);
832 PrintDirs('ScreenshotDirs', ScreenshotDirs);
833 PrintDirs('StatsDirs', StatsDirs);
834 PrintDirs('MapDownloadDirs', MapDownloadDirs);
835 PrintDirs('WadDownloadDirs', WadDownloadDirs);
837 {$IFDEF HEADLESS}
838 {$IFDEF USE_SDLMIXER}
839 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
840 {$ELSE}
841 NoSound := True; // FMOD backend will sort it out
842 {$ENDIF}
843 {$ELSE}
844 NoSound := False;
845 {$ENDIF}
847 GameWAD := e_FindWad(DataDirs, GameWADName);
848 if GameWad = '' then
849 begin
850 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
851 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
852 if forceBinDir = false then
853 SDL_ShowSimpleMessageBox(
854 SDL_MESSAGEBOX_ERROR,
855 'Doom 2D Forever',
856 PChar('WAD ' + GameWADName + ' not found in data directories.'),
857 nil
858 );
859 {$ENDIF}
860 e_DeinitLog;
861 Halt(1);
862 end
863 end;
865 {$IFDEF ENABLE_HOLMES}
866 procedure InitHolmes;
867 var flexloaded: Boolean;
868 begin
869 flexloaded := true;
870 if not fuiAddWad('flexui.wad') then
871 begin
872 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
873 end;
874 try
875 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
876 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
877 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
878 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
879 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
880 except on e: Exception do
881 begin
882 writeln('ERROR loading FlexUI fonts');
883 flexloaded := false;
884 //raise;
885 end;
886 else
887 begin
888 flexloaded := false;
889 //raise;
890 end;
891 end;
892 if flexloaded then
893 begin
894 try
895 e_LogWriteln('FlexUI: loading stylesheet...');
896 uiLoadStyles('flexui/widgets.wgs');
897 except on e: TParserException do
898 begin
899 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
900 //raise;
901 flexloaded := false;
902 end;
903 else
904 begin
905 //raise;
906 flexloaded := false;
907 end;
908 end;
909 end;
910 g_holmes_imfunctional := not flexloaded;
911 if not g_holmes_imfunctional then
912 begin
913 uiInitialize();
914 uiContext.font := 'win14';
915 end;
916 if assigned(oglInitCB) then oglInitCB;
917 end;
919 procedure FreeHolmes;
920 begin
921 if assigned(oglDeinitCB) then
922 oglDeinitCB
923 end;
924 {$ENDIF}
926 procedure InitSound;
927 {$IFDEF USE_SDLMIXER}
928 var timiditycfg: AnsiString;
929 var oldcwd, newcwd: RawByteString;
930 {$ENDIF}
931 begin
932 if NoSound = false then
933 begin
934 e_WriteLog('Initializing sound system', TMsgType.Notify);
935 {$IFDEF USE_SDLMIXER}
936 newcwd := '';
937 if UseNativeMusic then
938 SetEnvVar('SDL_NATIVE_MUSIC', '1');
939 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
940 if timiditycfg = '' then
941 begin
942 timiditycfg := 'timidity.cfg';
943 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
944 begin
945 timiditycfg := ExpandFileName(timiditycfg);
946 newcwd := ExtractFileDir(timiditycfg);
947 SetEnvVar('TIMIDITY_CFG', timiditycfg);
948 end
949 else
950 timiditycfg := '';
951 end;
952 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
953 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
954 {$ENDIF}
955 e_InitSoundSystem(NoSound);
956 {$IFDEF USE_SDLMIXER}
957 if e_TimidityDecoder and (newcwd <> '') then
958 begin
959 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
960 (* CWD not restored after sound init because timidity *)
961 (* store relative pathes internally and load patches *)
962 (* later. I hope game never relies on CWD. *)
963 oldcwd := '';
964 GetDir(0, oldcwd);
965 ChDir(newcwd);
966 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
967 end;
968 {$ENDIF}
969 end;
970 end;
972 procedure ScreenResize (w, h: Integer);
973 begin
974 {$IFNDEF HEADLESS}
975 r_Render_Resize(w, h);
976 {$IFDEF ENABLE_HOLMES}
977 fuiScrWdt := w;
978 fuiScrHgt := h;
979 {$ENDIF}
980 {$IFNDEF ANDROID}
981 (* This will fix menu reset on keyboard showing *)
982 {$IFDEF ENABLE_MENU}
983 g_Menu_Reset;
984 {$ENDIF}
985 {$ENDIF}
986 //g_Game_ClearLoading;
987 {$IFDEF ENABLE_HOLMES}
988 if assigned(oglInitCB) then oglInitCB;
989 {$ENDIF}
990 {$ENDIF}
991 end;
993 procedure Startup;
994 begin
995 Randomize;
996 InitPath;
997 InitLog;
998 InitPrep;
999 e_Input_Initialize;
1000 InitSound;
1001 {$IFNDEF HEADLESS}
1002 sys_Init;
1003 sys_CharPress := @CharPress; (* install hook *)
1004 sys_ScreenResize := @ScreenResize; (* install hook *)
1005 {$ENDIF}
1006 g_Options_SetDefault;
1007 g_Options_SetDefaultVideo;
1008 g_Console_Initialize;
1009 // TODO move load configs here
1010 g_Language_Set(gLanguage);
1011 {$IFNDEF HEADLESS}
1012 r_Render_Initialize;
1013 {$ENDIF}
1014 DebugOptions;
1015 g_Net_InitLowLevel;
1016 // TODO init serverlist
1017 {$IFDEF ENABLE_HOLMES}
1018 InitHolmes;
1019 {$ENDIF}
1020 {$IFDEF HEADLESS}
1021 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1022 {$ELSE}
1023 g_PlayerModel_LoadAll;
1024 r_Render_Load;
1025 {$ENDIF}
1026 g_Game_Init;
1027 {$IFDEF ENABLE_MENU}
1028 g_Menu_Init;
1029 g_GUI_Init;
1030 {$ENDIF}
1031 g_Game_Process_Params;
1032 // TODO reload GAME textures
1033 g_Console_Init; // welcome message
1034 {$IFDEF ENABLE_MENU}
1035 if (not gGameOn) and gAskLanguage then
1036 g_Menu_AskLanguage;
1037 {$ENDIF}
1038 Time_Old := GetTickCount64();
1039 while not ProcessMessage() do begin end;
1040 g_Console_WriteGameConfig;
1041 {$IFDEF ENABLE_MENU}
1042 g_GUI_Destroy;
1043 g_Menu_Free;
1044 {$ENDIF}
1045 {$IFNDEF HEADLESS}
1046 r_Render_Free;
1047 {$ENDIF}
1048 {$IFDEF ENABLE_HOLMES}
1049 FreeHolmes;
1050 {$ENDIF}
1051 g_Net_Slist_ShutdownAll;
1052 g_Net_DeinitLowLevel;
1053 (* g_Touch_Finalize; *)
1054 {$IFNDEF HEADLESS}
1055 r_Render_Finalize;
1056 sys_Final;
1057 {$ENDIF}
1058 g_Console_Finalize;
1059 e_ReleaseSoundSystem;
1060 e_Input_Finalize;
1061 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1062 end;
1064 procedure InitCVars;
1065 begin
1066 {$IFDEF USE_SDLMIXER}
1067 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1068 {$IFDEF DARWIN}
1069 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1070 {$ELSE}
1071 UseNativeMusic := false;
1072 {$ENDIF}
1073 {$ENDIF}
1074 end;
1076 procedure EntryPoint;
1077 begin
1078 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1079 InitCVars;
1080 EntryParams;
1081 e_Log_Initialize;
1082 {$IFDEF HEADLESS}
1083 conbufDumpToStdOut := true;
1084 {$ENDIF}
1085 if noct then
1086 Startup
1087 else
1088 try
1089 Startup
1090 except on e: Exception do
1091 e_WriteStackTrace(e.message)
1092 else
1093 e_WriteStackTrace('FATAL ERROR')
1094 end;
1095 e_Log_Finalize
1096 end;
1098 {$IFDEF ANDROID}
1099 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1100 begin
1101 {$IFDEF ANDROID}
1102 System.argc := argc;
1103 System.argv := argv;
1104 {$ENDIF}
1105 EntryPoint;
1106 result := 0
1107 end;
1109 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1110 begin
1111 result:= JNI_VERSION_1_6;
1112 end;
1114 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1115 begin
1116 end;
1118 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1119 exports SDL_main name 'SDL_main';
1120 exports JNI_OnLoad name 'JNI_OnLoad';
1121 exports JNI_OnUnload name 'JNI_Unload';
1122 {$ELSE}
1123 begin
1124 EntryPoint
1125 {$ENDIF}
1127 end.