DEADSOFTWARE

Added touchscreen settings
[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, g_touch,
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}
116 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
118 if SDL_Init(sdlflags) < 0 then
119 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
121 {$IFNDEF HEADLESS}
122 {$IFNDEF ANDROID}
123 SDL_StartTextInput();
124 {$ENDIF}
125 {$ENDIF}
127 {$IFNDEF HEADLESS}
128 flexloaded := true;
129 if not fuiAddWad('flexui.wad') then
130 begin
131 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
132 end;
133 try
134 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
135 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
136 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
137 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
138 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
139 except on e: Exception do
140 begin
141 writeln('ERROR loading FlexUI fonts');
142 flexloaded := false;
143 //raise;
144 end;
145 else
146 begin
147 flexloaded := false;
148 //raise;
149 end;
150 end;
151 if (flexloaded) then
152 begin
153 try
154 e_LogWriteln('FlexUI: loading stylesheet...');
155 uiLoadStyles('flexui/widgets.wgs');
156 except on e: TParserException do
157 begin
158 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
159 //raise;
160 flexloaded := false;
161 end;
162 else
163 begin
164 //raise;
165 flexloaded := false;
166 end;
167 end;
168 end;
169 g_holmes_imfunctional := not flexloaded;
170 {$ENDIF}
172 e_WriteLog('Entering SDLMain', TMsgType.Notify);
174 {$WARNINGS OFF}
175 SDLMain();
176 {$WARNINGS ON}
178 {$IFNDEF HEADLESS}
179 SDL_StopTextInput();
180 {$ENDIF}
182 e_WriteLog('Releasing SDL', TMsgType.Notify);
183 SDL_Quit();
184 end;
186 procedure Init();
187 var
188 NoSound: Boolean;
189 begin
190 Randomize;
192 {$IFDEF HEADLESS}
193 {$IFDEF USE_SDLMIXER}
194 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
195 {$ELSE}
196 NoSound := True; // FMOD backend will sort it out
197 {$ENDIF}
198 {$ELSE}
199 NoSound := False;
200 {$ENDIF}
202 e_WriteLog('Init Input', TMsgType.Notify);
203 e_InitInput();
204 g_Touch_Init;
206 if (e_JoysticksAvailable > 0) then
207 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
208 else
209 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
211 if (not gNoSound) then
212 begin
213 e_WriteLog('Initializing sound system', TMsgType.Notify);
214 e_InitSoundSystem(NoSound);
215 end;
217 e_WriteLog('Init game', TMsgType.Notify);
218 g_Game_Init();
220 FillChar(charbuff, sizeof(charbuff), ' ');
221 end;
224 procedure Release();
225 begin
226 e_WriteLog('Releasing engine', TMsgType.Notify);
227 e_ReleaseEngine();
229 e_WriteLog('Releasing Input', TMsgType.Notify);
230 e_ReleaseInput();
232 if not gNoSound then
233 begin
234 e_WriteLog('Releasing FMOD', TMsgType.Notify);
235 e_ReleaseSoundSystem();
236 end;
237 end;
240 procedure Update ();
241 begin
242 g_Game_Update();
243 end;
246 procedure Draw ();
247 begin
248 g_Game_Draw();
249 end;
252 function Translit (const S: AnsiString): AnsiString;
253 var
254 i: Integer;
255 begin
256 Result := S;
257 for i := 1 to Length(Result) do
258 begin
259 case Result[i] of
260 'É': Result[i] := 'Q';
261 'Ö': Result[i] := 'W';
262 'Ó': Result[i] := 'E';
263 'Ê': Result[i] := 'R';
264 'Å': Result[i] := 'T';
265 'Í': Result[i] := 'Y';
266 'Ã': Result[i] := 'U';
267 'Ø': Result[i] := 'I';
268 'Ù': Result[i] := 'O';
269 'Ç': Result[i] := 'P';
270 'Õ': Result[i] := '['; //Chr(219);
271 'Ú': Result[i] := ']'; //Chr(221);
272 'Ô': Result[i] := 'A';
273 'Û': Result[i] := 'S';
274 'Â': Result[i] := 'D';
275 'À': Result[i] := 'F';
276 'Ï': Result[i] := 'G';
277 'Ð': Result[i] := 'H';
278 'Î': Result[i] := 'J';
279 'Ë': Result[i] := 'K';
280 'Ä': Result[i] := 'L';
281 'Æ': Result[i] := ';'; //Chr(186);
282 'Ý': Result[i] := #39; //Chr(222);
283 'ß': Result[i] := 'Z';
284 '×': Result[i] := 'X';
285 'Ñ': Result[i] := 'C';
286 'Ì': Result[i] := 'V';
287 'È': Result[i] := 'B';
288 'Ò': Result[i] := 'N';
289 'Ü': Result[i] := 'M';
290 'Á': Result[i] := ','; //Chr(188);
291 'Þ': Result[i] := '.'; //Chr(190);
292 end;
293 end;
294 end;
297 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
298 var
299 ls1, ls2: string;
300 begin
301 ls1 := CheatEng[ct];
302 ls2 := Translit(CheatRus[ct]);
303 if length(ls1) = 0 then ls1 := '~';
304 if length(ls2) = 0 then ls2 := '~';
305 result :=
306 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
307 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
308 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
309 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
311 if ct = I_GAME_CHEAT_JETPACK then
312 begin
313 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
314 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
315 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
316 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
317 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
318 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
319 end;
321 end;
324 procedure Cheat ();
325 const
326 CHEAT_DAMAGE = 500;
327 label
328 Cheated;
329 var
330 s, s2: string;
331 c: ShortString;
332 a: Integer;
333 begin
335 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
336 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
337 or g_Game_IsNet then Exit;
339 if not gGameOn then exit;
340 if not conIsCheatsEnabled then exit;
342 s := 'SOUND_GAME_RADIO';
344 //
345 if CheckCheat(I_GAME_CHEAT_GODMODE) then
346 begin
347 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
348 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
349 goto Cheated;
350 end;
351 // RAMBO
352 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
353 begin
354 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
355 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
356 goto Cheated;
357 end;
358 // TANK
359 if CheckCheat(I_GAME_CHEAT_HEALTH) then
360 begin
361 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
362 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
363 goto Cheated;
364 end;
365 // IDDQD
366 if CheckCheat(I_GAME_CHEAT_DEATH) then
367 begin
368 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
369 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
370 s := 'SOUND_MONSTER_HAHA';
371 goto Cheated;
372 end;
373 //
374 if CheckCheat(I_GAME_CHEAT_DOORS) then
375 begin
376 g_Triggers_OpenAll();
377 goto Cheated;
378 end;
379 // GOODBYE
380 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
381 begin
382 if gTriggers <> nil then
383 for a := 0 to High(gTriggers) do
384 if gTriggers[a].TriggerType = TRIGGER_EXIT then
385 begin
386 gExitByTrigger := True;
387 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
388 g_Game_ExitLevel(gTriggers[a].tgcMap);
389 Break;
390 end;
391 goto Cheated;
392 end;
393 //
394 s2 := Copy(charbuff, 15, 2);
395 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
396 begin
397 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
398 begin
399 c := 'MAP'+s2;
400 g_Game_ExitLevel(c);
401 end;
402 goto Cheated;
403 end;
404 //
405 if CheckCheat(I_GAME_CHEAT_FLY) then
406 begin
407 gFly := not gFly;
408 goto Cheated;
409 end;
410 // BULLFROG
411 if CheckCheat(I_GAME_CHEAT_JUMPS) then
412 begin
413 VEL_JUMP := 30-VEL_JUMP;
414 goto Cheated;
415 end;
416 // FORMULA1
417 if CheckCheat(I_GAME_CHEAT_SPEED) then
418 begin
419 MAX_RUNVEL := 32-MAX_RUNVEL;
420 goto Cheated;
421 end;
422 // CONDOM
423 if CheckCheat(I_GAME_CHEAT_SUIT) then
424 begin
425 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
426 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
427 goto Cheated;
428 end;
429 //
430 if CheckCheat(I_GAME_CHEAT_AIR) then
431 begin
432 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
433 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
434 goto Cheated;
435 end;
436 // PURELOVE
437 if CheckCheat(I_GAME_CHEAT_BERSERK) then
438 begin
439 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
440 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
441 goto Cheated;
442 end;
443 //
444 if CheckCheat(I_GAME_CHEAT_JETPACK) then
445 begin
446 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
447 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
448 goto Cheated;
449 end;
450 // CASPER
451 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
452 begin
453 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
454 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
455 goto Cheated;
456 end;
457 //
458 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
459 begin
460 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
461 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
462 goto Cheated;
463 end;
464 // INFERNO
465 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
466 begin
467 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
468 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
469 goto Cheated;
470 end;
471 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
472 begin
473 gAimLine := not gAimLine;
474 goto Cheated;
475 end;
476 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
477 begin
478 gShowMap := not gShowMap;
479 goto Cheated;
480 end;
481 Exit;
483 Cheated:
484 g_Sound_PlayEx(s);
485 end;
488 procedure KeyPress (K: Word);
489 var
490 Msg: g_gui.TMessage;
491 begin
492 case K of
493 IK_PAUSE: // <Pause/Break>:
494 begin
495 if (g_ActiveWindow = nil) then g_Game_Pause(not gPause);
496 end;
498 IK_BACKQUOTE, VK_CONSOLE: // <`/~/¨/¸>:
499 begin
500 g_Console_Switch();
501 end;
503 IK_ESCAPE, VK_ESCAPE: // <Esc>:
504 begin
505 if gChatShow then
506 begin
507 g_Console_Chat_Switch();
508 Exit;
509 end;
511 if gConsoleShow then
512 begin
513 g_Console_Switch();
514 end
515 else if (g_ActiveWindow <> nil) then
516 begin
517 Msg.Msg := WM_KEYDOWN;
518 Msg.WParam := IK_ESCAPE;
519 g_ActiveWindow.OnMessage(Msg);
520 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
521 end
522 else if (gState <> STATE_FOLD) then
523 begin
524 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
525 begin
526 g_Game_InGameMenu(True);
527 end
528 else if (gExit = 0) and (gState <> STATE_SLIST) then
529 begin
530 if (gState <> STATE_MENU) then
531 begin
532 if (NetMode <> NET_NONE) then
533 begin
534 g_Game_StopAllSounds(True);
535 g_Game_Free;
536 gState := STATE_MENU;
537 Exit;
538 end;
539 end;
540 g_GUI_ShowWindow('MainMenu');
541 g_Sound_PlayEx('MENU_OPEN');
542 end;
543 end;
544 end;
546 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
547 begin // <F2> .. <F6> � <F12>
548 if gGameOn and (not gConsoleShow) and (not gChatShow) then
549 begin
550 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
551 if (not g_Game_IsNet) then g_Game_Pause(True);
552 case K of
553 IK_F2: g_Menu_Show_SaveMenu();
554 IK_F3: g_Menu_Show_LoadMenu();
555 IK_F4: g_Menu_Show_GameSetGame();
556 IK_F5: g_Menu_Show_OptionsVideo();
557 IK_F6: g_Menu_Show_OptionsSound();
558 IK_F7: g_Menu_Show_EndGameMenu();
559 IK_F10: g_Menu_Show_QuitGameMenu();
560 end;
561 end;
562 end;
564 else
565 begin
566 gJustChatted := False;
567 if gConsoleShow or gChatShow then
568 begin
569 g_Console_Control(K);
570 end
571 else if (g_ActiveWindow <> nil) then
572 begin
573 Msg.Msg := WM_KEYDOWN;
574 Msg.WParam := K;
575 g_ActiveWindow.OnMessage(Msg);
576 end
577 else if (gState = STATE_MENU) then
578 begin
579 g_GUI_ShowWindow('MainMenu');
580 g_Sound_PlayEx('MENU_OPEN');
581 end;
582 end;
583 end;
584 end;
587 procedure CharPress (C: AnsiChar);
588 var
589 Msg: g_gui.TMessage;
590 a: Integer;
591 begin
592 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then Exit;
594 if gConsoleShow or gChatShow then
595 begin
596 g_Console_Char(C);
597 end
598 else if (g_ActiveWindow <> nil) then
599 begin
600 Msg.Msg := WM_CHAR;
601 Msg.WParam := Ord(C);
602 g_ActiveWindow.OnMessage(Msg);
603 end
604 else
605 begin
606 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
607 charbuff[15] := upcase1251(C);
608 Cheat();
609 end;
610 end;
613 end.