1e358b9224d6d6a94f33242dc3fca5cbd417fccb
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$IFDEF ANDROID}library{$ELSE}program{$ENDIF} Doom2DF;
18 {$IFNDEF HEADLESS}
19 {$IFDEF WINDOWS}
20 {$APPTYPE GUI}
21 {$ENDIF}
22 {$ENDIF}
23 {$HINTS OFF}
25 uses
26 {$IFDEF ANDROID}
27 ctypes, jni,
28 {$ENDIF}
29 {$IFDEF UNIX}
30 cthreads, BaseUnix,
31 {$ENDIF}
32 {$IFDEF DARWIN}
33 MacOSAll, CocoaAll,
34 {$ENDIF}
35 mempool in '../shared/mempool.pas',
36 conbuf in '../shared/conbuf.pas',
37 geom in '../shared/geom.pas',
38 math,
40 {$IFDEF USE_MINIUPNPC}
41 miniupnpc in '../lib/miniupnpc/miniupnpc.pas',
42 {$ENDIF}
44 {$IFDEF USE_SDL}
45 SDL in '../lib/sdl/sdl.pas',
46 {$IFDEF USE_SDLMIXER}
47 SDL_mixer in '../lib/sdl/sdl_mixer.pas',
48 {$ENDIF}
49 {$ENDIF}
50 {$IFDEF USE_SDL2}
51 SDL2 in '../lib/sdl2/sdl2.pas',
52 {$IFDEF USE_SDLMIXER}
53 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
54 {$ENDIF}
55 {$ENDIF}
56 {$IFDEF USE_SYSSTUB}
57 {$IFDEF USE_SDLMIXER}
58 SDL2 in '../lib/sdl2/sdl2.pas',
59 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
60 {$ENDIF}
61 {$ENDIF}
63 {$IFDEF USE_OPENAL}
64 AL in '../lib/openal/al.pas',
65 e_soundfile in '../engine/e_soundfile.pas',
66 {$IF DEFINED(USE_SDL) OR DEFINED(USE_SDL2)}
67 e_soundfile_wav in '../engine/e_soundfile_wav.pas',
68 {$ENDIF}
69 {$IFDEF USE_VORBIS}
70 vorbis in '../lib/vorbis/vorbis.pas',
71 e_soundfile_vorbis in '../engine/e_soundfile_vorbis.pas',
72 {$ENDIF}
73 {$IFDEF USE_FLUIDSYNTH}
74 fluidsynth in '../lib/fluidsynth/fluidsynth.pas',
75 e_soundfile_fluid in '../engine/e_soundfile_fluid.pas',
76 {$ENDIF}
77 {$IFDEF USE_MODPLUG}
78 modplug in '../lib/modplug/modplug.pas',
79 e_soundfile_modplug in '../engine/e_soundfile_modplug.pas',
80 {$ENDIF}
81 {$IFDEF USE_XMP}
82 xmp in '../lib/xmp/xmp.pas',
83 e_soundfile_xmp in '../engine/e_soundfile_xmp.pas',
84 {$ENDIF}
85 {$IFDEF USE_GME}
86 gme in '../lib/gme/gme.pas',
87 e_soundfile_gme in '../engine/e_soundfile_gme.pas',
88 {$ENDIF}
89 {$IFDEF USE_MPG123}
90 mpg123 in '../lib/mpg123/mpg123.pas',
91 e_soundfile_mp3 in '../engine/e_soundfile_mp3.pas',
92 {$ENDIF}
93 {$IFDEF USE_OPUS}
94 opus in '../lib/opus/opus.pas',
95 e_soundfile_opus in '../engine/e_soundfile_opus.pas',
96 {$ENDIF}
97 {$IF DEFINED(USE_VORBIS) OR DEFINED(USE_OPUS)}
98 ogg in '../lib/vorbis/ogg.pas', // this has to come last because link order
99 {$ENDIF}
100 {$ENDIF}
102 ENet in '../lib/enet/enet.pp',
103 e_input in '../engine/e_input.pas',
104 e_log in '../engine/e_log.pas',
105 e_sound in '../engine/e_sound.pas',
106 e_msg in '../engine/e_msg.pas',
107 e_res in '../engine/e_res.pas',
108 utils in '../shared/utils.pas',
109 xstreams in '../shared/xstreams.pas',
110 sfs in '../sfs/sfs.pas',
111 sfsPlainFS in '../sfs/sfsPlainFS.pas',
112 sfsZipFS in '../sfs/sfsZipFS.pas',
113 wadreader in '../shared/wadreader.pas',
114 MAPDEF in '../shared/MAPDEF.pas',
115 CONFIG in '../shared/CONFIG.pas',
116 g_base in 'g_base.pas',
117 g_basic in 'g_basic.pas',
118 g_console in 'g_console.pas',
119 g_net in 'g_net.pas',
120 g_netmsg in 'g_netmsg.pas',
121 g_nethandler in 'g_nethandler.pas',
122 g_netmaster in 'g_netmaster.pas',
123 g_res_downloader in 'g_res_downloader.pas',
124 g_grid in 'g_grid.pas',
125 g_game in 'g_game.pas',
126 {$IFDEF ENABLE_GFX}
127 g_gfx in 'g_gfx.pas',
128 {$ENDIF}
129 {$IFDEF ENABLE_GIBS}
130 g_gibs in 'g_gibs.pas',
131 {$ENDIF}
132 {$IFDEF ENABLE_SHELLS}
133 g_shells in 'g_shells.pas',
134 {$ENDIF}
135 {$IFDEF ENABLE_CORPSES}
136 g_corpses in 'g_corpses.pas',
137 {$ENDIF}
138 g_items in 'g_items.pas',
139 g_map in 'g_map.pas',
140 g_monsters in 'g_monsters.pas',
141 g_options in 'g_options.pas',
142 g_phys in 'g_phys.pas',
143 g_player in 'g_player.pas',
144 g_playermodel in 'g_playermodel.pas',
145 g_saveload in 'g_saveload.pas',
146 g_sound in 'g_sound.pas',
147 g_animations in 'g_animations.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_MENU}
165 g_gui in 'g_gui.pas',
166 g_menu in 'g_menu.pas',
167 {$ENDIF}
169 {$IFDEF ENABLE_RENDER}
170 {$IF DEFINED(USE_OPENGL) OR DEFINED(USE_GLES1) OR DEFINED(USE_GLSTUB)}
171 {$I ../shared/vampimg.inc}
172 r_animations in 'opengl/r_animations.pas',
173 r_console in 'opengl/r_console.pas',
174 r_game in 'opengl/r_game.pas',
175 {$IFDEF ENABLE_GFX}
176 r_gfx in 'opengl/r_gfx.pas',
177 {$ENDIF}
178 r_graphics in 'opengl/r_graphics.pas',
179 r_items in 'opengl/r_items.pas',
180 r_map in 'opengl/r_map.pas',
181 r_monsters in 'opengl/r_monsters.pas',
182 r_netmaster in 'opengl/r_netmaster.pas',
183 r_player in 'opengl/r_player.pas',
184 r_playermodel in 'opengl/r_playermodel.pas',
185 r_render in 'opengl/r_render.pas',
186 r_texture in 'opengl/r_texture.pas',
187 r_textures in 'opengl/r_textures.pas',
188 r_weapons in 'opengl/r_weapons.pas',
189 r_window in 'opengl/r_window.pas',
190 {$IFDEF ENABLE_TOUCH}
191 r_touch in 'opengl/r_touch.pas',
192 {$ENDIF}
193 {$IFDEF ENABLE_MENU}
194 r_gui in 'opengl/r_gui.pas',
195 {$ENDIF}
196 {$ELSEIF DEFINED(USE_STUBRENDER)}
197 r_render in 'renders/stub/r_render.pas',
198 {$ELSEIF DEFINED(USE_NEWGL)}
199 {$I ../shared/vampimg.inc}
200 r_render in 'renders/opengl/r_render.pas',
201 r_atlas in 'renders/opengl/r_atlas.pas',
202 r_textures in 'renders/opengl/r_textures.pas',
203 r_draw in 'renders/opengl/r_draw.pas',
204 r_map in 'renders/opengl/r_map.pas',
205 r_fonts in 'renders/opengl/r_fonts.pas',
206 r_common in 'renders/opengl/r_common.pas',
207 r_console in 'renders/opengl/r_console.pas',
208 r_gui in 'renders/opengl/r_gui.pas',
209 {$ELSE}
210 {$FATAL render driver not selected}
211 {$ENDIF}
212 {$ENDIF}
214 {$IFDEF USE_FMOD}
215 fmod in '../lib/FMOD/fmod.pas',
216 fmoderrors in '../lib/FMOD/fmoderrors.pas',
217 fmodpresets in '../lib/FMOD/fmodpresets.pas',
218 fmodtypes in '../lib/FMOD/fmodtypes.pas',
219 {$ENDIF}
220 xprofiler in '../shared/xprofiler.pas',
221 binheap in '../shared/binheap.pas',
222 hashtable in '../shared/hashtable.pas',
223 fhashdb in '../shared/fhashdb.pas',
224 idpool in '../shared/idpool.pas',
225 xparser in '../shared/xparser.pas',
226 xdynrec in '../shared/xdynrec.pas',
227 exoma in '../shared/exoma.pas',
228 envvars in '../shared/envvars.pas',
229 g_panel in 'g_panel.pas',
230 g_language in 'g_language.pas',
232 {$IFDEF ENABLE_HOLMES}
233 g_holmes in 'g_holmes.pas',
235 sdlcarcass in '../flexui/sdlcarcass.pas',
236 //sdlstandalone in '../flexui/sdlstandalone.pas',
238 fui_wadread in '../flexui/fui_wadread.pas',
239 fui_common in '../flexui/fui_common.pas',
240 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
241 fui_events in '../flexui/fui_events.pas',
242 fui_style in '../flexui/fui_style.pas',
243 fui_flexlay in '../flexui/fui_flexlay.pas',
244 fui_ctls in '../flexui/fui_ctls.pas',
245 {$ENDIF}
247 SysUtils, Classes;
249 {$IFDEF WINDOWS}
250 {$R *.res}
251 {$ENDIF}
253 const
254 autoexecScript = 'autoexec.cfg';
256 var
257 noct: Boolean = False;
258 binPath: AnsiString = '';
259 forceBinDir: Boolean = False;
260 {$IFDEF USE_SDLMIXER}
261 UseNativeMusic: Boolean;
262 {$ENDIF}
264 Time_Old: Int64;
265 NoSound: Boolean;
267 procedure Update ();
268 begin
269 // remember old mobj positions, prepare for update
270 g_Game_PreUpdate();
271 // server: receive client commands for new frame
272 // client: receive game state changes from server
273 if (NetMode = NET_SERVER) then g_Net_Host_Update()
274 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
275 // think
276 {$IFDEF ENABLE_RENDER}
277 r_Render_Update;
278 {$ENDIF}
279 g_Game_Update();
280 // server: send any accumulated outgoing data to clients
281 if NetMode = NET_SERVER then g_Net_Flush();
282 end;
284 function ProcessMessage (): Boolean;
285 var
286 i, t: Integer;
287 flag: Boolean;
288 Time, Time_Delta: Int64;
289 Frame: Int64;
290 begin
291 {$IFDEF ENABLE_SYSTEM}
292 Result := sys_HandleInput();
293 {$ELSE}
294 Result := False;
295 {$ENDIF}
297 Time := GetTickCount64();
298 Time_Delta := Time-Time_Old;
300 flag := false;
302 if wNeedTimeReset then
303 begin
304 Frame := 0;
305 Time_Delta := 28;
306 wNeedTimeReset := false;
307 end;
309 g_Map_ProfilersBegin();
310 g_Mons_ProfilersBegin();
312 t := Time_Delta div 28;
313 if (t > 0) then
314 begin
315 flag := true;
316 for i := 1 to t do
317 Update();
318 end;
320 g_Map_ProfilersEnd();
321 g_Mons_ProfilersEnd();
323 if (gExit = EXIT_QUIT) then
324 begin
325 result := true;
326 exit;
327 end;
329 // Время предыдущего обновления
330 if flag then
331 Time_Old := Time - (Time_Delta mod 28);
333 // don't wait if VSync is on, GL already probably waits enough
334 if gLerpActors then
335 flag := (Time - Frame >= gFrameTime) or gVSync;
337 if flag then
338 begin
339 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
340 gLerpFactor := 1.0
341 else
342 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
343 {$IFDEF ENABLE_RENDER}
344 r_Render_Draw;
345 {$ENDIF}
346 Frame := Time
347 end
348 else
349 Sleep(1);
351 e_SoundUpdate();
352 end;
354 procedure DebugOptions;
355 var
356 idx: Integer;
357 arg: AnsiString;
358 mdfo: TStream;
359 {$IFDEF ENABLE_HOLMES}
360 itmp: Integer;
361 valres: Word;
362 {$ENDIF}
363 begin
364 idx := 1;
365 while (idx <= ParamCount) do
366 begin
367 arg := ParamStr(idx);
368 Inc(idx);
369 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
370 if arg = '--jah' then g_profile_history_size := 100;
371 if arg = '--no-los' then gmon_dbg_los_enabled := false;
373 if arg = '--profile-render' then g_profile_frame_draw := true;
374 if arg = '--profile-coldet' then g_profile_collision := true;
375 if arg = '--profile-los' then g_profile_los := true;
377 {$IFDEF ENABLE_GFX}
378 if arg = '--no-particles' then gpart_dbg_enabled := false;
379 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
380 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
381 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
382 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
383 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
384 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
385 {$ENDIF}
387 if arg = '--debug-input' then g_dbg_input := True;
389 {.$IF DEFINED(D2F_DEBUG)}
390 if arg = '--aimline' then g_dbg_aimline_on := true;
391 {.$ENDIF}
393 {$IFDEF ENABLE_HOLMES}
394 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
396 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
397 begin
398 if (idx <= ParamCount) then
399 begin
400 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
401 Inc(idx);
402 end;
403 end;
405 if (arg = '--holmes-font') or (arg = '-holmes-font') then
406 begin
407 if (idx <= ParamCount) then
408 begin
409 itmp := 0;
410 val(ParamStr(idx), itmp, valres);
411 if (valres = 0) and (not g_holmes_imfunctional) then
412 begin
413 case itmp of
414 8: uiContext.font := 'win8';
415 14: uiContext.font := 'win14';
416 16: uiContext.font := 'win16';
417 end;
418 end;
419 Inc(idx);
420 end;
421 end;
422 {$ENDIF}
424 if (arg = '--game-scale') or (arg = '-game-scale') then
425 begin
426 if (idx <= ParamCount) then
427 begin
428 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
429 Inc(idx);
430 end;
431 end;
433 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
434 begin
435 mdfo := createDiskFile('mapdef.txt');
436 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
437 mdfo.Free();
438 Halt(0);
439 end;
441 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
442 begin
443 if (idx <= ParamCount) then
444 begin
445 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
446 Inc(idx);
447 end;
448 end;
449 end;
450 end;
452 function GetBinaryPath (): AnsiString;
453 {$IFDEF LINUX}
454 var sl: AnsiString;
455 {$ENDIF}
456 begin
457 result := ExtractFilePath(ParamStr(0));
458 {$IFDEF LINUX}
459 // it may be a symlink; do some guesswork here
460 sl := fpReadLink(ExtractFileName(ParamStr(0)));
461 if (sl = ParamStr(0)) then
462 begin
463 // use current directory, as we don't have anything better
464 //result := '.';
465 GetDir(0, result);
466 end;
467 {$ENDIF}
468 result := fixSlashes(result);
469 if (length(result) > 0) and (result[length(result)] <> '/') then
470 result := result + '/';
471 end;
473 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
474 var dir: AnsiString;
475 begin
476 e_LogWriteln(msg + ':');
477 for dir in dirs do
478 e_LogWriteln(' ' + dir);
479 end;
481 {$IFDEF DARWIN}
482 function NSStringToAnsiString (s: NSString): AnsiString;
483 var i: Integer;
484 begin
485 result := '';
486 for i := 0 to s.length - 1 do
487 result := result + AnsiChar(s.characterAtIndex(i));
488 end;
490 function GetBundlePath (): AnsiString;
491 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
492 begin
493 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
494 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
495 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
496 CFRelease(pathRef);
497 CFRelease(pathCFStr);
498 Result := pathStr;
499 end;
500 {$ENDIF}
502 procedure InitPath;
503 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
505 procedure AddDir (var dirs: SSArray; append: AnsiString);
506 begin
507 SetLength(dirs, Length(dirs) + 1);
508 dirs[High(dirs)] := ExpandFileName(append)
509 end;
511 function IsSep (ch: Char): Boolean;
512 begin
513 {$IFDEF WINDOWS}
514 result := (ch = '/') or (ch = '\');
515 {$ELSE}
516 result := (ch = '/');
517 {$ENDIF}
518 end;
520 function OptimizePath (dir: AnsiString): AnsiString;
521 var i, len: Integer; s: AnsiString;
522 begin
523 i := 1; len := Length(dir); s := '';
524 while i <= len do
525 begin
526 if IsSep(dir[i]) then
527 begin
528 s := s + DirectorySeparator;
529 Inc(i);
530 while (i <= len) and IsSep(dir[i]) do Inc(i);
531 if (i <= len) and (dir[i] = '.') then
532 begin
533 if (i = len) or IsSep(dir[i + 1]) then
534 begin
535 Inc(i)
536 end
537 else if (i + 1 <= len) and (dir[i + 1] = '.') then
538 begin
539 if (i + 1 = len) or IsSep(dir[i + 2]) then
540 begin
541 s := e_UpperDir(s);
542 Inc(i, 2)
543 end
544 end
545 end
546 end
547 else
548 begin
549 s := s + dir[i];
550 Inc(i)
551 end
552 end;
553 result := s
554 end;
556 procedure OptimizeDirs (var dirs: SSArray);
557 var i, j, k: Integer;
558 begin
559 for i := 0 to High(dirs) do
560 dirs[i] := OptimizePath(dirs[i]);
561 // deduplicate
562 i := High(dirs);
563 while i >= 0 do
564 begin
565 j := 0;
566 while j < i do
567 begin
568 if dirs[j] = dirs[i] then
569 begin
570 for k := j + 1 to High(dirs) do
571 dirs[k - 1] := dirs[k];
572 Dec(i);
573 SetLength(dirs, High(dirs))
574 end
575 else
576 begin
577 Inc(j)
578 end
579 end;
580 Dec(i)
581 end
582 end;
584 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
585 var s: AnsiString;
586 begin
587 if Length(dirs) = 0 then
588 for s in base do
589 AddDir(dirs, e_CatPath(s, append));
590 OptimizeDirs(dirs)
591 end;
593 function GetDefaultRODirs (): SSArray;
594 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
595 var home: AnsiString;
596 {$ENDIF}
597 {$IFDEF WINDOWS}
598 var appdata: AnsiString;
599 {$ENDIF}
600 {$IFDEF DARWIN}
601 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
602 {$ENDIF}
603 begin
604 result := nil;
605 {$IFDEF DARWIN}
606 bundle := GetBundlePath();
607 if ExtractFileExt(bundle) <> '.app' then
608 AddDir(result, binpath);
609 {$ELSE}
610 AddDir(result, binPath);
611 {$ENDIF}
612 if forceBinDir = false then
613 begin
614 {$IFDEF USE_SDL2}
615 AddDir(result, SDL_GetBasePath());
616 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
617 {$ENDIF}
618 {$IFDEF WINDOWS}
619 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
620 if appdata <> '' then
621 AddDir(result, appdata);
622 {$ENDIF}
623 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
624 AddDir(result, '/usr/share/doom2df');
625 AddDir(result, '/usr/local/share/doom2df');
626 home := GetEnvironmentVariable('HOME');
627 if home <> '' then
628 AddDir(result, e_CatPath(home, '.doom2df'));
629 {$ENDIF}
630 {$IFDEF DARWIN}
631 bundle := GetBundlePath();
632 if bundle <> '' then
633 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
634 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
635 for i := 0 to dirArr.count - 1 do
636 begin
637 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
638 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
639 end;
640 {$ENDIF}
641 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
642 AddDir(result, SDL_AndroidGetInternalStoragePath());
643 if SDL_AndroidGetExternalStorageState() <> 0 then
644 AddDir(result, SDL_AndroidGetExternalStoragePath());
645 {$ENDIF}
646 end
647 end;
649 function GetDefaultRWDirs (): SSArray;
650 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
651 var home: AnsiString;
652 {$ENDIF}
653 {$IFDEF WINDOWS}
654 var appdata: AnsiString;
655 {$ENDIF}
656 {$IFDEF DARWIN}
657 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
658 {$ENDIF}
659 begin
660 result := nil;
661 {$IFDEF DARWIN}
662 bundle := GetBundlePath();
663 if ExtractFileExt(bundle) <> '.app' then
664 AddDir(result, binPath);
665 {$ELSE}
666 AddDir(result, binPath);
667 {$ENDIF}
668 if forceBinDir = false then
669 begin
670 {$IFDEF USE_SDL2}
671 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
672 {$ENDIF}
673 {$IFDEF WINDOWS}
674 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
675 if appdata <> '' then
676 AddDir(result, appdata);
677 {$ENDIF}
678 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
679 home := GetEnvironmentVariable('HOME');
680 if home <> '' then
681 AddDir(result, e_CatPath(home, '.doom2df'));
682 {$ENDIF}
683 {$IFDEF DARWIN}
684 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
685 for i := 0 to dirArr.count - 1 do
686 begin
687 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
688 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
689 end;
690 {$ENDIF}
691 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
692 if SDL_AndroidGetExternalStorageState() <> 0 then
693 AddDir(result, SDL_AndroidGetExternalStoragePath());
694 {$ENDIF}
695 end
696 end;
698 begin
699 forceBinDir := false;
700 binPath := GetBinaryPath();
702 i := 1;
703 while i < ParamCount do
704 begin
705 case ParamStr(i) of
706 '--like-windoze': forceBinDir := true;
707 '--rw-dir':
708 begin
709 Inc(i);
710 rwdir := ParamStr(i);
711 (* RW *)
712 AddDir(LogDirs, e_CatPath(rwdir, ''));
713 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
714 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
715 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
716 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
717 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
718 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
719 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
720 (* RO *)
721 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
722 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
723 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
724 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
725 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
726 end;
727 '--ro-dir':
728 begin
729 Inc(i);
730 rodir := ParamStr(i);
731 (* RO *)
732 AddDir(DataDirs, e_CatPath(rodir, 'data'));
733 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
734 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
735 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
736 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
737 end;
738 '--game-wad':
739 begin
740 Inc(i);
741 GameWADName := ParamStr(i);
742 end;
743 '--config':
744 begin
745 Inc(i);
746 gConfigScript := ParamStr(i);
747 end;
748 end;
749 Inc(i)
750 end;
752 // prefer bin dir if it writable and contains game.wad
753 if forceBinDir = false then
754 begin
755 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
756 if e_CanCreateFilesAt(binPath) then
757 forceBinDir := true
758 end;
760 (* RO *)
761 rodirs := GetDefaultRODirs();
762 AddDef(DataDirs, rodirs, 'data');
763 AddDef(ModelDirs, rodirs, 'data/models');
764 AddDef(MegawadDirs, rodirs, 'maps/megawads');
765 AddDef(MapDirs, rodirs, 'maps');
766 AddDef(WadDirs, rodirs, 'wads');
768 (* RW *)
769 rwdirs := GetDefaultRWDirs();
770 AddDef(LogDirs, rwdirs, '');
771 AddDef(SaveDirs, rwdirs, 'data');
772 AddDef(CacheDirs, rwdirs, 'data/cache');
773 AddDef(ConfigDirs, rwdirs, '');
774 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
775 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
776 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
777 AddDef(StatsDirs, rwdirs, 'stats');
779 for i := 0 to High(MapDirs) do
780 AddDir(AllMapDirs, MapDirs[i]);
781 for i := 0 to High(MegawadDirs) do
782 AddDir(AllMapDirs, MegawadDirs[i]);
783 OptimizeDirs(AllMapDirs);
785 // HACK: ensure the screenshots folder also has a stats subfolder in it
786 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
787 if rwdir <> '' then CreateDir(rwdir + '/stats');
788 end;
790 procedure EntryParams;
791 var i: Integer;
792 begin
793 i := 1;
794 while i <= ParamCount do
795 begin
796 case ParamStr(i) of
797 '--gdb': noct := true;
798 '--log', '--con-stdout': conbufDumpToStdOut := true;
799 '--safe-log': e_SetSafeSlowLog(true);
800 '--log-file':
801 if i + 1 <= ParamCount then
802 begin
803 Inc(i);
804 LogFileName := ParamStr(i)
805 end;
806 '--no-fbo': glRenderToFBO := false;
807 end;
808 Inc(i)
809 end
810 end;
812 procedure InitLog;
813 var rwdir: AnsiString;
814 begin
815 if LogFileName = '' then
816 begin
817 rwdir := e_GetWriteableDir(LogDirs, false);
818 if rwdir <> '' then
819 begin
820 {$IFDEF HEADLESS}
821 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
822 {$ELSE}
823 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
824 {$ENDIF}
825 end
826 end;
827 if LogFileName <> '' then
828 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
829 e_InitWritelnDriver
830 end;
832 procedure InitPrep;
833 begin
834 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
835 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
836 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
837 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
838 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
840 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
841 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
843 PrintDirs('DataDirs', DataDirs);
844 PrintDirs('ModelDirs', ModelDirs);
845 PrintDirs('MegawadDirs', MegawadDirs);
846 PrintDirs('MapDirs', MapDirs);
847 PrintDirs('WadDirs', WadDirs);
849 PrintDirs('LogDirs', LogDirs);
850 PrintDirs('SaveDirs', SaveDirs);
851 PrintDirs('CacheDirs', CacheDirs);
852 PrintDirs('ConfigDirs', ConfigDirs);
853 PrintDirs('ScreenshotDirs', ScreenshotDirs);
854 PrintDirs('StatsDirs', StatsDirs);
855 PrintDirs('MapDownloadDirs', MapDownloadDirs);
856 PrintDirs('WadDownloadDirs', WadDownloadDirs);
858 {$IFDEF HEADLESS}
859 {$IFDEF USE_SDLMIXER}
860 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
861 {$ELSE}
862 NoSound := True; // FMOD backend will sort it out
863 {$ENDIF}
864 {$ELSE}
865 NoSound := False;
866 {$ENDIF}
868 GameWAD := e_FindWad(DataDirs, GameWADName);
869 if GameWad = '' then
870 begin
871 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
872 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
873 if forceBinDir = false then
874 SDL_ShowSimpleMessageBox(
875 SDL_MESSAGEBOX_ERROR,
876 'Doom 2D Forever',
877 PChar('WAD ' + GameWADName + ' not found in data directories.'),
878 nil
879 );
880 {$ENDIF}
881 e_DeinitLog;
882 Halt(1);
883 end
884 end;
886 {$IFDEF ENABLE_HOLMES}
887 procedure InitHolmes;
888 var flexloaded: Boolean;
889 begin
890 flexloaded := true;
891 if not fuiAddWad('flexui.wad') then
892 begin
893 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
894 end;
895 try
896 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
897 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
898 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
899 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
900 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
901 except on e: Exception do
902 begin
903 writeln('ERROR loading FlexUI fonts');
904 flexloaded := false;
905 //raise;
906 end;
907 else
908 begin
909 flexloaded := false;
910 //raise;
911 end;
912 end;
913 if flexloaded then
914 begin
915 try
916 e_LogWriteln('FlexUI: loading stylesheet...');
917 uiLoadStyles('flexui/widgets.wgs');
918 except on e: TParserException do
919 begin
920 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
921 //raise;
922 flexloaded := false;
923 end;
924 else
925 begin
926 //raise;
927 flexloaded := false;
928 end;
929 end;
930 end;
931 g_holmes_imfunctional := not flexloaded;
932 if not g_holmes_imfunctional then
933 begin
934 uiInitialize();
935 uiContext.font := 'win14';
936 end;
937 if assigned(oglInitCB) then oglInitCB;
938 end;
940 procedure FreeHolmes;
941 begin
942 if assigned(oglDeinitCB) then
943 oglDeinitCB
944 end;
945 {$ENDIF}
947 procedure InitSound;
948 {$IFDEF USE_SDLMIXER}
949 var timiditycfg: AnsiString;
950 var oldcwd, newcwd: RawByteString;
951 {$ENDIF}
952 begin
953 if NoSound = false then
954 begin
955 e_WriteLog('Initializing sound system', TMsgType.Notify);
956 {$IFDEF USE_SDLMIXER}
957 newcwd := '';
958 if UseNativeMusic then
959 SetEnvVar('SDL_NATIVE_MUSIC', '1');
960 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
961 if timiditycfg = '' then
962 begin
963 timiditycfg := 'timidity.cfg';
964 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
965 begin
966 timiditycfg := ExpandFileName(timiditycfg);
967 newcwd := ExtractFileDir(timiditycfg);
968 SetEnvVar('TIMIDITY_CFG', timiditycfg);
969 end
970 else
971 timiditycfg := '';
972 end;
973 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
974 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
975 {$ENDIF}
976 e_InitSoundSystem(NoSound);
977 {$IFDEF USE_SDLMIXER}
978 if e_TimidityDecoder and (newcwd <> '') then
979 begin
980 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
981 (* CWD not restored after sound init because timidity *)
982 (* store relative pathes internally and load patches *)
983 (* later. I hope game never relies on CWD. *)
984 oldcwd := '';
985 GetDir(0, oldcwd);
986 ChDir(newcwd);
987 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
988 end;
989 {$ENDIF}
990 end;
991 end;
993 procedure ScreenResize (w, h: Integer);
994 begin
995 {$IFDEF ENABLE_RENDER}
996 r_Render_Resize(w, h);
997 {$IFDEF ENABLE_HOLMES}
998 fuiScrWdt := w;
999 fuiScrHgt := h;
1000 {$ENDIF}
1001 {$IFNDEF ANDROID}
1002 (* This will fix menu reset on keyboard showing *)
1003 {$IFDEF ENABLE_MENU}
1004 g_Menu_Reset;
1005 {$ENDIF}
1006 {$ENDIF}
1007 //g_Game_ClearLoading;
1008 {$IFDEF ENABLE_HOLMES}
1009 if assigned(oglInitCB) then oglInitCB;
1010 {$ENDIF}
1011 {$ENDIF}
1012 end;
1014 procedure Startup;
1015 begin
1016 Randomize;
1017 InitPath;
1018 InitLog;
1019 InitPrep;
1020 e_Input_Initialize;
1021 InitSound;
1022 {$IFDEF ENABLE_SYSTEM}
1023 sys_Init;
1024 sys_CharPress := @CharPress; (* install hook *)
1025 sys_ScreenResize := @ScreenResize; (* install hook *)
1026 {$ENDIF}
1027 g_Options_SetDefault;
1028 g_Options_SetDefaultVideo;
1029 g_Console_Initialize;
1030 // TODO move load configs here
1031 g_Language_Set(gLanguage);
1032 {$IFDEF ENABLE_RENDER}
1033 r_Render_Initialize;
1034 {$ENDIF}
1035 DebugOptions;
1036 g_Net_InitLowLevel;
1037 // TODO init serverlist
1038 {$IFDEF ENABLE_HOLMES}
1039 InitHolmes;
1040 {$ENDIF}
1041 {$IFDEF ENABLE_RENDER}
1042 g_PlayerModel_LoadAll;
1043 r_Render_Load;
1044 {$ELSE}
1045 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1046 {$ENDIF}
1047 g_Game_Init;
1048 {$IFDEF ENABLE_MENU}
1049 g_Menu_Init;
1050 {$ENDIF}
1051 g_Game_Process_Params;
1052 // TODO reload GAME textures
1053 g_Console_Init; // welcome message
1054 {$IFDEF ENABLE_MENU}
1055 if (not gGameOn) and gAskLanguage then
1056 g_Menu_AskLanguage;
1057 {$ENDIF}
1058 Time_Old := GetTickCount64();
1059 while not ProcessMessage() do begin end;
1060 g_Console_WriteGameConfig;
1061 {$IFDEF ENABLE_MENU}
1062 g_GUI_Destroy;
1063 g_Menu_Free;
1064 {$ENDIF}
1065 {$IFDEF ENABLE_RENDER}
1066 r_Render_Free;
1067 {$ENDIF}
1068 {$IFDEF ENABLE_HOLMES}
1069 FreeHolmes;
1070 {$ENDIF}
1071 g_Net_Slist_ShutdownAll;
1072 g_Net_DeinitLowLevel;
1073 (* g_Touch_Finalize; *)
1074 {$IFDEF ENABLE_RENDER}
1075 r_Render_Finalize;
1076 {$ENDIF}
1077 {$IFDEF ENABLE_SYSTEM}
1078 sys_Final;
1079 {$ENDIF}
1080 g_Console_Finalize;
1081 e_ReleaseSoundSystem;
1082 e_Input_Finalize;
1083 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1084 end;
1086 procedure InitCVars;
1087 begin
1088 {$IFDEF USE_SDLMIXER}
1089 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1090 {$IFDEF DARWIN}
1091 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1092 {$ELSE}
1093 UseNativeMusic := false;
1094 {$ENDIF}
1095 {$ENDIF}
1096 end;
1098 procedure EntryPoint;
1099 begin
1100 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1101 InitCVars;
1102 EntryParams;
1103 e_Log_Initialize;
1104 {$IFDEF HEADLESS}
1105 conbufDumpToStdOut := true;
1106 {$ENDIF}
1107 if noct then
1108 Startup
1109 else
1110 try
1111 Startup
1112 except on e: Exception do
1113 e_WriteStackTrace(e.message)
1114 else
1115 e_WriteStackTrace('FATAL ERROR')
1116 end;
1117 e_Log_Finalize
1118 end;
1120 {$IFDEF ANDROID}
1121 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1122 begin
1123 {$IFDEF ANDROID}
1124 System.argc := argc;
1125 System.argv := argv;
1126 {$ENDIF}
1127 EntryPoint;
1128 result := 0
1129 end;
1131 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1132 begin
1133 result:= JNI_VERSION_1_6;
1134 end;
1136 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1137 begin
1138 end;
1140 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1141 exports SDL_main name 'SDL_main';
1142 exports JNI_OnLoad name 'JNI_OnLoad';
1143 exports JNI_OnUnload name 'JNI_Unload';
1144 {$ELSE}
1145 begin
1146 EntryPoint
1147 {$ENDIF}
1149 end.