DEADSOFTWARE

e_input cleanup
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 5 Apr 2019 22:08:17 +0000 (01:08 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 5 Apr 2019 22:20:03 +0000 (01:20 +0300)
src/engine/e_input.pas
src/game/g_game.pas
src/game/g_main.pas
src/game/g_map.pas
src/game/g_menu.pas
src/game/g_window.pas

index 83508f3ae090329f44ee33599377eceb31c9e6c1..1f1ba160be591fe9771b9779a295c7b13d140c76 100644 (file)
@@ -18,10 +18,7 @@ unit e_input;
 
 interface
 
-uses
-  SysUtils,
-  e_log,
-  SDL2;
+uses SysUtils, SDL2;
 
 const
   e_MaxKbdKeys  = SDL_NUM_SCANCODES;
@@ -41,7 +38,7 @@ const
   // $$$..$$$ -   48 Virtual buttons/keys
 
   // these are apparently used in g_gui and g_game and elsewhere
-  IK_INVALID = 65535;
+  IK_INVALID = 0;
   IK_ESCAPE  = SDL_SCANCODE_ESCAPE;
   IK_RETURN  = SDL_SCANCODE_RETURN;
   IK_KPRETURN= SDL_SCANCODE_KP_ENTER;
@@ -185,49 +182,27 @@ const
   HAT_RIGHT = 2;
   HAT_DOWN  = 3;
 
-function  e_InitInput(): Boolean;
-procedure e_ReleaseInput();
-procedure e_ClearInputBuffer();
-//function  e_PollInput(): Boolean;
-procedure e_PollJoysticks(); // call this from message loop to update joysticks
-function  e_KeyPressed(Key: Word): Boolean;
-function  e_AnyKeyPressed(): Boolean;
-function  e_GetFirstKeyPressed(): Word;
-function  e_JoystickStateToString(mode: Integer): String;
-function  e_JoyByHandle(handle: Word): Integer;
-function  e_JoyButtonToKey(id: Word; btn: Byte): Word;
-function  e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
-function  e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
-procedure e_SetKeyState(key: Word; state: Integer);
-
-procedure e_UnpressAllKeys ();
-procedure e_KeyUpDown (key: Word; down: Boolean);
-
-type
-  TJoystick = record
-    ID:      Byte;
-    Handle:  PSDL_Joystick;
-    Axes:    Byte;
-    Buttons: Byte;
-    Hats:    Byte;
-    ButtBuf: array [0..e_MaxJoyBtns] of Boolean;
-    AxisBuf: array [0..e_MaxJoyAxes] of Integer;
-    AxisZero: array [0..e_MaxJoyAxes] of Integer;
-    HatBuf:  array [0..e_MaxJoyHats] of array [HAT_LEFT..HAT_DOWN] of Boolean;
-  end;
+function  e_InitInput: Boolean;
+procedure e_ReleaseInput;
+procedure e_UnpressAllKeys;
+procedure e_KeyUpDown (key: Integer; down: Boolean);
+
+function  e_KeyPressed (key: Integer): Boolean;
+function  e_AnyKeyPressed: Boolean;
+function  e_GetFirstKeyPressed: Integer;
+function  e_HasJoysticks: Boolean;
+
+function  e_JoyButtonToKey (id, btn: Integer): Integer;
+function  e_JoyAxisToKey (id, ax, dir: Integer): Integer;
+function  e_JoyHatToKey (id, hat, dir: Integer): Integer;
 
 var
-  {e_MouseInfo:          TMouseInfo;}
-  e_EnableInput:        Boolean = False;
-  e_JoysticksAvailable: Byte    = 0;
-  e_JoystickDeadzones:  array [0..e_MaxJoys-1] of Integer = (8192, 8192, 8192, 8192);
-  e_KeyNames:           array [0..e_MaxInputKeys] of String;
-  Joysticks: array of TJoystick = nil;
+  e_JoystickAvailable: array [0..e_MaxJoys - 1] of Boolean;
+  e_JoystickDeadzones: array [0..e_MaxJoys - 1] of Integer = (8192, 8192, 8192, 8192);
+  e_KeyNames: array [0..e_MaxInputKeys] of String;
 
 implementation
 
-uses Math;
-
 const
   KBRD_END = e_MaxKbdKeys;
   JOYK_BEG = KBRD_END;
@@ -240,124 +215,25 @@ const
   VIRT_END = VIRT_BEG + e_MaxVirtKeys;
 
 var
-  KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
-  JoyBuffer: array [JOYK_BEG..JOYH_END] of Boolean;
-  VirtBuffer: array [0..e_MaxVirtKeys] of Boolean;
-
-function OpenJoysticks(): Byte;
-var
-  i, j, k, c: Integer;
-  joy: PSDL_Joystick;
-begin
-  Result := 0;
-  k := Min(e_MaxJoys, SDL_NumJoysticks());
-  if k = 0 then Exit;
-  c := 0;
-  for i := 0 to k do
-  begin
-    joy := SDL_JoystickOpen(i);
-    if joy <> nil then
-    begin
-      Inc(c);
-      e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' (' + SDL_JoystickName(joy) +
-                 ') as joystick ' + IntToStr(c) + ':', TMsgType.Notify);
-      SetLength(Joysticks, c);
-      with Joysticks[c-1] do
-      begin
-        ID := i;
-        Handle := joy;
-        Axes := Min(e_MaxJoyAxes, SDL_JoystickNumAxes(joy));
-        Buttons := Min(e_MaxJoyBtns, SDL_JoystickNumButtons(joy));
-        Hats := Min(e_MaxJoyHats, SDL_JoystickNumHats(joy));
-        // TODO: find proper solution for this xbox trigger shit
-        for j := 0 to Axes do AxisZero[j] := SDL_JoystickGetAxis(joy, j);
-        e_WriteLog('       ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
-                   IntToStr(Hats) + ' hats.', TMsgType.Notify);
-      end;
-    end;
-  end;
-  Result := c;
-end;
-
-procedure ReleaseJoysticks();
-var
-  i: Integer;
-begin
-  if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
-  for i := Low(Joysticks) to High(Joysticks) do
-    with Joysticks[i] do
-      SDL_JoystickClose(Handle);
-  SetLength(Joysticks, 0);
-end;
-
-
-procedure e_UnpressAllKeys ();
-var
-  i: Integer;
-begin
-  for i := 0 to High(KeyBuffer) do KeyBuffer[i] := False;
-  for i := JOYK_BEG to JOYH_END do JoyBuffer[i] := False;
-  for i := 0 to High(VirtBuffer) do VirtBuffer[i] := False;
-end;
+  InputBuffer: array [0..e_MaxInputKeys - 1] of Boolean;
 
