DEADSOFTWARE

fixed unneded pause on some Fn
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_main;
19 interface
21 procedure Main ();
22 procedure Init ();
23 procedure Release ();
24 procedure Update ();
25 procedure Draw ();
26 procedure KeyPress (K: Word);
27 procedure CharPress (C: AnsiChar);
29 var
30 GameDir: string;
31 DataDir: string;
32 MapsDir: string;
33 ModelsDir: string;
34 GameWAD: string;
37 implementation
39 uses
40 SDL2, GL, GLExt, wadreader, e_log, g_window,
41 e_graphics, e_input, g_game, g_console, g_gui,
42 e_sound, g_options, g_sound, g_player,
43 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
44 g_menu, g_language, g_net, g_holmes,
45 utils, conbuf, envvars, fui_wadread, fui_style,
46 fui_gfx_gl, xparser;
49 var
50 charbuff: packed array [0..15] of AnsiChar;
52 procedure Main();
53 var
54 sdlflags: LongWord;
55 begin
56 e_InitWritelnDriver();
58 GetDir(0, GameDir);
59 MapsDir := GameDir + '/maps/';
60 DataDir := GameDir + '/data/';
61 ModelsDir := DataDir + 'models/';
62 GameWAD := DataDir + 'Game.wad';
64 e_InitLog(GameDir + '/' + LOG_FILENAME, TWriteMode.WM_NEWFILE);
66 e_WriteLog('Read config file', TMsgType.Notify);
67 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
69 {$IFDEF HEADLESS}
70 conbufDumpToStdOut := true;
71 {$ENDIF}
72 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
74 //GetSystemDefaultLCID()
76 //e_WriteLog('Read language file', MSG_NOTIFY);
77 //g_Language_Load(DataDir + gLanguage + '.txt');
78 e_WriteLog(gLanguage, TMsgType.Notify);
79 g_Language_Set(gLanguage);
81 {$IFDEF HEADLESS}
82 {$IFDEF USE_SDLMIXER}
83 sdlflags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
84 // HACK: shit this into env and hope for the best
85 SetEnvVar('SDL_AUDIODRIVER', 'dummy');
86 {$ELSE}
87 sdlflags := SDL_INIT_TIMER or $00004000;
88 {$ENDIF}
89 {$ELSE}
90 {$IFDEF USE_SDLMIXER}
91 sdlflags := SDL_INIT_EVERYTHING;
92 {$ELSE}
93 sdlflags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
94 {$ENDIF}
95 {$ENDIF}
96 if SDL_Init(sdlflags) < 0 then
97 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
99 {$IFNDEF HEADLESS}
100 SDL_StartTextInput();
101 {$ENDIF}
103 {$IFNDEF HEADLESS}
104 if not fuiAddWad('flexui.wad') then
105 begin
106 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
107 end;
108 g_holmes_imfunctional := true;
109 try
110 e_LogWriteln('FlexUI: loading stylesheet...');
111 uiLoadStyles('flexui/widgets.wgs');
112 except on e: TParserException do
113 begin
114 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
115 //raise;
116 end;
117 else
118 begin
119 //raise;
120 end;
121 end;
122 try
123 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
124 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
125 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
126 g_holmes_imfunctional := false;
127 except on e: Exception do
128 begin
129 writeln('ERROR loading FlexUI fonts');
130 //raise;
131 end;
132 else
133 begin
134 //raise;
135 end;
136 end;
137 {$ENDIF}
139 e_WriteLog('Entering SDLMain', TMsgType.Notify);
141 {$WARNINGS OFF}
142 SDLMain();
143 {$WARNINGS ON}
145 {$IFNDEF HEADLESS}
146 SDL_StopTextInput();
147 {$ENDIF}
149 e_WriteLog('Releasing SDL', TMsgType.Notify);
150 SDL_Quit();
151 end;
153 procedure Init();
154 var
155 NoSound: Boolean;
156 begin
157 Randomize;
159 {$IFDEF HEADLESS}
160 {$IFDEF USE_SDLMIXER}
161 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
162 {$ELSE}
163 NoSound := True; // FMOD backend will sort it out
164 {$ENDIF}
165 {$ELSE}
166 NoSound := False;
167 {$ENDIF}
169 e_WriteLog('Init Input', TMsgType.Notify);
170 e_InitInput();
172 if (e_JoysticksAvailable > 0) then
173 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
174 else
175 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
177 if (not gNoSound) then
178 begin
179 e_WriteLog('Initializing sound system', TMsgType.Notify);
180 e_InitSoundSystem(NoSound);
181 end;
183 e_WriteLog('Init game', TMsgType.Notify);
184 g_Game_Init();
186 FillChar(charbuff, sizeof(charbuff), ' ');
187 end;
190 procedure Release();
191 begin
192 e_WriteLog('Releasing engine', TMsgType.Notify);
193 e_ReleaseEngine();
195 e_WriteLog('Releasing Input', TMsgType.Notify);
196 e_ReleaseInput();
198 if not gNoSound then
199 begin
200 e_WriteLog('Releasing FMOD', TMsgType.Notify);
201 e_ReleaseSoundSystem();
202 end;
203 end;
206 procedure Update ();
207 begin
208 g_Game_Update();
209 end;
212 procedure Draw ();
213 begin
214 g_Game_Draw();
215 end;
218 function Translit (const S: AnsiString): AnsiString;
219 var
220 i: Integer;
221 begin
222 Result := S;
223 for i := 1 to Length(Result) do
224 begin
225 case Result[i] of
226 'É': Result[i] := 'Q';
227 'Ö': Result[i] := 'W';
228 'Ó': Result[i] := 'E';
229 'Ê': Result[i] := 'R';
230 'Å': Result[i] := 'T';
231 'Í': Result[i] := 'Y';
232 'Ã': Result[i] := 'U';
233 'Ø': Result[i] := 'I';
234 'Ù': Result[i] := 'O';
235 'Ç': Result[i] := 'P';
236 'Õ': Result[i] := '['; //Chr(219);
237 'Ú': Result[i] := ']'; //Chr(221);
238 'Ô': Result[i] := 'A';
239 'Û': Result[i] := 'S';
240 'Â': Result[i] := 'D';
241 'À': Result[i] := 'F';
242 'Ï': Result[i] := 'G';
243 'Ð': Result[i] := 'H';
244 'Î': Result[i] := 'J';
245 'Ë': Result[i] := 'K';
246 'Ä': Result[i] := 'L';
247 'Æ': Result[i] := ';'; //Chr(186);
248 'Ý': Result[i] := #39; //Chr(222);
249 'ß': Result[i] := 'Z';
250 '×': Result[i] := 'X';
251 'Ñ': Result[i] := 'C';
252 'Ì': Result[i] := 'V';
253 'È': Result[i] := 'B';
254 'Ò': Result[i] := 'N';
255 'Ü': Result[i] := 'M';
256 'Á': Result[i] := ','; //Chr(188);
257 'Þ': Result[i] := '.'; //Chr(190);
258 end;
259 end;
260 end;
263 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
264 var
265 ls1, ls2: string;
266 begin
267 ls1 := CheatEng[ct];
268 ls2 := Translit(CheatRus[ct]);
269 if length(ls1) = 0 then ls1 := '~';
270 if length(ls2) = 0 then ls2 := '~';
271 result :=
272 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
273 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
274 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
275 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
277 if ct = I_GAME_CHEAT_JETPACK then
278 begin
279 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
280 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
281 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
282 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
283 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
284 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
285 end;
287 end;
290 procedure Cheat ();
291 const
292 CHEAT_DAMAGE = 500;
293 label
294 Cheated;
295 var
296 s, s2: string;
297 c: ShortString;
298 a: Integer;
299 begin
301 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
302 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
303 or g_Game_IsNet then Exit;
305 if not gGameOn then exit;
306 if not conIsCheatsEnabled then exit;
308 s := 'SOUND_GAME_RADIO';
310 //
311 if CheckCheat(I_GAME_CHEAT_GODMODE) then
312 begin
313 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
314 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
315 goto Cheated;
316 end;
317 // RAMBO
318 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
319 begin
320 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
321 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
322 goto Cheated;
323 end;
324 // TANK
325 if CheckCheat(I_GAME_CHEAT_HEALTH) then
326 begin
327 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
328 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
329 goto Cheated;
330 end;
331 // IDDQD
332 if CheckCheat(I_GAME_CHEAT_DEATH) then
333 begin
334 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
335 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
336 s := 'SOUND_MONSTER_HAHA';
337 goto Cheated;
338 end;
339 //
340 if CheckCheat(I_GAME_CHEAT_DOORS) then
341 begin
342 g_Triggers_OpenAll();
343 goto Cheated;
344 end;
345 // GOODBYE
346 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
347 begin
348 if gTriggers <> nil then
349 for a := 0 to High(gTriggers) do
350 if gTriggers[a].TriggerType = TRIGGER_EXIT then
351 begin
352 gExitByTrigger := True;
353 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
354 g_Game_ExitLevel(gTriggers[a].tgcMap);
355 Break;
356 end;
357 goto Cheated;
358 end;
359 //
360 s2 := Copy(charbuff, 15, 2);
361 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
362 begin
363 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
364 begin
365 c := 'MAP00';
366 c[3] := s2[1];
367 c[4] := s2[2];
368 g_Game_ExitLevel(c);
369 end;
370 goto Cheated;
371 end;
372 //
373 if CheckCheat(I_GAME_CHEAT_FLY) then
374 begin
375 gFly := not gFly;
376 goto Cheated;
377 end;
378 // BULLFROG
379 if CheckCheat(I_GAME_CHEAT_JUMPS) then
380 begin
381 VEL_JUMP := 30-VEL_JUMP;
382 goto Cheated;
383 end;
384 // FORMULA1
385 if CheckCheat(I_GAME_CHEAT_SPEED) then
386 begin
387 MAX_RUNVEL := 32-MAX_RUNVEL;
388 goto Cheated;
389 end;
390 // CONDOM
391 if CheckCheat(I_GAME_CHEAT_SUIT) then
392 begin
393 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
394 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
395 goto Cheated;
396 end;
397 //
398 if CheckCheat(I_GAME_CHEAT_AIR) then
399 begin
400 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
401 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
402 goto Cheated;
403 end;
404 // PURELOVE
405 if CheckCheat(I_GAME_CHEAT_BERSERK) then
406 begin
407 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
408 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
409 goto Cheated;
410 end;
411 //
412 if CheckCheat(I_GAME_CHEAT_JETPACK) then
413 begin
414 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
415 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
416 goto Cheated;
417 end;
418 // CASPER
419 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
420 begin
421 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
422 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
423 goto Cheated;
424 end;
425 //
426 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
427 begin
428 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
429 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
430 goto Cheated;
431 end;
432 // INFERNO
433 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
434 begin
435 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
436 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
437 goto Cheated;
438 end;
439 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
440 begin
441 gAimLine := not gAimLine;
442 goto Cheated;
443 end;
444 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
445 begin
446 gShowMap := not gShowMap;
447 goto Cheated;
448 end;
449 Exit;
451 Cheated:
452 g_Sound_PlayEx(s);
453 end;
456 procedure KeyPress (K: Word);
457 var
458 Msg: g_gui.TMessage;
459 begin
460 case K of
461 IK_PAUSE: // <Pause/Break>:
462 begin
463 if (g_ActiveWindow = nil) then g_Game_Pause(not gPause);
464 end;
466 IK_BACKQUOTE: // <`/~/¨/¸>:
467 begin
468 g_Console_Switch();
469 end;
471 IK_ESCAPE: // <Esc>:
472 begin
473 if gChatShow then
474 begin
475 g_Console_Chat_Switch();
476 Exit;
477 end;
479 if gConsoleShow then
480 begin
481 g_Console_Switch();
482 end
483 else if (g_ActiveWindow <> nil) then
484 begin
485 Msg.Msg := WM_KEYDOWN;
486 Msg.WParam := IK_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 end;
555 procedure CharPress (C: AnsiChar);
556 var
557 Msg: g_gui.TMessage;
558 a: Integer;
559 begin
560 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then Exit;
562 if gConsoleShow or gChatShow then
563 begin
564 g_Console_Char(C);
565 end
566 else if (g_ActiveWindow <> nil) then
567 begin
568 Msg.Msg := WM_CHAR;
569 Msg.WParam := Ord(C);
570 g_ActiveWindow.OnMessage(Msg);
571 end
572 else
573 begin
574 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
575 charbuff[15] := upcase1251(C);
576 Cheat();
577 end;
578 end;
581 end.