DEADSOFTWARE

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