-
-procedure e_KeyUpDown (key: Word; down: Boolean);
+procedure e_UnpressAllKeys;
+  var i: Integer;
 begin
-  if (key > 0) and (key < Length(KeyBuffer)) then KeyBuffer[key] := down
-  else if (key >= JOYK_BEG) and (key < JOYH_END) then JoyBuffer[key] := down
-  else if (key >= VIRT_BEG) and (key < VIRT_END) then VirtBuffer[key - VIRT_BEG] := down
+  for i := 0 to High(InputBuffer) do
+    InputBuffer[i] := False
 end;
 
-
-function PollKeyboard(): Boolean;
-{
-var
-  Keys: PByte;
-  NKeys: Integer;
-  i: NativeUInt;
-}
+procedure e_KeyUpDown (key: Integer; down: Boolean);
 begin
-  Result := False;
-  {
-  Keys := SDL_GetKeyboardState(@NKeys);
-  if (Keys = nil) or (NKeys < 1) then Exit;
-  for i := 0 to NKeys do
-  begin
-    if ((PByte(NativeUInt(Keys) + i)^) <> 0) then KeyBuffer[i] := false;
-  end;
-  for i := NKeys to High(KeyBuffer) do KeyBuffer[i] := False;
-  }
+  ASSERT(key >= 0);
+  ASSERT(key < e_MaxInputKeys);
+  if key > 0 then
+    InputBuffer[key] := down
 end;
 
