DEADSOFTWARE

game/FlexUI: textinput events
[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,
45 utils, conbuf, envvars;
48 var
49 charbuff: packed array [0..15] of AnsiChar;
51 procedure Main();
52 var
53 sdlflags: LongWord;
54 begin
55 e_InitWritelnDriver();
57 GetDir(0, GameDir);
58 MapsDir := GameDir + '/maps/';
59 DataDir := GameDir + '/data/';
60 ModelsDir := DataDir + 'models/';
61 GameWAD := DataDir + 'Game.wad';
63 e_InitLog(GameDir + '/' + LOG_FILENAME, TWriteMode.WM_NEWFILE);
65 e_WriteLog('Read config file', TMsgType.Notify);
66 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
68 {$IFDEF HEADLESS}
69 conbufDumpToStdOut := true;
70 {$ENDIF}
71 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
73 //GetSystemDefaultLCID()
75 //e_WriteLog('Read language file', MSG_NOTIFY);
76 //g_Language_Load(DataDir + gLanguage + '.txt');
77 e_WriteLog(gLanguage, TMsgType.Notify);
78 g_Language_Set(gLanguage);
80 {$IFDEF HEADLESS}
81 {$IFDEF USE_SDLMIXER}
82 sdlflags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
83 // HACK: shit this into env and hope for the best
84 SetEnvVar('SDL_AUDIODRIVER', 'dummy');
85 {$ELSE}
86 sdlflags := SDL_INIT_TIMER or $00004000;
87 {$ENDIF}
88 {$ELSE}
89 {$IFDEF USE_SDLMIXER}
90 sdlflags := SDL_INIT_EVERYTHING;
91 {$ELSE}
92 sdlflags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
93 {$ENDIF}
94 {$ENDIF}
95 if SDL_Init(sdlflags) < 0 then
96 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
98 {$IFNDEF HEADLESS}
99 SDL_StartTextInput();
100 {$ENDIF}
102 e_WriteLog('Entering SDLMain', TMsgType.Notify);
104 {$WARNINGS OFF}
105 SDLMain();
106 {$WARNINGS ON}
108 {$IFDEF HEADLESS}
109 SDL_StopTextInput();
110 {$ENDIF}
112 e_WriteLog('Releasing SDL', TMsgType.Notify);
113 SDL_Quit();
114 end;
116 procedure Init();
117 var
118 NoSound: Boolean;
119 begin
120 Randomize;
122 {$IFDEF HEADLESS}
123 {$IFDEF USE_SDLMIXER}
124 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
125 {$ELSE}
126 NoSound := True; // FMOD backend will sort it out
127 {$ENDIF}
128 {$ELSE}
129 NoSound := False;
130 {$ENDIF}
132 e_WriteLog('Init Input', TMsgType.Notify);
133 e_InitInput();
135 if (e_JoysticksAvailable > 0) then
136 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
137 else
138 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
140 if (not gNoSound) then
141 begin
142 e_WriteLog('Initializing sound system', TMsgType.Notify);
143 e_InitSoundSystem(NoSound);
144 end;
146 e_WriteLog('Init game', TMsgType.Notify);
147 g_Game_Init();
149 FillChar(charbuff, sizeof(charbuff), ' ');
150 end;
153 procedure Release();
154 begin
155 e_WriteLog('Releasing engine', TMsgType.Notify);
156 e_ReleaseEngine();
158 e_WriteLog('Releasing Input', TMsgType.Notify);
159 e_ReleaseInput();
161 if not gNoSound then
162 begin
163 e_WriteLog('Releasing FMOD', TMsgType.Notify);
164 e_ReleaseSoundSystem();
165 end;
166 end;
169 procedure Update ();
170 begin
171 g_Game_Update();
172 end;
175 procedure Draw ();
176 begin
177 g_Game_Draw();
178 end;
181 function Translit (const S: AnsiString): AnsiString;
182 var
183 i: Integer;
184 begin
185 Result := S;
186 for i := 1 to Length(Result) do
187 begin
188 case Result[i] of
189 'É': Result[i] := 'Q';
190 'Ö': Result[i] := 'W';
191 'Ó': Result[i] := 'E';
192 'Ê': Result[i] := 'R';
193 'Å': Result[i] := 'T';
194 'Í': Result[i] := 'Y';
195 'Ã': Result[i] := 'U';
196 'Ø': Result[i] := 'I';
197 'Ù': Result[i] := 'O';
198 'Ç': Result[i] := 'P';
199 'Õ': Result[i] := '['; //Chr(219);
200 'Ú': Result[i] := ']'; //Chr(221);
201 'Ô': Result[i] := 'A';
202 'Û': Result[i] := 'S';
203 'Â': Result[i] := 'D';
204 'À': Result[i] := 'F';
205 'Ï': Result[i] := 'G';
206 'Ð': Result[i] := 'H';
207 'Î': Result[i] := 'J';
208 'Ë': Result[i] := 'K';
209 'Ä': Result[i] := 'L';
210 'Æ': Result[i] := ';'; //Chr(186);
211 'Ý': Result[i] := #39; //Chr(222);
212 'ß': Result[i] := 'Z';
213 '×': Result[i] := 'X';
214 'Ñ': Result[i] := 'C';
215 'Ì': Result[i] := 'V';
216 'È': Result[i] := 'B';
217 'Ò': Result[i] := 'N';
218 'Ü': Result[i] := 'M';
219 'Á': Result[i] := ','; //Chr(188);
220 'Þ': Result[i] := '.'; //Chr(190);
221 end;
222 end;
223 end;
226 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
227 var
228 ls1, ls2: string;
229 begin
230 ls1 := CheatEng[ct];
231 ls2 := Translit(CheatRus[ct]);
232 if length(ls1) = 0 then ls1 := '~';
233 if length(ls2) = 0 then ls2 := '~';
234 result :=
235 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
236 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
237 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
238 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
240 if ct = I_GAME_CHEAT_JETPACK then
241 begin
242 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
243 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
244 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
245 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
246 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
247 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
248 end;
250 end;
253 procedure Cheat ();
254 const
255 CHEAT_DAMAGE = 500;
256 label
257 Cheated;
258 var
259 s, s2: string;
260 c: ShortString;
261 a: Integer;
262 begin
264 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
265 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
266 or g_Game_IsNet then Exit;
268 if not gGameOn then exit;
269 if not conIsCheatsEnabled then exit;
271 s := 'SOUND_GAME_RADIO';
273 //
274 if CheckCheat(I_GAME_CHEAT_GODMODE) then
275 begin
276 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
277 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
278 goto Cheated;
279 end;
280 // RAMBO
281 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
282 begin
283 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
284 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
285 goto Cheated;
286 end;
287 // TANK
288 if CheckCheat(I_GAME_CHEAT_HEALTH) then
289 begin
290 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
291 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
292 goto Cheated;
293 end;
294 // IDDQD
295 if CheckCheat(I_GAME_CHEAT_DEATH) then
296 begin
297 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
298 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
299 s := 'SOUND_MONSTER_HAHA';
300 goto Cheated;
301 end;
302 //
303 if CheckCheat(I_GAME_CHEAT_DOORS) then
304 begin
305 g_Triggers_OpenAll();
306 goto Cheated;
307 end;
308 // GOODBYE
309 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
310 begin
311 if gTriggers <> nil then
312 for a := 0 to High(gTriggers) do
313 if gTriggers[a].TriggerType = TRIGGER_EXIT then
314 begin
315 gExitByTrigger := True;
316 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
317 g_Game_ExitLevel(gTriggers[a].tgcMap);
318 Break;
319 end;
320 goto Cheated;
321 end;
322 //
323 s2 := Copy(charbuff, 15, 2);
324 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
325 begin
326 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
327 begin
328 c := 'MAP00';
329 c[3] := s2[1];
330 c[4] := s2[2];
331 g_Game_ExitLevel(c);
332 end;
333 goto Cheated;
334 end;
335 //
336 if CheckCheat(I_GAME_CHEAT_FLY) then
337 begin
338 gFly := not gFly;
339 goto Cheated;
340 end;
341 // BULLFROG
342 if CheckCheat(I_GAME_CHEAT_JUMPS) then
343 begin
344 VEL_JUMP := 30-VEL_JUMP;
345 goto Cheated;
346 end;
347 // FORMULA1
348 if CheckCheat(I_GAME_CHEAT_SPEED) then
349 begin
350 MAX_RUNVEL := 32-MAX_RUNVEL;
351 goto Cheated;
352 end;
353 // CONDOM
354 if CheckCheat(I_GAME_CHEAT_SUIT) then
355 begin
356 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
357 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
358 goto Cheated;
359 end;
360 //
361 if CheckCheat(I_GAME_CHEAT_AIR) then
362 begin
363 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
364 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
365 goto Cheated;
366 end;
367 // PURELOVE
368 if CheckCheat(I_GAME_CHEAT_BERSERK) then
369 begin
370 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
371 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
372 goto Cheated;
373 end;
374 //
375 if CheckCheat(I_GAME_CHEAT_JETPACK) then
376 begin
377 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
378 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
379 goto Cheated;
380 end;
381 // CASPER
382 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
383 begin
384 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
385 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
386 goto Cheated;
387 end;
388 //
389 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
390 begin
391 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
392 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
393 goto Cheated;
394 end;
395 // INFERNO
396 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
397 begin
398 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
399 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
400 goto Cheated;
401 end;
402 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
403 begin
404 gAimLine := not gAimLine;
405 goto Cheated;
406 end;
407 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
408 begin
409 gShowMap := not gShowMap;
410 goto Cheated;
411 end;
412 Exit;
414 Cheated:
415 g_Sound_PlayEx(s);
416 end;
419 procedure KeyPress (K: Word);
420 var
421 Msg: g_gui.TMessage;
422 begin
423 case K of
424 IK_PAUSE: // <Pause/Break>:
425 begin
426 if (g_ActiveWindow = nil) then g_Game_Pause(not gPause);
427 end;
429 IK_BACKQUOTE: // <`/~/¨/¸>:
430 begin
431 g_Console_Switch();
432 end;
434 IK_ESCAPE: // <Esc>:
435 begin
436 if gChatShow then
437 begin
438 g_Console_Chat_Switch();
439 Exit;
440 end;
442 if gConsoleShow then
443 begin
444 g_Console_Switch();
445 end
446 else if (g_ActiveWindow <> nil) then
447 begin
448 Msg.Msg := WM_KEYDOWN;
449 Msg.WParam := IK_ESCAPE;
450 g_ActiveWindow.OnMessage(Msg);
451 end
452 else if (gState <> STATE_FOLD) then
453 begin
454 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
455 begin
456 g_Game_InGameMenu(True);
457 end
458 else if (gExit = 0) and (gState <> STATE_SLIST) then
459 begin
460 if (gState <> STATE_MENU) then
461 begin
462 if (NetMode <> NET_NONE) then
463 begin
464 g_Game_StopAllSounds(True);
465 g_Game_Free;
466 gState := STATE_MENU;
467 Exit;
468 end;
469 end;
470 g_GUI_ShowWindow('MainMenu');
471 g_Sound_PlayEx('MENU_OPEN');
472 end;
473 end;
474 end;
476 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
477 begin // <F2> .. <F6> � <F12>
478 if gGameOn and (not gConsoleShow) and (not gChatShow) then
479 begin
480 while g_ActiveWindow <> nil do g_GUI_HideWindow(False);
481 if (not g_Game_IsNet) then g_Game_Pause(True);
482 case K of
483 IK_F2: g_Menu_Show_SaveMenu();
484 IK_F3: g_Menu_Show_LoadMenu();
485 IK_F4: g_Menu_Show_GameSetGame();
486 IK_F5: g_Menu_Show_OptionsVideo();
487 IK_F6: g_Menu_Show_OptionsSound();
488 IK_F7: g_Menu_Show_EndGameMenu();
489 IK_F10: g_Menu_Show_QuitGameMenu();
490 end;
491 end;
492 end;
494 else
495 begin
496 gJustChatted := False;
497 if gConsoleShow or gChatShow then
498 begin
499 g_Console_Control(K);
500 end
501 else if (g_ActiveWindow <> nil) then
502 begin
503 Msg.Msg := WM_KEYDOWN;
504 Msg.WParam := K;
505 g_ActiveWindow.OnMessage(Msg);
506 end
507 else if (gState = STATE_MENU) then
508 begin
509 g_GUI_ShowWindow('MainMenu');
510 g_Sound_PlayEx('MENU_OPEN');
511 end;
512 end;
513 end;
514 end;
517 procedure CharPress (C: AnsiChar);
518 var
519 Msg: g_gui.TMessage;
520 a: Integer;
521 begin
522 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then Exit;
524 if gConsoleShow or gChatShow then
525 begin
526 g_Console_Char(C);
527 end
528 else if (g_ActiveWindow <> nil) then
529 begin
530 Msg.Msg := WM_CHAR;
531 Msg.WParam := Ord(C);
532 g_ActiveWindow.OnMessage(Msg);
533 end
534 else
535 begin
536 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
537 charbuff[15] := upcase1251(C);
538 Cheat();
539 end;
540 end;
543 end.