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