DEADSOFTWARE

system: add option -dDISABLE_SYSTEM
[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',
152 {$IFDEF ENABLE_SYSTEM}
153 {$IFDEF USE_SYSSTUB}
154 g_system in 'stub/g_system.pas',
155 {$ENDIF}
156 {$IFDEF USE_SDL}
157 g_system in 'sdl/g_system.pas',
158 {$ENDIF}
159 {$IFDEF USE_SDL2}
160 g_system in 'sdl2/g_system.pas',
161 {$ENDIF}
162 {$ENDIF}
164 {$IFDEF ENABLE_RENDER}
165 {$I ../shared/vampimg.inc}
166 r_animations in 'opengl/r_animations.pas',
167 r_console in 'opengl/r_console.pas',
168 r_game in 'opengl/r_game.pas',
169 {$IFDEF ENABLE_GFX}
170 r_gfx in 'opengl/r_gfx.pas',
171 {$ENDIF}
172 r_graphics in 'opengl/r_graphics.pas',
173 r_items in 'opengl/r_items.pas',
174 r_map in 'opengl/r_map.pas',
175 r_monsters in 'opengl/r_monsters.pas',
176 r_netmaster in 'opengl/r_netmaster.pas',
177 r_player in 'opengl/r_player.pas',
178 r_playermodel in 'opengl/r_playermodel.pas',
179 r_render in 'opengl/r_render.pas',
180 r_texture in 'opengl/r_texture.pas',
181 r_textures in 'opengl/r_textures.pas',
182 r_weapons in 'opengl/r_weapons.pas',
183 r_window in 'opengl/r_window.pas',
184 {$IFDEF ENABLE_TOUCH}
185 r_touch in 'opengl/r_touch.pas',
186 {$ENDIF}
187 {$IFDEF ENABLE_MENU}
188 g_gui in 'g_gui.pas',
189 g_menu in 'g_menu.pas',
190 {$ENDIF}
191 {$ENDIF}
193 {$IFDEF USE_FMOD}
194 fmod in '../lib/FMOD/fmod.pas',
195 fmoderrors in '../lib/FMOD/fmoderrors.pas',
196 fmodpresets in '../lib/FMOD/fmodpresets.pas',
197 fmodtypes in '../lib/FMOD/fmodtypes.pas',
198 {$ENDIF}
199 xprofiler in '../shared/xprofiler.pas',
200 binheap in '../shared/binheap.pas',
201 hashtable in '../shared/hashtable.pas',
202 fhashdb in '../shared/fhashdb.pas',
203 idpool in '../shared/idpool.pas',
204 xparser in '../shared/xparser.pas',
205 xdynrec in '../shared/xdynrec.pas',
206 exoma in '../shared/exoma.pas',
207 envvars in '../shared/envvars.pas',
208 g_panel in 'g_panel.pas',
209 g_language in 'g_language.pas',
211 {$IFDEF ENABLE_HOLMES}
212 g_holmes in 'g_holmes.pas',
214 sdlcarcass in '../flexui/sdlcarcass.pas',
215 //sdlstandalone in '../flexui/sdlstandalone.pas',
217 fui_wadread in '../flexui/fui_wadread.pas',
218 fui_common in '../flexui/fui_common.pas',
219 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
220 fui_events in '../flexui/fui_events.pas',
221 fui_style in '../flexui/fui_style.pas',
222 fui_flexlay in '../flexui/fui_flexlay.pas',
223 fui_ctls in '../flexui/fui_ctls.pas',
224 {$ENDIF}
226 SysUtils, Classes;
228 {$IFDEF WINDOWS}
229 {$R *.res}
230 {$ENDIF}
232 const
233 autoexecScript = 'autoexec.cfg';
235 var
236 noct: Boolean = False;
237 binPath: AnsiString = '';
238 forceBinDir: Boolean = False;
239 {$IFDEF USE_SDLMIXER}
240 UseNativeMusic: Boolean;
241 {$ENDIF}
243 Time_Old: Int64;
244 NoSound: Boolean;
246 procedure Update ();
247 begin
248 // remember old mobj positions, prepare for update
249 g_Game_PreUpdate();
250 // server: receive client commands for new frame
251 // client: receive game state changes from server
252 if (NetMode = NET_SERVER) then g_Net_Host_Update()
253 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
254 // think
255 {$IFDEF ENABLE_RENDER}
256 r_Render_Update;
257 {$ENDIF}
258 g_Game_Update();
259 // server: send any accumulated outgoing data to clients
260 if NetMode = NET_SERVER then g_Net_Flush();
261 end;
263 function ProcessMessage (): Boolean;
264 var
265 i, t: Integer;
266 flag: Boolean;
267 Time, Time_Delta: Int64;
268 Frame: Int64;
269 begin
270 {$IFDEF ENABLE_SYSTEM}
271 Result := sys_HandleInput();
272 {$ELSE}
273 Result := False;
274 {$ENDIF}
276 Time := GetTickCount64();
277 Time_Delta := Time-Time_Old;
279 flag := false;
281 if wNeedTimeReset then
282 begin
283 Frame := 0;
284 Time_Delta := 28;
285 wNeedTimeReset := false;
286 end;
288 g_Map_ProfilersBegin();
289 g_Mons_ProfilersBegin();
291 t := Time_Delta div 28;
292 if (t > 0) then
293 begin
294 flag := true;
295 for i := 1 to t do
296 Update();
297 end;
299 g_Map_ProfilersEnd();
300 g_Mons_ProfilersEnd();
302 if (gExit = EXIT_QUIT) then
303 begin
304 result := true;
305 exit;
306 end;
308 // Время предыдущего обновления
309 if flag then
310 Time_Old := Time - (Time_Delta mod 28);
312 // don't wait if VSync is on, GL already probably waits enough
313 if gLerpActors then
314 flag := (Time - Frame >= gFrameTime) or gVSync;
316 if flag then
317 begin
318 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
319 gLerpFactor := 1.0
320 else
321 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
322 {$IFDEF ENABLE_RENDER}
323 r_Render_Draw;
324 {$ENDIF}
325 {$IFDEF ENABLE_SYSTEM}
326 sys_Repaint;
327 {$ENDIF}
328 Frame := Time
329 end
330 else
331 Sleep(1);
333 e_SoundUpdate();
334 end;
336 procedure DebugOptions;
337 var
338 idx: Integer;
339 arg: AnsiString;
340 mdfo: TStream;
341 {$IFDEF ENABLE_HOLMES}
342 itmp: Integer;
343 valres: Word;
344 {$ENDIF}
345 begin
346 idx := 1;
347 while (idx <= ParamCount) do
348 begin
349 arg := ParamStr(idx);
350 Inc(idx);
351 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
352 if arg = '--jah' then g_profile_history_size := 100;
353 if arg = '--no-los' then gmon_dbg_los_enabled := false;
355 if arg = '--profile-render' then g_profile_frame_draw := true;
356 if arg = '--profile-coldet' then g_profile_collision := true;
357 if arg = '--profile-los' then g_profile_los := true;
359 {$IFDEF ENABLE_GFX}
360 if arg = '--no-particles' then gpart_dbg_enabled := false;
361 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
362 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
363 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
364 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
365 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
366 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
367 {$ENDIF}
369 if arg = '--debug-input' then g_dbg_input := True;
371 {.$IF DEFINED(D2F_DEBUG)}
372 if arg = '--aimline' then g_dbg_aimline_on := true;
373 {.$ENDIF}
375 {$IFDEF ENABLE_HOLMES}
376 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
378 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
379 begin
380 if (idx <= ParamCount) then
381 begin
382 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
383 Inc(idx);
384 end;
385 end;
387 if (arg = '--holmes-font') or (arg = '-holmes-font') then
388 begin
389 if (idx <= ParamCount) then
390 begin
391 itmp := 0;
392 val(ParamStr(idx), itmp, valres);
393 {$IFNDEF HEADLESS}
394 if (valres = 0) and (not g_holmes_imfunctional) then
395 begin
396 case itmp of
397 8: uiContext.font := 'win8';
398 14: uiContext.font := 'win14';
399 16: uiContext.font := 'win16';
400 end;
401 end;
402 {$ELSE}
403 // fuck off, fpc!
404 itmp := itmp;
405 valres := valres;
406 {$ENDIF}
407 Inc(idx);
408 end;
409 end;
410 {$ENDIF}
412 if (arg = '--game-scale') or (arg = '-game-scale') then
413 begin
414 if (idx <= ParamCount) then
415 begin
416 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
417 Inc(idx);
418 end;
419 end;
421 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
422 begin
423 mdfo := createDiskFile('mapdef.txt');
424 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
425 mdfo.Free();
426 Halt(0);
427 end;
429 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
430 begin
431 if (idx <= ParamCount) then
432 begin
433 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
434 Inc(idx);
435 end;
436 end;
437 end;
438 end;
440 function GetBinaryPath (): AnsiString;
441 {$IFDEF LINUX}
442 var sl: AnsiString;
443 {$ENDIF}
444 begin
445 result := ExtractFilePath(ParamStr(0));
446 {$IFDEF LINUX}
447 // it may be a symlink; do some guesswork here
448 sl := fpReadLink(ExtractFileName(ParamStr(0)));
449 if (sl = ParamStr(0)) then
450 begin
451 // use current directory, as we don't have anything better
452 //result := '.';
453 GetDir(0, result);
454 end;
455 {$ENDIF}
456 result := fixSlashes(result);
457 if (length(result) > 0) and (result[length(result)] <> '/') then
458 result := result + '/';
459 end;
461 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
462 var dir: AnsiString;
463 begin
464 e_LogWriteln(msg + ':');
465 for dir in dirs do
466 e_LogWriteln(' ' + dir);
467 end;
469 {$IFDEF DARWIN}
470 function NSStringToAnsiString (s: NSString): AnsiString;
471 var i: Integer;
472 begin
473 result := '';
474 for i := 0 to s.length - 1 do
475 result := result + AnsiChar(s.characterAtIndex(i));
476 end;
478 function GetBundlePath (): AnsiString;
479 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
480 begin
481 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
482 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
483 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
484 CFRelease(pathRef);
485 CFRelease(pathCFStr);
486 Result := pathStr;
487 end;
488 {$ENDIF}
490 procedure InitPath;
491 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
493 procedure AddDir (var dirs: SSArray; append: AnsiString);
494 begin
495 SetLength(dirs, Length(dirs) + 1);
496 dirs[High(dirs)] := ExpandFileName(append)
497 end;
499 function IsSep (ch: Char): Boolean;
500 begin
501 {$IFDEF WINDOWS}
502 result := (ch = '/') or (ch = '\');
503 {$ELSE}
504 result := (ch = '/');
505 {$ENDIF}
506 end;
508 function OptimizePath (dir: AnsiString): AnsiString;
509 var i, len: Integer; s: AnsiString;
510 begin
511 i := 1; len := Length(dir); s := '';
512 while i <= len do
513 begin
514 if IsSep(dir[i]) then
515 begin
516 s := s + DirectorySeparator;
517 Inc(i);
518 while (i <= len) and IsSep(dir[i]) do Inc(i);
519 if (i <= len) and (dir[i] = '.') then
520 begin
521 if (i = len) or IsSep(dir[i + 1]) then
522 begin
523 Inc(i)
524 end
525 else if (i + 1 <= len) and (dir[i + 1] = '.') then
526 begin
527 if (i + 1 = len) or IsSep(dir[i + 2]) then
528 begin
529 s := e_UpperDir(s);
530 Inc(i, 2)
531 end
532 end
533 end
534 end
535 else
536 begin
537 s := s + dir[i];
538 Inc(i)
539 end
540 end;
541 result := s
542 end;
544 procedure OptimizeDirs (var dirs: SSArray);
545 var i, j, k: Integer;
546 begin
547 for i := 0 to High(dirs) do
548 dirs[i] := OptimizePath(dirs[i]);
549 // deduplicate
550 i := High(dirs);
551 while i >= 0 do
552 begin
553 j := 0;
554 while j < i do
555 begin
556 if dirs[j] = dirs[i] then
557 begin
558 for k := j + 1 to High(dirs) do
559 dirs[k - 1] := dirs[k];
560 Dec(i);
561 SetLength(dirs, High(dirs))
562 end
563 else
564 begin
565 Inc(j)
566 end
567 end;
568 Dec(i)
569 end
570 end;
572 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
573 var s: AnsiString;
574 begin
575 if Length(dirs) = 0 then
576 for s in base do
577 AddDir(dirs, e_CatPath(s, append));
578 OptimizeDirs(dirs)
579 end;
581 function GetDefaultRODirs (): SSArray;
582 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
583 var home: AnsiString;
584 {$ENDIF}
585 {$IFDEF WINDOWS}
586 var appdata: AnsiString;
587 {$ENDIF}
588 {$IFDEF DARWIN}
589 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
590 {$ENDIF}
591 begin
592 result := nil;
593 {$IFDEF DARWIN}
594 bundle := GetBundlePath();
595 if ExtractFileExt(bundle) <> '.app' then
596 AddDir(result, binpath);
597 {$ELSE}
598 AddDir(result, binPath);
599 {$ENDIF}
600 if forceBinDir = false then
601 begin
602 {$IFDEF USE_SDL2}
603 AddDir(result, SDL_GetBasePath());
604 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
605 {$ENDIF}
606 {$IFDEF WINDOWS}
607 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
608 if appdata <> '' then
609 AddDir(result, appdata);
610 {$ENDIF}
611 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
612 AddDir(result, '/usr/share/doom2df');
613 AddDir(result, '/usr/local/share/doom2df');
614 home := GetEnvironmentVariable('HOME');
615 if home <> '' then
616 AddDir(result, e_CatPath(home, '.doom2df'));
617 {$ENDIF}
618 {$IFDEF DARWIN}
619 bundle := GetBundlePath();
620 if bundle <> '' then
621 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
622 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
623 for i := 0 to dirArr.count - 1 do
624 begin
625 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
626 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
627 end;
628 {$ENDIF}
629 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
630 AddDir(result, SDL_AndroidGetInternalStoragePath());
631 if SDL_AndroidGetExternalStorageState() <> 0 then
632 AddDir(result, SDL_AndroidGetExternalStoragePath());
633 {$ENDIF}
634 end
635 end;
637 function GetDefaultRWDirs (): SSArray;
638 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
639 var home: AnsiString;
640 {$ENDIF}
641 {$IFDEF WINDOWS}
642 var appdata: AnsiString;
643 {$ENDIF}
644 {$IFDEF DARWIN}
645 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
646 {$ENDIF}
647 begin
648 result := nil;
649 {$IFDEF DARWIN}
650 bundle := GetBundlePath();
651 if ExtractFileExt(bundle) <> '.app' then
652 AddDir(result, binPath);
653 {$ELSE}
654 AddDir(result, binPath);
655 {$ENDIF}
656 if forceBinDir = false then
657 begin
658 {$IFDEF USE_SDL2}
659 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
660 {$ENDIF}
661 {$IFDEF WINDOWS}
662 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
663 if appdata <> '' then
664 AddDir(result, appdata);
665 {$ENDIF}
666 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
667 home := GetEnvironmentVariable('HOME');
668 if home <> '' then
669 AddDir(result, e_CatPath(home, '.doom2df'));
670 {$ENDIF}
671 {$IFDEF DARWIN}
672 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
673 for i := 0 to dirArr.count - 1 do
674 begin
675 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
676 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
677 end;
678 {$ENDIF}
679 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
680 if SDL_AndroidGetExternalStorageState() <> 0 then
681 AddDir(result, SDL_AndroidGetExternalStoragePath());
682 {$ENDIF}
683 end
684 end;
686 begin
687 forceBinDir := false;
688 binPath := GetBinaryPath();
690 i := 1;
691 while i < ParamCount do
692 begin
693 case ParamStr(i) of
694 '--like-windoze': forceBinDir := true;
695 '--rw-dir':
696 begin
697 Inc(i);
698 rwdir := ParamStr(i);
699 (* RW *)
700 AddDir(LogDirs, e_CatPath(rwdir, ''));
701 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
702 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
703 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
704 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
705 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
706 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
707 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
708 (* RO *)
709 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
710 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
711 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
712 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
713 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
714 end;
715 '--ro-dir':
716 begin
717 Inc(i);
718 rodir := ParamStr(i);
719 (* RO *)
720 AddDir(DataDirs, e_CatPath(rodir, 'data'));
721 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
722 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
723 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
724 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
725 end;
726 '--game-wad':
727 begin
728 Inc(i);
729 GameWADName := ParamStr(i);
730 end;
731 '--config':
732 begin
733 Inc(i);
734 gConfigScript := ParamStr(i);
735 end;
736 end;
737 Inc(i)
738 end;
740 // prefer bin dir if it writable and contains game.wad
741 if forceBinDir = false then
742 begin
743 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
744 if e_CanCreateFilesAt(binPath) then
745 forceBinDir := true
746 end;
748 (* RO *)
749 rodirs := GetDefaultRODirs();
750 AddDef(DataDirs, rodirs, 'data');
751 AddDef(ModelDirs, rodirs, 'data/models');
752 AddDef(MegawadDirs, rodirs, 'maps/megawads');
753 AddDef(MapDirs, rodirs, 'maps');
754 AddDef(WadDirs, rodirs, 'wads');
756 (* RW *)
757 rwdirs := GetDefaultRWDirs();
758 AddDef(LogDirs, rwdirs, '');
759 AddDef(SaveDirs, rwdirs, 'data');
760 AddDef(CacheDirs, rwdirs, 'data/cache');
761 AddDef(ConfigDirs, rwdirs, '');
762 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
763 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
764 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
765 AddDef(StatsDirs, rwdirs, 'stats');
767 for i := 0 to High(MapDirs) do
768 AddDir(AllMapDirs, MapDirs[i]);
769 for i := 0 to High(MegawadDirs) do
770 AddDir(AllMapDirs, MegawadDirs[i]);
771 OptimizeDirs(AllMapDirs);
773 // HACK: ensure the screenshots folder also has a stats subfolder in it
774 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
775 if rwdir <> '' then CreateDir(rwdir + '/stats');
776 end;
778 procedure EntryParams;
779 var i: Integer;
780 begin
781 i := 1;
782 while i <= ParamCount do
783 begin
784 case ParamStr(i) of
785 '--gdb': noct := true;
786 '--log', '--con-stdout': conbufDumpToStdOut := true;
787 '--safe-log': e_SetSafeSlowLog(true);
788 '--log-file':
789 if i + 1 <= ParamCount then
790 begin
791 Inc(i);
792 LogFileName := ParamStr(i)
793 end;
794 '--no-fbo': glRenderToFBO := false;
795 end;
796 Inc(i)
797 end
798 end;
800 procedure InitLog;
801 var rwdir: AnsiString;
802 begin
803 if LogFileName = '' then
804 begin
805 rwdir := e_GetWriteableDir(LogDirs, false);
806 if rwdir <> '' then
807 begin
808 {$IFDEF HEADLESS}
809 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
810 {$ELSE}
811 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
812 {$ENDIF}
813 end
814 end;
815 if LogFileName <> '' then
816 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
817 e_InitWritelnDriver
818 end;
820 procedure InitPrep;
821 begin
822 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
823 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
824 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
825 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
826 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
828 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
829 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
831 PrintDirs('DataDirs', DataDirs);
832 PrintDirs('ModelDirs', ModelDirs);
833 PrintDirs('MegawadDirs', MegawadDirs);
834 PrintDirs('MapDirs', MapDirs);
835 PrintDirs('WadDirs', WadDirs);
837 PrintDirs('LogDirs', LogDirs);
838 PrintDirs('SaveDirs', SaveDirs);
839 PrintDirs('CacheDirs', CacheDirs);
840 PrintDirs('ConfigDirs', ConfigDirs);
841 PrintDirs('ScreenshotDirs', ScreenshotDirs);
842 PrintDirs('StatsDirs', StatsDirs);
843 PrintDirs('MapDownloadDirs', MapDownloadDirs);
844 PrintDirs('WadDownloadDirs', WadDownloadDirs);
846 {$IFDEF HEADLESS}
847 {$IFDEF USE_SDLMIXER}
848 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
849 {$ELSE}
850 NoSound := True; // FMOD backend will sort it out
851 {$ENDIF}
852 {$ELSE}
853 NoSound := False;
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 InitSound;
936 {$IFDEF USE_SDLMIXER}
937 var timiditycfg: AnsiString;
938 var oldcwd, newcwd: RawByteString;
939 {$ENDIF}
940 begin
941 if NoSound = false then
942 begin
943 e_WriteLog('Initializing sound system', TMsgType.Notify);
944 {$IFDEF USE_SDLMIXER}
945 newcwd := '';
946 if UseNativeMusic then
947 SetEnvVar('SDL_NATIVE_MUSIC', '1');
948 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
949 if timiditycfg = '' then
950 begin
951 timiditycfg := 'timidity.cfg';
952 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
953 begin
954 timiditycfg := ExpandFileName(timiditycfg);
955 newcwd := ExtractFileDir(timiditycfg);
956 SetEnvVar('TIMIDITY_CFG', timiditycfg);
957 end
958 else
959 timiditycfg := '';
960 end;
961 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
962 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
963 {$ENDIF}
964 e_InitSoundSystem(NoSound);
965 {$IFDEF USE_SDLMIXER}
966 if e_TimidityDecoder and (newcwd <> '') then
967 begin
968 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
969 (* CWD not restored after sound init because timidity *)
970 (* store relative pathes internally and load patches *)
971 (* later. I hope game never relies on CWD. *)
972 oldcwd := '';
973 GetDir(0, oldcwd);
974 ChDir(newcwd);
975 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
976 end;
977 {$ENDIF}
978 end;
979 end;
981 procedure ScreenResize (w, h: Integer);
982 begin
983 {$IFDEF ENABLE_RENDER}
984 r_Render_Resize(w, h);
985 {$IFDEF ENABLE_HOLMES}
986 fuiScrWdt := w;
987 fuiScrHgt := h;
988 {$ENDIF}
989 {$IFNDEF ANDROID}
990 (* This will fix menu reset on keyboard showing *)
991 {$IFDEF ENABLE_MENU}
992 g_Menu_Reset;
993 {$ENDIF}
994 {$ENDIF}
995 //g_Game_ClearLoading;
996 {$IFDEF ENABLE_HOLMES}
997 if assigned(oglInitCB) then oglInitCB;
998 {$ENDIF}
999 {$ENDIF}
1000 end;
1002 procedure Startup;
1003 begin
1004 Randomize;
1005 InitPath;
1006 InitLog;
1007 InitPrep;
1008 e_Input_Initialize;
1009 InitSound;
1010 {$IFDEF ENABLE_SYSTEM}
1011 sys_Init;
1012 sys_CharPress := @CharPress; (* install hook *)
1013 sys_ScreenResize := @ScreenResize; (* install hook *)
1014 {$ENDIF}
1015 g_Options_SetDefault;
1016 g_Options_SetDefaultVideo;
1017 g_Console_Initialize;
1018 // TODO move load configs here
1019 g_Language_Set(gLanguage);
1020 {$IFDEF ENABLE_RENDER}
1021 r_Render_Initialize;
1022 {$ENDIF}
1023 DebugOptions;
1024 g_Net_InitLowLevel;
1025 // TODO init serverlist
1026 {$IFDEF ENABLE_HOLMES}
1027 InitHolmes;
1028 {$ENDIF}
1029 {$IFDEF ENABLE_RENDER}
1030 g_PlayerModel_LoadAll;
1031 r_Render_Load;
1032 {$ELSE}
1033 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1034 {$ENDIF}
1035 g_Game_Init;
1036 {$IFDEF ENABLE_MENU}
1037 g_Menu_Init;
1038 g_GUI_Init;
1039 {$ENDIF}
1040 g_Game_Process_Params;
1041 // TODO reload GAME textures
1042 g_Console_Init; // welcome message
1043 {$IFDEF ENABLE_MENU}
1044 if (not gGameOn) and gAskLanguage then
1045 g_Menu_AskLanguage;
1046 {$ENDIF}
1047 Time_Old := GetTickCount64();
1048 while not ProcessMessage() do begin end;
1049 g_Console_WriteGameConfig;
1050 {$IFDEF ENABLE_MENU}
1051 g_GUI_Destroy;
1052 g_Menu_Free;
1053 {$ENDIF}
1054 {$IFDEF ENABLE_RENDER}
1055 r_Render_Free;
1056 {$ENDIF}
1057 {$IFDEF ENABLE_HOLMES}
1058 FreeHolmes;
1059 {$ENDIF}
1060 g_Net_Slist_ShutdownAll;
1061 g_Net_DeinitLowLevel;
1062 (* g_Touch_Finalize; *)
1063 {$IFDEF ENABLE_RENDER}
1064 r_Render_Finalize;
1065 {$ENDIF}
1066 {$IFDEF ENABLE_SYSTEM}
1067 sys_Final;
1068 {$ENDIF}
1069 g_Console_Finalize;
1070 e_ReleaseSoundSystem;
1071 e_Input_Finalize;
1072 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1073 end;
1075 procedure InitCVars;
1076 begin
1077 {$IFDEF USE_SDLMIXER}
1078 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1079 {$IFDEF DARWIN}
1080 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1081 {$ELSE}
1082 UseNativeMusic := false;
1083 {$ENDIF}
1084 {$ENDIF}
1085 end;
1087 procedure EntryPoint;
1088 begin
1089 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1090 InitCVars;
1091 EntryParams;
1092 e_Log_Initialize;
1093 {$IFDEF HEADLESS}
1094 conbufDumpToStdOut := true;
1095 {$ENDIF}
1096 if noct then
1097 Startup
1098 else
1099 try
1100 Startup
1101 except on e: Exception do
1102 e_WriteStackTrace(e.message)
1103 else
1104 e_WriteStackTrace('FATAL ERROR')
1105 end;
1106 e_Log_Finalize
1107 end;
1109 {$IFDEF ANDROID}
1110 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1111 begin
1112 {$IFDEF ANDROID}
1113 System.argc := argc;
1114 System.argv := argv;
1115 {$ENDIF}
1116 EntryPoint;
1117 result := 0
1118 end;
1120 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1121 begin
1122 result:= JNI_VERSION_1_6;
1123 end;
1125 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1126 begin
1127 end;
1129 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1130 exports SDL_main name 'SDL_main';
1131 exports JNI_OnLoad name 'JNI_OnLoad';
1132 exports JNI_OnUnload name 'JNI_Unload';
1133 {$ELSE}
1134 begin
1135 EntryPoint
1136 {$ENDIF}
1138 end.