DEADSOFTWARE

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