-procedure e_PollJoysticks();
-var
-  i, j: Word;
-  hat: Byte;
-begin
-  //Result := False;
-  if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
-  SDL_JoystickUpdate();
-  for j := Low(Joysticks) to High(Joysticks) do
-  begin
-    with Joysticks[j] do
-    begin
-      for i := 0 to Buttons do ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
-      for i := 0 to Axes do AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
-      for i := 0 to Hats do
-      begin
-        hat := SDL_JoystickGetHat(Handle, i);
-        HatBuf[i, HAT_UP] := LongBool(hat and SDL_HAT_UP);
-        HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);
-        HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);
-        HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
-      end;
-    end;
-  end;
-end;
-
-procedure GenerateKeyNames();
-var
-  i, j, k: LongWord;
+procedure GenerateKeyNames;
+  var i, j, k: Integer;
 begin
   // keyboard key names
   e_KeyNames[IK_0] := '0';
@@ -370,8 +246,10 @@ begin
   e_KeyNames[IK_7] := '7';
   e_KeyNames[IK_8] := '8';
   e_KeyNames[IK_9] := '9';
+
   for i := IK_A to IK_Z do
     e_KeyNames[i] := '' + chr(ord('a') + (i - IK_a));
+
   e_KeyNames[IK_ESCAPE] := 'ESCAPE';
   e_KeyNames[IK_ENTER] := 'ENTER';
   e_KeyNames[IK_TAB] := 'TAB';
@@ -441,9 +319,6 @@ begin
   e_KeyNames[IK_MINUS] := '-';
   e_KeyNames[IK_EQUALS] := '=';
 
-  //for i := 0 to IK_LASTKEY do
-  //  e_KeyNames[i] := SDL_GetScancodeName(i);
-
   // joysticks
   for j := 0 to e_MaxJoys-1 do
   begin
@@ -474,242 +349,80 @@ begin
     e_KeyNames[VIRT_BEG + i] := 'VIRTUAL' + IntToStr(i);
 end;
 
-function e_InitInput(): Boolean;
-begin
-  Result := False;
-
-  e_JoysticksAvailable := OpenJoysticks();
-  e_EnableInput := True;
-  GenerateKeyNames();
-
-  Result := True;
-end;
-
-procedure e_ReleaseInput();
-begin
-  ReleaseJoysticks();
-  e_JoysticksAvailable := 0;
-end;
-
-procedure e_ClearInputBuffer();
-var
-  i, j, d: Integer;
-begin
-  for i := Low(KeyBuffer) to High(KeyBuffer) do
-    KeyBuffer[i] := False;
-  for i := JOYK_BEG to JOYH_END do
-    JoyBuffer[i] := False;
-  for i := Low(VirtBuffer) to High(VirtBuffer) do
-    VirtBuffer[i] := False;
-(***
-  if (Joysticks = nil) or (e_JoysticksAvailable = 0) then
-  for i := Low(Joysticks) to High(Joysticks) do
-  begin
-    for j := Low(Joysticks[i].ButtBuf) to High(Joysticks[i].ButtBuf) do
-      Joysticks[i].ButtBuf[j] := False;
-    for j := Low(Joysticks[i].AxisBuf) to High(Joysticks[i].AxisBuf) do
-      Joysticks[i].AxisBuf[j] := 0;
-    for j := Low(Joysticks[i].HatBuf) to High(Joysticks[i].HatBuf) do
-      for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
-        Joysticks[i].HatBuf[j, d] := False;
-  end;
-***)
-end;
-
-{
-function e_PollInput(): Boolean;
-var
-  kb, js: Boolean;
+function e_HasJoysticks: Boolean;
+  var i: Integer;
 begin
-  kb := PollKeyboard();
-  js := e_PollJoysticks();
-
-  Result := kb or js;
-end;
-}
-
-function e_KeyPressed(Key: Word): Boolean;
-var
-  joyi, dir: Integer;
-begin
-  Result := False;
-  if (Key = IK_INVALID) or (Key = 0) then Exit;
-
-  if (Key < KBRD_END) then
-    Result := KeyBuffer[Key]
-  else if (Key >= JOYK_BEG) and (Key < JOYH_END) then
-    Result := JoyBuffer[Key]
-  else if (Key >= VIRT_BEG) and (Key < VIRT_END) then
-    Result := VirtBuffer[Key - VIRT_BEG]
-
-(***
-  else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
-  begin // Joystick buttons
-    JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
-    if JoyI >= e_JoysticksAvailable then
-      Result := False
-    else
-    begin
-      Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
-      Result := Joysticks[JoyI].ButtBuf[Key];
-    end;
-  end
-
-  else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
-  begin // Joystick axes
-    JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
-    if JoyI >= e_JoysticksAvailable then
-      Result := False
-    else
-    begin
-      Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
-      dir := Key mod 2;
-      if dir = AX_MINUS then
-        Result := Joysticks[JoyI].AxisBuf[Key div 2] <
-          Joysticks[JoyI].AxisZero[Key div 2] - e_JoystickDeadzones[JoyI]
-      else
-        Result := Joysticks[JoyI].AxisBuf[Key div 2] >
-          Joysticks[JoyI].AxisZero[Key div 2] + e_JoystickDeadzones[JoyI]
-    end;
-  end
-
-  else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
-  begin // Joystick hats
-    JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
-    if JoyI >= e_JoysticksAvailable then
-      Result := False
-    else
-    begin
-      Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
-      dir := Key mod 4;
-      Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
-    end;
-  end
-***)
+  i := 0;
+  while (i < e_MaxJoys) and (e_JoystickAvailable[i] = False) do inc(i);
+  result := i < e_MaxJoys
 end;
 
