DEADSOFTWARE

render: remove flag animation from player model state
[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 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_animations in 'opengl/r_animations.pas',
156 r_console in 'opengl/r_console.pas',
157 r_game in 'opengl/r_game.pas',
158 r_gfx in 'opengl/r_gfx.pas',
159 r_graphics in 'opengl/r_graphics.pas',
160 r_items in 'opengl/r_items.pas',
161 r_map in 'opengl/r_map.pas',
162 r_monsters in 'opengl/r_monsters.pas',
163 r_netmaster in 'opengl/r_netmaster.pas',
164 r_panel in 'opengl/r_panel.pas',
165 r_player in 'opengl/r_player.pas',
166 r_playermodel in 'opengl/r_playermodel.pas',
167 r_render in 'opengl/r_render.pas',
168 r_texture in 'opengl/r_texture.pas',
169 r_textures in 'opengl/r_textures.pas',
170 r_weapons in 'opengl/r_weapons.pas',
171 r_window in 'opengl/r_window.pas',
173 {$IFDEF USE_FMOD}
174 fmod in '../lib/FMOD/fmod.pas',
175 fmoderrors in '../lib/FMOD/fmoderrors.pas',
176 fmodpresets in '../lib/FMOD/fmodpresets.pas',
177 fmodtypes in '../lib/FMOD/fmodtypes.pas',
178 {$ENDIF}
179 xprofiler in '../shared/xprofiler.pas',
180 binheap in '../shared/binheap.pas',
181 hashtable in '../shared/hashtable.pas',
182 fhashdb in '../shared/fhashdb.pas',
183 idpool in '../shared/idpool.pas',
184 xparser in '../shared/xparser.pas',
185 xdynrec in '../shared/xdynrec.pas',
186 exoma in '../shared/exoma.pas',
187 envvars in '../shared/envvars.pas',
188 g_panel in 'g_panel.pas',
189 g_language in 'g_language.pas',
191 {$IFDEF ENABLE_HOLMES}
192 g_holmes in 'g_holmes.pas',
194 sdlcarcass in '../flexui/sdlcarcass.pas',
195 //sdlstandalone in '../flexui/sdlstandalone.pas',
197 fui_wadread in '../flexui/fui_wadread.pas',
198 fui_common in '../flexui/fui_common.pas',
199 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
200 fui_events in '../flexui/fui_events.pas',
201 fui_style in '../flexui/fui_style.pas',
202 fui_flexlay in '../flexui/fui_flexlay.pas',
203 fui_ctls in '../flexui/fui_ctls.pas',
204 {$ENDIF}
205 {$I ../shared/vampimg.inc}
207 SysUtils, Classes;
209 {$IFDEF WINDOWS}
210 {$R *.res}
211 {$ENDIF}
213 const
214 autoexecScript = 'autoexec.cfg';
216 var
217 noct: Boolean = False;
218 binPath: AnsiString = '';
219 forceBinDir: Boolean = False;
221 Time_Old: Int64;
222 NoSound: Boolean;
224 procedure Update ();
225 begin
226 // remember old mobj positions, prepare for update
227 g_Game_PreUpdate();
228 // server: receive client commands for new frame
229 // client: receive game state changes from server
230 if (NetMode = NET_SERVER) then g_Net_Host_Update()
231 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
232 // think
233 r_Render_Update;
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 Sleep(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 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
788 var timiditycfg: AnsiString;
789 {$ENDIF}
790 begin
791 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
792 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
793 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
794 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
796 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
797 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
799 PrintDirs('DataDirs', DataDirs);
800 PrintDirs('ModelDirs', ModelDirs);
801 PrintDirs('MegawadDirs', MegawadDirs);
802 PrintDirs('MapDirs', MapDirs);
803 PrintDirs('WadDirs', WadDirs);
805 PrintDirs('LogDirs', LogDirs);
806 PrintDirs('SaveDirs', SaveDirs);
807 PrintDirs('CacheDirs', CacheDirs);
808 PrintDirs('ConfigDirs', ConfigDirs);
809 PrintDirs('ScreenshotDirs', ScreenshotDirs);
810 PrintDirs('StatsDirs', StatsDirs);
811 PrintDirs('MapDownloadDirs', MapDownloadDirs);
812 PrintDirs('WadDownloadDirs', WadDownloadDirs);
814 {$IFDEF HEADLESS}
815 {$IFDEF USE_SDLMIXER}
816 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
817 {$ELSE}
818 NoSound := True; // FMOD backend will sort it out
819 {$ENDIF}
820 {$ELSE}
821 NoSound := False;
822 {$ENDIF}
824 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
825 timiditycfg := 'timidity.cfg';
826 if e_FindResource(ConfigDirs, timiditycfg) = true then
827 begin
828 timiditycfg := ExpandFileName(timiditycfg);
829 SetEnvVar('TIMIDITY_CFG', timiditycfg);
830 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
831 end;
832 {$ENDIF}
834 GameWAD := e_FindWad(DataDirs, GameWADName);
835 if GameWad = '' then
836 begin
837 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
838 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
839 if forceBinDir = false then
840 SDL_ShowSimpleMessageBox(
841 SDL_MESSAGEBOX_ERROR,
842 'Doom 2D Forever',
843 PChar('WAD ' + GameWADName + ' not found in data directories.'),
844 nil
845 );
846 {$ENDIF}
847 e_DeinitLog;
848 Halt(1);
849 end
850 end;
852 {$IFDEF ENABLE_HOLMES}
853 procedure InitHolmes;
854 var flexloaded: Boolean;
855 begin
856 flexloaded := true;
857 if not fuiAddWad('flexui.wad') then
858 begin
859 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
860 end;
861 try
862 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
863 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
864 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
865 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
866 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
867 except on e: Exception do
868 begin
869 writeln('ERROR loading FlexUI fonts');
870 flexloaded := false;
871 //raise;
872 end;
873 else
874 begin
875 flexloaded := false;
876 //raise;
877 end;
878 end;
879 if flexloaded then
880 begin
881 try
882 e_LogWriteln('FlexUI: loading stylesheet...');
883 uiLoadStyles('flexui/widgets.wgs');
884 except on e: TParserException do
885 begin
886 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
887 //raise;
888 flexloaded := false;
889 end;
890 else
891 begin
892 //raise;
893 flexloaded := false;
894 end;
895 end;
896 end;
897 g_holmes_imfunctional := not flexloaded;
898 if not g_holmes_imfunctional then
899 begin
900 uiInitialize();
901 uiContext.font := 'win14';
902 end;
903 if assigned(oglInitCB) then oglInitCB;
904 end;
906 procedure FreeHolmes;
907 begin
908 if assigned(oglDeinitCB) then
909 oglDeinitCB
910 end;
911 {$ENDIF}
913 procedure ScreenResize (w, h: Integer);
914 begin
915 r_Render_Resize(w, h);
916 {$IFDEF ENABLE_HOLMES}
917 fuiScrWdt := w;
918 fuiScrHgt := h;
919 {$ENDIF}
920 g_Game_SetupScreenSize;
921 {$IFNDEF ANDROID}
922 (* This will fix menu reset on keyboard showing *)
923 g_Menu_Reset;
924 {$ENDIF}
925 //g_Game_ClearLoading;
926 {$IFDEF ENABLE_HOLMES}
927 if assigned(oglInitCB) then oglInitCB;
928 {$ENDIF}
929 end;
931 procedure Startup;
932 begin
933 Randomize;
934 InitPath;
935 InitLog;
936 InitPrep;
937 e_Input_Initialize;
938 e_InitSoundSystem(NoSound);
939 sys_Init;
940 sys_CharPress := @CharPress; (* install hook *)
941 sys_ScreenResize := @ScreenResize; (* install hook *)
942 g_Options_SetDefault;
943 g_Options_SetDefaultVideo;
944 g_Console_Initialize;
945 // TODO move load configs here
946 g_Language_Set(gLanguage);
947 r_Render_Initialize;
948 g_Touch_Init;
949 DebugOptions;
950 g_Net_InitLowLevel;
951 // TODO init serverlist
952 {$IFDEF ENABLE_HOLMES}
953 InitHolmes;
954 {$ENDIF}
955 g_PlayerModel_LoadAll;
956 r_Render_Load;
957 g_Game_Init;
958 {$IFNDEF HEADLESS}
959 g_Menu_Init;
960 g_GUI_Init;
961 {$ENDIF}
962 g_Game_Process_Params;
963 // TODO reload GAME textures
964 g_Console_Init; // welcome message
965 {$IFNDEF HEADLESS}
966 if (not gGameOn) and gAskLanguage then
967 g_Menu_AskLanguage;
968 {$ENDIF}
969 Time_Old := GetTickCount64();
970 while not ProcessMessage() do begin end;
971 g_Console_WriteGameConfig;
972 {$IFNDEF HEADLESS}
973 g_GUI_Destroy;
974 g_Menu_Free;
975 {$ENDIF}
976 r_Render_Free;
977 {$IFDEF ENABLE_HOLMES}
978 FreeHolmes;
979 {$ENDIF}
980 g_Net_Slist_ShutdownAll;
981 g_Net_DeinitLowLevel;
982 (* g_Touch_Finalize; *)
983 r_Render_Finalize;
984 sys_Final;
985 g_Console_Finalize;
986 e_ReleaseSoundSystem;
987 e_Input_Finalize;
988 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
989 end;
991 procedure EntryPoint;
992 begin
993 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
994 EntryParams;
995 e_Log_Initialize;
996 {$IFDEF HEADLESS}
997 conbufDumpToStdOut := true;
998 {$ENDIF}
999 if noct then
1000 Startup
1001 else
1002 try
1003 Startup
1004 except on e: Exception do
1005 e_WriteStackTrace(e.message)
1006 else
1007 e_WriteStackTrace('FATAL ERROR')
1008 end;
1009 e_Log_Finalize
1010 end;
1012 {$IFDEF ANDROID}
1013 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1014 begin
1015 // TODO pass argc+argv to rtl
1016 EntryPoint;
1017 result := 0
1018 end;
1020 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1021 begin
1022 result:= JNI_VERSION_1_6;
1023 end;
1025 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1026 begin
1027 end;
1029 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1030 exports SDL_main name 'SDL_main';
1031 exports JNI_OnLoad name 'JNI_OnLoad';
1032 exports JNI_OnUnload name 'JNI_Unload';
1033 {$ELSE}
1034 begin
1035 EntryPoint
1036 {$ENDIF}
1038 end.