DEADSOFTWARE

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