-procedure e_SetKeyState(key: Word; state: Integer);
-var
-  JoyI, dir: Integer;
+function e_InitInput: Boolean;
+  var i: Integer;
 begin
-  if (Key = IK_INVALID) or (Key = 0) then Exit;
-
-  if (Key < KBRD_END) then
-    keyBuffer[key] := (state <> 0)
-  else if (Key >= JOYK_BEG) and (Key <= JOYH_END) then
-    JoyBuffer[key] := (state <> 0)
-  else if (Key >= VIRT_BEG) and (Key <= VIRT_END) then
-    VirtBuffer[Key - VIRT_BEG] := (state <> 0)
-
-(***
-  else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
-  begin // Joystick buttons
-    JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
-    if JoyI >= e_JoysticksAvailable then
-      Exit
-    else
-    begin
-      Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
-      Joysticks[JoyI].ButtBuf[Key] := (state <> 0);
-    end;
-  end
-
-  else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
-  begin // Joystick axes
-    JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
-    if JoyI >= e_JoysticksAvailable then
-      Exit
-    else
-    begin
-      Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
-      Joysticks[JoyI].AxisBuf[Key div 2] := state;
-    end;
-  end
-
-  else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
-  begin // Joystick hats
-    JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
-    if JoyI >= e_JoysticksAvailable then
-      Exit
-    else
-    begin
-      Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
-      dir := Key mod 4;
-      Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
-    end;
-  end
-***)
+  for i := 0 to e_MaxJoys - 1 do
+    e_JoystickAvailable[i] := False;
+  GenerateKeyNames;
+  result := True
 end;
 
-function e_AnyKeyPressed(): Boolean;
-var
-  k: Word;
+procedure e_ReleaseInput;
+  var i: Integer;
 begin
-  Result := False;
-
-  for k := 1 to e_MaxInputKeys do
-    if e_KeyPressed(k) then
-    begin
-      Result := True;
-      Break;
-    end;
+  for i := 0 to e_MaxJoys - 1 do
+    e_JoystickAvailable[i] := False
 end;
 
-function e_GetFirstKeyPressed(): Word;
-var
-  k: Word;
+function e_KeyPressed (key: Integer): Boolean;
 begin
-  Result := IK_INVALID;
-
-  for k := 1 to e_MaxInputKeys do
-    if e_KeyPressed(k) then
-    begin
-      Result := k;
-      Break;
-    end;
+  ASSERT(key >= 0);
+  ASSERT(key < e_MaxInputKeys);
+  result := InputBuffer[key]
 end;
 
-////////////////////////////////////////////////////////////////////////////////
-
-function e_JoystickStateToString(mode: Integer): String;
+function e_AnyKeyPressed: Boolean;
 begin
-  Result := '';
+  result := e_GetFirstKeyPressed <> IK_INVALID;
 end;
 
