DEADSOFTWARE

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