DEADSOFTWARE

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