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