DEADSOFTWARE

render: completely remove opengl calls form game code
[d2df-sdl.git] / src / game / g_main.pas
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 unit g_main;
18 interface
20 uses Utils;
22 procedure Main ();
23 procedure Init ();
24 procedure Release ();
25 procedure Update ();
26 procedure Draw ();
27 procedure KeyPress (K: Word);
28 procedure CharPress (C: AnsiChar);
30 var
31 {--- Read-only dirs ---}
32 GameWAD: string;
33 DataDirs: SSArray;
34 ModelDirs: SSArray;
35 MegawadDirs: SSArray;
36 MapDirs: SSArray;
37 WadDirs: SSArray;
38 AllMapDirs: SSArray; // Maps + Megawads
40 {--- Read-Write dirs ---}
41 LogFileName: string;
42 LogDirs: SSArray;
43 SaveDirs: SSArray;
44 CacheDirs: SSArray;
45 ConfigDirs: SSArray;
46 ScreenshotDirs: SSArray;
47 StatsDirs: SSArray;
48 MapDownloadDirs: SSArray;
49 WadDownloadDirs: SSArray;
51 GameWADName: string = 'GAME';
53 implementation
55 uses
56 {$IFDEF ENABLE_HOLMES}
57 g_holmes, sdlcarcass, fui_ctls, fui_wadread, fui_style, fui_gfx_gl,
58 {$ENDIF}
59 {$IFDEF LINUX}
60 BaseUnix,
61 {$ENDIF}
62 {$IFDEF DARWIN}
63 MacOSAll, CocoaAll,
64 {$ENDIF}
65 {$IFDEF USE_SDL2}
66 SDL2,
67 {$ENDIF}
68 wadreader, e_log, g_window,
69 r_graphics, e_input, g_game, g_console, g_gui,
70 e_sound, g_options, g_sound, g_player, g_basic,
71 g_weapons, SysUtils, g_triggers, MAPDEF, g_map, e_res,
72 g_menu, g_language, g_net, g_touch, g_system, g_res_downloader,
73 conbuf, envvars, r_game,
74 xparser;
77 var
78 charbuff: packed array [0..15] of AnsiChar;
79 binPath: AnsiString = '';
80 forceBinDir: Boolean;
81 {$IFDEF USE_SDLMIXER}
82 UseNativeMusic: Boolean;
83 {$ENDIF}
85 function GetBinaryPath (): AnsiString;
86 {$IFDEF LINUX}
87 var
88 //cd: AnsiString;
89 sl: AnsiString;
90 {$ENDIF}
91 begin
92 result := ExtractFilePath(ParamStr(0));
93 {$IFDEF LINUX}
94 // it may be a symlink; do some guesswork here
95 sl := fpReadLink(ExtractFileName(ParamStr(0)));
96 if (sl = ParamStr(0)) then
97 begin
98 // use current directory, as we don't have anything better
99 //result := '.';
100 GetDir(0, result);
101 end;
102 {$ENDIF}
103 result := fixSlashes(result);
104 if (length(result) > 0) and (result[length(result)] <> '/') then result := result+'/';
105 end;
107 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
108 var dir: AnsiString;
109 begin
110 e_LogWriteln(msg + ':');
111 for dir in dirs do
112 e_LogWriteln(' ' + dir);
113 end;
115 {$IFDEF DARWIN}
116 function NSStringToAnsiString (s: NSString): AnsiString;
117 var i: Integer;
118 begin
119 result := '';
120 for i := 0 to s.length - 1 do
121 result := result + AnsiChar(s.characterAtIndex(i));
122 end;
124 function GetBundlePath (): AnsiString;
125 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
126 begin
127 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
128 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
129 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
130 CFRelease(pathRef);
131 CFRelease(pathCFStr);
132 Result := pathStr;
133 end;
134 {$ENDIF}
136 procedure InitPath;
137 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
139 procedure AddDir (var dirs: SSArray; append: AnsiString);
140 begin
141 SetLength(dirs, Length(dirs) + 1);
142 dirs[High(dirs)] := ExpandFileName(append)
143 end;
145 function IsSep (ch: Char): Boolean;
146 begin
147 {$IFDEF WINDOWS}
148 result := (ch = '/') or (ch = '\');
149 {$ELSE}
150 result := (ch = '/');
151 {$ENDIF}
152 end;
154 function OptimizePath (dir: AnsiString): AnsiString;
155 var i, len: Integer; s: AnsiString;
156 begin
157 i := 1; len := Length(dir); s := '';
158 while i <= len do
159 begin
160 if IsSep(dir[i]) then
161 begin
162 s := s + DirectorySeparator;
163 Inc(i);
164 while (i <= len) and IsSep(dir[i]) do Inc(i);
165 if (i <= len) and (dir[i] = '.') then
166 begin
167 if (i = len) or IsSep(dir[i + 1]) then
168 begin
169 Inc(i)
170 end
171 else if (i + 1 <= len) and (dir[i + 1] = '.') then
172 begin
173 if (i + 1 = len) or IsSep(dir[i + 2]) then
174 begin
175 s := e_UpperDir(s);
176 Inc(i, 2)
177 end
178 end
179 end
180 end
181 else
182 begin
183 s := s + dir[i];
184 Inc(i)
185 end
186 end;
187 result := s
188 end;
190 procedure OptimizeDirs (var dirs: SSArray);
191 var i, j, k: Integer;
192 begin
193 for i := 0 to High(dirs) do
194 dirs[i] := OptimizePath(dirs[i]);
195 // deduplicate
196 i := High(dirs);
197 while i >= 0 do
198 begin
199 j := 0;
200 while j < i do
201 begin
202 if dirs[j] = dirs[i] then
203 begin
204 for k := j + 1 to High(dirs) do
205 dirs[k - 1] := dirs[k];
206 Dec(i);
207 SetLength(dirs, High(dirs))
208 end
209 else
210 begin
211 Inc(j)
212 end
213 end;
214 Dec(i)
215 end
216 end;
218 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
219 var s: AnsiString;
220 begin
221 if Length(dirs) = 0 then
222 for s in base do
223 AddDir(dirs, e_CatPath(s, append));
224 OptimizeDirs(dirs)
225 end;
227 function GetDefaultRODirs (): SSArray;
228 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
229 var home: AnsiString;
230 {$ENDIF}
231 {$IFDEF WINDOWS}
232 var appdata: AnsiString;
233 {$ENDIF}
234 {$IFDEF DARWIN}
235 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
236 {$ENDIF}
237 begin
238 result := nil;
239 {$IFDEF DARWIN}
240 bundle := GetBundlePath();
241 if ExtractFileExt(bundle) <> '.app' then
242 AddDir(result, binpath);
243 {$ELSE}
244 AddDir(result, binPath);
245 {$ENDIF}
246 if forceBinDir = false then
247 begin
248 {$IFDEF USE_SDL2}
249 AddDir(result, SDL_GetBasePath());
250 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
251 {$ENDIF}
252 {$IFDEF WINDOWS}
253 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
254 if appdata <> '' then
255 AddDir(result, appdata);
256 {$ENDIF}
257 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
258 AddDir(result, '/usr/share/doom2df');
259 AddDir(result, '/usr/local/share/doom2df');
260 home := GetEnvironmentVariable('HOME');
261 if home <> '' then
262 AddDir(result, e_CatPath(home, '.doom2df'));
263 {$ENDIF}
264 {$IFDEF DARWIN}
265 bundle := GetBundlePath();
266 if bundle <> '' then
267 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
268 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
269 for i := 0 to dirArr.count - 1 do
270 begin
271 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
272 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
273 end;
274 {$ENDIF}
275 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
276 AddDir(result, SDL_AndroidGetInternalStoragePath());
277 if SDL_AndroidGetExternalStorageState() <> 0 then
278 AddDir(result, SDL_AndroidGetExternalStoragePath());
279 {$ENDIF}
280 end
281 end;
283 function GetDefaultRWDirs (): SSArray;
284 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
285 var home: AnsiString;
286 {$ENDIF}
287 {$IFDEF WINDOWS}
288 var appdata: AnsiString;
289 {$ENDIF}
290 {$IFDEF DARWIN}
291 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
292 {$ENDIF}
293 begin
294 result := nil;
295 {$IFDEF DARWIN}
296 bundle := GetBundlePath();
297 if ExtractFileExt(bundle) <> '.app' then
298 AddDir(result, binPath);
299 {$ELSE}
300 AddDir(result, binPath);
301 {$ENDIF}
302 if forceBinDir = false then
303 begin
304 {$IFDEF USE_SDL2}
305 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
306 {$ENDIF}
307 {$IFDEF WINDOWS}
308 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
309 if appdata <> '' then
310 AddDir(result, appdata);
311 {$ENDIF}
312 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
313 home := GetEnvironmentVariable('HOME');
314 if home <> '' then
315 AddDir(result, e_CatPath(home, '.doom2df'));
316 {$ENDIF}
317 {$IFDEF DARWIN}
318 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
319 for i := 0 to dirArr.count - 1 do
320 begin
321 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
322 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
323 end;
324 {$ENDIF}
325 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
326 if SDL_AndroidGetExternalStorageState() <> 0 then
327 AddDir(result, SDL_AndroidGetExternalStoragePath());
328 {$ENDIF}
329 end
330 end;
332 begin
333 forceBinDir := false;
334 binPath := GetBinaryPath();
336 i := 1;
337 while i < ParamCount do
338 begin
339 case ParamStr(i) of
340 '--like-windoze': forceBinDir := true;
341 '--rw-dir':
342 begin
343 Inc(i);
344 rwdir := ParamStr(i);
345 (* RW *)
346 AddDir(LogDirs, e_CatPath(rwdir, ''));
347 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
348 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
349 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
350 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
351 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
352 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
353 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
354 (* RO *)
355 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
356 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
357 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
358 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
359 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
360 end;
361 '--ro-dir':
362 begin
363 Inc(i);
364 rodir := ParamStr(i);
365 (* RO *)
366 AddDir(DataDirs, e_CatPath(rodir, 'data'));
367 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
368 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
369 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
370 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
371 end;
372 '--game-wad':
373 begin
374 Inc(i);
375 GameWADName := ParamStr(i);
376 end;
377 '--config':
378 begin
379 Inc(i);
380 gConfigScript := ParamStr(i);
381 end;
382 end;
383 Inc(i)
384 end;
386 // prefer bin dir if it writable and contains game.wad
387 if forceBinDir = false then
388 begin
389 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
390 if e_CanCreateFilesAt(binPath) then
391 forceBinDir := true
392 end;
394 (* RO *)
395 rodirs := GetDefaultRODirs();
396 AddDef(DataDirs, rodirs, 'data');
397 AddDef(ModelDirs, rodirs, 'data/models');
398 AddDef(MegawadDirs, rodirs, 'maps/megawads');
399 AddDef(MapDirs, rodirs, 'maps');
400 AddDef(WadDirs, rodirs, 'wads');
402 (* RW *)
403 rwdirs := GetDefaultRWDirs();
404 AddDef(LogDirs, rwdirs, '');
405 AddDef(SaveDirs, rwdirs, 'data');
406 AddDef(CacheDirs, rwdirs, 'data/cache');
407 AddDef(ConfigDirs, rwdirs, '');
408 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
409 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
410 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
411 AddDef(StatsDirs, rwdirs, 'stats');
413 for i := 0 to High(MapDirs) do
414 AddDir(AllMapDirs, MapDirs[i]);
415 for i := 0 to High(MegawadDirs) do
416 AddDir(AllMapDirs, MegawadDirs[i]);
417 OptimizeDirs(AllMapDirs);
419 if LogFileName = '' then
420 begin
421 rwdir := e_GetWriteableDir(LogDirs, false);
422 if rwdir <> '' then
423 begin
424 {$IFDEF HEADLESS}
425 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
426 {$ELSE}
427 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
428 {$ENDIF}
429 end
430 end;
432 // HACK: ensure the screenshots folder also has a stats subfolder in it
433 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
434 if rwdir <> '' then CreateDir(rwdir + '/stats');
435 end;
437 procedure InitPrep;
438 var i: Integer;
439 begin
440 {$IFDEF HEADLESS}
441 conbufDumpToStdOut := true;
442 {$ENDIF}
443 for i := 1 to ParamCount do
444 begin
445 case ParamStr(i) of
446 '--con-stdout': conbufDumpToStdOut := true;
447 '--no-fbo': glRenderToFBO := false;
448 end
449 end;
451 if LogFileName <> '' then
452 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
453 e_InitWritelnDriver();
454 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
455 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
456 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
457 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
458 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
460 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
461 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
463 PrintDirs('DataDirs', DataDirs);
464 PrintDirs('ModelDirs', ModelDirs);
465 PrintDirs('MegawadDirs', MegawadDirs);
466 PrintDirs('MapDirs', MapDirs);
467 PrintDirs('WadDirs', WadDirs);
469 PrintDirs('LogDirs', LogDirs);
470 PrintDirs('SaveDirs', SaveDirs);
471 PrintDirs('CacheDirs', CacheDirs);
472 PrintDirs('ConfigDirs', ConfigDirs);
473 PrintDirs('ScreenshotDirs', ScreenshotDirs);
474 PrintDirs('StatsDirs', StatsDirs);
475 PrintDirs('MapDownloadDirs', MapDownloadDirs);
476 PrintDirs('WadDownloadDirs', WadDownloadDirs);
478 GameWAD := e_FindWad(DataDirs, GameWADName);
479 if GameWad = '' then
480 begin
481 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
482 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
483 if forceBinDir = false then
484 SDL_ShowSimpleMessageBox(
485 SDL_MESSAGEBOX_ERROR,
486 'Doom 2D Forever',
487 PChar('WAD ' + GameWADName + ' not found in data directories.'),
488 nil
489 );
490 {$ENDIF}
491 e_DeinitLog;
492 Halt(1);
493 end;
494 end;
496 procedure Main();
497 {$IFDEF ENABLE_HOLMES}
498 var flexloaded: Boolean;
499 {$ENDIF}
500 begin
501 InitPath;
502 InitPrep;
503 e_InitInput;
504 sys_Init;
506 g_Options_SetDefault;
507 g_Options_SetDefaultVideo;
508 g_Console_SysInit;
509 if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
510 raise Exception.Create('Failed to set videomode on startup.');
512 e_WriteLog(gLanguage, TMsgType.Notify);
513 g_Language_Set(gLanguage);
515 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
516 flexloaded := true;
517 if not fuiAddWad('flexui.wad') then
518 begin
519 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
520 end;
521 try
522 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
523 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
524 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
525 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
526 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
527 except on e: Exception do
528 begin
529 writeln('ERROR loading FlexUI fonts');
530 flexloaded := false;
531 //raise;
532 end;
533 else
534 begin
535 flexloaded := false;
536 //raise;
537 end;
538 end;
539 if (flexloaded) then
540 begin
541 try
542 e_LogWriteln('FlexUI: loading stylesheet...');
543 uiLoadStyles('flexui/widgets.wgs');
544 except on e: TParserException do
545 begin
546 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
547 //raise;
548 flexloaded := false;
549 end;
550 else
551 begin
552 //raise;
553 flexloaded := false;
554 end;
555 end;
556 end;
557 g_holmes_imfunctional := not flexloaded;
559 if (not g_holmes_imfunctional) then
560 begin
561 uiInitialize();
562 uiContext.font := 'win14';
563 end;
565 if assigned(oglInitCB) then oglInitCB;
566 {$ENDIF}
568 //g_Res_CreateDatabases(true); // it will be done before connecting to the server for the first time
570 e_WriteLog('Entering SDLMain', TMsgType.Notify);
572 {$WARNINGS OFF}
573 SDLMain();
574 {$WARNINGS ON}
576 {$IFDEF ENABLE_HOLMES}
577 if assigned(oglDeinitCB) then oglDeinitCB;
578 {$ENDIF}
580 g_Console_WriteGameConfig;
581 sys_Final;
582 end;
584 procedure Init();
585 {$IFDEF USE_SDLMIXER}
586 var timiditycfg: AnsiString;
587 var oldcwd, newcwd: RawByteString;
588 {$ENDIF}
589 var NoSound: Boolean;
590 begin
591 Randomize;
593 {$IFDEF HEADLESS}
594 {$IFDEF USE_SDLMIXER}
595 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
596 {$ELSE}
597 NoSound := True; // FMOD backend will sort it out
598 {$ENDIF}
599 {$ELSE}
600 NoSound := False;
601 {$ENDIF}
603 g_Touch_Init;
605 (*
606 if (e_JoysticksAvailable > 0) then
607 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
608 else
609 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
610 *)
612 if gNoSound = false then
613 begin
614 e_WriteLog('Initializing sound system', TMsgType.Notify);
615 {$IFDEF USE_SDLMIXER}
616 newcwd := '';
617 if UseNativeMusic then
618 SetEnvVar('SDL_NATIVE_MUSIC', '1');
619 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
620 if timiditycfg = '' then
621 begin
622 timiditycfg := 'timidity.cfg';
623 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
624 begin
625 timiditycfg := ExpandFileName(timiditycfg);
626 newcwd := ExtractFileDir(timiditycfg);
627 SetEnvVar('TIMIDITY_CFG', timiditycfg);
628 end
629 else
630 timiditycfg := '';
631 end;
632 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
633 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
634 {$ENDIF}
635 e_InitSoundSystem(NoSound);
636 {$IFDEF USE_SDLMIXER}
637 if e_TimidityDecoder and (newcwd <> '') then
638 begin
639 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
640 (* CWD not restored after sound init because timidity *)
641 (* store relative pathes internally and load patches *)
642 (* later. I hope game never relies on CWD. *)
643 oldcwd := '';
644 GetDir(0, oldcwd);
645 ChDir(newcwd);
646 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
647 end;
648 {$ENDIF}
649 end;
651 e_WriteLog('Init game', TMsgType.Notify);
652 g_Game_Init();
654 FillChar(charbuff, sizeof(charbuff), ' ');
655 end;
658 procedure Release();
659 begin
660 e_WriteLog('Releasing engine', TMsgType.Notify);
661 e_ReleaseEngine();
663 e_WriteLog('Releasing input', TMsgType.Notify);
664 e_ReleaseInput();
666 if not gNoSound then
667 begin
668 e_WriteLog('Releasing sound', TMsgType.Notify);
669 e_ReleaseSoundSystem();
670 end;
671 end;
674 procedure Update ();
675 begin
676 // remember old mobj positions, prepare for update
677 g_Game_PreUpdate();
678 // server: receive client commands for new frame
679 // client: receive game state changes from server
680 if (NetMode = NET_SERVER) then g_Net_Host_Update()
681 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
682 // think
683 g_Game_Update();
684 // server: send any accumulated outgoing data to clients
685 if NetMode = NET_SERVER then g_Net_Flush();
686 end;
689 procedure Draw ();
690 begin
691 r_Game_Draw();
692 end;
695 function Translit (const S: AnsiString): AnsiString;
696 var
697 i: Integer;
698 begin
699 Result := S;
700 for i := 1 to Length(Result) do
701 begin
702 case Result[i] of
703 'É': Result[i] := 'Q';
704 'Ö': Result[i] := 'W';
705 'Ó': Result[i] := 'E';
706 'Ê': Result[i] := 'R';
707 'Å': Result[i] := 'T';
708 'Í': Result[i] := 'Y';
709 'Ã': Result[i] := 'U';
710 'Ø': Result[i] := 'I';
711 'Ù': Result[i] := 'O';
712 'Ç': Result[i] := 'P';
713 'Õ': Result[i] := '['; //Chr(219);
714 'Ú': Result[i] := ']'; //Chr(221);
715 'Ô': Result[i] := 'A';
716 'Û': Result[i] := 'S';
717 'Â': Result[i] := 'D';
718 'À': Result[i] := 'F';
719 'Ï': Result[i] := 'G';
720 'Ð': Result[i] := 'H';
721 'Î': Result[i] := 'J';
722 'Ë': Result[i] := 'K';
723 'Ä': Result[i] := 'L';
724 'Æ': Result[i] := ';'; //Chr(186);
725 'Ý': Result[i] := #39; //Chr(222);
726 'ß': Result[i] := 'Z';
727 '×': Result[i] := 'X';
728 'Ñ': Result[i] := 'C';
729 'Ì': Result[i] := 'V';
730 'È': Result[i] := 'B';
731 'Ò': Result[i] := 'N';
732 'Ü': Result[i] := 'M';
733 'Á': Result[i] := ','; //Chr(188);
734 'Þ': Result[i] := '.'; //Chr(190);
735 end;
736 end;
737 end;
740 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
741 var
742 ls1, ls2: string;
743 begin
744 ls1 := CheatEng[ct];
745 ls2 := Translit(CheatRus[ct]);
746 if length(ls1) = 0 then ls1 := '~';
747 if length(ls2) = 0 then ls2 := '~';
748 result :=
749 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
750 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
751 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
752 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
754 if ct = I_GAME_CHEAT_JETPACK then
755 begin
756 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
757 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
758 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
759 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
760 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
761 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
762 end;
764 end;
767 procedure Cheat ();
768 const
769 CHEAT_DAMAGE = 500;
770 label
771 Cheated;
772 var
773 s, s2: string;
774 c: ShortString;
775 a: Integer;
776 begin
778 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
779 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
780 or g_Game_IsNet then Exit;
782 if not gGameOn then exit;
783 if not conIsCheatsEnabled then exit;
785 s := 'SOUND_GAME_RADIO';
787 //
788 if CheckCheat(I_GAME_CHEAT_GODMODE) then
789 begin
790 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
791 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
792 goto Cheated;
793 end;
794 // RAMBO
795 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
796 begin
797 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
798 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
799 goto Cheated;
800 end;
801 // TANK
802 if CheckCheat(I_GAME_CHEAT_HEALTH) then
803 begin
804 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
805 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
806 goto Cheated;
807 end;
808 // IDDQD
809 if CheckCheat(I_GAME_CHEAT_DEATH) then
810 begin
811 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
812 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
813 s := 'SOUND_MONSTER_HAHA';
814 goto Cheated;
815 end;
816 //
817 if CheckCheat(I_GAME_CHEAT_DOORS) then
818 begin
819 g_Triggers_OpenAll();
820 goto Cheated;
821 end;
822 // GOODBYE
823 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
824 begin
825 if gTriggers <> nil then
826 for a := 0 to High(gTriggers) do
827 if gTriggers[a].TriggerType = TRIGGER_EXIT then
828 begin
829 gExitByTrigger := True;
830 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
831 g_Game_ExitLevel(gTriggers[a].tgcMap);
832 Break;
833 end;
834 goto Cheated;
835 end;
836 //
837 s2 := Copy(charbuff, 15, 2);
838 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
839 begin
840 if g_Map_Exist(gGameSettings.WAD + ':\MAP' + s2) then
841 begin
842 c := 'MAP' + s2;
843 g_Game_ExitLevel(c);
844 end;
845 goto Cheated;
846 end;
847 //
848 if CheckCheat(I_GAME_CHEAT_FLY) then
849 begin
850 gFly := not gFly;
851 goto Cheated;
852 end;
853 // BULLFROG
854 if CheckCheat(I_GAME_CHEAT_JUMPS) then
855 begin
856 VEL_JUMP := 30-VEL_JUMP;
857 goto Cheated;
858 end;
859 // FORMULA1
860 if CheckCheat(I_GAME_CHEAT_SPEED) then
861 begin
862 MAX_RUNVEL := 32-MAX_RUNVEL;
863 goto Cheated;
864 end;
865 // CONDOM
866 if CheckCheat(I_GAME_CHEAT_SUIT) then
867 begin
868 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
869 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
870 goto Cheated;
871 end;
872 //
873 if CheckCheat(I_GAME_CHEAT_AIR) then
874 begin
875 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
876 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
877 goto Cheated;
878 end;
879 // PURELOVE
880 if CheckCheat(I_GAME_CHEAT_BERSERK) then
881 begin
882 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
883 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
884 goto Cheated;
885 end;
886 //
887 if CheckCheat(I_GAME_CHEAT_JETPACK) then
888 begin
889 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
890 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
891 goto Cheated;
892 end;
893 // CASPER
894 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
895 begin
896 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
897 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
898 goto Cheated;
899 end;
900 //
901 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
902 begin
903 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
904 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
905 goto Cheated;
906 end;
907 // INFERNO
908 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
909 begin
910 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
911 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
912 goto Cheated;
913 end;
914 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
915 begin
916 gAimLine := not gAimLine;
917 goto Cheated;
918 end;
919 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
920 begin
921 gShowMap := not gShowMap;
922 goto Cheated;
923 end;
924 Exit;
926 Cheated:
927 g_Sound_PlayEx(s);
928 end;
931 procedure KeyPress (K: Word);
932 {$IFNDEF HEADLESS}
933 var
934 Msg: g_gui.TMessage;
935 {$ENDIF}
936 begin
937 {$IFNDEF HEADLESS}
938 case K of
939 VK_ESCAPE: // <Esc>:
940 begin
941 if (g_ActiveWindow <> nil) then
942 begin
943 Msg.Msg := WM_KEYDOWN;
944 Msg.WParam := VK_ESCAPE;
945 g_ActiveWindow.OnMessage(Msg);
946 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
947 end
948 else if (gState <> STATE_FOLD) then
949 begin
950 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
951 begin
952 g_Game_InGameMenu(True);
953 end
954 else if (gExit = 0) and (gState <> STATE_SLIST) then
955 begin
956 if (gState <> STATE_MENU) then
957 begin
958 if (NetMode <> NET_NONE) then
959 begin
960 g_Game_StopAllSounds(True);
961 g_Game_Free;
962 gState := STATE_MENU;
963 Exit;
964 end;
965 end;
966 g_GUI_ShowWindow('MainMenu');
967 g_Sound_PlayEx('MENU_OPEN');
968 end;
969 end;
970 end;
972 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
973 begin // <F2> .. <F6> � <F12>
974 if gGameOn and (not gConsoleShow) and (not gChatShow) then
975 begin
976 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
977 if (not g_Game_IsNet) then g_Game_Pause(True);
978 case K of
979 IK_F2: g_Menu_Show_SaveMenu();
980 IK_F3: g_Menu_Show_LoadMenu();
981 IK_F4: g_Menu_Show_GameSetGame();
982 IK_F5: g_Menu_Show_OptionsVideo();
983 IK_F6: g_Menu_Show_OptionsSound();
984 IK_F7: g_Menu_Show_EndGameMenu();
985 IK_F10: g_Menu_Show_QuitGameMenu();
986 end;
987 end;
988 end;
990 else
991 begin
992 gJustChatted := False;
993 if gConsoleShow or gChatShow then
994 begin
995 g_Console_Control(K);
996 end
997 else if (g_ActiveWindow <> nil) then
998 begin
999 Msg.Msg := WM_KEYDOWN;
1000 Msg.WParam := K;
1001 g_ActiveWindow.OnMessage(Msg);
1002 end
1003 else if (gState = STATE_MENU) then
1004 begin
1005 g_GUI_ShowWindow('MainMenu');
1006 g_Sound_PlayEx('MENU_OPEN');
1007 end;
1008 end;
1009 end;
1010 {$ENDIF}
1011 end;
1014 procedure CharPress (C: AnsiChar);
1015 var
1016 Msg: g_gui.TMessage;
1017 a: Integer;
1018 begin
1019 if gConsoleShow or gChatShow then
1020 begin
1021 g_Console_Char(C)
1022 end
1023 else if (g_ActiveWindow <> nil) then
1024 begin
1025 Msg.Msg := WM_CHAR;
1026 Msg.WParam := Ord(C);
1027 g_ActiveWindow.OnMessage(Msg);
1028 end
1029 else
1030 begin
1031 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
1032 charbuff[15] := upcase1251(C);
1033 Cheat();
1034 end;
1035 end;
1037 initialization
1038 {$IFDEF USE_SDLMIXER}
1039 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1040 {$IFDEF DARWIN}
1041 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1042 {$ELSE}
1043 UseNativeMusic := false;
1044 {$ENDIF}
1045 {$ENDIF}
1046 end.