DEADSOFTWARE

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