DEADSOFTWARE

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