DEADSOFTWARE

fmod -> sdl2 mixer, first part: we have sounds now, but no music and no advanced...
[d2df-sdl.git] / src / game / g_main.pas
1 unit g_main;
3 interface
5 procedure Main();
6 procedure Init();
7 procedure Release();
8 procedure Update();
9 procedure Draw();
10 procedure KeyPress(K: Word);
11 procedure CharPress(C: Char);
13 var
14 GameDir: string;
15 DataDir: string;
16 MapsDir: string;
17 ModelsDir: string;
18 GameWAD: string;
20 implementation
22 uses
23 SDL2, GL, GLExt, WADEDITOR, e_log, g_window,
24 e_graphics, e_input, g_game, g_console, g_gui,
25 e_sound, g_options, g_sound, g_player,
26 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
27 MAPSTRUCT, g_menu, g_language, g_net;
29 var
30 charbuff: Array [0..15] of Char;
32 procedure Main();
33 begin
34 GetDir(0, GameDir);
35 MapsDir := GameDir + '/maps/';
36 DataDir := GameDir + '/data/';
37 ModelsDir := DataDir + 'models/';
38 GameWAD := DataDir + 'Game.wad';
40 e_InitLog(GameDir + '/' + LOG_FILENAME, WM_NEWFILE);
42 e_WriteLog('Read config file', MSG_NOTIFY);
43 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
45 //GetSystemDefaultLCID()
47 //e_WriteLog('Read language file', MSG_NOTIFY);
48 //g_Language_Load(DataDir + gLanguage + '.txt');
49 e_WriteLog(gLanguage, MSG_NOTIFY);
50 g_Language_Set(gLanguage);
52 if SDL_Init(SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO) < 0 then
53 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
55 SDL_StartTextInput();
57 e_WriteLog('Entering SDLMain', MSG_NOTIFY);
59 {$WARNINGS OFF}
60 SDLMain();
61 {$WARNINGS ON}
63 SDL_StopTextInput();
65 e_WriteLog('Releasing SDL', MSG_NOTIFY);
66 SDL_Quit();
67 end;
69 procedure Init();
70 var
71 a: Integer;
72 begin
73 Randomize;
75 e_WriteLog('Init Input', MSG_NOTIFY);
76 e_InitInput();
78 if (e_JoysticksAvailable > 0) then
79 e_WriteLog('Input: Joysticks available.', MSG_NOTIFY)
80 else
81 e_WriteLog('Input: No Joysticks.', MSG_NOTIFY);
83 if not gNoSound then
84 begin
85 e_WriteLog('Initializing SDL2 mixer', MSG_NOTIFY);
86 e_InitSoundSystem();
87 end;
89 e_WriteLog('Init game', MSG_NOTIFY);
90 g_Game_Init();
92 for a := 0 to 15 do charbuff[a] := ' ';
93 end;
95 procedure Release();
96 begin
97 e_WriteLog('Releasing engine', MSG_NOTIFY);
98 e_ReleaseEngine();
100 e_WriteLog('Releasing Input', MSG_NOTIFY);
101 e_ReleaseInput();
103 if not gNoSound then
104 begin
105 e_WriteLog('Releasing FMOD', MSG_NOTIFY);
106 e_ReleaseSoundSystem();
107 end;
108 end;
110 procedure Update();
111 begin
112 g_Game_Update();
113 end;
115 procedure Draw();
116 begin
117 g_Game_Draw();
118 end;
120 function Translit(S: String): String;
121 var
122 i: Integer;
123 begin
124 Result := S;
125 for i := 1 to Length(Result) do
126 case Result[i] of
127 'É': Result[i] := 'Q';
128 'Ö': Result[i] := 'W';
129 'Ó': Result[i] := 'E';
130 'Ê': Result[i] := 'R';
131 'Å': Result[i] := 'T';
132 'Í': Result[i] := 'Y';
133 'Ã': Result[i] := 'U';
134 'Ø': Result[i] := 'I';
135 'Ù': Result[i] := 'O';
136 'Ç': Result[i] := 'P';
137 'Õ': Result[i] := Chr(219);
138 'Ú': Result[i] := Chr(221);
139 'Ô': Result[i] := 'A';
140 'Û': Result[i] := 'S';
141 'Â': Result[i] := 'D';
142 'À': Result[i] := 'F';
143 'Ï': Result[i] := 'G';
144 'Ð': Result[i] := 'H';
145 'Î': Result[i] := 'J';
146 'Ë': Result[i] := 'K';
147 'Ä': Result[i] := 'L';
148 'Æ': Result[i] := Chr(186);
149 'Ý': Result[i] := Chr(222);
150 'ß': Result[i] := 'Z';
151 '×': Result[i] := 'X';
152 'Ñ': Result[i] := 'C';
153 'Ì': Result[i] := 'V';
154 'È': Result[i] := 'B';
155 'Ò': Result[i] := 'N';
156 'Ü': Result[i] := 'M';
157 'Á': Result[i] := Chr(188);
158 'Þ': Result[i] := Chr(190);
159 end;
160 end;
162 procedure Cheat();
163 const
164 CHEAT_DAMAGE = 500;
165 label
166 Cheated;
167 var
168 s, s2, ls1, ls2: string;
169 c: Char16;
170 a: Integer;
171 begin
172 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
173 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
174 or g_Game_IsNet then Exit;
176 s := 'SOUND_GAME_RADIO';
178 //
179 ls1 := CheatEng[I_GAME_CHEAT_GODMODE];
180 ls2 := Translit(CheatRus[I_GAME_CHEAT_GODMODE]);
181 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
182 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
183 begin
184 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
185 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
186 goto Cheated;
187 end;
188 // RAMBO
189 ls1 := CheatEng[I_GAME_CHEAT_WEAPONS];
190 ls2 := Translit(CheatRus[I_GAME_CHEAT_WEAPONS]);
191 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
192 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
193 begin
194 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
195 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
196 goto Cheated;
197 end;
198 // TANK
199 ls1 := CheatEng[I_GAME_CHEAT_HEALTH];
200 ls2 := Translit(CheatRus[I_GAME_CHEAT_HEALTH]);
201 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
202 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
203 begin
204 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
205 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
206 goto Cheated;
207 end;
208 // IDDQD
209 ls1 := CheatEng[I_GAME_CHEAT_DEATH];
210 ls2 := Translit(CheatRus[I_GAME_CHEAT_DEATH]);
211 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
212 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
213 begin
214 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
215 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
216 s := 'SOUND_MONSTER_HAHA';
217 goto Cheated;
218 end;
219 //
220 ls1 := CheatEng[I_GAME_CHEAT_DOORS];
221 ls2 := Translit(CheatRus[I_GAME_CHEAT_DOORS]);
222 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
223 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
224 begin
225 g_Triggers_OpenAll();
226 goto Cheated;
227 end;
228 // GOODBYE
229 ls1 := CheatEng[I_GAME_CHEAT_NEXTMAP];
230 ls2 := Translit(CheatRus[I_GAME_CHEAT_NEXTMAP]);
231 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
232 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
233 begin
234 if gTriggers <> nil then
235 for a := 0 to High(gTriggers) do
236 if gTriggers[a].TriggerType = TRIGGER_EXIT then
237 begin
238 gExitByTrigger := True;
239 g_Game_ExitLevel(gTriggers[a].Data.MapName);
240 Break;
241 end;
242 goto Cheated;
243 end;
244 //
245 ls1 := CheatEng[I_GAME_CHEAT_CHANGEMAP];
246 ls2 := Translit(CheatRus[I_GAME_CHEAT_CHANGEMAP]);
247 s2 := Copy(charbuff, 15, 2);
248 if ((Copy(charbuff, 15 - Length(ls1), Length(ls1)) = ls1) or
249 (Copy(charbuff, 15 - Length(ls2), Length(ls2)) = ls2))
250 and (s2[1] >= '0') and (s2[1] <= '9')
251 and (s2[2] >= '0') and (s2[2] <= '9') then
252 begin
253 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
254 begin
255 c := 'MAP00';
256 c[3] := s2[1];
257 c[4] := s2[2];
258 g_Game_ExitLevel(c);
259 end;
260 goto Cheated;
261 end;
262 //
263 ls1 := CheatEng[I_GAME_CHEAT_FLY];
264 ls2 := Translit(CheatRus[I_GAME_CHEAT_FLY]);
265 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
266 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
267 begin
268 gFly := not gFly;
269 goto Cheated;
270 end;
271 // BULLFROG
272 ls1 := CheatEng[I_GAME_CHEAT_JUMPS];
273 ls2 := Translit(CheatRus[I_GAME_CHEAT_JUMPS]);
274 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
275 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
276 begin
277 VEL_JUMP := 30-VEL_JUMP;
278 goto Cheated;
279 end;
280 // FORMULA1
281 ls1 := CheatEng[I_GAME_CHEAT_SPEED];
282 ls2 := Translit(CheatRus[I_GAME_CHEAT_SPEED]);
283 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
284 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
285 begin
286 MAX_RUNVEL := 32-MAX_RUNVEL;
287 goto Cheated;
288 end;
289 // CONDOM
290 ls1 := CheatEng[I_GAME_CHEAT_SUIT];
291 ls2 := Translit(CheatRus[I_GAME_CHEAT_SUIT]);
292 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
293 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
294 begin
295 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
296 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
297 goto Cheated;
298 end;
299 //
300 ls1 := CheatEng[I_GAME_CHEAT_AIR];
301 ls2 := Translit(CheatRus[I_GAME_CHEAT_AIR]);
302 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
303 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
304 begin
305 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
306 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
307 goto Cheated;
308 end;
309 // PURELOVE
310 ls1 := CheatEng[I_GAME_CHEAT_BERSERK];
311 ls2 := Translit(CheatRus[I_GAME_CHEAT_BERSERK]);
312 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
313 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
314 begin
315 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
316 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
317 goto Cheated;
318 end;
319 //
320 ls1 := CheatEng[I_GAME_CHEAT_JETPACK];
321 ls2 := Translit(CheatRus[I_GAME_CHEAT_JETPACK]);
322 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
323 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
324 begin
325 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
326 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
327 goto Cheated;
328 end;
329 // CASPER
330 ls1 := CheatEng[I_GAME_CHEAT_NOCLIP];
331 ls2 := Translit(CheatRus[I_GAME_CHEAT_NOCLIP]);
332 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
333 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
334 begin
335 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
336 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
337 goto Cheated;
338 end;
339 //
340 ls1 := CheatEng[I_GAME_CHEAT_NOTARGET];
341 ls2 := Translit(CheatRus[I_GAME_CHEAT_NOTARGET]);
342 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
343 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
344 begin
345 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
346 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
347 goto Cheated;
348 end;
349 // INFERNO
350 ls1 := CheatEng[I_GAME_CHEAT_NORELOAD];
351 ls2 := Translit(CheatRus[I_GAME_CHEAT_NORELOAD]);
352 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
353 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
354 begin
355 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
356 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
357 goto Cheated;
358 end;
359 ls1 := CheatEng[I_GAME_CHEAT_AIMLINE];
360 ls2 := Translit(CheatRus[I_GAME_CHEAT_AIMLINE]);
361 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
362 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
363 begin
364 gAimLine := not gAimLine;
365 goto Cheated;
366 end;
367 ls1 := CheatEng[I_GAME_CHEAT_AUTOMAP];
368 ls2 := Translit(CheatRus[I_GAME_CHEAT_AUTOMAP]);
369 if (Copy(charbuff, 17 - Length(ls1), Length(ls1)) = ls1) or
370 (Copy(charbuff, 17 - Length(ls2), Length(ls2)) = ls2) then
371 begin
372 gShowMap := not gShowMap;
373 goto Cheated;
374 end;
375 Exit;
377 Cheated:
378 g_Sound_PlayEx(s);
379 end;
381 procedure KeyPress(K: Word);
382 var
383 Msg: g_gui.TMessage;
384 begin
385 case K of
386 IK_PAUSE: // <Pause/Break>:
387 begin
388 if (g_ActiveWindow = nil) then
389 g_Game_Pause(not gPause);
390 end;
392 IK_BACKQUOTE: // <`/~/¨/¸>:
393 begin
394 g_Console_Switch();
395 end;
397 IK_ESCAPE: // <Esc>:
398 begin
399 if gChatShow then
400 begin
401 g_Console_Chat_Switch();
402 Exit;
403 end;
405 if gConsoleShow then
406 g_Console_Switch()
407 else
408 if g_ActiveWindow <> nil then
409 begin
410 Msg.Msg := WM_KEYDOWN;
411 Msg.WParam := IK_ESCAPE;
412 g_ActiveWindow.OnMessage(Msg);
413 end
414 else
415 if gState <> STATE_FOLD then
416 if gGameOn
417 or (gState = STATE_INTERSINGLE)
418 or (gState = STATE_INTERCUSTOM)
419 then
420 g_Game_InGameMenu(True)
421 else
422 if (gExit = 0) and (gState <> STATE_SLIST) then
423 begin
424 if gState <> STATE_MENU then
425 if NetMode <> NET_NONE then
426 begin
427 g_Game_StopAllSounds(True);
428 g_Game_Free;
429 gState := STATE_MENU;
430 Exit;
431 end;
433 g_GUI_ShowWindow('MainMenu');
434 g_Sound_PlayEx('MENU_OPEN');
435 end;
436 end;
438 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
439 begin // <F2> .. <F6> � <F12>
440 if gGameOn and (not gConsoleShow) and (not gChatShow) then
441 begin
442 while g_ActiveWindow <> nil do
443 g_GUI_HideWindow(False);
445 if (not g_Game_IsNet) then
446 g_Game_Pause(True);
448 case K of
449 IK_F2:
450 g_Menu_Show_SaveMenu();
451 IK_F3:
452 g_Menu_Show_LoadMenu();
453 IK_F4:
454 g_Menu_Show_GameSetGame();
455 IK_F5:
456 g_Menu_Show_OptionsVideo();
457 IK_F6:
458 g_Menu_Show_OptionsSound();
459 IK_F7:
460 g_Menu_Show_EndGameMenu();
461 IK_F10:
462 g_Menu_Show_QuitGameMenu();
463 end;
464 end;
465 end;
467 else
468 begin
469 gJustChatted := False;
470 if gConsoleShow or gChatShow then
471 g_Console_Control(K)
472 else
473 if g_ActiveWindow <> nil then
474 begin
475 Msg.Msg := WM_KEYDOWN;
476 Msg.WParam := K;
477 g_ActiveWindow.OnMessage(Msg);
478 end
479 else
480 begin
481 if (gState = STATE_MENU) then
482 begin
483 g_GUI_ShowWindow('MainMenu');
484 g_Sound_PlayEx('MENU_OPEN');
485 end;
486 end;
487 end;
488 end;
489 end;
491 procedure CharPress(C: Char);
492 var
493 Msg: g_gui.TMessage;
494 a: Integer;
495 begin
496 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then
497 Exit;
499 if gConsoleShow or gChatShow then
500 g_Console_Char(C)
501 else
502 if g_ActiveWindow <> nil then
503 begin
504 Msg.Msg := WM_CHAR;
505 Msg.WParam := Ord(C);
506 g_ActiveWindow.OnMessage(Msg);
507 end
508 else
509 begin
510 for a := 0 to 14 do
511 charbuff[a] := charbuff[a+1];
512 charbuff[15] := UpCase(C);
513 Cheat();
514 end;
515 end;
517 end.