DEADSOFTWARE

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