DEADSOFTWARE

5a06f52c4e40bc39f5e4624cac70e2ab1ce6c27d
[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 {$IFNDEF HEADLESS}
391 if (valres = 0) and (not g_holmes_imfunctional) then
392 begin
393 case itmp of
394 8: uiContext.font := 'win8';
395 14: uiContext.font := 'win14';
396 16: uiContext.font := 'win16';
397 end;
398 end;
399 {$ELSE}
400 // fuck off, fpc!
401 itmp := itmp;
402 valres := valres;
403 {$ENDIF}
404 Inc(idx);
405 end;
406 end;
407 {$ENDIF}
409 if (arg = '--game-scale') or (arg = '-game-scale') then
410 begin
411 if (idx <= ParamCount) then
412 begin
413 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
414 Inc(idx);
415 end;
416 end;
418 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
419 begin
420 mdfo := createDiskFile('mapdef.txt');
421 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
422 mdfo.Free();
423 Halt(0);
424 end;
426 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
427 begin
428 if (idx <= ParamCount) then
429 begin
430 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
431 Inc(idx);
432 end;
433 end;
434 end;
435 end;
437 function GetBinaryPath (): AnsiString;
438 {$IFDEF LINUX}
439 var sl: AnsiString;
440 {$ENDIF}
441 begin
442 result := ExtractFilePath(ParamStr(0));
443 {$IFDEF LINUX}
444 // it may be a symlink; do some guesswork here
445 sl := fpReadLink(ExtractFileName(ParamStr(0)));
446 if (sl = ParamStr(0)) then
447 begin
448 // use current directory, as we don't have anything better
449 //result := '.';
450 GetDir(0, result);
451 end;
452 {$ENDIF}
453 result := fixSlashes(result);
454 if (length(result) > 0) and (result[length(result)] <> '/') then
455 result := result + '/';
456 end;
458 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
459 var dir: AnsiString;
460 begin
461 e_LogWriteln(msg + ':');
462 for dir in dirs do
463 e_LogWriteln(' ' + dir);
464 end;
466 {$IFDEF DARWIN}
467 function NSStringToAnsiString (s: NSString): AnsiString;
468 var i: Integer;
469 begin
470 result := '';
471 for i := 0 to s.length - 1 do
472 result := result + AnsiChar(s.characterAtIndex(i));
473 end;
475 function GetBundlePath (): AnsiString;
476 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
477 begin
478 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
479 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
480 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
481 CFRelease(pathRef);
482 CFRelease(pathCFStr);
483 Result := pathStr;
484 end;
485 {$ENDIF}
487 procedure InitPath;
488 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
490 procedure AddDir (var dirs: SSArray; append: AnsiString);
491 begin
492 SetLength(dirs, Length(dirs) + 1);
493 dirs[High(dirs)] := ExpandFileName(append)
494 end;
496 function IsSep (ch: Char): Boolean;
497 begin
498 {$IFDEF WINDOWS}
499 result := (ch = '/') or (ch = '\');
500 {$ELSE}
501 result := (ch = '/');
502 {$ENDIF}
503 end;
505 function OptimizePath (dir: AnsiString): AnsiString;
506 var i, len: Integer; s: AnsiString;
507 begin
508 i := 1; len := Length(dir); s := '';
509 while i <= len do
510 begin
511 if IsSep(dir[i]) then
512 begin
513 s := s + DirectorySeparator;
514 Inc(i);
515 while (i <= len) and IsSep(dir[i]) do Inc(i);
516 if (i <= len) and (dir[i] = '.') then
517 begin
518 if (i = len) or IsSep(dir[i + 1]) then
519 begin
520 Inc(i)
521 end
522 else if (i + 1 <= len) and (dir[i + 1] = '.') then
523 begin
524 if (i + 1 = len) or IsSep(dir[i + 2]) then
525 begin
526 s := e_UpperDir(s);
527 Inc(i, 2)
528 end
529 end
530 end
531 end
532 else
533 begin
534 s := s + dir[i];
535 Inc(i)
536 end
537 end;
538 result := s
539 end;
541 procedure OptimizeDirs (var dirs: SSArray);
542 var i, j, k: Integer;
543 begin
544 for i := 0 to High(dirs) do
545 dirs[i] := OptimizePath(dirs[i]);
546 // deduplicate
547 i := High(dirs);
548 while i >= 0 do
549 begin
550 j := 0;
551 while j < i do
552 begin
553 if dirs[j] = dirs[i] then
554 begin
555 for k := j + 1 to High(dirs) do
556 dirs[k - 1] := dirs[k];
557 Dec(i);
558 SetLength(dirs, High(dirs))
559 end
560 else
561 begin
562 Inc(j)
563 end
564 end;
565 Dec(i)
566 end
567 end;
569 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
570 var s: AnsiString;
571 begin
572 if Length(dirs) = 0 then
573 for s in base do
574 AddDir(dirs, e_CatPath(s, append));
575 OptimizeDirs(dirs)
576 end;
578 function GetDefaultRODirs (): SSArray;
579 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
580 var home: AnsiString;
581 {$ENDIF}
582 {$IFDEF WINDOWS}
583 var appdata: AnsiString;
584 {$ENDIF}
585 {$IFDEF DARWIN}
586 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
587 {$ENDIF}
588 begin
589 result := nil;
590 {$IFDEF DARWIN}
591 bundle := GetBundlePath();
592 if ExtractFileExt(bundle) <> '.app' then
593 AddDir(result, binpath);
594 {$ELSE}
595 AddDir(result, binPath);
596 {$ENDIF}
597 if forceBinDir = false then
598 begin
599 {$IFDEF USE_SDL2}
600 AddDir(result, SDL_GetBasePath());
601 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
602 {$ENDIF}
603 {$IFDEF WINDOWS}
604 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
605 if appdata <> '' then
606 AddDir(result, appdata);
607 {$ENDIF}
608 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
609 AddDir(result, '/usr/share/doom2df');
610 AddDir(result, '/usr/local/share/doom2df');
611 home := GetEnvironmentVariable('HOME');
612 if home <> '' then
613 AddDir(result, e_CatPath(home, '.doom2df'));
614 {$ENDIF}
615 {$IFDEF DARWIN}
616 bundle := GetBundlePath();
617 if bundle <> '' then
618 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
619 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
620 for i := 0 to dirArr.count - 1 do
621 begin
622 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
623 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
624 end;
625 {$ENDIF}
626 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
627 AddDir(result, SDL_AndroidGetInternalStoragePath());
628 if SDL_AndroidGetExternalStorageState() <> 0 then
629 AddDir(result, SDL_AndroidGetExternalStoragePath());
630 {$ENDIF}
631 end
632 end;
634 function GetDefaultRWDirs (): SSArray;
635 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
636 var home: AnsiString;
637 {$ENDIF}
638 {$IFDEF WINDOWS}
639 var appdata: AnsiString;
640 {$ENDIF}
641 {$IFDEF DARWIN}
642 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
643 {$ENDIF}
644 begin
645 result := nil;
646 {$IFDEF DARWIN}
647 bundle := GetBundlePath();
648 if ExtractFileExt(bundle) <> '.app' then
649 AddDir(result, binPath);
650 {$ELSE}
651 AddDir(result, binPath);
652 {$ENDIF}
653 if forceBinDir = false then
654 begin
655 {$IFDEF USE_SDL2}
656 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
657 {$ENDIF}
658 {$IFDEF WINDOWS}
659 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
660 if appdata <> '' then
661 AddDir(result, appdata);
662 {$ENDIF}
663 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
664 home := GetEnvironmentVariable('HOME');
665 if home <> '' then
666 AddDir(result, e_CatPath(home, '.doom2df'));
667 {$ENDIF}
668 {$IFDEF DARWIN}
669 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
670 for i := 0 to dirArr.count - 1 do
671 begin
672 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
673 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
674 end;
675 {$ENDIF}
676 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
677 if SDL_AndroidGetExternalStorageState() <> 0 then
678 AddDir(result, SDL_AndroidGetExternalStoragePath());
679 {$ENDIF}
680 end
681 end;
683 begin
684 forceBinDir := false;
685 binPath := GetBinaryPath();
687 i := 1;
688 while i < ParamCount do
689 begin
690 case ParamStr(i) of
691 '--like-windoze': forceBinDir := true;
692 '--rw-dir':
693 begin
694 Inc(i);
695 rwdir := ParamStr(i);
696 (* RW *)
697 AddDir(LogDirs, e_CatPath(rwdir, ''));
698 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
699 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
700 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
701 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
702 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
703 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
704 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
705 (* RO *)
706 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
707 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
708 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
709 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
710 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
711 end;
712 '--ro-dir':
713 begin
714 Inc(i);
715 rodir := ParamStr(i);
716 (* RO *)
717 AddDir(DataDirs, e_CatPath(rodir, 'data'));
718 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
719 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
720 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
721 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
722 end;
723 '--game-wad':
724 begin
725 Inc(i);
726 GameWADName := ParamStr(i);
727 end;
728 '--config':
729 begin
730 Inc(i);
731 gConfigScript := ParamStr(i);
732 end;
733 end;
734 Inc(i)
735 end;
737 // prefer bin dir if it writable and contains game.wad
738 if forceBinDir = false then
739 begin
740 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
741 if e_CanCreateFilesAt(binPath) then
742 forceBinDir := true
743 end;
745 (* RO *)
746 rodirs := GetDefaultRODirs();
747 AddDef(DataDirs, rodirs, 'data');
748 AddDef(ModelDirs, rodirs, 'data/models');
749 AddDef(MegawadDirs, rodirs, 'maps/megawads');
750 AddDef(MapDirs, rodirs, 'maps');
751 AddDef(WadDirs, rodirs, 'wads');
753 (* RW *)
754 rwdirs := GetDefaultRWDirs();
755 AddDef(LogDirs, rwdirs, '');
756 AddDef(SaveDirs, rwdirs, 'data');
757 AddDef(CacheDirs, rwdirs, 'data/cache');
758 AddDef(ConfigDirs, rwdirs, '');
759 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
760 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
761 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
762 AddDef(StatsDirs, rwdirs, 'stats');
764 for i := 0 to High(MapDirs) do
765 AddDir(AllMapDirs, MapDirs[i]);
766 for i := 0 to High(MegawadDirs) do
767 AddDir(AllMapDirs, MegawadDirs[i]);
768 OptimizeDirs(AllMapDirs);
770 // HACK: ensure the screenshots folder also has a stats subfolder in it
771 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
772 if rwdir <> '' then CreateDir(rwdir + '/stats');
773 end;
775 procedure EntryParams;
776 var i: Integer;
777 begin
778 i := 1;
779 while i <= ParamCount do
780 begin
781 case ParamStr(i) of
782 '--gdb': noct := true;
783 '--log', '--con-stdout': conbufDumpToStdOut := true;
784 '--safe-log': e_SetSafeSlowLog(true);
785 '--log-file':
786 if i + 1 <= ParamCount then
787 begin
788 Inc(i);
789 LogFileName := ParamStr(i)
790 end;
791 '--no-fbo': glRenderToFBO := false;
792 end;
793 Inc(i)
794 end
795 end;
797 procedure InitLog;
798 var rwdir: AnsiString;
799 begin
800 if LogFileName = '' then
801 begin
802 rwdir := e_GetWriteableDir(LogDirs, false);
803 if rwdir <> '' then
804 begin
805 {$IFDEF HEADLESS}
806 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
807 {$ELSE}
808 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
809 {$ENDIF}
810 end
811 end;
812 if LogFileName <> '' then
813 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
814 e_InitWritelnDriver
815 end;
817 procedure InitPrep;
818 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
819 var timiditycfg: AnsiString;
820 {$ENDIF}
821 begin
822 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
823 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
824 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
825 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
827 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
828 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
830 PrintDirs('DataDirs', DataDirs);
831 PrintDirs('ModelDirs', ModelDirs);
832 PrintDirs('MegawadDirs', MegawadDirs);
833 PrintDirs('MapDirs', MapDirs);
834 PrintDirs('WadDirs', WadDirs);
836 PrintDirs('LogDirs', LogDirs);
837 PrintDirs('SaveDirs', SaveDirs);
838 PrintDirs('CacheDirs', CacheDirs);
839 PrintDirs('ConfigDirs', ConfigDirs);
840 PrintDirs('ScreenshotDirs', ScreenshotDirs);
841 PrintDirs('StatsDirs', StatsDirs);
842 PrintDirs('MapDownloadDirs', MapDownloadDirs);
843 PrintDirs('WadDownloadDirs', WadDownloadDirs);
845 {$IFDEF HEADLESS}
846 {$IFDEF USE_SDLMIXER}
847 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
848 {$ELSE}
849 NoSound := True; // FMOD backend will sort it out
850 {$ENDIF}
851 {$ELSE}
852 NoSound := False;
853 {$ENDIF}
855 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
856 timiditycfg := 'timidity.cfg';
857 if e_FindResource(ConfigDirs, timiditycfg) = true then
858 begin
859 timiditycfg := ExpandFileName(timiditycfg);
860 SetEnvVar('TIMIDITY_CFG', timiditycfg);
861 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
862 end;
863 {$ENDIF}
865 GameWAD := e_FindWad(DataDirs, GameWADName);
866 if GameWad = '' then
867 begin
868 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
869 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
870 if forceBinDir = false then
871 SDL_ShowSimpleMessageBox(
872 SDL_MESSAGEBOX_ERROR,
873 'Doom 2D Forever',
874 PChar('WAD ' + GameWADName + ' not found in data directories.'),
875 nil
876 );
877 {$ENDIF}
878 e_DeinitLog;
879 Halt(1);
880 end
881 end;
883 {$IFDEF ENABLE_HOLMES}
884 procedure InitHolmes;
885 var flexloaded: Boolean;
886 begin
887 flexloaded := true;
888 if not fuiAddWad('flexui.wad') then
889 begin
890 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
891 end;
892 try
893 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
894 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
895 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
896 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
897 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
898 except on e: Exception do
899 begin
900 writeln('ERROR loading FlexUI fonts');
901 flexloaded := false;
902 //raise;
903 end;
904 else
905 begin
906 flexloaded := false;
907 //raise;
908 end;
909 end;
910 if flexloaded then
911 begin
912 try
913 e_LogWriteln('FlexUI: loading stylesheet...');
914 uiLoadStyles('flexui/widgets.wgs');
915 except on e: TParserException do
916 begin
917 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
918 //raise;
919 flexloaded := false;
920 end;
921 else
922 begin
923 //raise;
924 flexloaded := false;
925 end;
926 end;
927 end;
928 g_holmes_imfunctional := not flexloaded;
929 if not g_holmes_imfunctional then
930 begin
931 uiInitialize();
932 uiContext.font := 'win14';
933 end;
934 if assigned(oglInitCB) then oglInitCB;
935 end;
937 procedure FreeHolmes;
938 begin
939 if assigned(oglDeinitCB) then
940 oglDeinitCB
941 end;
942 {$ENDIF}
944 procedure ScreenResize (w, h: Integer);
945 begin
946 {$IFDEF ENABLE_RENDER}
947 r_Render_Resize(w, h);
948 {$IFDEF ENABLE_HOLMES}
949 fuiScrWdt := w;
950 fuiScrHgt := h;
951 {$ENDIF}
952 {$IFNDEF ANDROID}
953 (* This will fix menu reset on keyboard showing *)
954 {$IFDEF ENABLE_MENU}
955 g_Menu_Reset;
956 {$ENDIF}
957 {$ENDIF}
958 //g_Game_ClearLoading;
959 {$IFDEF ENABLE_HOLMES}
960 if assigned(oglInitCB) then oglInitCB;
961 {$ENDIF}
962 {$ENDIF}
963 end;
965 procedure Startup;
966 begin
967 Randomize;
968 InitPath;
969 InitLog;
970 InitPrep;
971 e_Input_Initialize;
972 e_InitSoundSystem(NoSound);
973 {$IFDEF ENABLE_SYSTEM}
974 sys_Init;
975 sys_CharPress := @CharPress; (* install hook *)
976 sys_ScreenResize := @ScreenResize; (* install hook *)
977 {$ENDIF}
978 g_Options_SetDefault;
979 g_Options_SetDefaultVideo;
980 g_Console_Initialize;
981 // TODO move load configs here
982 g_Language_Set(gLanguage);
983 {$IFDEF ENABLE_RENDER}
984 r_Render_Initialize;
985 {$ENDIF}
986 DebugOptions;
987 g_Net_InitLowLevel;
988 // TODO init serverlist
989 {$IFDEF ENABLE_HOLMES}
990 InitHolmes;
991 {$ENDIF}
992 {$IFDEF ENABLE_RENDER}
993 g_PlayerModel_LoadAll;
994 r_Render_Load;
995 {$ELSE}
996 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
997 {$ENDIF}
998 g_Game_Init;
999 {$IFDEF ENABLE_MENU}
1000 g_Menu_Init;
1001 g_GUI_Init;
1002 {$ENDIF}
1003 g_Game_Process_Params;
1004 // TODO reload GAME textures
1005 g_Console_Init; // welcome message
1006 {$IFDEF ENABLE_MENU}
1007 if (not gGameOn) and gAskLanguage then
1008 g_Menu_AskLanguage;
1009 {$ENDIF}
1010 Time_Old := GetTickCount64();
1011 while not ProcessMessage() do begin end;
1012 g_Console_WriteGameConfig;
1013 {$IFDEF ENABLE_MENU}
1014 g_GUI_Destroy;
1015 g_Menu_Free;
1016 {$ENDIF}
1017 {$IFDEF ENABLE_RENDER}
1018 r_Render_Free;
1019 {$ENDIF}
1020 {$IFDEF ENABLE_HOLMES}
1021 FreeHolmes;
1022 {$ENDIF}
1023 g_Net_Slist_ShutdownAll;
1024 g_Net_DeinitLowLevel;
1025 (* g_Touch_Finalize; *)
1026 {$IFDEF ENABLE_RENDER}
1027 r_Render_Finalize;
1028 {$ENDIF}
1029 {$IFDEF ENABLE_SYSTEM}
1030 sys_Final;
1031 {$ENDIF}
1032 g_Console_Finalize;
1033 e_ReleaseSoundSystem;
1034 e_Input_Finalize;
1035 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1036 end;
1038 procedure EntryPoint;
1039 begin
1040 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1041 EntryParams;
1042 e_Log_Initialize;
1043 {$IFDEF HEADLESS}
1044 conbufDumpToStdOut := true;
1045 {$ENDIF}
1046 if noct then
1047 Startup
1048 else
1049 try
1050 Startup
1051 except on e: Exception do
1052 e_WriteStackTrace(e.message)
1053 else
1054 e_WriteStackTrace('FATAL ERROR')
1055 end;
1056 e_Log_Finalize
1057 end;
1059 {$IFDEF ANDROID}
1060 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1061 begin
1062 // TODO pass argc+argv to rtl
1063 EntryPoint;
1064 result := 0
1065 end;
1067 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1068 begin
1069 result:= JNI_VERSION_1_6;
1070 end;
1072 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1073 begin
1074 end;
1076 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1077 exports SDL_main name 'SDL_main';
1078 exports JNI_OnLoad name 'JNI_OnLoad';
1079 exports JNI_OnUnload name 'JNI_Unload';
1080 {$ELSE}
1081 begin
1082 EntryPoint
1083 {$ENDIF}
1085 end.