DEADSOFTWARE

gl: draw gui controls
[d2df-sdl.git] / src / game / Doom2DF.lpr
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$IFDEF ANDROID}library{$ELSE}program{$ENDIF} Doom2DF;
18 {$IFNDEF HEADLESS}
19 {$IFDEF WINDOWS}
20 {$APPTYPE GUI}
21 {$ENDIF}
22 {$ENDIF}
23 {$HINTS OFF}
25 uses
26 {$IFDEF ANDROID}
27 ctypes, jni,
28 {$ENDIF}
29 {$IFDEF UNIX}
30 cthreads, BaseUnix,
31 {$ENDIF}
32 {$IFDEF DARWIN}
33 MacOSAll, CocoaAll,
34 {$ENDIF}
35 mempool in '../shared/mempool.pas',
36 conbuf in '../shared/conbuf.pas',
37 geom in '../shared/geom.pas',
38 math,
40 {$IFDEF USE_MINIUPNPC}
41 miniupnpc in '../lib/miniupnpc/miniupnpc.pas',
42 {$ENDIF}
44 {$IFDEF USE_SDL}
45 SDL in '../lib/sdl/sdl.pas',
46 {$IFDEF USE_SDLMIXER}
47 SDL_mixer in '../lib/sdl/sdl_mixer.pas',
48 {$ENDIF}
49 {$ENDIF}
50 {$IFDEF USE_SDL2}
51 SDL2 in '../lib/sdl2/sdl2.pas',
52 {$IFDEF USE_SDLMIXER}
53 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
54 {$ENDIF}
55 {$ENDIF}
56 {$IFDEF USE_SYSSTUB}
57 {$IFDEF USE_SDLMIXER}
58 SDL2 in '../lib/sdl2/sdl2.pas',
59 SDL2_mixer in '../lib/sdl2/SDL2_mixer.pas',
60 {$ENDIF}
61 {$ENDIF}
63 {$IFDEF USE_OPENAL}
64 AL in '../lib/openal/al.pas',
65 e_soundfile in '../engine/e_soundfile.pas',
66 {$IF DEFINED(USE_SDL) OR DEFINED(USE_SDL2)}
67 e_soundfile_wav in '../engine/e_soundfile_wav.pas',
68 {$ENDIF}
69 {$IFDEF USE_VORBIS}
70 vorbis in '../lib/vorbis/vorbis.pas',
71 e_soundfile_vorbis in '../engine/e_soundfile_vorbis.pas',
72 {$ENDIF}
73 {$IFDEF USE_FLUIDSYNTH}
74 fluidsynth in '../lib/fluidsynth/fluidsynth.pas',
75 e_soundfile_fluid in '../engine/e_soundfile_fluid.pas',
76 {$ENDIF}
77 {$IFDEF USE_MODPLUG}
78 modplug in '../lib/modplug/modplug.pas',
79 e_soundfile_modplug in '../engine/e_soundfile_modplug.pas',
80 {$ENDIF}
81 {$IFDEF USE_XMP}
82 xmp in '../lib/xmp/xmp.pas',
83 e_soundfile_xmp in '../engine/e_soundfile_xmp.pas',
84 {$ENDIF}
85 {$IFDEF USE_GME}
86 gme in '../lib/gme/gme.pas',
87 e_soundfile_gme in '../engine/e_soundfile_gme.pas',
88 {$ENDIF}
89 {$IFDEF USE_MPG123}
90 mpg123 in '../lib/mpg123/mpg123.pas',
91 e_soundfile_mp3 in '../engine/e_soundfile_mp3.pas',
92 {$ENDIF}
93 {$IFDEF USE_OPUS}
94 opus in '../lib/opus/opus.pas',
95 e_soundfile_opus in '../engine/e_soundfile_opus.pas',
96 {$ENDIF}
97 {$IF DEFINED(USE_VORBIS) OR DEFINED(USE_OPUS)}
98 ogg in '../lib/vorbis/ogg.pas', // this has to come last because link order
99 {$ENDIF}
100 {$ENDIF}
102 ENet in '../lib/enet/enet.pp',
103 e_input in '../engine/e_input.pas',
104 e_log in '../engine/e_log.pas',
105 e_sound in '../engine/e_sound.pas',
106 e_msg in '../engine/e_msg.pas',
107 e_res in '../engine/e_res.pas',
108 utils in '../shared/utils.pas',
109 xstreams in '../shared/xstreams.pas',
110 sfs in '../sfs/sfs.pas',
111 sfsPlainFS in '../sfs/sfsPlainFS.pas',
112 sfsZipFS in '../sfs/sfsZipFS.pas',
113 wadreader in '../shared/wadreader.pas',
114 MAPDEF in '../shared/MAPDEF.pas',
115 CONFIG in '../shared/CONFIG.pas',
116 g_base in 'g_base.pas',
117 g_basic in 'g_basic.pas',
118 g_console in 'g_console.pas',
119 g_net in 'g_net.pas',
120 g_netmsg in 'g_netmsg.pas',
121 g_nethandler in 'g_nethandler.pas',
122 g_netmaster in 'g_netmaster.pas',
123 g_res_downloader in 'g_res_downloader.pas',
124 g_grid in 'g_grid.pas',
125 g_game in 'g_game.pas',
126 {$IFDEF ENABLE_GFX}
127 g_gfx in 'g_gfx.pas',
128 {$ENDIF}
129 {$IFDEF ENABLE_GIBS}
130 g_gibs in 'g_gibs.pas',
131 {$ENDIF}
132 {$IFDEF ENABLE_SHELLS}
133 g_shells in 'g_shells.pas',
134 {$ENDIF}
135 {$IFDEF ENABLE_CORPSES}
136 g_corpses in 'g_corpses.pas',
137 {$ENDIF}
138 g_items in 'g_items.pas',
139 g_map in 'g_map.pas',
140 g_monsters in 'g_monsters.pas',
141 g_options in 'g_options.pas',
142 g_phys in 'g_phys.pas',
143 g_player in 'g_player.pas',
144 g_playermodel in 'g_playermodel.pas',
145 g_saveload in 'g_saveload.pas',
146 g_sound in 'g_sound.pas',
147 g_textures in 'g_textures.pas',
148 g_triggers in 'g_triggers.pas',
149 g_weapons in 'g_weapons.pas',
150 g_window in 'g_window.pas',
152 {$IFDEF ENABLE_SYSTEM}
153 {$IFDEF USE_SYSSTUB}
154 g_system in 'stub/g_system.pas',
155 {$ENDIF}
156 {$IFDEF USE_SDL}
157 g_system in 'sdl/g_system.pas',
158 {$ENDIF}
159 {$IFDEF USE_SDL2}
160 g_system in 'sdl2/g_system.pas',
161 {$ENDIF}
162 {$ENDIF}
164 {$IFDEF ENABLE_MENU}
165 g_gui in 'g_gui.pas',
166 g_menu in 'g_menu.pas',
167 {$ENDIF}
169 {$IFDEF ENABLE_RENDER}
170 {$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_gui in 'renders/opengl/r_gui.pas',
208 {$ELSE}
209 {$FATAL render driver not selected}
210 {$ENDIF}
211 {$ENDIF}
213 {$IFDEF USE_FMOD}
214 fmod in '../lib/FMOD/fmod.pas',
215 fmoderrors in '../lib/FMOD/fmoderrors.pas',
216 fmodpresets in '../lib/FMOD/fmodpresets.pas',
217 fmodtypes in '../lib/FMOD/fmodtypes.pas',
218 {$ENDIF}
219 xprofiler in '../shared/xprofiler.pas',
220 binheap in '../shared/binheap.pas',
221 hashtable in '../shared/hashtable.pas',
222 fhashdb in '../shared/fhashdb.pas',
223 idpool in '../shared/idpool.pas',
224 xparser in '../shared/xparser.pas',
225 xdynrec in '../shared/xdynrec.pas',
226 exoma in '../shared/exoma.pas',
227 envvars in '../shared/envvars.pas',
228 g_panel in 'g_panel.pas',
229 g_language in 'g_language.pas',
231 {$IFDEF ENABLE_HOLMES}
232 g_holmes in 'g_holmes.pas',
234 sdlcarcass in '../flexui/sdlcarcass.pas',
235 //sdlstandalone in '../flexui/sdlstandalone.pas',
237 fui_wadread in '../flexui/fui_wadread.pas',
238 fui_common in '../flexui/fui_common.pas',
239 fui_gfx_gl in '../flexui/fui_gfx_gl.pas',
240 fui_events in '../flexui/fui_events.pas',
241 fui_style in '../flexui/fui_style.pas',
242 fui_flexlay in '../flexui/fui_flexlay.pas',
243 fui_ctls in '../flexui/fui_ctls.pas',
244 {$ENDIF}
246 SysUtils, Classes;
248 {$IFDEF WINDOWS}
249 {$R *.res}
250 {$ENDIF}
252 const
253 autoexecScript = 'autoexec.cfg';
255 var
256 noct: Boolean = False;
257 binPath: AnsiString = '';
258 forceBinDir: Boolean = False;
259 {$IFDEF USE_SDLMIXER}
260 UseNativeMusic: Boolean;
261 {$ENDIF}
263 Time_Old: Int64;
264 NoSound: Boolean;
266 procedure Update ();
267 begin
268 // remember old mobj positions, prepare for update
269 g_Game_PreUpdate();
270 // server: receive client commands for new frame
271 // client: receive game state changes from server
272 if (NetMode = NET_SERVER) then g_Net_Host_Update()
273 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
274 // think
275 {$IFDEF ENABLE_RENDER}
276 r_Render_Update;
277 {$ENDIF}
278 g_Game_Update();
279 // server: send any accumulated outgoing data to clients
280 if NetMode = NET_SERVER then g_Net_Flush();
281 end;
283 function ProcessMessage (): Boolean;
284 var
285 i, t: Integer;
286 flag: Boolean;
287 Time, Time_Delta: Int64;
288 Frame: Int64;
289 begin
290 {$IFDEF ENABLE_SYSTEM}
291 Result := sys_HandleInput();
292 {$ELSE}
293 Result := False;
294 {$ENDIF}
296 Time := GetTickCount64();
297 Time_Delta := Time-Time_Old;
299 flag := false;
301 if wNeedTimeReset then
302 begin
303 Frame := 0;
304 Time_Delta := 28;
305 wNeedTimeReset := false;
306 end;
308 g_Map_ProfilersBegin();
309 g_Mons_ProfilersBegin();
311 t := Time_Delta div 28;
312 if (t > 0) then
313 begin
314 flag := true;
315 for i := 1 to t do
316 Update();
317 end;
319 g_Map_ProfilersEnd();
320 g_Mons_ProfilersEnd();
322 if (gExit = EXIT_QUIT) then
323 begin
324 result := true;
325 exit;
326 end;
328 // Время предыдущего обновления
329 if flag then
330 Time_Old := Time - (Time_Delta mod 28);
332 // don't wait if VSync is on, GL already probably waits enough
333 if gLerpActors then
334 flag := (Time - Frame >= gFrameTime) or gVSync;
336 if flag then
337 begin
338 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
339 gLerpFactor := 1.0
340 else
341 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
342 {$IFDEF ENABLE_RENDER}
343 r_Render_Draw;
344 {$ENDIF}
345 {$IFDEF ENABLE_SYSTEM}
346 sys_Repaint;
347 {$ENDIF}
348 Frame := Time
349 end
350 else
351 Sleep(1);
353 e_SoundUpdate();
354 end;
356 procedure DebugOptions;
357 var
358 idx: Integer;
359 arg: AnsiString;
360 mdfo: TStream;
361 {$IFDEF ENABLE_HOLMES}
362 itmp: Integer;
363 valres: Word;
364 {$ENDIF}
365 begin
366 idx := 1;
367 while (idx <= ParamCount) do
368 begin
369 arg := ParamStr(idx);
370 Inc(idx);
371 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
372 if arg = '--jah' then g_profile_history_size := 100;
373 if arg = '--no-los' then gmon_dbg_los_enabled := false;
375 if arg = '--profile-render' then g_profile_frame_draw := true;
376 if arg = '--profile-coldet' then g_profile_collision := true;
377 if arg = '--profile-los' then g_profile_los := true;
379 {$IFDEF ENABLE_GFX}
380 if arg = '--no-particles' then gpart_dbg_enabled := false;
381 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
382 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
383 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
384 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
385 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
386 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
387 {$ENDIF}
389 if arg = '--debug-input' then g_dbg_input := True;
391 {.$IF DEFINED(D2F_DEBUG)}
392 if arg = '--aimline' then g_dbg_aimline_on := true;
393 {.$ENDIF}
395 {$IFDEF ENABLE_HOLMES}
396 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
398 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
399 begin
400 if (idx <= ParamCount) then
401 begin
402 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
403 Inc(idx);
404 end;
405 end;
407 if (arg = '--holmes-font') or (arg = '-holmes-font') then
408 begin
409 if (idx <= ParamCount) then
410 begin
411 itmp := 0;
412 val(ParamStr(idx), itmp, valres);
413 if (valres = 0) and (not g_holmes_imfunctional) then
414 begin
415 case itmp of
416 8: uiContext.font := 'win8';
417 14: uiContext.font := 'win14';
418 16: uiContext.font := 'win16';
419 end;
420 end;
421 Inc(idx);
422 end;
423 end;
424 {$ENDIF}
426 if (arg = '--game-scale') or (arg = '-game-scale') then
427 begin
428 if (idx <= ParamCount) then
429 begin
430 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
431 Inc(idx);
432 end;
433 end;
435 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
436 begin
437 mdfo := createDiskFile('mapdef.txt');
438 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
439 mdfo.Free();
440 Halt(0);
441 end;
443 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
444 begin
445 if (idx <= ParamCount) then
446 begin
447 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
448 Inc(idx);
449 end;
450 end;
451 end;
452 end;
454 function GetBinaryPath (): AnsiString;
455 {$IFDEF LINUX}
456 var sl: AnsiString;
457 {$ENDIF}
458 begin
459 result := ExtractFilePath(ParamStr(0));
460 {$IFDEF LINUX}
461 // it may be a symlink; do some guesswork here
462 sl := fpReadLink(ExtractFileName(ParamStr(0)));
463 if (sl = ParamStr(0)) then
464 begin
465 // use current directory, as we don't have anything better
466 //result := '.';
467 GetDir(0, result);
468 end;
469 {$ENDIF}
470 result := fixSlashes(result);
471 if (length(result) > 0) and (result[length(result)] <> '/') then
472 result := result + '/';
473 end;
475 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
476 var dir: AnsiString;
477 begin
478 e_LogWriteln(msg + ':');
479 for dir in dirs do
480 e_LogWriteln(' ' + dir);
481 end;
483 {$IFDEF DARWIN}
484 function NSStringToAnsiString (s: NSString): AnsiString;
485 var i: Integer;
486 begin
487 result := '';
488 for i := 0 to s.length - 1 do
489 result := result + AnsiChar(s.characterAtIndex(i));
490 end;
492 function GetBundlePath (): AnsiString;
493 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
494 begin
495 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
496 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
497 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
498 CFRelease(pathRef);
499 CFRelease(pathCFStr);
500 Result := pathStr;
501 end;
502 {$ENDIF}
504 procedure InitPath;
505 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
507 procedure AddDir (var dirs: SSArray; append: AnsiString);
508 begin
509 SetLength(dirs, Length(dirs) + 1);
510 dirs[High(dirs)] := ExpandFileName(append)
511 end;
513 function IsSep (ch: Char): Boolean;
514 begin
515 {$IFDEF WINDOWS}
516 result := (ch = '/') or (ch = '\');
517 {$ELSE}
518 result := (ch = '/');
519 {$ENDIF}
520 end;
522 function OptimizePath (dir: AnsiString): AnsiString;
523 var i, len: Integer; s: AnsiString;
524 begin
525 i := 1; len := Length(dir); s := '';
526 while i <= len do
527 begin
528 if IsSep(dir[i]) then
529 begin
530 s := s + DirectorySeparator;
531 Inc(i);
532 while (i <= len) and IsSep(dir[i]) do Inc(i);
533 if (i <= len) and (dir[i] = '.') then
534 begin
535 if (i = len) or IsSep(dir[i + 1]) then
536 begin
537 Inc(i)
538 end
539 else if (i + 1 <= len) and (dir[i + 1] = '.') then
540 begin
541 if (i + 1 = len) or IsSep(dir[i + 2]) then
542 begin
543 s := e_UpperDir(s);
544 Inc(i, 2)
545 end
546 end
547 end
548 end
549 else
550 begin
551 s := s + dir[i];
552 Inc(i)
553 end
554 end;
555 result := s
556 end;
558 procedure OptimizeDirs (var dirs: SSArray);
559 var i, j, k: Integer;
560 begin
561 for i := 0 to High(dirs) do
562 dirs[i] := OptimizePath(dirs[i]);
563 // deduplicate
564 i := High(dirs);
565 while i >= 0 do
566 begin
567 j := 0;
568 while j < i do
569 begin
570 if dirs[j] = dirs[i] then
571 begin
572 for k := j + 1 to High(dirs) do
573 dirs[k - 1] := dirs[k];
574 Dec(i);
575 SetLength(dirs, High(dirs))
576 end
577 else
578 begin
579 Inc(j)
580 end
581 end;
582 Dec(i)
583 end
584 end;
586 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
587 var s: AnsiString;
588 begin
589 if Length(dirs) = 0 then
590 for s in base do
591 AddDir(dirs, e_CatPath(s, append));
592 OptimizeDirs(dirs)
593 end;
595 function GetDefaultRODirs (): SSArray;
596 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
597 var home: AnsiString;
598 {$ENDIF}
599 {$IFDEF WINDOWS}
600 var appdata: AnsiString;
601 {$ENDIF}
602 {$IFDEF DARWIN}
603 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
604 {$ENDIF}
605 begin
606 result := nil;
607 {$IFDEF DARWIN}
608 bundle := GetBundlePath();
609 if ExtractFileExt(bundle) <> '.app' then
610 AddDir(result, binpath);
611 {$ELSE}
612 AddDir(result, binPath);
613 {$ENDIF}
614 if forceBinDir = false then
615 begin
616 {$IFDEF USE_SDL2}
617 AddDir(result, SDL_GetBasePath());
618 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
619 {$ENDIF}
620 {$IFDEF WINDOWS}
621 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
622 if appdata <> '' then
623 AddDir(result, appdata);
624 {$ENDIF}
625 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
626 AddDir(result, '/usr/share/doom2df');
627 AddDir(result, '/usr/local/share/doom2df');
628 home := GetEnvironmentVariable('HOME');
629 if home <> '' then
630 AddDir(result, e_CatPath(home, '.doom2df'));
631 {$ENDIF}
632 {$IFDEF DARWIN}
633 bundle := GetBundlePath();
634 if bundle <> '' then
635 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
636 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
637 for i := 0 to dirArr.count - 1 do
638 begin
639 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
640 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
641 end;
642 {$ENDIF}
643 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
644 AddDir(result, SDL_AndroidGetInternalStoragePath());
645 if SDL_AndroidGetExternalStorageState() <> 0 then
646 AddDir(result, SDL_AndroidGetExternalStoragePath());
647 {$ENDIF}
648 end
649 end;
651 function GetDefaultRWDirs (): SSArray;
652 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
653 var home: AnsiString;
654 {$ENDIF}
655 {$IFDEF WINDOWS}
656 var appdata: AnsiString;
657 {$ENDIF}
658 {$IFDEF DARWIN}
659 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
660 {$ENDIF}
661 begin
662 result := nil;
663 {$IFDEF DARWIN}
664 bundle := GetBundlePath();
665 if ExtractFileExt(bundle) <> '.app' then
666 AddDir(result, binPath);
667 {$ELSE}
668 AddDir(result, binPath);
669 {$ENDIF}
670 if forceBinDir = false then
671 begin
672 {$IFDEF USE_SDL2}
673 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
674 {$ENDIF}
675 {$IFDEF WINDOWS}
676 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
677 if appdata <> '' then
678 AddDir(result, appdata);
679 {$ENDIF}
680 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
681 home := GetEnvironmentVariable('HOME');
682 if home <> '' then
683 AddDir(result, e_CatPath(home, '.doom2df'));
684 {$ENDIF}
685 {$IFDEF DARWIN}
686 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
687 for i := 0 to dirArr.count - 1 do
688 begin
689 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
690 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
691 end;
692 {$ENDIF}
693 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
694 if SDL_AndroidGetExternalStorageState() <> 0 then
695 AddDir(result, SDL_AndroidGetExternalStoragePath());
696 {$ENDIF}
697 end
698 end;
700 begin
701 forceBinDir := false;
702 binPath := GetBinaryPath();
704 i := 1;
705 while i < ParamCount do
706 begin
707 case ParamStr(i) of
708 '--like-windoze': forceBinDir := true;
709 '--rw-dir':
710 begin
711 Inc(i);
712 rwdir := ParamStr(i);
713 (* RW *)
714 AddDir(LogDirs, e_CatPath(rwdir, ''));
715 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
716 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
717 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
718 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
719 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
720 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
721 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
722 (* RO *)
723 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
724 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
725 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
726 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
727 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
728 end;
729 '--ro-dir':
730 begin
731 Inc(i);
732 rodir := ParamStr(i);
733 (* RO *)
734 AddDir(DataDirs, e_CatPath(rodir, 'data'));
735 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
736 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
737 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
738 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
739 end;
740 '--game-wad':
741 begin
742 Inc(i);
743 GameWADName := ParamStr(i);
744 end;
745 '--config':
746 begin
747 Inc(i);
748 gConfigScript := ParamStr(i);
749 end;
750 end;
751 Inc(i)
752 end;
754 // prefer bin dir if it writable and contains game.wad
755 if forceBinDir = false then
756 begin
757 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
758 if e_CanCreateFilesAt(binPath) then
759 forceBinDir := true
760 end;
762 (* RO *)
763 rodirs := GetDefaultRODirs();
764 AddDef(DataDirs, rodirs, 'data');
765 AddDef(ModelDirs, rodirs, 'data/models');
766 AddDef(MegawadDirs, rodirs, 'maps/megawads');
767 AddDef(MapDirs, rodirs, 'maps');
768 AddDef(WadDirs, rodirs, 'wads');
770 (* RW *)
771 rwdirs := GetDefaultRWDirs();
772 AddDef(LogDirs, rwdirs, '');
773 AddDef(SaveDirs, rwdirs, 'data');
774 AddDef(CacheDirs, rwdirs, 'data/cache');
775 AddDef(ConfigDirs, rwdirs, '');
776 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
777 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
778 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
779 AddDef(StatsDirs, rwdirs, 'stats');
781 for i := 0 to High(MapDirs) do
782 AddDir(AllMapDirs, MapDirs[i]);
783 for i := 0 to High(MegawadDirs) do
784 AddDir(AllMapDirs, MegawadDirs[i]);
785 OptimizeDirs(AllMapDirs);
787 // HACK: ensure the screenshots folder also has a stats subfolder in it
788 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
789 if rwdir <> '' then CreateDir(rwdir + '/stats');
790 end;
792 procedure EntryParams;
793 var i: Integer;
794 begin
795 i := 1;
796 while i <= ParamCount do
797 begin
798 case ParamStr(i) of
799 '--gdb': noct := true;
800 '--log', '--con-stdout': conbufDumpToStdOut := true;
801 '--safe-log': e_SetSafeSlowLog(true);
802 '--log-file':
803 if i + 1 <= ParamCount then
804 begin
805 Inc(i);
806 LogFileName := ParamStr(i)
807 end;
808 '--no-fbo': glRenderToFBO := false;
809 end;
810 Inc(i)
811 end
812 end;
814 procedure InitLog;
815 var rwdir: AnsiString;
816 begin
817 if LogFileName = '' then
818 begin
819 rwdir := e_GetWriteableDir(LogDirs, false);
820 if rwdir <> '' then
821 begin
822 {$IFDEF HEADLESS}
823 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
824 {$ELSE}
825 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
826 {$ENDIF}
827 end
828 end;
829 if LogFileName <> '' then
830 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
831 e_InitWritelnDriver
832 end;
834 procedure InitPrep;
835 begin
836 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
837 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
838 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
839 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
840 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
842 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
843 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
845 PrintDirs('DataDirs', DataDirs);
846 PrintDirs('ModelDirs', ModelDirs);
847 PrintDirs('MegawadDirs', MegawadDirs);
848 PrintDirs('MapDirs', MapDirs);
849 PrintDirs('WadDirs', WadDirs);
851 PrintDirs('LogDirs', LogDirs);
852 PrintDirs('SaveDirs', SaveDirs);
853 PrintDirs('CacheDirs', CacheDirs);
854 PrintDirs('ConfigDirs', ConfigDirs);
855 PrintDirs('ScreenshotDirs', ScreenshotDirs);
856 PrintDirs('StatsDirs', StatsDirs);
857 PrintDirs('MapDownloadDirs', MapDownloadDirs);
858 PrintDirs('WadDownloadDirs', WadDownloadDirs);
860 {$IFDEF HEADLESS}
861 {$IFDEF USE_SDLMIXER}
862 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
863 {$ELSE}
864 NoSound := True; // FMOD backend will sort it out
865 {$ENDIF}
866 {$ELSE}
867 NoSound := False;
868 {$ENDIF}
870 GameWAD := e_FindWad(DataDirs, GameWADName);
871 if GameWad = '' then
872 begin
873 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
874 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
875 if forceBinDir = false then
876 SDL_ShowSimpleMessageBox(
877 SDL_MESSAGEBOX_ERROR,
878 'Doom 2D Forever',
879 PChar('WAD ' + GameWADName + ' not found in data directories.'),
880 nil
881 );
882 {$ENDIF}
883 e_DeinitLog;
884 Halt(1);
885 end
886 end;
888 {$IFDEF ENABLE_HOLMES}
889 procedure InitHolmes;
890 var flexloaded: Boolean;
891 begin
892 flexloaded := true;
893 if not fuiAddWad('flexui.wad') then
894 begin
895 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
896 end;
897 try
898 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
899 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
900 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
901 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
902 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
903 except on e: Exception do
904 begin
905 writeln('ERROR loading FlexUI fonts');
906 flexloaded := false;
907 //raise;
908 end;
909 else
910 begin
911 flexloaded := false;
912 //raise;
913 end;
914 end;
915 if flexloaded then
916 begin
917 try
918 e_LogWriteln('FlexUI: loading stylesheet...');
919 uiLoadStyles('flexui/widgets.wgs');
920 except on e: TParserException do
921 begin
922 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
923 //raise;
924 flexloaded := false;
925 end;
926 else
927 begin
928 //raise;
929 flexloaded := false;
930 end;
931 end;
932 end;
933 g_holmes_imfunctional := not flexloaded;
934 if not g_holmes_imfunctional then
935 begin
936 uiInitialize();
937 uiContext.font := 'win14';
938 end;
939 if assigned(oglInitCB) then oglInitCB;
940 end;
942 procedure FreeHolmes;
943 begin
944 if assigned(oglDeinitCB) then
945 oglDeinitCB
946 end;
947 {$ENDIF}
949 procedure InitSound;
950 {$IFDEF USE_SDLMIXER}
951 var timiditycfg: AnsiString;
952 var oldcwd, newcwd: RawByteString;
953 {$ENDIF}
954 begin
955 if NoSound = false then
956 begin
957 e_WriteLog('Initializing sound system', TMsgType.Notify);
958 {$IFDEF USE_SDLMIXER}
959 newcwd := '';
960 if UseNativeMusic then
961 SetEnvVar('SDL_NATIVE_MUSIC', '1');
962 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
963 if timiditycfg = '' then
964 begin
965 timiditycfg := 'timidity.cfg';
966 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
967 begin
968 timiditycfg := ExpandFileName(timiditycfg);
969 newcwd := ExtractFileDir(timiditycfg);
970 SetEnvVar('TIMIDITY_CFG', timiditycfg);
971 end
972 else
973 timiditycfg := '';
974 end;
975 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
976 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
977 {$ENDIF}
978 e_InitSoundSystem(NoSound);
979 {$IFDEF USE_SDLMIXER}
980 if e_TimidityDecoder and (newcwd <> '') then
981 begin
982 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
983 (* CWD not restored after sound init because timidity *)
984 (* store relative pathes internally and load patches *)
985 (* later. I hope game never relies on CWD. *)
986 oldcwd := '';
987 GetDir(0, oldcwd);
988 ChDir(newcwd);
989 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
990 end;
991 {$ENDIF}
992 end;
993 end;
995 procedure ScreenResize (w, h: Integer);
996 begin
997 {$IFDEF ENABLE_RENDER}
998 r_Render_Resize(w, h);
999 {$IFDEF ENABLE_HOLMES}
1000 fuiScrWdt := w;
1001 fuiScrHgt := h;
1002 {$ENDIF}
1003 {$IFNDEF ANDROID}
1004 (* This will fix menu reset on keyboard showing *)
1005 {$IFDEF ENABLE_MENU}
1006 g_Menu_Reset;
1007 {$ENDIF}
1008 {$ENDIF}
1009 //g_Game_ClearLoading;
1010 {$IFDEF ENABLE_HOLMES}
1011 if assigned(oglInitCB) then oglInitCB;
1012 {$ENDIF}
1013 {$ENDIF}
1014 end;
1016 procedure Startup;
1017 begin
1018 Randomize;
1019 InitPath;
1020 InitLog;
1021 InitPrep;
1022 e_Input_Initialize;
1023 InitSound;
1024 {$IFDEF ENABLE_SYSTEM}
1025 sys_Init;
1026 sys_CharPress := @CharPress; (* install hook *)
1027 sys_ScreenResize := @ScreenResize; (* install hook *)
1028 {$ENDIF}
1029 g_Options_SetDefault;
1030 g_Options_SetDefaultVideo;
1031 g_Console_Initialize;
1032 // TODO move load configs here
1033 g_Language_Set(gLanguage);
1034 {$IFDEF ENABLE_RENDER}
1035 r_Render_Initialize;
1036 {$ENDIF}
1037 DebugOptions;
1038 g_Net_InitLowLevel;
1039 // TODO init serverlist
1040 {$IFDEF ENABLE_HOLMES}
1041 InitHolmes;
1042 {$ENDIF}
1043 {$IFDEF ENABLE_RENDER}
1044 g_PlayerModel_LoadAll;
1045 r_Render_Load;
1046 {$ELSE}
1047 g_PlayerModel_LoadFake('doomer', 'doomer.wad');
1048 {$ENDIF}
1049 g_Game_Init;
1050 {$IFDEF ENABLE_MENU}
1051 g_Menu_Init;
1052 {$ENDIF}
1053 g_Game_Process_Params;
1054 // TODO reload GAME textures
1055 g_Console_Init; // welcome message
1056 {$IFDEF ENABLE_MENU}
1057 if (not gGameOn) and gAskLanguage then
1058 g_Menu_AskLanguage;
1059 {$ENDIF}
1060 Time_Old := GetTickCount64();
1061 while not ProcessMessage() do begin end;
1062 g_Console_WriteGameConfig;
1063 {$IFDEF ENABLE_MENU}
1064 g_GUI_Destroy;
1065 g_Menu_Free;
1066 {$ENDIF}
1067 {$IFDEF ENABLE_RENDER}
1068 r_Render_Free;
1069 {$ENDIF}
1070 {$IFDEF ENABLE_HOLMES}
1071 FreeHolmes;
1072 {$ENDIF}
1073 g_Net_Slist_ShutdownAll;
1074 g_Net_DeinitLowLevel;
1075 (* g_Touch_Finalize; *)
1076 {$IFDEF ENABLE_RENDER}
1077 r_Render_Finalize;
1078 {$ENDIF}
1079 {$IFDEF ENABLE_SYSTEM}
1080 sys_Final;
1081 {$ENDIF}
1082 g_Console_Finalize;
1083 e_ReleaseSoundSystem;
1084 e_Input_Finalize;
1085 e_WriteLog('Shutdown with no errors.', TMsgType.Notify)
1086 end;
1088 procedure InitCVars;
1089 begin
1090 {$IFDEF USE_SDLMIXER}
1091 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1092 {$IFDEF DARWIN}
1093 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1094 {$ELSE}
1095 UseNativeMusic := false;
1096 {$ENDIF}
1097 {$ENDIF}
1098 end;
1100 procedure EntryPoint;
1101 begin
1102 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
1103 InitCVars;
1104 EntryParams;
1105 e_Log_Initialize;
1106 {$IFDEF HEADLESS}
1107 conbufDumpToStdOut := true;
1108 {$ENDIF}
1109 if noct then
1110 Startup
1111 else
1112 try
1113 Startup
1114 except on e: Exception do
1115 e_WriteStackTrace(e.message)
1116 else
1117 e_WriteStackTrace('FATAL ERROR')
1118 end;
1119 e_Log_Finalize
1120 end;
1122 {$IFDEF ANDROID}
1123 function SDL_main (argc: CInt; argv: PPChar): CInt; cdecl;
1124 begin
1125 {$IFDEF ANDROID}
1126 System.argc := argc;
1127 System.argv := argv;
1128 {$ENDIF}
1129 EntryPoint;
1130 result := 0
1131 end;
1133 function JNI_OnLoad (vm: PJavaVM; reserved: pointer): JInt; cdecl;
1134 begin
1135 result:= JNI_VERSION_1_6;
1136 end;
1138 procedure JNI_OnUnload(vm: PJavaVM; reserved: pointer); cdecl;
1139 begin
1140 end;
1142 // DONT REMOVE JNI FUNCTIONS. SPECIAL HANDLING BY FPC.
1143 exports SDL_main name 'SDL_main';
1144 exports JNI_OnLoad name 'JNI_OnLoad';
1145 exports JNI_OnUnload name 'JNI_Unload';
1146 {$ELSE}
1147 begin
1148 EntryPoint
1149 {$ENDIF}
1151 end.