From: DeaDDooMER Date: Fri, 5 Apr 2019 16:38:18 +0000 (+0300) Subject: joysticks handled using SDL_PollEvent X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;ds=inline;h=843ef00143d2dd922eaa56362d4afbeb41aa54bf;p=d2df-sdl.git joysticks handled using SDL_PollEvent --- diff --git a/src/engine/e_input.pas b/src/engine/e_input.pas index b93ca59..83508f3 100644 --- a/src/engine/e_input.pas +++ b/src/engine/e_input.pas @@ -203,12 +203,26 @@ 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; + 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; implementation @@ -225,23 +239,10 @@ const VIRT_BEG = JOYH_END; VIRT_END = VIRT_BEG + e_MaxVirtKeys; -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; - var KeyBuffer: array [0..e_MaxKbdKeys] of Boolean; + JoyBuffer: array [JOYK_BEG..JOYH_END] of Boolean; VirtBuffer: array [0..e_MaxVirtKeys] of Boolean; - Joysticks: array of TJoystick = nil; function OpenJoysticks(): Byte; var @@ -295,6 +296,7 @@ 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; @@ -302,6 +304,7 @@ end; procedure e_KeyUpDown (key: Word; down: Boolean); 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 end; @@ -494,6 +497,11 @@ var 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 @@ -505,8 +513,7 @@ begin for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do Joysticks[i].HatBuf[j, d] := False; end; - for i := Low(VirtBuffer) to High(VirtBuffer) do - VirtBuffer[i] := False; +***) end; { @@ -529,10 +536,13 @@ begin if (Key = IK_INVALID) or (Key = 0) then Exit; if (Key < KBRD_END) then - begin // Keyboard buttons/keys - Result := KeyBuffer[Key]; - end + 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; @@ -575,9 +585,7 @@ begin Result := Joysticks[JoyI].HatBuf[Key div 4, dir]; end; end - - else if (Key >= VIRT_BEG) and (Key < VIRT_END) then - Result := VirtBuffer[Key - VIRT_BEG] +***) end; procedure e_SetKeyState(key: Word; state: Integer); @@ -587,10 +595,13 @@ begin if (Key = IK_INVALID) or (Key = 0) then Exit; if (Key < KBRD_END) then - begin // Keyboard buttons/keys - keyBuffer[key] := (state <> 0); - end + 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; @@ -627,9 +638,7 @@ begin Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0); end; end - - else if (Key >= VIRT_BEG) and (Key < VIRT_END) then - VirtBuffer[Key - VIRT_BEG] := (state <> 0) +***) end; function e_AnyKeyPressed(): Boolean; diff --git a/src/game/g_console.pas b/src/game/g_console.pas index fd8e24b..2de108c 100644 --- a/src/game/g_console.pas +++ b/src/game/g_console.pas @@ -1716,10 +1716,10 @@ begin (* for i := 0 to e_MaxJoys - 1 do *) for i := 0 to 1 do begin - g_Console_BindKey(e_JoyAxisToKey(i, 0, 0), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft'); - g_Console_BindKey(e_JoyAxisToKey(i, 0, 1), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright'); - g_Console_BindKey(e_JoyAxisToKey(i, 1, 0), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup'); - g_Console_BindKey(e_JoyAxisToKey(i, 1, 1), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown'); + g_Console_BindKey(e_JoyAxisToKey(i, 0, AX_MINUS), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft'); + g_Console_BindKey(e_JoyAxisToKey(i, 0, AX_PLUS), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright'); + g_Console_BindKey(e_JoyAxisToKey(i, 1, AX_MINUS), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup'); + g_Console_BindKey(e_JoyAxisToKey(i, 1, AX_PLUS), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown'); g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump'); g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack'); g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate'); diff --git a/src/game/g_window.pas b/src/game/g_window.pas index a5dd79c..5689ceb 100644 --- a/src/game/g_window.pas +++ b/src/game/g_window.pas @@ -84,7 +84,7 @@ var ticksOverflow: Int64 = -1; lastTicks: Uint32 = 0; // to detect overflow {$ENDIF} - + JoystickHatState: array [0..e_MaxJoyHats, HAT_LEFT..HAT_DOWN] of Boolean; procedure KillGLWindow (preserveGL: Boolean); begin @@ -467,6 +467,8 @@ var key, keychr: Word; uc: UnicodeChar; down: Boolean; + i: Integer; + hat: array [HAT_LEFT..HAT_DOWN] of Boolean; begin result := false; @@ -513,6 +515,57 @@ begin if down then KeyPress(key); end; + SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: + if (ev.jbutton.which < e_MaxJoys) and (ev.jbutton.button < e_MaxJoyBtns) then + begin + key := e_JoyButtonToKey(ev.jbutton.which, ev.jbutton.button); + down := ev.type_ = SDL_JOYBUTTONDOWN; + e_KeyUpDown(key, down); + g_Console_ProcessBind(key, down); + if down then KeyPress(key) + end; + + SDL_JOYAXISMOTION: + if (ev.jaxis.which < e_MaxJoys) and (ev.jaxis.axis < e_MaxJoyAxes) then + begin + down := ev.jaxis.value <> Joysticks[ev.jaxis.which].AxisZero[ev.jaxis.axis]; + if ev.jaxis.value < Joysticks[ev.jaxis.which].AxisZero[ev.jaxis.axis] - e_JoystickDeadzones[ev.jaxis.which] then + key := e_JoyAxisToKey(ev.jaxis.which, ev.jaxis.axis, AX_MINUS) + else + key := e_JoyAxisToKey(ev.jaxis.which, ev.jaxis.axis, AX_PLUS); + e_KeyUpDown(key, down); + g_Console_ProcessBind(key, down); + if down then KeyPress(key) + end; + + SDL_JOYHATMOTION: + if (ev.jhat.which < e_MaxJoys) and (ev.jhat.hat < e_MaxJoyHats) then + begin + hat[HAT_UP] := LongBool(ev.jhat.value and SDL_HAT_UP); + hat[HAT_DOWN] := LongBool(ev.jhat.value and SDL_HAT_DOWN); + hat[HAT_LEFT] := LongBool(ev.jhat.value and SDL_HAT_LEFT); + 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 + begin + down := hat[i]; + key := e_JoyHatToKey(ev.jhat.which, ev.jhat.hat, i); + e_KeyUpDown(key, down); + g_Console_ProcessBind(key, down); + if down then KeyPress(key) + end + end; + JoystickHatState[ev.jhat.which] := hat + end; + +(* + SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED: + begin + // TODO update menu here + end +*) + {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)} SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev); @@ -663,7 +716,7 @@ begin EventHandler(ev); if (ev.type_ = SDL_QUITEV) then break; end; - e_PollJoysticks(); + //e_PollJoysticks(); if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then begin @@ -730,7 +783,7 @@ begin result := EventHandler(ev); if (ev.type_ = SDL_QUITEV) then exit; end; - e_PollJoysticks(); + //e_PollJoysticks(); end;