DEADSOFTWARE

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