DEADSOFTWARE

fix regresion: holmes with sdl2
[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 procedure Main ();
21 procedure Init ();
22 procedure Release ();
23 procedure Update ();
24 procedure Draw ();
25 procedure KeyPress (K: Word);
26 procedure CharPress (C: AnsiChar);
28 var
29 GameDir: string;
30 DataDir: string;
31 MapsDir: string;
32 ModelsDir: string;
33 GameWAD: string;
34 LogFileName: string;
36 implementation
38 uses
39 {$INCLUDE ../nogl/noGLuses.inc}
40 {$IFDEF ENABLE_HOLMES}
41 g_holmes, sdlcarcass, fui_ctls, fui_wadread, fui_style, fui_gfx_gl,
42 {$ENDIF}
43 wadreader, e_log, g_window,
44 e_graphics, e_input, g_game, g_console, g_gui,
45 e_sound, g_options, g_sound, g_player, g_basic,
46 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
47 g_menu, g_language, g_net, g_touch, g_system, g_res_downloader,
48 utils, conbuf, envvars,
49 xparser;
52 var
53 charbuff: packed array [0..15] of AnsiChar;
55 procedure Main();
56 var
57 sdlflags: LongWord;
58 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
59 flexloaded: Boolean;
60 {$ENDIF}
61 begin
62 e_InitWritelnDriver();
64 GetDir(0, GameDir);
65 MapsDir := GameDir + '/maps/';
66 DataDir := GameDir + '/data/';
67 ModelsDir := DataDir + 'models/';
68 GameWAD := DataDir + 'Game.wad';
70 e_InitLog(GameDir + '/' + LogFileName, TWriteMode.WM_NEWFILE);
72 e_WriteLog(
73 'Doom 2D: Forever version ' + GAME_VERSION +
74 ' proto ' + IntToStr(NET_PROTOCOL_VER),
75 TMsgType.Notify
76 );
77 e_WriteLog(
78 'Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME,
79 TMsgType.Notify
80 );
82 {$IFDEF HEADLESS}
83 conbufDumpToStdOut := true;
84 {$ENDIF}
85 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
87 e_WriteLog('Init Input', TMsgType.Notify);
88 e_InitInput;
90 e_WriteLog('Read config file', TMsgType.Notify);
91 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
92 g_Console_SysInit;
94 //GetSystemDefaultLCID()
96 //e_WriteLog('Read language file', MSG_NOTIFY);
97 //g_Language_Load(DataDir + gLanguage + '.txt');
98 e_WriteLog(gLanguage, TMsgType.Notify);
99 g_Language_Set(gLanguage);
101 sys_Init;
103 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
104 flexloaded := true;
105 if not fuiAddWad('flexui.wad') then
106 begin
107 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
108 end;
109 try
110 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
111 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
112 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
113 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
114 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
115 except on e: Exception do
116 begin
117 writeln('ERROR loading FlexUI fonts');
118 flexloaded := false;
119 //raise;
120 end;
121 else
122 begin
123 flexloaded := false;
124 //raise;
125 end;
126 end;
127 if (flexloaded) then
128 begin
129 try
130 e_LogWriteln('FlexUI: loading stylesheet...');
131 uiLoadStyles('flexui/widgets.wgs');
132 except on e: TParserException do
133 begin
134 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
135 //raise;
136 flexloaded := false;
137 end;
138 else
139 begin
140 //raise;
141 flexloaded := false;
142 end;
143 end;
144 end;
145 g_holmes_imfunctional := not flexloaded;
147 if (not g_holmes_imfunctional) then
148 begin
149 uiInitialize();
150 uiContext.font := 'win14';
151 end;
153 if assigned(oglInitCB) then oglInitCB;
154 {$ENDIF}
156 //g_Res_CreateDatabases(); // it will be done before connecting to the server for the first time
158 e_WriteLog('Entering SDLMain', TMsgType.Notify);
160 {$WARNINGS OFF}
161 SDLMain();
162 {$WARNINGS ON}
164 {$IFDEF ENABLE_HOLMES}
165 if assigned(oglDeinitCB) then oglDeinitCB;
166 {$ENDIF}
168 sys_Final;
169 end;
171 procedure Init();
172 var
173 NoSound: Boolean;
174 begin
175 Randomize;
177 {$IFDEF HEADLESS}
178 {$IFDEF USE_SDLMIXER}
179 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
180 {$ELSE}
181 NoSound := True; // FMOD backend will sort it out
182 {$ENDIF}
183 {$ELSE}
184 NoSound := False;
185 {$ENDIF}
187 g_Touch_Init;
189 (*
190 if (e_JoysticksAvailable > 0) then
191 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
192 else
193 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
194 *)
196 if (not gNoSound) then
197 begin
198 e_WriteLog('Initializing sound system', TMsgType.Notify);
199 e_InitSoundSystem(NoSound);
200 end;
202 e_WriteLog('Init game', TMsgType.Notify);
203 g_Game_Init();
205 FillChar(charbuff, sizeof(charbuff), ' ');
206 end;
209 procedure Release();
210 begin
211 e_WriteLog('Releasing engine', TMsgType.Notify);
212 e_ReleaseEngine();
214 e_WriteLog('Releasing Input', TMsgType.Notify);
215 e_ReleaseInput();
217 if not gNoSound then
218 begin
219 e_WriteLog('Releasing FMOD', TMsgType.Notify);
220 e_ReleaseSoundSystem();
221 end;
222 end;
225 procedure Update ();
226 begin
227 g_Game_Update();
228 end;
231 procedure Draw ();
232 begin
233 g_Game_Draw();
234 end;
237 function Translit (const S: AnsiString): AnsiString;
238 var
239 i: Integer;
240 begin
241 Result := S;
242 for i := 1 to Length(Result) do
243 begin
244 case Result[i] of
245 'É': Result[i] := 'Q';
246 'Ö': Result[i] := 'W';
247 'Ó': Result[i] := 'E';
248 'Ê': Result[i] := 'R';
249 'Å': Result[i] := 'T';
250 'Í': Result[i] := 'Y';
251 'Ã': Result[i] := 'U';
252 'Ø': Result[i] := 'I';
253 'Ù': Result[i] := 'O';
254 'Ç': Result[i] := 'P';
255 'Õ': Result[i] := '['; //Chr(219);
256 'Ú': Result[i] := ']'; //Chr(221);
257 'Ô': Result[i] := 'A';
258 'Û': Result[i] := 'S';
259 'Â': Result[i] := 'D';
260 'À': Result[i] := 'F';
261 'Ï': Result[i] := 'G';
262 'Ð': Result[i] := 'H';
263 'Î': Result[i] := 'J';
264 'Ë': Result[i] := 'K';
265 'Ä': Result[i] := 'L';
266 'Æ': Result[i] := ';'; //Chr(186);
267 'Ý': Result[i] := #39; //Chr(222);
268 'ß': Result[i] := 'Z';
269 '×': Result[i] := 'X';
270 'Ñ': Result[i] := 'C';
271 'Ì': Result[i] := 'V';
272 'È': Result[i] := 'B';
273 'Ò': Result[i] := 'N';
274 'Ü': Result[i] := 'M';
275 'Á': Result[i] := ','; //Chr(188);
276 'Þ': Result[i] := '.'; //Chr(190);
277 end;
278 end;
279 end;
282 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
283 var
284 ls1, ls2: string;
285 begin
286 ls1 := CheatEng[ct];
287 ls2 := Translit(CheatRus[ct]);
288 if length(ls1) = 0 then ls1 := '~';
289 if length(ls2) = 0 then ls2 := '~';
290 result :=
291 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
292 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
293 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
294 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
296 if ct = I_GAME_CHEAT_JETPACK then
297 begin
298 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
299 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
300 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
301 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
302 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
303 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
304 end;
306 end;
309 procedure Cheat ();
310 const
311 CHEAT_DAMAGE = 500;
312 label
313 Cheated;
314 var
315 s, s2: string;
316 c: ShortString;
317 a: Integer;
318 begin
320 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
321 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
322 or g_Game_IsNet then Exit;
324 if not gGameOn then exit;
325 if not conIsCheatsEnabled then exit;
327 s := 'SOUND_GAME_RADIO';
329 //
330 if CheckCheat(I_GAME_CHEAT_GODMODE) then
331 begin
332 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
333 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
334 goto Cheated;
335 end;
336 // RAMBO
337 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
338 begin
339 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
340 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
341 goto Cheated;
342 end;
343 // TANK
344 if CheckCheat(I_GAME_CHEAT_HEALTH) then
345 begin
346 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
347 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
348 goto Cheated;
349 end;
350 // IDDQD
351 if CheckCheat(I_GAME_CHEAT_DEATH) then
352 begin
353 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
354 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
355 s := 'SOUND_MONSTER_HAHA';
356 goto Cheated;
357 end;
358 //
359 if CheckCheat(I_GAME_CHEAT_DOORS) then
360 begin
361 g_Triggers_OpenAll();
362 goto Cheated;
363 end;
364 // GOODBYE
365 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
366 begin
367 if gTriggers <> nil then
368 for a := 0 to High(gTriggers) do
369 if gTriggers[a].TriggerType = TRIGGER_EXIT then
370 begin
371 gExitByTrigger := True;
372 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
373 g_Game_ExitLevel(gTriggers[a].tgcMap);
374 Break;
375 end;
376 goto Cheated;
377 end;
378 //
379 s2 := Copy(charbuff, 15, 2);
380 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
381 begin
382 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
383 begin
384 c := 'MAP'+s2;
385 g_Game_ExitLevel(c);
386 end;
387 goto Cheated;
388 end;
389 //
390 if CheckCheat(I_GAME_CHEAT_FLY) then
391 begin
392 gFly := not gFly;
393 goto Cheated;
394 end;
395 // BULLFROG
396 if CheckCheat(I_GAME_CHEAT_JUMPS) then
397 begin
398 VEL_JUMP := 30-VEL_JUMP;
399 goto Cheated;
400 end;
401 // FORMULA1
402 if CheckCheat(I_GAME_CHEAT_SPEED) then
403 begin
404 MAX_RUNVEL := 32-MAX_RUNVEL;
405 goto Cheated;
406 end;
407 // CONDOM
408 if CheckCheat(I_GAME_CHEAT_SUIT) then
409 begin
410 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
411 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
412 goto Cheated;
413 end;
414 //
415 if CheckCheat(I_GAME_CHEAT_AIR) then
416 begin
417 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
418 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
419 goto Cheated;
420 end;
421 // PURELOVE
422 if CheckCheat(I_GAME_CHEAT_BERSERK) then
423 begin
424 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
425 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
426 goto Cheated;
427 end;
428 //
429 if CheckCheat(I_GAME_CHEAT_JETPACK) then
430 begin
431 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
432 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
433 goto Cheated;
434 end;
435 // CASPER
436 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
437 begin
438 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
439 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
440 goto Cheated;
441 end;
442 //
443 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
444 begin
445 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
446 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
447 goto Cheated;
448 end;
449 // INFERNO
450 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
451 begin
452 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
453 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
454 goto Cheated;
455 end;
456 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
457 begin
458 gAimLine := not gAimLine;
459 goto Cheated;
460 end;
461 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
462 begin
463 gShowMap := not gShowMap;
464 goto Cheated;
465 end;
466 Exit;
468 Cheated:
469 g_Sound_PlayEx(s);
470 end;
473 procedure KeyPress (K: Word);
474 {$IFNDEF HEADLESS}
475 var
476 Msg: g_gui.TMessage;
477 {$ENDIF}
478 begin
479 {$IFNDEF HEADLESS}
480 case K of
481 VK_ESCAPE: // <Esc>:
482 begin
483 if (g_ActiveWindow <> nil) then
484 begin
485 Msg.Msg := WM_KEYDOWN;
486 Msg.WParam := VK_ESCAPE;
487 g_ActiveWindow.OnMessage(Msg);
488 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
489 end
490 else if (gState <> STATE_FOLD) then
491 begin
492 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
493 begin
494 g_Game_InGameMenu(True);
495 end
496 else if (gExit = 0) and (gState <> STATE_SLIST) then
497 begin
498 if (gState <> STATE_MENU) then
499 begin
500 if (NetMode <> NET_NONE) then
501 begin
502 g_Game_StopAllSounds(True);
503 g_Game_Free;
504 gState := STATE_MENU;
505 Exit;
506 end;
507 end;
508 g_GUI_ShowWindow('MainMenu');
509 g_Sound_PlayEx('MENU_OPEN');
510 end;
511 end;
512 end;
514 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
515 begin // <F2> .. <F6> � <F12>
516 if gGameOn and (not gConsoleShow) and (not gChatShow) then
517 begin
518 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
519 if (not g_Game_IsNet) then g_Game_Pause(True);
520 case K of
521 IK_F2: g_Menu_Show_SaveMenu();
522 IK_F3: g_Menu_Show_LoadMenu();
523 IK_F4: g_Menu_Show_GameSetGame();
524 IK_F5: g_Menu_Show_OptionsVideo();
525 IK_F6: g_Menu_Show_OptionsSound();
526 IK_F7: g_Menu_Show_EndGameMenu();
527 IK_F10: g_Menu_Show_QuitGameMenu();
528 end;
529 end;
530 end;
532 else
533 begin
534 gJustChatted := False;
535 if gConsoleShow or gChatShow then
536 begin
537 g_Console_Control(K);
538 end
539 else if (g_ActiveWindow <> nil) then
540 begin
541 Msg.Msg := WM_KEYDOWN;
542 Msg.WParam := K;
543 g_ActiveWindow.OnMessage(Msg);
544 end
545 else if (gState = STATE_MENU) then
546 begin
547 g_GUI_ShowWindow('MainMenu');
548 g_Sound_PlayEx('MENU_OPEN');
549 end;
550 end;
551 end;
552 {$ENDIF}
553 end;
556 procedure CharPress (C: AnsiChar);
557 var
558 Msg: g_gui.TMessage;
559 a: Integer;
560 begin
561 if gConsoleShow or gChatShow then
562 begin
563 g_Console_Char(C)
564 end
565 else if (g_ActiveWindow <> nil) then
566 begin
567 Msg.Msg := WM_CHAR;
568 Msg.WParam := Ord(C);
569 g_ActiveWindow.OnMessage(Msg);
570 end
571 else
572 begin
573 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
574 charbuff[15] := upcase1251(C);
575 Cheat();
576 end;
577 end;
580 end.