DEADSOFTWARE

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