-function  e_JoyByHandle(handle: Word): Integer;
-var
-  i: Integer;
+function e_GetFirstKeyPressed: Integer;
+  var i: Integer;
 begin
-  Result := -1;
-  if Joysticks = nil then Exit;
-  for i := Low(Joysticks) to High(Joysticks) do
-    if Joysticks[i].ID = handle then
-    begin
-      Result := i;
-      Exit;
-    end;
+  i := 1;
+  while (i < e_MaxInputKeys) and (InputBuffer[i] = False) do inc(i);
+  if i < e_MaxInputKeys then
+    result := i
+  else
+    result := IK_INVALID
 end;
 
-function e_JoyButtonToKey(id: Word; btn: Byte): Word;
+function e_JoyButtonToKey (id, btn: Integer): Integer;
 begin
-  Result := 0;
-  if id >= e_MaxJoys then Exit;
-  Result := JOYK_BEG + id*e_MaxJoyBtns + btn;
+  ASSERT(id >= 0);
+  ASSERT(id < e_MaxJoys);
+  ASSERT(btn >= 0);
+  ASSERT(btn < e_MaxJoyBtns);
+  result := JOYK_BEG + id*e_MaxJoyBtns + btn
 end;
 
-function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
+function e_JoyAxisToKey (id, ax, dir: Integer): Integer;
 begin
-  Result := 0;
-  if id >= e_MaxJoys then Exit;
-  Result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir;
+  ASSERT(id >= 0);
+  ASSERT(id < e_MaxJoys);
+  ASSERT(ax >= 0);
+  ASSERT(ax < e_MaxJoyAxes);
+  ASSERT(dir in [AX_MINUS, AX_PLUS]);
+  result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir
 end;
 
-function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
+function e_JoyHatToKey (id, hat, dir: Integer): Integer;
 begin
-  Result := 0;
-  if id >= e_MaxJoys then Exit;
-  Result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir;
+  ASSERT(id >= 0);
+  ASSERT(id < e_MaxJoys);
+  ASSERT(hat >= 0);
+  ASSERT(hat < e_MaxJoyHats);
+  ASSERT(dir in [HAT_LEFT, HAT_UP, HAT_RIGHT, HAT_DOWN]);
+  result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir
 end;
 
-
 end.
index ad9330d7cdd293233479985cf73ac3852f1f84b2..6ef589569664476b8e65e89dfb06b61e96e3a2ee 100644 (file)
@@ -5802,11 +5802,6 @@ begin
 
       g_Console_Add(Format(cmd + ' is %d', [Byte(g_Debug_Player)]));
     end
-    else if (cmd = 'd_joy') then
-    begin
-      for a := 1 to 8 do
-        g_Console_Add(e_JoystickStateToString(a));
-    end
     else if (cmd = 'd_mem') then
     begin
       PrintHeapStats();
index ccad27b86c8e700b0b8ecaa43ff0f4d00d7a44b3..7f5d5b95c13739b6037e226450e11aa3b618a705 100644 (file)
@@ -202,10 +202,12 @@ begin
   e_InitInput();
   g_Touch_Init;
 
+(*
   if (e_JoysticksAvailable > 0) then
     e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
   else
     e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
+*)
 
   if (not gNoSound) then
   begin
index 81eb17e904044c36be49914f5172380a7b8cbdd6..9943dba6998a8a40944a89aa3d03346db8189293 100644 (file)
@@ -2261,7 +2261,7 @@ begin
   finally
     sfsGCEnable(); // enable releasing unused volumes
     //mapReader.Free();
-    e_ClearInputBuffer(); // why not?
+    e_UnpressAllKeys; // why not?
     if not mapOk then
     begin
       gCurrentMap.Free();
index 0e272d4b2368003b24f37f72edc929add4947cfd..c6e5339a15d0f562e9eaf555373a7e2fb56d40d9 100644 (file)
@@ -293,14 +293,15 @@ begin
     end;
   end;
 
-  if e_JoysticksAvailable > 0 then
+  if e_HasJoysticks then
   begin
     menu := TGUIMenu(g_GUI_GetWindow('OptionsControlsJoystickMenu').GetControl('mOptionsControlsJoystickMenu'));
     with menu do
     begin
