DEADSOFTWARE

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