DEADSOFTWARE

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