DEADSOFTWARE

render: separate animation drawing from game code
[d2df-sdl.git] / src / game / Doom2DF.lpr
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$IFDEF ANDROID}library{$ELSE}program{$ENDIF} Doom2DF;
18 {$IFNDEF HEADLESS}
19 {$IFDEF WINDOWS}
20 {$APPTYPE GUI}
21 {$ENDIF}
22 {$ENDIF}
23 {$HINTS OFF}
25 uses
26 {$IFDEF ANDROID}
27 ctypes, jni,
28 {$ENDIF}
29 {$IFDEF UNIX}
30 cthreads, BaseUnix,
31 {$ENDIF}
32 {$IFDEF DARWIN}
33 MacOSAll, CocoaAll,
34 {$ENDIF}
35 mempool in '../shared/mempool.pas',
36 conbuf in '../shared/conbuf.pas',
37 geom in '../shared/geom.pas',
38 math,
40 {$IFDEF USE_MINIUPNPC}
41 miniupnpc in '../lib/miniupnpc/miniupnpc.pas',
42 {$ENDIF}
44 {$IFDEF USE_SDL}
45 SDL,
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_weapons in 'opengl/r_weapons.pas',
170 r_window in 'opengl/r_window.pas',
172 {$IFDEF USE_FMOD}
173 fmod in '../lib/FMOD/fmod.pas',
174 fmoderrors in '../lib/FMOD/fmoderrors.pas',
175 fmodpresets in '../lib/FMOD/fmodpresets.pas',
176 fmodtypes in '../lib/FMOD/fmodtypes.pas',
177 {$ENDIF}
178 xprofiler in '../shared/xprofiler.pas',
179 binheap in '../shared/binheap.pas',
180 hashtable in '../shared/hashtable.pas',
181 fhashdb in '../shared/fhashdb.pas',
182 idpool in '../shared/idpool.pas',
183 xparser in '../shared/xparser.pas',
184 xdynrec in '../shared/xdynrec.pas',
185 exoma in '../shared/exoma.pas',
186 envvars in '../shared/envvars.pas',
187 g_panel in 'g_panel.pas',
188 g_language in 'g_language.pas',
190 {$IFDEF ENABLE_HOLMES}
191 g_holmes in 'g_holmes.pas',
193 sdlcarcass in '../flexui/sdlcarcass.pas',
194 //sdlstandalone in '../flexui/sdlstandalone.pas',
196 fui_wadread in '../flexui/fui_wadread.pas',
197 fui_common in '../flexui/fui_common.pas',
198 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
199 fui_events in '../flexui/fui_events.pas',
200 fui_style in '../flexui/fui_style.pas',
201 fui_flexlay in '../flexui/fui_flexlay.pas',
202 fui_ctls in '../flexui/fui_ctls.pas',
203 {$ENDIF}
204 {$I ../shared/vampimg.inc}
206 SysUtils, Classes;
208 {$IFDEF WINDOWS}
209 {$R *.res}
210 {$ENDIF}
212 const
213 autoexecScript = 'autoexec.cfg';
215 var
216 noct: Boolean = False;
217 binPath: AnsiString = '';
218 forceBinDir: Boolean = False;
220 Time_Old: Int64;
221 NoSound: Boolean;
223 procedure Update ();
224 begin
225 // remember old mobj positions, prepare for update
226 g_Game_PreUpdate();
227 // server: receive client commands for new frame
228 // client: receive game state changes from server
229 if (NetMode = NET_SERVER) then g_Net_Host_Update()
230 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
231 // think
232 g_Game_Update();
233 // server: send any accumulated outgoing data to clients
234 if NetMode = NET_SERVER then g_Net_Flush();
235 end;
237 function ProcessMessage (): Boolean;
238 var
239 i, t: Integer;
240 flag: Boolean;
241 Time, Time_Delta: Int64;
242 Frame: Int64;
243 begin
244 result := sys_HandleInput();
246 Time := GetTickCount64();
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 (gExit = EXIT_QUIT) then
273 begin
274 result := true;
275 exit;
276 end;
278 // Время предыдущего обновления
279 if flag then
280 Time_Old := Time - (Time_Delta mod 28);
282 // don't wait if VSync is on, GL already probably waits enough
283 if gLerpActors then
284 flag := (Time - Frame >= gFrameTime) or gVSync;
286 if flag then
287 begin
288 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
289 gLerpFactor := 1.0
290 else
291 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
292 r_Game_Draw;
293 sys_Repaint;
294 Frame := Time
295 end
296 else
297 Sleep(1);
299 e_SoundUpdate();
300 end;
302 procedure DebugOptions;
303 var
304 idx: Integer;
305 arg: AnsiString;
306 mdfo: TStream;
307 {$IFDEF ENABLE_HOLMES}
308 itmp: Integer;
309 valres: Word;
310 {$ENDIF}
311 begin
312 idx := 1;
313 while (idx <= ParamCount) do
314 begin
315 arg := ParamStr(idx);
316 Inc(idx);
317 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
318 if arg = '--jah' then g_profile_history_size := 100;
319 if arg = '--no-particles' then gpart_dbg_enabled := false;
320 if arg = '--no-los' then gmon_dbg_los_enabled := false;
322 if arg = '--profile-render' then g_profile_frame_draw := true;
323 if arg = '--profile-coldet' then g_profile_collision := true;
324 if arg = '--profile-los' then g_profile_los := true;
326 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
327 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
328 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
329 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
330 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
331 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
333 if arg = '--debug-input' then g_dbg_input := True;
335 {.$IF DEFINED(D2F_DEBUG)}
336 if arg = '--aimline' then g_dbg_aimline_on := true;
337 {.$ENDIF}
339 {$IFDEF ENABLE_HOLMES}
340 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
342 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
343 begin
344 if (idx <= ParamCount) then
345 begin
346 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
347 Inc(idx);
348 end;
349 end;
351 if (arg = '--holmes-font') or (arg = '-holmes-font') then
352 begin
353 if (idx <= ParamCount) then
354 begin
355 itmp := 0;
356 val(ParamStr(idx), itmp, valres);
357 {$IFNDEF HEADLESS}
358 if (valres = 0) and (not g_holmes_imfunctional) then
359 begin
360 case itmp of
361 8: uiContext.font := 'win8';
362 14: uiContext.font := 'win14';
363 16: uiContext.font := 'win16';
364 end;
365 end;
366 {$ELSE}
367 // fuck off, fpc!
368 itmp := itmp;
369 valres := valres;
370 {$ENDIF}
371 Inc(idx);
372 end;
373 end;
374 {$ENDIF}
376 if (arg = '--game-scale') or (arg = '-game-scale') then
377 begin
378 if (idx <= ParamCount) then
379 begin
380 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
381 Inc(idx);
382 end;
383 end;
385 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
386 begin
387 mdfo := createDiskFile('mapdef.txt');
388 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
389 mdfo.Free();
390 Halt(0);
391 end;
393 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
394 begin
395 if (idx <= ParamCount) then
396 begin
397 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
398 Inc(idx);
399 end;
400 end;
401 end;
402 end;
404 function GetBinaryPath (): AnsiString;
405 {$IFDEF LINUX}
406 var sl: AnsiString;
407 {$ENDIF}
408 begin
409 result := ExtractFilePath(ParamStr(0));
410 {$IFDEF LINUX}
411 // it may be a symlink; do some guesswork here
412 sl := fpReadLink(ExtractFileName(ParamStr(0)));
413 if (sl = ParamStr(0)) then
414 begin
415 // use current directory, as we don't have anything better
416 //result := '.';
417 GetDir(0, result);
418 end;
419 {$ENDIF}
420 result := fixSlashes(result);
421 if (length(result) > 0) and (result[length(result)] <> '/') then
422 result := result + '/';
423 end;
425 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
426 var dir: AnsiString;
427 begin
428 e_LogWriteln(msg + ':');
429 for dir in dirs do
430 e_LogWriteln(' ' + dir);
431 end;
433 {$IFDEF DARWIN}
434 function NSStringToAnsiString (s: NSString): AnsiString;
435 var i: Integer;
436 begin
437 result := '';
438 for i := 0 to s.length - 1 do
439 result := result + AnsiChar(s.characterAtIndex(i));
440 end;
442 function GetBundlePath (): AnsiString;
443 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
444 begin
445 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
446 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
447 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
448 CFRelease(pathRef);
449 CFRelease(pathCFStr);
450 Result := pathStr;
451 end;
452 {$ENDIF}
454 procedure InitPath;
455 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
457 procedure AddDir (var dirs: SSArray; append: AnsiString);
458 begin
459 SetLength(dirs, Length(dirs) + 1);
460 dirs[High(dirs)] := ExpandFileName(append)
461 end;
463 function IsSep (ch: Char): Boolean;
464 begin
465 {$IFDEF WINDOWS}
466 result := (ch = '/') or (ch = '\');
467 {$ELSE}
468 result := (ch = '/');
469 {$ENDIF}
470 end;
472 function OptimizePath (dir: AnsiString): AnsiString;
473 var i, len: Integer; s: AnsiString;
474 begin
475 i := 1; len := Length(dir); s := '';
476 while i <= len do
477 begin
478 if IsSep(dir[i]) then
479 begin
480 s := s + DirectorySeparator;
481 Inc(i);
482 while (i <= len) and IsSep(dir[i]) do Inc(i);
483 if (i <= len) and (dir[i] = '.') then
484 begin
485 if (i = len) or IsSep(dir[i + 1]) then
486 begin
487 Inc(i)
488 end
489 else if (i + 1 <= len) and (dir[i + 1] = '.') then
490 begin
491 if (i + 1 = len) or IsSep(dir[i + 2]) then
492 begin
493 s := e_UpperDir(s);
494 Inc(i, 2)
495 end
496 end
497 end
498 end
499 else
500 begin
501 s := s + dir[i];
502 Inc(i)
503 end
504 end;
505 result := s
506 end;
508 procedure OptimizeDirs (var dirs: SSArray);
509 var i, j, k: Integer;
510 begin
511 for i := 0 to High(dirs) do
512 dirs[i] := OptimizePath(dirs[i]);
513 // deduplicate
514 i := High(dirs);
515 while i >= 0 do
516 begin
517 j := 0;
518 while j < i do
519 begin
520 if dirs[j] = dirs[i] then
521 begin
522 for k := j + 1 to High(dirs) do
523 dirs[k - 1] := dirs[k];
524 Dec(i);
525 SetLength(dirs, High(dirs))
526 end
527 else
528 begin
529 Inc(j)
530 end
531 end;
532 Dec(i)
533 end
534 end;
536 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
537 var s: AnsiString;
538 begin
539 if Length(dirs) = 0 then
540 for s in base do
541 AddDir(dirs, e_CatPath(s, append));
542 OptimizeDirs(dirs)
543 end;
545 function GetDefaultRODirs (): SSArray;
546 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
547 var home: AnsiString;
548 {$ENDIF}
549 {$IFDEF WINDOWS}
550 var appdata: AnsiString;
551 {$ENDIF}
552 {$IFDEF DARWIN}
553 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
554 {$ENDIF}
555 begin
556 result := nil;
557 {$IFDEF DARWIN}
558 bundle := GetBundlePath();
559 if ExtractFileExt(bundle) <> '.app' then
560 AddDir(result, binpath);
561 {$ELSE}
562 AddDir(result, binPath);
563 {$ENDIF}
564 if forceBinDir = false then
565 begin
566 {$IFDEF USE_SDL2}
567 AddDir(result, SDL_GetBasePath());
568 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
569 {$ENDIF}
570 {$IFDEF WINDOWS}
571 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
572 if appdata <> '' then
573 AddDir(result, appdata);
574 {$ENDIF}
575 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
576 AddDir(result, '/usr/share/doom2df');
577 AddDir(result, '/usr/local/share/doom2df');
578 home := GetEnvironmentVariable('HOME');
579 if home <> '' then
580 AddDir(result, e_CatPath(home, '.doom2df'));
581 {$ENDIF}
582 {$IFDEF DARWIN}
583 bundle := GetBundlePath();
584 if bundle <> '' then
585 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
586 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
587 for i := 0 to dirArr.count - 1 do
588 begin
589 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
590 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
591 end;
592 {$ENDIF}
593 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
594 AddDir(result, SDL_AndroidGetInternalStoragePath());
595 if SDL_AndroidGetExternalStorageState() <> 0 then
596 AddDir(result, SDL_AndroidGetExternalStoragePath());
597 {$ENDIF}
598 end
599 end;
601 function GetDefaultRWDirs (): SSArray;
602 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
603 var home: AnsiString;
604 {$ENDIF}
605 {$IFDEF WINDOWS}
606 var appdata: AnsiString;
607 {$ENDIF}
608 {$IFDEF DARWIN}
609 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
610 {$ENDIF}
611 begin
612 result := nil;
613 {$IFDEF DARWIN}
614 bundle := GetBundlePath();
615 if ExtractFileExt(bundle) <> '.app' then
616 AddDir(result, binPath);
617 {$ELSE}
618 AddDir(result, binPath);
619 {$ENDIF}
620 if forceBinDir = false then
621 begin
622 {$IFDEF USE_SDL2}
623 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
624 {$ENDIF}
625 {$IFDEF WINDOWS}
626 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
627 if appdata <> '' then
628 AddDir(result, appdata);
629 {$ENDIF}
630 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
631 home := GetEnvironmentVariable('HOME');
632 if home <> '' then
633 AddDir(result, e_CatPath(home, '.doom2df'));
634 {$ENDIF}
635 {$IFDEF DARWIN}
636 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
637 for i := 0 to dirArr.count - 1 do
638 begin
639 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
640 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
641 end;
642 {$ENDIF}
643 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
644 if SDL_AndroidGetExternalStorageState() <> 0 then
645 AddDir(result, SDL_AndroidGetExternalStoragePath());
646 {$ENDIF}
647 end
648 end;
650 begin
651 forceBinDir := false;
652 binPath := GetBinaryPath();
654 i := 1;
655 while i < ParamCount do
656 begin
657 case ParamStr(i) of
658 '--like-windoze': forceBinDir := true;
659 '--rw-dir':
660 begin
661 Inc(i);
662 rwdir := ParamStr(i);
663 (* RW *)
664 AddDir(LogDirs, e_CatPath(rwdir, ''));
665 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
666 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
667 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
668 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
669 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
670 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
671 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
672 (* RO *)
673 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
674 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
675 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
676 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
677 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
678 end;
679 '--ro-dir':
680 begin
681 Inc(i);
682 rodir := ParamStr(i);
683 (* RO *)
684 AddDir(DataDirs, e_CatPath(rodir, 'data'));
685 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
686 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
687 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
688 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
689 end;
690 '--game-wad':
691 begin
692 Inc(i);
693 GameWADName := ParamStr(i);
694 end;
695 '--config':
696 begin
697 Inc(i);
698 gConfigScript := ParamStr(i);
699 end;
700 end;
701 Inc(i)
702 end;
704 // prefer bin dir if it writable and contains game.wad
705 if forceBinDir = false then
706 begin
707 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
708 if e_CanCreateFilesAt(binPath) then
709 forceBinDir := true
710 end;
712 (* RO *)
713 rodirs := GetDefaultRODirs();
714 AddDef(DataDirs, rodirs, 'data');
715 AddDef(ModelDirs, rodirs, 'data/models');
716 AddDef(MegawadDirs, rodirs, 'maps/megawads');
717 AddDef(MapDirs, rodirs, 'maps');
718 AddDef(WadDirs, rodirs, 'wads');
720 (* RW *)
721 rwdirs := GetDefaultRWDirs();
722 AddDef(LogDirs, rwdirs, '');
723 AddDef(SaveDirs, rwdirs, 'data');
724 AddDef(CacheDirs, rwdirs, 'data/cache');
725 AddDef(ConfigDirs, rwdirs, '');
726 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
727 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
728 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
729 AddDef(StatsDirs, rwdirs, 'stats');
731 for i := 0 to High(MapDirs) do
732 AddDir(AllMapDirs, MapDirs[i]);
733 for i := 0 to High(MegawadDirs) do
734 AddDir(AllMapDirs, MegawadDirs[i]);
735 OptimizeDirs(AllMapDirs);
737 // HACK: ensure the screenshots folder also has a stats subfolder in it
738 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
739 if rwdir <> '' then CreateDir(rwdir + '/stats');
740 end;
742 procedure EntryParams;
743 var i: Integer;
744 begin
745 i := 1;
746 while i <= ParamCount do
747 begin
748 case ParamStr(i) of
749 '--gdb': noct := true;
750 '--log', '--con-stdout': conbufDumpToStdOut := true;
751 '--safe-log': e_SetSafeSlowLog(true);
752 '--log-file':
753 if i + 1 <= ParamCount then
754 begin
755 Inc(i);
756 LogFileName := ParamStr(i)
757 end;
758 '--no-fbo': glRenderToFBO := false;
759 end;
760 Inc(i)
761 end
762 end;
764 procedure InitLog;
765 var rwdir: AnsiString;
766 begin
767 if LogFileName = '' then
768 begin
769 rwdir := e_GetWriteableDir(LogDirs, false);
770 if rwdir <> '' then
771 begin
772 {$IFDEF HEADLESS}
773 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
774 {$ELSE}
775 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
776 {$ENDIF}
777 end
778 end;
779 if LogFileName <> '' then
780 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
781 e_InitWritelnDriver
782 end;
784 procedure InitPrep;
785 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
786 var timiditycfg: AnsiString;
787 {$ENDIF}
788 begin
789 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), 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 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
823 timiditycfg := 'timidity.cfg';
824 if e_FindResource(ConfigDirs, timiditycfg) = true then
825 begin
826 timiditycfg := ExpandFileName(timiditycfg);
827 SetEnvVar('TIMIDITY_CFG', timiditycfg);
828 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
829 end;
830 {$ENDIF}
832 GameWAD := e_FindWad(DataDirs, GameWADName);
833 if GameWad = '' then
834 begin
835 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
836 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
837 if forceBinDir = false then
838 SDL_ShowSimpleMessageBox(
839 SDL_MESSAGEBOX_ERROR,
840 'Doom 2D Forever',
841 PChar('WAD ' + GameWADName + ' not found in data directories.'),
842 nil
843 );
844 {$ENDIF}
845 e_DeinitLog;
846 Halt(1);
847 end
848 end;
850 {$IFDEF ENABLE_HOLMES}
851 procedure InitHolmes;
852 var flexloaded: Boolean;
853 begin
854 flexloaded := true;
855 if not fuiAddWad('flexui.wad') then
856 begin
857 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
858 end;
859 try
860 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
861 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
862 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
863 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
864 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
865 except on e: Exception do
866 begin
867 writeln('ERROR loading FlexUI fonts');
868 flexloaded := false;
869 //raise;
870 end;
871 else
872 begin
873 flexloaded := false;
874 //raise;
875 end;
876 end;
877 if flexloaded then
878 begin
879 try
880 e_LogWriteln('FlexUI: loading stylesheet...');
881 uiLoadStyles('flexui/widgets.wgs');
882 except on e: TParserException do
883 begin
884 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
885 //raise;
886 flexloaded := false;
887 end;
888 else
889 begin
890 //raise;
891 flexloaded := false;
892 end;
893 end;
894 end;
895 g_holmes_imfunctional := not flexloaded;
896 if not g_holmes_imfunctional then
897 begin
898 uiInitialize();
899 uiContext.font := 'win14';
900 end;
901 if assigned(oglInitCB) then oglInitCB;
902 end;
904 procedure FreeHolmes;
905 begin
906 if assigned(oglDeinitCB) then
907 oglDeinitCB
908 end;
909 {$ENDIF}
911 procedure ScreenResize (w, h: Integer);
912 begin
913 r_Render_Resize(w, h);
914 {$IFDEF ENABLE_HOLMES}
915 fuiScrWdt := w;
916 fuiScrHgt := h;
917 {$ENDIF}
918 g_Game_SetupScreenSize;
919 {$IFNDEF ANDROID}
920 (* This will fix menu reset on keyboard showing *)
921 g_Menu_Reset;
922 {$ENDIF}
923 //g_Game_ClearLoading;
924 {$IFDEF ENABLE_HOLMES}
925 if assigned(oglInitCB) then oglInitCB;
926 {$ENDIF}
927 end;
929 procedure Startup;
930 begin
931 Randomize;
932 InitPath;
933 InitLog;
934 InitPrep;
935 e_Input_Initialize;
936 e_InitSoundSystem(NoSound);
937 sys_Init;
938 sys_CharPress := @CharPress; (* install hook *)
939 sys_ScreenResize := @ScreenResize; (* install hook *)
940 g_Options_SetDefault;
941 g_Options_SetDefaultVideo;
942 g_Console_Initialize;
943 // TODO move load configs here
944 g_Language_Set(gLanguage);
945 r_Render_Initialize;
946 g_Touch_Init;
947 DebugOptions;
948 g_Net_InitLowLevel;
949 // TODO init serverlist
950 {$IFDEF ENABLE_HOLMES}
951 InitHolmes;
952 {$ENDIF}
953 g_Game_Init;
954 {$IFNDEF HEADLESS}
955 g_Menu_Init;
956 g_GUI_Init;
957 {$ENDIF}
958 g_Game_Process_Params;
959 // TODO reload GAME textures
960 g_Console_Init; // welcome message
961 {$IFNDEF HEADLESS}
962 if (not gGameOn) and gAskLanguage then
963 g_Menu_AskLanguage;
964 {$ENDIF}
965 Time_Old := GetTickCount64();
966 while not ProcessMessage() do begin end;
967 g_Console_WriteGameConfig;
968 {$IFNDEF HEADLESS}
969 g_GUI_Destroy;
970 g_Menu_Free;
971 {$ENDIF}
972 {$IFDEF ENABLE_HOLMES}
973 FreeHolmes;
974 {$ENDIF}
975 g_Net_Slist_ShutdownAll;
976 g_Net_DeinitLowLevel;
977 (* g_Touch_Finalize; *)
978 r_Render_Finalize;
979 sys_Final;
980 g_Console_Finalize;
981 e_ReleaseSoundSystem;
982 e_Input_Finalize;
983 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
984 end;
986 procedure EntryPoint;
987 begin
988 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
989 EntryParams;
990 e_Log_Initialize;
991 {$IFDEF HEADLESS}
992 conbufDumpToStdOut := true;
993 {$ENDIF}
994 if noct then
995 Startup
996 else
997 try
998 Startup
999 except on e: Exception do
1000 e_WriteStackTrace(e.message)
1001 else
1002 e_WriteStackTrace('FATAL ERROR')
1003 end;
1004 e_Log_Finalize
1005 end;
1007 {$IFDEF ANDROID}
1008 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1009 begin
1010 // TODO pass argc+argv to rtl
1011 EntryPoint;
1012 result := 0
1013 end;
1015 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1016 begin
1017 result:= JNI_VERSION_1_6;
1018 end;
1020 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1021 begin
1022 end;
1024 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1025 exports SDL_main name 'SDL_main';
1026 exports JNI_OnLoad name 'JNI_OnLoad';
1027 exports JNI_OnUnload name 'JNI_Unload';
1028 {$ELSE}
1029 begin
1030 EntryPoint
1031 {$ENDIF}
1033 end.