DEADSOFTWARE

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