DEADSOFTWARE

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