DEADSOFTWARE

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