DEADSOFTWARE

98c39b5d6c5b15c55b27976b3878433cdea94d66
[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 {$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 {$IFDEF ENABLE_SYSTEM}
347 sys_Repaint;
348 {$ENDIF}
349 Frame := Time
350 end
351 else
352 Sleep(1);
354 e_SoundUpdate();
355 end;
357 procedure DebugOptions;
358 var
359 idx: Integer;
360 arg: AnsiString;
361 mdfo: TStream;
362 {$IFDEF ENABLE_HOLMES}
363 itmp: Integer;
364 valres: Word;
365 {$ENDIF}
366 begin
367 idx := 1;
368 while (idx <= ParamCount) do
369 begin
370 arg := ParamStr(idx);
371 Inc(idx);
372 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
373 if arg = '--jah' then g_profile_history_size := 100;
374 if arg = '--no-los' then gmon_dbg_los_enabled := false;
376 if arg = '--profile-render' then g_profile_frame_draw := true;
377 if arg = '--profile-coldet' then g_profile_collision := true;
378 if arg = '--profile-los' then g_profile_los := true;
380 {$IFDEF ENABLE_GFX}
381 if arg = '--no-particles' then gpart_dbg_enabled := false;
382 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
383 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
384 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
385 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
386 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
387 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
388 {$ENDIF}
390 if arg = '--debug-input' then g_dbg_input := True;
392 {.$IF DEFINED(D2F_DEBUG)}
393 if arg = '--aimline' then g_dbg_aimline_on := true;
394 {.$ENDIF}
396 {$IFDEF ENABLE_HOLMES}
397 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
399 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
400 begin
401 if (idx <= ParamCount) then
402 begin
403 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
404 Inc(idx);
405 end;
406 end;
408 if (arg = '--holmes-font') or (arg = '-holmes-font') then
409 begin
410 if (idx <= ParamCount) then
411 begin
412 itmp := 0;
413 val(ParamStr(idx), itmp, valres);
414 if (valres = 0) and (not g_holmes_imfunctional) then
415 begin
416 case itmp of
417 8: uiContext.font := 'win8';
418 14: uiContext.font := 'win14';
419 16: uiContext.font := 'win16';
420 end;
421 end;
422 Inc(idx);
423 end;
424 end;
425 {$ENDIF}
427 if (arg = '--game-scale') or (arg = '-game-scale') then
428 begin
429 if (idx <= ParamCount) then
430 begin
431 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
432 Inc(idx);
433 end;
434 end;
436 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
437 begin
438 mdfo := createDiskFile('mapdef.txt');
439 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
440 mdfo.Free();
441 Halt(0);
442 end;
444 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
445 begin
446 if (idx <= ParamCount) then
447 begin
448 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
449 Inc(idx);
450 end;
451 end;
452 end;
453 end;
455 function GetBinaryPath (): AnsiString;
456 {$IFDEF LINUX}
457 var sl: AnsiString;
458 {$ENDIF}
459 begin
460 result := ExtractFilePath(ParamStr(0));
461 {$IFDEF LINUX}
462 // it may be a symlink; do some guesswork here
463 sl := fpReadLink(ExtractFileName(ParamStr(0)));
464 if (sl = ParamStr(0)) then
465 begin
466 // use current directory, as we don't have anything better
467 //result := '.';
468 GetDir(0, result);
469 end;
470 {$ENDIF}
471 result := fixSlashes(result);
472 if (length(result) > 0) and (result[length(result)] <> '/') then
473 result := result + '/';
474 end;
476 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
477 var dir: AnsiString;
478 begin
479 e_LogWriteln(msg + ':');
480 for dir in dirs do
481 e_LogWriteln(' ' + dir);
482 end;
484 {$IFDEF DARWIN}
485 function NSStringToAnsiString (s: NSString): AnsiString;
486 var i: Integer;
487 begin
488 result := '';
489 for i := 0 to s.length - 1 do
490 result := result + AnsiChar(s.characterAtIndex(i));
491 end;
493 function GetBundlePath (): AnsiString;
494 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
495 begin
496 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
497 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
498 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
499 CFRelease(pathRef);
500 CFRelease(pathCFStr);
501 Result := pathStr;
502 end;
503 {$ENDIF}
505 procedure InitPath;
506 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
508 procedure AddDir (var dirs: SSArray; append: AnsiString);
509 begin
510 SetLength(dirs, Length(dirs) + 1);
511 dirs[High(dirs)] := ExpandFileName(append)
512 end;
514 function IsSep (ch: Char): Boolean;
515 begin
516 {$IFDEF WINDOWS}
517 result := (ch = '/') or (ch = '\');
518 {$ELSE}
519 result := (ch = '/');
520 {$ENDIF}
521 end;
523 function OptimizePath (dir: AnsiString): AnsiString;
524 var i, len: Integer; s: AnsiString;
525 begin
526 i := 1; len := Length(dir); s := '';
527 while i <= len do
528 begin
529 if IsSep(dir[i]) then
530 begin
531 s := s + DirectorySeparator;
532 Inc(i);
533 while (i <= len) and IsSep(dir[i]) do Inc(i);
534 if (i <= len) and (dir[i] = '.') then
535 begin
536 if (i = len) or IsSep(dir[i + 1]) then
537 begin
538 Inc(i)
539 end
540 else if (i + 1 <= len) and (dir[i + 1] = '.') then
541 begin
542 if (i + 1 = len) or IsSep(dir[i + 2]) then
543 begin
544 s := e_UpperDir(s);
545 Inc(i, 2)
546 end
547 end
548 end
549 end
550 else
551 begin
552 s := s + dir[i];
553 Inc(i)
554 end
555 end;
556 result := s
557 end;
559 procedure OptimizeDirs (var dirs: SSArray);
560 var i, j, k: Integer;
561 begin
562 for i := 0 to High(dirs) do
563 dirs[i] := OptimizePath(dirs[i]);
564 // deduplicate
565 i := High(dirs);
566 while i >= 0 do
567 begin
568 j := 0;
569 while j < i do
570 begin
571 if dirs[j] = dirs[i] then
572 begin
573 for k := j + 1 to High(dirs) do
574 dirs[k - 1] := dirs[k];
575 Dec(i);
576 SetLength(dirs, High(dirs))
577 end
578 else
579 begin
580 Inc(j)
581 end
582 end;
583 Dec(i)
584 end
585 end;
587 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
588 var s: AnsiString;
589 begin
590 if Length(dirs) = 0 then
591 for s in base do
592 AddDir(dirs, e_CatPath(s, append));
593 OptimizeDirs(dirs)
594 end;
596 function GetDefaultRODirs (): SSArray;
597 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
598 var home: AnsiString;
599 {$ENDIF}
600 {$IFDEF WINDOWS}
601 var appdata: AnsiString;
602 {$ENDIF}
603 {$IFDEF DARWIN}
604 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
605 {$ENDIF}
606 begin
607 result := nil;
608 {$IFDEF DARWIN}
609 bundle := GetBundlePath();
610 if ExtractFileExt(bundle) <> '.app' then
611 AddDir(result, binpath);
612 {$ELSE}
613 AddDir(result, binPath);
614 {$ENDIF}
615 if forceBinDir = false then
616 begin
617 {$IFDEF USE_SDL2}
618 AddDir(result, SDL_GetBasePath());
619 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
620 {$ENDIF}
621 {$IFDEF WINDOWS}
622 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
623 if appdata <> '' then
624 AddDir(result, appdata);
625 {$ENDIF}
626 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
627 AddDir(result, '/usr/share/doom2df');
628 AddDir(result, '/usr/local/share/doom2df');
629 home := GetEnvironmentVariable('HOME');
630 if home <> '' then
631 AddDir(result, e_CatPath(home, '.doom2df'));
632 {$ENDIF}
633 {$IFDEF DARWIN}
634 bundle := GetBundlePath();
635 if bundle <> '' then
636 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
637 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
638 for i := 0 to dirArr.count - 1 do
639 begin
640 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
641 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
642 end;
643 {$ENDIF}
644 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
645 AddDir(result, SDL_AndroidGetInternalStoragePath());
646 if SDL_AndroidGetExternalStorageState() <> 0 then
647 AddDir(result, SDL_AndroidGetExternalStoragePath());
648 {$ENDIF}
649 end
650 end;
652 function GetDefaultRWDirs (): SSArray;
653 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
654 var home: AnsiString;
655 {$ENDIF}
656 {$IFDEF WINDOWS}
657 var appdata: AnsiString;
658 {$ENDIF}
659 {$IFDEF DARWIN}
660 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
661 {$ENDIF}
662 begin
663 result := nil;
664 {$IFDEF DARWIN}
665 bundle := GetBundlePath();
666 if ExtractFileExt(bundle) <> '.app' then
667 AddDir(result, binPath);
668 {$ELSE}
669 AddDir(result, binPath);
670 {$ENDIF}
671 if forceBinDir = false then
672 begin
673 {$IFDEF USE_SDL2}
674 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
675 {$ENDIF}
676 {$IFDEF WINDOWS}
677 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
678 if appdata <> '' then
679 AddDir(result, appdata);
680 {$ENDIF}
681 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
682 home := GetEnvironmentVariable('HOME');
683 if home <> '' then
684 AddDir(result, e_CatPath(home, '.doom2df'));
685 {$ENDIF}
686 {$IFDEF DARWIN}
687 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
688 for i := 0 to dirArr.count - 1 do
689 begin
690 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
691 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
692 end;
693 {$ENDIF}
694 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
695 if SDL_AndroidGetExternalStorageState() <> 0 then
696 AddDir(result, SDL_AndroidGetExternalStoragePath());
697 {$ENDIF}
698 end
699 end;
701 begin
702 forceBinDir := false;
703 binPath := GetBinaryPath();
705 i := 1;
706 while i < ParamCount do
707 begin
708 case ParamStr(i) of
709 '--like-windoze': forceBinDir := true;
710 '--rw-dir':
711 begin
712 Inc(i);
713 rwdir := ParamStr(i);
714 (* RW *)
715 AddDir(LogDirs, e_CatPath(rwdir, ''));
716 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
717 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
718 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
719 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
720 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
721 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
722 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
723 (* RO *)
724 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
725 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
726 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
727 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
728 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
729 end;
730 '--ro-dir':
731 begin
732 Inc(i);
733 rodir := ParamStr(i);
734 (* RO *)
735 AddDir(DataDirs, e_CatPath(rodir, 'data'));
736 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
737 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
738 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
739 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
740 end;
741 '--game-wad':
742 begin
743 Inc(i);
744 GameWADName := ParamStr(i);
745 end;
746 '--config':
747 begin
748 Inc(i);
749 gConfigScript := ParamStr(i);
750 end;
751 end;
752 Inc(i)
753 end;
755 // prefer bin dir if it writable and contains game.wad
756 if forceBinDir = false then
757 begin
758 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
759 if e_CanCreateFilesAt(binPath) then
760 forceBinDir := true
761 end;
763 (* RO *)
764 rodirs := GetDefaultRODirs();
765 AddDef(DataDirs, rodirs, 'data');
766 AddDef(ModelDirs, rodirs, 'data/models');
767 AddDef(MegawadDirs, rodirs, 'maps/megawads');
768 AddDef(MapDirs, rodirs, 'maps');
769 AddDef(WadDirs, rodirs, 'wads');
771 (* RW *)
772 rwdirs := GetDefaultRWDirs();
773 AddDef(LogDirs, rwdirs, '');
774 AddDef(SaveDirs, rwdirs, 'data');
775 AddDef(CacheDirs, rwdirs, 'data/cache');
776 AddDef(ConfigDirs, rwdirs, '');
777 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
778 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
779 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
780 AddDef(StatsDirs, rwdirs, 'stats');
782 for i := 0 to High(MapDirs) do
783 AddDir(AllMapDirs, MapDirs[i]);
784 for i := 0 to High(MegawadDirs) do
785 AddDir(AllMapDirs, MegawadDirs[i]);
786 OptimizeDirs(AllMapDirs);
788 // HACK: ensure the screenshots folder also has a stats subfolder in it
789 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
790 if rwdir <> '' then CreateDir(rwdir + '/stats');
791 end;
793 procedure EntryParams;
794 var i: Integer;
795 begin
796 i := 1;
797 while i <= ParamCount do
798 begin
799 case ParamStr(i) of
800 '--gdb': noct := true;
801 '--log', '--con-stdout': conbufDumpToStdOut := true;
802 '--safe-log': e_SetSafeSlowLog(true);
803 '--log-file':
804 if i + 1 <= ParamCount then
805 begin
806 Inc(i);
807 LogFileName := ParamStr(i)
808 end;
809 '--no-fbo': glRenderToFBO := false;
810 end;
811 Inc(i)
812 end
813 end;
815 procedure InitLog;
816 var rwdir: AnsiString;
817 begin
818 if LogFileName = '' then
819 begin
820 rwdir := e_GetWriteableDir(LogDirs, false);
821 if rwdir <> '' then
822 begin
823 {$IFDEF HEADLESS}
824 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
825 {$ELSE}
826 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
827 {$ENDIF}
828 end
829 end;
830 if LogFileName <> '' then
831 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
832 e_InitWritelnDriver
833 end;
835 procedure InitPrep;
836 begin
837 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
838 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
839 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
840 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
841 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
843 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
844 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
846 PrintDirs('DataDirs', DataDirs);
847 PrintDirs('ModelDirs', ModelDirs);
848 PrintDirs('MegawadDirs', MegawadDirs);
849 PrintDirs('MapDirs', MapDirs);
850 PrintDirs('WadDirs', WadDirs);
852 PrintDirs('LogDirs', LogDirs);
853 PrintDirs('SaveDirs', SaveDirs);
854 PrintDirs('CacheDirs', CacheDirs);
855 PrintDirs('ConfigDirs', ConfigDirs);
856 PrintDirs('ScreenshotDirs', ScreenshotDirs);
857 PrintDirs('StatsDirs', StatsDirs);
858 PrintDirs('MapDownloadDirs', MapDownloadDirs);
859 PrintDirs('WadDownloadDirs', WadDownloadDirs);
861 {$IFDEF HEADLESS}
862 {$IFDEF USE_SDLMIXER}
863 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
864 {$ELSE}
865 NoSound := True; // FMOD backend will sort it out
866 {$ENDIF}
867 {$ELSE}
868 NoSound := False;
869 {$ENDIF}
871 GameWAD := e_FindWad(DataDirs, GameWADName);
872 if GameWad = '' then
873 begin
874 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
875 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
876 if forceBinDir = false then
877 SDL_ShowSimpleMessageBox(
878 SDL_MESSAGEBOX_ERROR,
879 'Doom 2D Forever',
880 PChar('WAD ' + GameWADName + ' not found in data directories.'),
881 nil
882 );
883 {$ENDIF}
884 e_DeinitLog;
885 Halt(1);
886 end
887 end;
889 {$IFDEF ENABLE_HOLMES}
890 procedure InitHolmes;
891 var flexloaded: Boolean;
892 begin
893 flexloaded := true;
894 if not fuiAddWad('flexui.wad') then
895 begin
896 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
897 end;
898 try
899 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
900 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
901 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
902 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
903 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
904 except on e: Exception do
905 begin
906 writeln('ERROR loading FlexUI fonts');
907 flexloaded := false;
908 //raise;
909 end;
910 else
911 begin
912 flexloaded := false;
913 //raise;
914 end;
915 end;
916 if flexloaded then
917 begin
918 try
919 e_LogWriteln('FlexUI: loading stylesheet...');
920 uiLoadStyles('flexui/widgets.wgs');
921 except on e: TParserException do
922 begin
923 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
924 //raise;
925 flexloaded := false;
926 end;
927 else
928 begin
929 //raise;
930 flexloaded := false;
931 end;
932 end;
933 end;
934 g_holmes_imfunctional := not flexloaded;
935 if not g_holmes_imfunctional then
936 begin
937 uiInitialize();
938 uiContext.font := 'win14';
939 end;
940 if assigned(oglInitCB) then oglInitCB;
941 end;
943 procedure FreeHolmes;
944 begin
945 if assigned(oglDeinitCB) then
946 oglDeinitCB
947 end;
948 {$ENDIF}
950 procedure InitSound;
951 {$IFDEF USE_SDLMIXER}
952 var timiditycfg: AnsiString;
953 var oldcwd, newcwd: RawByteString;
954 {$ENDIF}
955 begin
956 if NoSound = false then
957 begin
958 e_WriteLog('Initializing sound system', TMsgType.Notify);
959 {$IFDEF USE_SDLMIXER}
960 newcwd := '';
961 if UseNativeMusic then
962 SetEnvVar('SDL_NATIVE_MUSIC', '1');
963 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
964 if timiditycfg = '' then
965 begin
966 timiditycfg := 'timidity.cfg';
967 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
968 begin
969 timiditycfg := ExpandFileName(timiditycfg);
970 newcwd := ExtractFileDir(timiditycfg);
971 SetEnvVar('TIMIDITY_CFG', timiditycfg);
972 end
973 else
974 timiditycfg := '';
975 end;
976 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
977 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
978 {$ENDIF}
979 e_InitSoundSystem(NoSound);
980 {$IFDEF USE_SDLMIXER}
981 if e_TimidityDecoder and (newcwd <> '') then
982 begin
983 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
984 (* CWD not restored after sound init because timidity *)
985 (* store relative pathes internally and load patches *)
986 (* later. I hope game never relies on CWD. *)
987 oldcwd := '';
988 GetDir(0, oldcwd);
989 ChDir(newcwd);
990 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
991 end;
992 {$ENDIF}
993 end;
994 end;
996 procedure ScreenResize (w, h: Integer);
997 begin
998 {$IFDEF ENABLE_RENDER}
999 r_Render_Resize(w, h);
1000 {$IFDEF ENABLE_HOLMES}
1001 fuiScrWdt := w;
1002 fuiScrHgt := h;
1003 {$ENDIF}
1004 {$IFNDEF ANDROID}
1005 (* This will fix menu reset on keyboard showing *)
1006 {$IFDEF ENABLE_MENU}
1007 g_Menu_Reset;
1008 {$ENDIF}
1009 {$ENDIF}
1010 //g_Game_ClearLoading;
1011 {$IFDEF ENABLE_HOLMES}
1012 if assigned(oglInitCB) then oglInitCB;
1013 {$ENDIF}
1014 {$ENDIF}
1015 end;
1017 procedure Startup;
1018 begin
1019 Randomize;
1020 InitPath;
1021 InitLog;
1022 InitPrep;
1023 e_Input_Initialize;
1024 InitSound;
1025 {$IFDEF ENABLE_SYSTEM}
1026 sys_Init;
1027 sys_CharPress := @CharPress; (* install hook *)
1028 sys_ScreenResize := @ScreenResize; (* install hook *)
1029 {$ENDIF}
1030 g_Options_SetDefault;
1031 g_Options_SetDefaultVideo;
1032 g_Console_Initialize;
1033 // TODO move load configs here
1034 g_Language_Set(gLanguage);
1035 {$IFDEF ENABLE_RENDER}
1036 r_Render_Initialize;
1037 {$ENDIF}
1038 DebugOptions;
1039 g_Net_InitLowLevel;
1040 // TODO init serverlist
1041 {$IFDEF ENABLE_HOLMES}
1042 InitHolmes;
1043 {$ENDIF}
1044 {$IFDEF ENABLE_RENDER}
1045 g_PlayerModel_LoadAll;
1046 r_Render_Load;
1047 {$ELSE}
1048 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1049 {$ENDIF}
1050 g_Game_Init;
1051 {$IFDEF ENABLE_MENU}
1052 g_Menu_Init;
1053 {$ENDIF}
1054 g_Game_Process_Params;
1055 // TODO reload GAME textures
1056 g_Console_Init; // welcome message
1057 {$IFDEF ENABLE_MENU}
1058 if (not gGameOn) and gAskLanguage then
1059 g_Menu_AskLanguage;
1060 {$ENDIF}
1061 Time_Old := GetTickCount64();
1062 while not ProcessMessage() do begin end;
1063 g_Console_WriteGameConfig;
1064 {$IFDEF ENABLE_MENU}
1065 g_GUI_Destroy;
1066 g_Menu_Free;
1067 {$ENDIF}
1068 {$IFDEF ENABLE_RENDER}
1069 r_Render_Free;
1070 {$ENDIF}
1071 {$IFDEF ENABLE_HOLMES}
1072 FreeHolmes;
1073 {$ENDIF}
1074 g_Net_Slist_ShutdownAll;
1075 g_Net_DeinitLowLevel;
1076 (* g_Touch_Finalize; *)
1077 {$IFDEF ENABLE_RENDER}
1078 r_Render_Finalize;
1079 {$ENDIF}
1080 {$IFDEF ENABLE_SYSTEM}
1081 sys_Final;
1082 {$ENDIF}
1083 g_Console_Finalize;
1084 e_ReleaseSoundSystem;
1085 e_Input_Finalize;
1086 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1087 end;
1089 procedure InitCVars;
1090 begin
1091 {$IFDEF USE_SDLMIXER}
1092 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1093 {$IFDEF DARWIN}
1094 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1095 {$ELSE}
1096 UseNativeMusic := false;
1097 {$ENDIF}
1098 {$ENDIF}
1099 end;
1101 procedure EntryPoint;
1102 begin
1103 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1104 InitCVars;
1105 EntryParams;
1106 e_Log_Initialize;
1107 {$IFDEF HEADLESS}
1108 conbufDumpToStdOut := true;
1109 {$ENDIF}
1110 if noct then
1111 Startup
1112 else
1113 try
1114 Startup
1115 except on e: Exception do
1116 e_WriteStackTrace(e.message)
1117 else
1118 e_WriteStackTrace('FATAL ERROR')
1119 end;
1120 e_Log_Finalize
1121 end;
1123 {$IFDEF ANDROID}
1124 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1125 begin
1126 {$IFDEF ANDROID}
1127 System.argc := argc;
1128 System.argv := argv;
1129 {$ENDIF}
1130 EntryPoint;
1131 result := 0
1132 end;
1134 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1135 begin
1136 result:= JNI_VERSION_1_6;
1137 end;
1139 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1140 begin
1141 end;
1143 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1144 exports SDL_main name 'SDL_main';
1145 exports JNI_OnLoad name 'JNI_OnLoad';
1146 exports JNI_OnUnload name 'JNI_Unload';
1147 {$ELSE}
1148 begin
1149 EntryPoint
1150 {$ENDIF}
1152 end.