DEADSOFTWARE

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