DEADSOFTWARE

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