DEADSOFTWARE

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