DEADSOFTWARE

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