DEADSOFTWARE

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