-      for i := 0 to e_JoysticksAvailable-1 do
-        e_JoystickDeadzones[i] := TGUIScroll(menu.GetControl('scDeadzone' + IntToStr(i))).Value*(32767 div 20);
-    end;
+      for i := 0 to e_MaxJoys - 1 do
+        if e_JoystickAvailable[i] then
+          e_JoystickDeadzones[i] := TGUIScroll(menu.GetControl('scDeadzone' + IntToStr(i))).Value*(32767 div 20)
+    end
   end;
 
   if g_touch_enabled then
@@ -491,14 +492,15 @@ begin
     end;
   end;
 
-  if e_JoysticksAvailable > 0 then
+  if e_HasJoysticks then
   begin
     menu := TGUIMenu(g_GUI_GetWindow('OptionsControlsJoystickMenu').GetControl('mOptionsControlsJoystickMenu'));
     with menu do
     begin
-      for i := 0 to e_JoysticksAvailable-1 do
-        TGUIScroll(menu.GetControl('scDeadzone' + IntToStr(i))).Value := e_JoystickDeadzones[i] div (32767 div 20);
-    end;
+      for i := 0 to e_MaxJoys - 1 do
+        if e_JoystickAvailable[i] then
+          TGUIScroll(menu.GetControl('scDeadzone' + IntToStr(i))).Value := e_JoystickDeadzones[i] div (32767 div 20)
+    end
   end;
 
   if g_touch_enabled then
@@ -2797,7 +2799,7 @@ begin
     AddButton(nil, _lc[I_MENU_PLAYER_2_KBD], 'OptionsControlsP2Menu');
     {AddButton(nil, _lc[I_MENU_PLAYER_2_ALT], 'OptionsControlsP2MenuAlt');}
     AddButton(nil, _lc[I_MENU_PLAYER_2_WEAPONS], 'OptionsControlsP2MenuWeapons');
-    if e_JoysticksAvailable <> 0 then
+    if e_HasJoysticks then
     begin
       AddSpace();
       AddButton(nil, _lc[I_MENU_CONTROL_JOYSTICKS], 'OptionsControlsJoystickMenu');
@@ -2873,12 +2875,13 @@ begin
   with TGUIMenu(Menu.AddChild(TGUIMenu.Create(gMenuFont, gMenuSmallFont, _lc[I_MENU_CONTROL_JOYSTICKS]))) do
   begin
     Name := 'mOptionsControlsJoystickMenu';
-    for i := 0 to e_JoysticksAvailable-1 do
-      with AddScroll(Format(_lc[I_MENU_CONTROL_DEADZONE], [i + 1])) do
-      begin
-        Name := 'scDeadzone' + IntToStr(i);
-        Max := 20;
-      end;
+    for i := 0 to e_MaxJoys - 1 do
+      if e_JoystickAvailable[i] then
+        with AddScroll(Format(_lc[I_MENU_CONTROL_DEADZONE], [i + 1])) do
+        begin
+          Name := 'scDeadzone' + IntToStr(i);
+          Max := 20
+        end
   end;
   Menu.DefControl := 'mOptionsControlsJoystickMenu';
   g_GUI_AddWindow(Menu);
index 245efa3a1ee2d425efcd2e100f10db90cab708bd..44997de63c8046824eb8b1c762ad9fb636fe57f1 100644 (file)
@@ -55,7 +55,7 @@ uses
 {$IFDEF ENABLE_HOLMES}
   g_holmes, sdlcarcass, fui_ctls,
 {$ENDIF}
-  SysUtils, Classes, MAPDEF,
+  SysUtils, Classes, MAPDEF, Math,
   SDL2, e_graphics, e_log, e_texture, g_main,
   g_console, e_input, g_options, g_game,
   g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
@@ -84,7 +84,8 @@ var
   ticksOverflow: Int64 = -1;
   lastTicks: Uint32 = 0; // to detect overflow
 {$ENDIF}
