DEADSOFTWARE

render: draw menu via 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,
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 {$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_RENDER}
165 {$I ../shared/vampimg.inc}
166 r_animations in 'opengl/r_animations.pas',
167 r_console in 'opengl/r_console.pas',
168 r_game in 'opengl/r_game.pas',
169 {$IFDEF ENABLE_GFX}
170 r_gfx in 'opengl/r_gfx.pas',
171 {$ENDIF}
172 r_graphics in 'opengl/r_graphics.pas',
173 r_items in 'opengl/r_items.pas',
174 r_map in 'opengl/r_map.pas',
175 r_monsters in 'opengl/r_monsters.pas',
176 r_netmaster in 'opengl/r_netmaster.pas',
177 r_player in 'opengl/r_player.pas',
178 r_playermodel in 'opengl/r_playermodel.pas',
179 r_render in 'opengl/r_render.pas',
180 r_texture in 'opengl/r_texture.pas',
181 r_textures in 'opengl/r_textures.pas',
182 r_weapons in 'opengl/r_weapons.pas',
183 r_window in 'opengl/r_window.pas',
184 {$IFDEF ENABLE_TOUCH}
185 r_touch in 'opengl/r_touch.pas',
186 {$ENDIF}
187 {$IFDEF ENABLE_MENU}
188 g_gui in 'g_gui.pas',
189 g_menu in 'g_menu.pas',
190 r_gui in 'opengl/r_gui.pas',
191 {$ENDIF}
192 {$ENDIF}
194 {$IFDEF USE_FMOD}
195 fmod in '../lib/FMOD/fmod.pas',
196 fmoderrors in '../lib/FMOD/fmoderrors.pas',
197 fmodpresets in '../lib/FMOD/fmodpresets.pas',
198 fmodtypes in '../lib/FMOD/fmodtypes.pas',
199 {$ENDIF}
200 xprofiler in '../shared/xprofiler.pas',
201 binheap in '../shared/binheap.pas',
202 hashtable in '../shared/hashtable.pas',
203 fhashdb in '../shared/fhashdb.pas',
204 idpool in '../shared/idpool.pas',
205 xparser in '../shared/xparser.pas',
206 xdynrec in '../shared/xdynrec.pas',
207 exoma in '../shared/exoma.pas',
208 envvars in '../shared/envvars.pas',
209 g_panel in 'g_panel.pas',
210 g_language in 'g_language.pas',
212 {$IFDEF ENABLE_HOLMES}
213 g_holmes in 'g_holmes.pas',
215 sdlcarcass in '../flexui/sdlcarcass.pas',
216 //sdlstandalone in '../flexui/sdlstandalone.pas',
218 fui_wadread in '../flexui/fui_wadread.pas',
219 fui_common in '../flexui/fui_common.pas',
220 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
221 fui_events in '../flexui/fui_events.pas',
222 fui_style in '../flexui/fui_style.pas',
223 fui_flexlay in '../flexui/fui_flexlay.pas',
224 fui_ctls in '../flexui/fui_ctls.pas',
225 {$ENDIF}
227 SysUtils, Classes;
229 {$IFDEF WINDOWS}
230 {$R *.res}
231 {$ENDIF}
233 const
234 autoexecScript = 'autoexec.cfg';
236 var
237 noct: Boolean = False;
238 binPath: AnsiString = '';
239 forceBinDir: Boolean = False;
241 Time_Old: Int64;
242 NoSound: Boolean;
244 procedure Update ();
245 begin
246 // remember old mobj positions, prepare for update
247 g_Game_PreUpdate();
248 // server: receive client commands for new frame
249 // client: receive game state changes from server
250 if (NetMode = NET_SERVER) then g_Net_Host_Update()
251 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
252 // think
253 {$IFDEF ENABLE_RENDER}
254 r_Render_Update;
255 {$ENDIF}
256 g_Game_Update();
257 // server: send any accumulated outgoing data to clients
258 if NetMode = NET_SERVER then g_Net_Flush();
259 end;
261 function ProcessMessage (): Boolean;
262 var
263 i, t: Integer;
264 flag: Boolean;
265 Time, Time_Delta: Int64;
266 Frame: Int64;
267 begin
268 {$IFDEF ENABLE_SYSTEM}
269 Result := sys_HandleInput();
270 {$ELSE}
271 Result := False;
272 {$ENDIF}
274 Time := GetTickCount64();
275 Time_Delta := Time-Time_Old;
277 flag := false;
279 if wNeedTimeReset then
280 begin
281 Frame := 0;
282 Time_Delta := 28;
283 wNeedTimeReset := false;
284 end;
286 g_Map_ProfilersBegin();
287 g_Mons_ProfilersBegin();
289 t := Time_Delta div 28;
290 if (t > 0) then
291 begin
292 flag := true;
293 for i := 1 to t do
294 Update();
295 end;
297 g_Map_ProfilersEnd();
298 g_Mons_ProfilersEnd();
300 if (gExit = EXIT_QUIT) then
301 begin
302 result := true;
303 exit;
304 end;
306 // Время предыдущего обновления
307 if flag then
308 Time_Old := Time - (Time_Delta mod 28);
310 // don't wait if VSync is on, GL already probably waits enough
311 if gLerpActors then
312 flag := (Time - Frame >= gFrameTime) or gVSync;
314 if flag then
315 begin
316 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
317 gLerpFactor := 1.0
318 else
319 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
320 {$IFDEF ENABLE_RENDER}
321 r_Render_Draw;
322 {$ENDIF}
323 {$IFDEF ENABLE_SYSTEM}
324 sys_Repaint;
325 {$ENDIF}
326 Frame := Time
327 end
328 else
329 Sleep(1);
331 e_SoundUpdate();
332 end;
334 procedure DebugOptions;
335 var
336 idx: Integer;
337 arg: AnsiString;
338 mdfo: TStream;
339 {$IFDEF ENABLE_HOLMES}
340 itmp: Integer;
341 valres: Word;
342 {$ENDIF}
343 begin
344 idx := 1;
345 while (idx <= ParamCount) do
346 begin
347 arg := ParamStr(idx);
348 Inc(idx);
349 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
350 if arg = '--jah' then g_profile_history_size := 100;
351 if arg = '--no-los' then gmon_dbg_los_enabled := false;
353 if arg = '--profile-render' then g_profile_frame_draw := true;
354 if arg = '--profile-coldet' then g_profile_collision := true;
355 if arg = '--profile-los' then g_profile_los := true;
357 {$IFDEF ENABLE_GFX}
358 if arg = '--no-particles' then gpart_dbg_enabled := false;
359 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
360 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
361 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
362 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
363 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
364 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
365 {$ENDIF}
367 if arg = '--debug-input' then g_dbg_input := True;
369 {.$IF DEFINED(D2F_DEBUG)}
370 if arg = '--aimline' then g_dbg_aimline_on := true;
371 {.$ENDIF}
373 {$IFDEF ENABLE_HOLMES}
374 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
376 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
377 begin
378 if (idx <= ParamCount) then
379 begin
380 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
381 Inc(idx);
382 end;
383 end;
385 if (arg = '--holmes-font') or (arg = '-holmes-font') then
386 begin
387 if (idx <= ParamCount) then
388 begin
389 itmp := 0;
390 val(ParamStr(idx), itmp, valres);
391 if (valres = 0) and (not g_holmes_imfunctional) then
392 begin
393 case itmp of
394 8: uiContext.font := 'win8';
395 14: uiContext.font := 'win14';
396 16: uiContext.font := 'win16';
397 end;
398 end;
399 Inc(idx);
400 end;
401 end;
402 {$ENDIF}
404 if (arg = '--game-scale') or (arg = '-game-scale') then
405 begin
406 if (idx <= ParamCount) then
407 begin
408 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
409 Inc(idx);
410 end;
411 end;
413 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
414 begin
415 mdfo := createDiskFile('mapdef.txt');
416 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
417 mdfo.Free();
418 Halt(0);
419 end;
421 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
422 begin
423 if (idx <= ParamCount) then
424 begin
425 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
426 Inc(idx);
427 end;
428 end;
429 end;
430 end;
432 function GetBinaryPath (): AnsiString;
433 {$IFDEF LINUX}
434 var sl: AnsiString;
435 {$ENDIF}
436 begin
437 result := ExtractFilePath(ParamStr(0));
438 {$IFDEF LINUX}
439 // it may be a symlink; do some guesswork here
440 sl := fpReadLink(ExtractFileName(ParamStr(0)));
441 if (sl = ParamStr(0)) then
442 begin
443 // use current directory, as we don't have anything better
444 //result := '.';
445 GetDir(0, result);
446 end;
447 {$ENDIF}
448 result := fixSlashes(result);
449 if (length(result) > 0) and (result[length(result)] <> '/') then
450 result := result + '/';
451 end;
453 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
454 var dir: AnsiString;
455 begin
456 e_LogWriteln(msg + ':');
457 for dir in dirs do
458 e_LogWriteln(' ' + dir);
459 end;
461 {$IFDEF DARWIN}
462 function NSStringToAnsiString (s: NSString): AnsiString;
463 var i: Integer;
464 begin
465 result := '';
466 for i := 0 to s.length - 1 do
467 result := result + AnsiChar(s.characterAtIndex(i));
468 end;
470 function GetBundlePath (): AnsiString;
471 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
472 begin
473 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
474 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
475 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
476 CFRelease(pathRef);
477 CFRelease(pathCFStr);
478 Result := pathStr;
479 end;
480 {$ENDIF}
482 procedure InitPath;
483 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
485 procedure AddDir (var dirs: SSArray; append: AnsiString);
486 begin
487 SetLength(dirs, Length(dirs) + 1);
488 dirs[High(dirs)] := ExpandFileName(append)
489 end;
491 function IsSep (ch: Char): Boolean;
492 begin
493 {$IFDEF WINDOWS}
494 result := (ch = '/') or (ch = '\');
495 {$ELSE}
496 result := (ch = '/');
497 {$ENDIF}
498 end;
500 function OptimizePath (dir: AnsiString): AnsiString;
501 var i, len: Integer; s: AnsiString;
502 begin
503 i := 1; len := Length(dir); s := '';
504 while i <= len do
505 begin
506 if IsSep(dir[i]) then
507 begin
508 s := s + DirectorySeparator;
509 Inc(i);
510 while (i <= len) and IsSep(dir[i]) do Inc(i);
511 if (i <= len) and (dir[i] = '.') then
512 begin
513 if (i = len) or IsSep(dir[i + 1]) then
514 begin
515 Inc(i)
516 end
517 else if (i + 1 <= len) and (dir[i + 1] = '.') then
518 begin
519 if (i + 1 = len) or IsSep(dir[i + 2]) then
520 begin
521 s := e_UpperDir(s);
522 Inc(i, 2)
523 end
524 end
525 end
526 end
527 else
528 begin
529 s := s + dir[i];
530 Inc(i)
531 end
532 end;
533 result := s
534 end;
536 procedure OptimizeDirs (var dirs: SSArray);
537 var i, j, k: Integer;
538 begin
539 for i := 0 to High(dirs) do
540 dirs[i] := OptimizePath(dirs[i]);
541 // deduplicate
542 i := High(dirs);
543 while i >= 0 do
544 begin
545 j := 0;
546 while j < i do
547 begin
548 if dirs[j] = dirs[i] then
549 begin
550 for k := j + 1 to High(dirs) do
551 dirs[k - 1] := dirs[k];
552 Dec(i);
553 SetLength(dirs, High(dirs))
554 end
555 else
556 begin
557 Inc(j)
558 end
559 end;
560 Dec(i)
561 end
562 end;
564 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
565 var s: AnsiString;
566 begin
567 if Length(dirs) = 0 then
568 for s in base do
569 AddDir(dirs, e_CatPath(s, append));
570 OptimizeDirs(dirs)
571 end;
573 function GetDefaultRODirs (): SSArray;
574 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
575 var home: AnsiString;
576 {$ENDIF}
577 {$IFDEF WINDOWS}
578 var appdata: AnsiString;
579 {$ENDIF}
580 {$IFDEF DARWIN}
581 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
582 {$ENDIF}
583 begin
584 result := nil;
585 {$IFDEF DARWIN}
586 bundle := GetBundlePath();
587 if ExtractFileExt(bundle) <> '.app' then
588 AddDir(result, binpath);
589 {$ELSE}
590 AddDir(result, binPath);
591 {$ENDIF}
592 if forceBinDir = false then
593 begin
594 {$IFDEF USE_SDL2}
595 AddDir(result, SDL_GetBasePath());
596 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
597 {$ENDIF}
598 {$IFDEF WINDOWS}
599 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
600 if appdata <> '' then
601 AddDir(result, appdata);
602 {$ENDIF}
603 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
604 AddDir(result, '/usr/share/doom2df');
605 AddDir(result, '/usr/local/share/doom2df');
606 home := GetEnvironmentVariable('HOME');
607 if home <> '' then
608 AddDir(result, e_CatPath(home, '.doom2df'));
609 {$ENDIF}
610 {$IFDEF DARWIN}
611 bundle := GetBundlePath();
612 if bundle <> '' then
613 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
614 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
615 for i := 0 to dirArr.count - 1 do
616 begin
617 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
618 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
619 end;
620 {$ENDIF}
621 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
622 AddDir(result, SDL_AndroidGetInternalStoragePath());
623 if SDL_AndroidGetExternalStorageState() <> 0 then
624 AddDir(result, SDL_AndroidGetExternalStoragePath());
625 {$ENDIF}
626 end
627 end;
629 function GetDefaultRWDirs (): SSArray;
630 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
631 var home: AnsiString;
632 {$ENDIF}
633 {$IFDEF WINDOWS}
634 var appdata: AnsiString;
635 {$ENDIF}
636 {$IFDEF DARWIN}
637 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
638 {$ENDIF}
639 begin
640 result := nil;
641 {$IFDEF DARWIN}
642 bundle := GetBundlePath();
643 if ExtractFileExt(bundle) <> '.app' then
644 AddDir(result, binPath);
645 {$ELSE}
646 AddDir(result, binPath);
647 {$ENDIF}
648 if forceBinDir = false then
649 begin
650 {$IFDEF USE_SDL2}
651 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
652 {$ENDIF}
653 {$IFDEF WINDOWS}
654 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
655 if appdata <> '' then
656 AddDir(result, appdata);
657 {$ENDIF}
658 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
659 home := GetEnvironmentVariable('HOME');
660 if home <> '' then
661 AddDir(result, e_CatPath(home, '.doom2df'));
662 {$ENDIF}
663 {$IFDEF DARWIN}
664 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
665 for i := 0 to dirArr.count - 1 do
666 begin
667 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
668 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
669 end;
670 {$ENDIF}
671 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
672 if SDL_AndroidGetExternalStorageState() <> 0 then
673 AddDir(result, SDL_AndroidGetExternalStoragePath());
674 {$ENDIF}
675 end
676 end;
678 begin
679 forceBinDir := false;
680 binPath := GetBinaryPath();
682 i := 1;
683 while i < ParamCount do
684 begin
685 case ParamStr(i) of
686 '--like-windoze': forceBinDir := true;
687 '--rw-dir':
688 begin
689 Inc(i);
690 rwdir := ParamStr(i);
691 (* RW *)
692 AddDir(LogDirs, e_CatPath(rwdir, ''));
693 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
694 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
695 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
696 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
697 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
698 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
699 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
700 (* RO *)
701 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
702 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
703 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
704 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
705 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
706 end;
707 '--ro-dir':
708 begin
709 Inc(i);
710 rodir := ParamStr(i);
711 (* RO *)
712 AddDir(DataDirs, e_CatPath(rodir, 'data'));
713 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
714 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
715 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
716 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
717 end;
718 '--game-wad':
719 begin
720 Inc(i);
721 GameWADName := ParamStr(i);
722 end;
723 '--config':
724 begin
725 Inc(i);
726 gConfigScript := ParamStr(i);
727 end;
728 end;
729 Inc(i)
730 end;
732 // prefer bin dir if it writable and contains game.wad
733 if forceBinDir = false then
734 begin
735 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
736 if e_CanCreateFilesAt(binPath) then
737 forceBinDir := true
738 end;
740 (* RO *)
741 rodirs := GetDefaultRODirs();
742 AddDef(DataDirs, rodirs, 'data');
743 AddDef(ModelDirs, rodirs, 'data/models');
744 AddDef(MegawadDirs, rodirs, 'maps/megawads');
745 AddDef(MapDirs, rodirs, 'maps');
746 AddDef(WadDirs, rodirs, 'wads');
748 (* RW *)
749 rwdirs := GetDefaultRWDirs();
750 AddDef(LogDirs, rwdirs, '');
751 AddDef(SaveDirs, rwdirs, 'data');
752 AddDef(CacheDirs, rwdirs, 'data/cache');
753 AddDef(ConfigDirs, rwdirs, '');
754 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
755 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
756 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
757 AddDef(StatsDirs, rwdirs, 'stats');
759 for i := 0 to High(MapDirs) do
760 AddDir(AllMapDirs, MapDirs[i]);
761 for i := 0 to High(MegawadDirs) do
762 AddDir(AllMapDirs, MegawadDirs[i]);
763 OptimizeDirs(AllMapDirs);
765 // HACK: ensure the screenshots folder also has a stats subfolder in it
766 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
767 if rwdir <> '' then CreateDir(rwdir + '/stats');
768 end;
770 procedure EntryParams;
771 var i: Integer;
772 begin
773 i := 1;
774 while i <= ParamCount do
775 begin
776 case ParamStr(i) of
777 '--gdb': noct := true;
778 '--log', '--con-stdout': conbufDumpToStdOut := true;
779 '--safe-log': e_SetSafeSlowLog(true);
780 '--log-file':
781 if i + 1 <= ParamCount then
782 begin
783 Inc(i);
784 LogFileName := ParamStr(i)
785 end;
786 '--no-fbo': glRenderToFBO := false;
787 end;
788 Inc(i)
789 end
790 end;
792 procedure InitLog;
793 var rwdir: AnsiString;
794 begin
795 if LogFileName = '' then
796 begin
797 rwdir := e_GetWriteableDir(LogDirs, false);
798 if rwdir <> '' then
799 begin
800 {$IFDEF HEADLESS}
801 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
802 {$ELSE}
803 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
804 {$ENDIF}
805 end
806 end;
807 if LogFileName <> '' then
808 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
809 e_InitWritelnDriver
810 end;
812 procedure InitPrep;
813 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
814 var timiditycfg: AnsiString;
815 {$ENDIF}
816 begin
817 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
818 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
819 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
820 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
822 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
823 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
825 PrintDirs('DataDirs', DataDirs);
826 PrintDirs('ModelDirs', ModelDirs);
827 PrintDirs('MegawadDirs', MegawadDirs);
828 PrintDirs('MapDirs', MapDirs);
829 PrintDirs('WadDirs', WadDirs);
831 PrintDirs('LogDirs', LogDirs);
832 PrintDirs('SaveDirs', SaveDirs);
833 PrintDirs('CacheDirs', CacheDirs);
834 PrintDirs('ConfigDirs', ConfigDirs);
835 PrintDirs('ScreenshotDirs', ScreenshotDirs);
836 PrintDirs('StatsDirs', StatsDirs);
837 PrintDirs('MapDownloadDirs', MapDownloadDirs);
838 PrintDirs('WadDownloadDirs', WadDownloadDirs);
840 {$IFDEF HEADLESS}
841 {$IFDEF USE_SDLMIXER}
842 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
843 {$ELSE}
844 NoSound := True; // FMOD backend will sort it out
845 {$ENDIF}
846 {$ELSE}
847 NoSound := False;
848 {$ENDIF}
850 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
851 timiditycfg := 'timidity.cfg';
852 if e_FindResource(ConfigDirs, timiditycfg) = true then
853 begin
854 timiditycfg := ExpandFileName(timiditycfg);
855 SetEnvVar('TIMIDITY_CFG', timiditycfg);
856 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
857 end;
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 ScreenResize (w, h: Integer);
940 begin
941 {$IFDEF ENABLE_RENDER}
942 r_Render_Resize(w, h);
943 {$IFDEF ENABLE_HOLMES}
944 fuiScrWdt := w;
945 fuiScrHgt := h;
946 {$ENDIF}
947 {$IFNDEF ANDROID}
948 (* This will fix menu reset on keyboard showing *)
949 {$IFDEF ENABLE_MENU}
950 g_Menu_Reset;
951 {$ENDIF}
952 {$ENDIF}
953 //g_Game_ClearLoading;
954 {$IFDEF ENABLE_HOLMES}
955 if assigned(oglInitCB) then oglInitCB;
956 {$ENDIF}
957 {$ENDIF}
958 end;
960 procedure Startup;
961 begin
962 Randomize;
963 InitPath;
964 InitLog;
965 InitPrep;
966 e_Input_Initialize;
967 e_InitSoundSystem(NoSound);
968 {$IFDEF ENABLE_SYSTEM}
969 sys_Init;
970 sys_CharPress := @CharPress; (* install hook *)
971 sys_ScreenResize := @ScreenResize; (* install hook *)
972 {$ENDIF}
973 g_Options_SetDefault;
974 g_Options_SetDefaultVideo;
975 g_Console_Initialize;
976 // TODO move load configs here
977 g_Language_Set(gLanguage);
978 {$IFDEF ENABLE_RENDER}
979 r_Render_Initialize;
980 {$ENDIF}
981 DebugOptions;
982 g_Net_InitLowLevel;
983 // TODO init serverlist
984 {$IFDEF ENABLE_HOLMES}
985 InitHolmes;
986 {$ENDIF}
987 {$IFDEF ENABLE_RENDER}
988 g_PlayerModel_LoadAll;
989 r_Render_Load;
990 {$ELSE}
991 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
992 {$ENDIF}
993 g_Game_Init;
994 {$IFDEF ENABLE_MENU}
995 g_Menu_Init;
996 {$ENDIF}
997 g_Game_Process_Params;
998 // TODO reload GAME textures
999 g_Console_Init; // welcome message
1000 {$IFDEF ENABLE_MENU}
1001 if (not gGameOn) and gAskLanguage then
1002 g_Menu_AskLanguage;
1003 {$ENDIF}
1004 Time_Old := GetTickCount64();
1005 while not ProcessMessage() do begin end;
1006 g_Console_WriteGameConfig;
1007 {$IFDEF ENABLE_MENU}
1008 g_GUI_Destroy;
1009 g_Menu_Free;
1010 {$ENDIF}
1011 {$IFDEF ENABLE_RENDER}
1012 r_Render_Free;
1013 {$ENDIF}
1014 {$IFDEF ENABLE_HOLMES}
1015 FreeHolmes;
1016 {$ENDIF}
1017 g_Net_Slist_ShutdownAll;
1018 g_Net_DeinitLowLevel;
1019 (* g_Touch_Finalize; *)
1020 {$IFDEF ENABLE_RENDER}
1021 r_Render_Finalize;
1022 {$ENDIF}
1023 {$IFDEF ENABLE_SYSTEM}
1024 sys_Final;
1025 {$ENDIF}
1026 g_Console_Finalize;
1027 e_ReleaseSoundSystem;
1028 e_Input_Finalize;
1029 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1030 end;
1032 procedure EntryPoint;
1033 begin
1034 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1035 EntryParams;
1036 e_Log_Initialize;
1037 {$IFDEF HEADLESS}
1038 conbufDumpToStdOut := true;
1039 {$ENDIF}
1040 if noct then
1041 Startup
1042 else
1043 try
1044 Startup
1045 except on e: Exception do
1046 e_WriteStackTrace(e.message)
1047 else
1048 e_WriteStackTrace('FATAL ERROR')
1049 end;
1050 e_Log_Finalize
1051 end;
1053 {$IFDEF ANDROID}
1054 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1055 begin
1056 // TODO pass argc+argv to rtl
1057 EntryPoint;
1058 result := 0
1059 end;
1061 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1062 begin
1063 result:= JNI_VERSION_1_6;
1064 end;
1066 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1067 begin
1068 end;
1070 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1071 exports SDL_main name 'SDL_main';
1072 exports JNI_OnLoad name 'JNI_OnLoad';
1073 exports JNI_OnUnload name 'JNI_Unload';
1074 {$ELSE}
1075 begin
1076 EntryPoint
1077 {$ENDIF}
1079 end.