DEADSOFTWARE

63c895ff61327403c229a522bd5fa4d70b9c604b
[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 {$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;
240 Time_Old: Int64;
241 NoSound: Boolean;
243 procedure Update ();
244 begin
245 // remember old mobj positions, prepare for update
246 g_Game_PreUpdate();
247 // server: receive client commands for new frame
248 // client: receive game state changes from server
249 if (NetMode = NET_SERVER) then g_Net_Host_Update()
250 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
251 // think
252 {$IFDEF ENABLE_RENDER}
253 r_Render_Update;
254 {$ENDIF}
255 g_Game_Update();
256 // server: send any accumulated outgoing data to clients
257 if NetMode = NET_SERVER then g_Net_Flush();
258 end;
260 function ProcessMessage (): Boolean;
261 var
262 i, t: Integer;
263 flag: Boolean;
264 Time, Time_Delta: Int64;
265 Frame: Int64;
266 begin
267 {$IFDEF ENABLE_SYSTEM}
268 Result := sys_HandleInput();
269 {$ELSE}
270 Result := False;
271 {$ENDIF}
273 Time := GetTickCount64();
274 Time_Delta := Time-Time_Old;
276 flag := false;
278 if wNeedTimeReset then
279 begin
280 Frame := 0;
281 Time_Delta := 28;
282 wNeedTimeReset := false;
283 end;
285 g_Map_ProfilersBegin();
286 g_Mons_ProfilersBegin();
288 t := Time_Delta div 28;
289 if (t > 0) then
290 begin
291 flag := true;
292 for i := 1 to t do
293 Update();
294 end;
296 g_Map_ProfilersEnd();
297 g_Mons_ProfilersEnd();
299 if (gExit = EXIT_QUIT) then
300 begin
301 result := true;
302 exit;
303 end;
305 // Время предыдущего обновления
306 if flag then
307 Time_Old := Time - (Time_Delta mod 28);
309 // don't wait if VSync is on, GL already probably waits enough
310 if gLerpActors then
311 flag := (Time - Frame >= gFrameTime) or gVSync;
313 if flag then
314 begin
315 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
316 gLerpFactor := 1.0
317 else
318 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
319 {$IFDEF ENABLE_RENDER}
320 r_Render_Draw;
321 {$ENDIF}
322 {$IFDEF ENABLE_SYSTEM}
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 if (valres = 0) and (not g_holmes_imfunctional) then
391 begin
392 case itmp of
393 8: uiContext.font := 'win8';
394 14: uiContext.font := 'win14';
395 16: uiContext.font := 'win16';
396 end;
397 end;
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 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
813 var timiditycfg: AnsiString;
814 {$ENDIF}
815 begin
816 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
817 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
818 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
819 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
821 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
822 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
824 PrintDirs('DataDirs', DataDirs);
825 PrintDirs('ModelDirs', ModelDirs);
826 PrintDirs('MegawadDirs', MegawadDirs);
827 PrintDirs('MapDirs', MapDirs);
828 PrintDirs('WadDirs', WadDirs);
830 PrintDirs('LogDirs', LogDirs);
831 PrintDirs('SaveDirs', SaveDirs);
832 PrintDirs('CacheDirs', CacheDirs);
833 PrintDirs('ConfigDirs', ConfigDirs);
834 PrintDirs('ScreenshotDirs', ScreenshotDirs);
835 PrintDirs('StatsDirs', StatsDirs);
836 PrintDirs('MapDownloadDirs', MapDownloadDirs);
837 PrintDirs('WadDownloadDirs', WadDownloadDirs);
839 {$IFDEF HEADLESS}
840 {$IFDEF USE_SDLMIXER}
841 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
842 {$ELSE}
843 NoSound := True; // FMOD backend will sort it out
844 {$ENDIF}
845 {$ELSE}
846 NoSound := False;
847 {$ENDIF}
849 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
850 timiditycfg := 'timidity.cfg';
851 if e_FindResource(ConfigDirs, timiditycfg) = true then
852 begin
853 timiditycfg := ExpandFileName(timiditycfg);
854 SetEnvVar('TIMIDITY_CFG', timiditycfg);
855 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
856 end;
857 {$ENDIF}
859 GameWAD := e_FindWad(DataDirs, GameWADName);
860 if GameWad = '' then
861 begin
862 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
863 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
864 if forceBinDir = false then
865 SDL_ShowSimpleMessageBox(
866 SDL_MESSAGEBOX_ERROR,
867 'Doom 2D Forever',
868 PChar('WAD ' + GameWADName + ' not found in data directories.'),
869 nil
870 );
871 {$ENDIF}
872 e_DeinitLog;
873 Halt(1);
874 end
875 end;
877 {$IFDEF ENABLE_HOLMES}
878 procedure InitHolmes;
879 var flexloaded: Boolean;
880 begin
881 flexloaded := true;
882 if not fuiAddWad('flexui.wad') then
883 begin
884 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
885 end;
886 try
887 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
888 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
889 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
890 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
891 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
892 except on e: Exception do
893 begin
894 writeln('ERROR loading FlexUI fonts');
895 flexloaded := false;
896 //raise;
897 end;
898 else
899 begin
900 flexloaded := false;
901 //raise;
902 end;
903 end;
904 if flexloaded then
905 begin
906 try
907 e_LogWriteln('FlexUI: loading stylesheet...');
908 uiLoadStyles('flexui/widgets.wgs');
909 except on e: TParserException do
910 begin
911 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
912 //raise;
913 flexloaded := false;
914 end;
915 else
916 begin
917 //raise;
918 flexloaded := false;
919 end;
920 end;
921 end;
922 g_holmes_imfunctional := not flexloaded;
923 if not g_holmes_imfunctional then
924 begin
925 uiInitialize();
926 uiContext.font := 'win14';
927 end;
928 if assigned(oglInitCB) then oglInitCB;
929 end;
931 procedure FreeHolmes;
932 begin
933 if assigned(oglDeinitCB) then
934 oglDeinitCB
935 end;
936 {$ENDIF}
938 procedure ScreenResize (w, h: Integer);
939 begin
940 {$IFDEF ENABLE_RENDER}
941 r_Render_Resize(w, h);
942 {$IFDEF ENABLE_HOLMES}
943 fuiScrWdt := w;
944 fuiScrHgt := h;
945 {$ENDIF}
946 {$IFNDEF ANDROID}
947 (* This will fix menu reset on keyboard showing *)
948 {$IFDEF ENABLE_MENU}
949 g_Menu_Reset;
950 {$ENDIF}
951 {$ENDIF}
952 //g_Game_ClearLoading;
953 {$IFDEF ENABLE_HOLMES}
954 if assigned(oglInitCB) then oglInitCB;
955 {$ENDIF}
956 {$ENDIF}
957 end;
959 procedure Startup;
960 begin
961 Randomize;
962 InitPath;
963 InitLog;
964 InitPrep;
965 e_Input_Initialize;
966 e_InitSoundSystem(NoSound);
967 {$IFDEF ENABLE_SYSTEM}
968 sys_Init;
969 sys_CharPress := @CharPress; (* install hook *)
970 sys_ScreenResize := @ScreenResize; (* install hook *)
971 {$ENDIF}
972 g_Options_SetDefault;
973 g_Options_SetDefaultVideo;
974 g_Console_Initialize;
975 // TODO move load configs here
976 g_Language_Set(gLanguage);
977 {$IFDEF ENABLE_RENDER}
978 r_Render_Initialize;
979 {$ENDIF}
980 DebugOptions;
981 g_Net_InitLowLevel;
982 // TODO init serverlist
983 {$IFDEF ENABLE_HOLMES}
984 InitHolmes;
985 {$ENDIF}
986 {$IFDEF ENABLE_RENDER}
987 g_PlayerModel_LoadAll;
988 r_Render_Load;
989 {$ELSE}
990 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
991 {$ENDIF}
992 g_Game_Init;
993 {$IFDEF ENABLE_MENU}
994 g_Menu_Init;
995 g_GUI_Init;
996 {$ENDIF}
997 g_Game_Process_Params;
998 // TODO reload GAME textures
999 g_Console_Init; // welcome message
1000 {$IFDEF ENABLE_MENU}
1001 if (not gGameOn) and gAskLanguage then
1002 g_Menu_AskLanguage;
1003 {$ENDIF}
1004 Time_Old := GetTickCount64();
1005 while not ProcessMessage() do begin end;
1006 g_Console_WriteGameConfig;
1007 {$IFDEF ENABLE_MENU}
1008 g_GUI_Destroy;
1009 g_Menu_Free;
1010 {$ENDIF}
1011 {$IFDEF ENABLE_RENDER}
1012 r_Render_Free;
1013 {$ENDIF}
1014 {$IFDEF ENABLE_HOLMES}
1015 FreeHolmes;
1016 {$ENDIF}
1017 g_Net_Slist_ShutdownAll;
1018 g_Net_DeinitLowLevel;
1019 (* g_Touch_Finalize; *)
1020 {$IFDEF ENABLE_RENDER}
1021 r_Render_Finalize;
1022 {$ENDIF}
1023 {$IFDEF ENABLE_SYSTEM}
1024 sys_Final;
1025 {$ENDIF}
1026 g_Console_Finalize;
1027 e_ReleaseSoundSystem;
1028 e_Input_Finalize;
1029 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1030 end;
1032 procedure EntryPoint;
1033 begin
1034 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1035 EntryParams;
1036 e_Log_Initialize;
1037 {$IFDEF HEADLESS}
1038 conbufDumpToStdOut := true;
1039 {$ENDIF}
1040 if noct then
1041 Startup
1042 else
1043 try
1044 Startup
1045 except on e: Exception do
1046 e_WriteStackTrace(e.message)
1047 else
1048 e_WriteStackTrace('FATAL ERROR')
1049 end;
1050 e_Log_Finalize
1051 end;
1053 {$IFDEF ANDROID}
1054 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1055 begin
1056 // TODO pass argc+argv to rtl
1057 EntryPoint;
1058 result := 0
1059 end;
1061 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1062 begin
1063 result:= JNI_VERSION_1_6;
1064 end;
1066 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1067 begin
1068 end;
1070 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1071 exports SDL_main name 'SDL_main';
1072 exports JNI_OnLoad name 'JNI_OnLoad';
1073 exports JNI_OnUnload name 'JNI_Unload';
1074 {$ELSE}
1075 begin
1076 EntryPoint
1077 {$ENDIF}
1079 end.