-  JoystickHatState: array [0..e_MaxJoyHats, HAT_LEFT..HAT_DOWN] of Boolean;
+  JoystickHatState: array [0..e_MaxJoys, 0..e_MaxJoyHats, HAT_LEFT..HAT_DOWN] of Boolean;
+  JoystickZeroAxes: array [0..e_MaxJoys, 0..e_MaxJoyAxes] of Integer;
 
 procedure KillGLWindow (preserveGL: Boolean);
 begin
@@ -411,8 +412,7 @@ begin
     if gWinActive then
     begin
       e_WriteLog('deactivating window', TMsgType.Notify);
-      e_EnableInput := false;
-      e_ClearInputBuffer();
+      e_UnpressAllKeys;
 
       if gMuteWhenInactive then
       begin
@@ -438,7 +438,6 @@ begin
     if not gWinActive then
     begin
       //e_WriteLog('activating window', MSG_NOTIFY);
-      e_EnableInput := true;
 
       if gMuteWhenInactive then
       begin
@@ -469,6 +468,7 @@ var
   down: Boolean;
   i: Integer;
   hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
+  joy: PSDL_Joystick;
 begin
   result := false;
 
@@ -531,7 +531,7 @@ begin
         key := e_JoyAxisToKey(ev.jaxis.which, ev.jaxis.axis, AX_PLUS);
         minuskey := e_JoyAxisToKey(ev.jaxis.which, ev.jaxis.axis, AX_MINUS);
 
-        if ev.jaxis.value < Joysticks[ev.jaxis.which].AxisZero[ev.jaxis.axis] - e_JoystickDeadzones[ev.jaxis.which] then
+        if ev.jaxis.value < JoystickZeroAxes[ev.jaxis.which, ev.jaxis.axis] - e_JoystickDeadzones[ev.jaxis.which] then
         begin
           if (e_KeyPressed(key)) then
           begin
@@ -542,7 +542,7 @@ begin
           g_Console_ProcessBind(minuskey, True);
           KeyPress(minuskey);
         end
-        else if ev.jaxis.value > Joysticks[ev.jaxis.which].AxisZero[ev.jaxis.axis] + e_JoystickDeadzones[ev.jaxis.which] then
+        else if ev.jaxis.value > JoystickZeroAxes[ev.jaxis.which, ev.jaxis.axis] + e_JoystickDeadzones[ev.jaxis.which] then
         begin
           if (e_KeyPressed(minuskey)) then
           begin
@@ -577,7 +577,7 @@ begin
         hat[HAT_RIGHT] := LongBool(ev.jhat.value and SDL_HAT_RIGHT);
         for i := HAT_LEFT to HAT_DOWN do
         begin
-          if JoystickHatState[ev.jhat.which, i] <> hat[i] then
+          if JoystickHatState[ev.jhat.which, ev.jhat.hat, i] <> hat[i] then
           begin
             down := hat[i];
             key := e_JoyHatToKey(ev.jhat.which, ev.jhat.hat, i);
@@ -586,15 +586,27 @@ begin
             if down then KeyPress(key)
           end
         end;
-        JoystickHatState[ev.jhat.which] := hat
+        JoystickHatState[ev.jhat.which, ev.jhat.hat] := hat
       end;
 
-(*
-    SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED:
+    SDL_JOYDEVICEADDED:
+      if (ev.jdevice.which < e_MaxJoys) then
       begin
-        // TODO update menu here
-      end
-*)
+        joy := SDL_JoystickOpen(ev.jdevice.which);
+        ASSERT(joy <> nil);
+        e_LogWritefln('Added Joystick %s', [ev.jdevice.which]);
+        e_JoystickAvailable[ev.jdevice.which] := True;
+        for i := 0 to Min(SDL_JoystickNumAxes(joy), e_MaxJoyAxes) do
+          JoystickZeroAxes[ev.jdevice.which, i] := SDL_JoystickGetAxis(joy, i);
+        SDL_JoystickClose(joy)
+      end;
+
+    SDL_JOYDEVICEREMOVED:
+      if (ev.jdevice.which < e_MaxJoys) then
+      begin
+        e_JoystickAvailable[ev.jdevice.which] := False;
+        e_LogWritefln('Removed Joystick %s', [ev.jdevice.which])
+      end;
 
     {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
     SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: