DEADSOFTWARE

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