From 0668d044b6d0f5debeadc070ccc4bbf2b25b27f1 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 29 Aug 2019 10:26:18 +0200 Subject: [PATCH] only check input for connected gamepads to increase performance --- MLEM/Input/InputHandler.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index a8a9983..c772448 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -25,6 +25,7 @@ namespace MLEM.Input { private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount]; private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount]; private readonly bool handleGamepads; + public int ConnectedGamepads { get; private set; } public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true) { this.handleKeyboard = handleKeyboard; @@ -45,6 +46,8 @@ namespace MLEM.Input { for (var i = 0; i < GamePad.MaximumGamePadCount; i++) { this.lastGamepads[i] = this.gamepads[i]; this.gamepads[i] = GamePad.GetState(i); + if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected) + this.ConnectedGamepads = i; } } } @@ -112,7 +115,7 @@ namespace MLEM.Input { public bool IsGamepadButtonDown(Buttons button, int index = -1) { if (index < 0) { - for (var i = 0; i < GamePad.MaximumGamePadCount; i++) + for (var i = 0; i < this.ConnectedGamepads; i++) if (this.GetGamepadState(i).IsButtonDown(button)) return true; return false; @@ -122,7 +125,7 @@ namespace MLEM.Input { public bool IsGamepadButtonUp(Buttons button, int index = -1) { if (index < 0) { - for (var i = 0; i < GamePad.MaximumGamePadCount; i++) + for (var i = 0; i < this.ConnectedGamepads; i++) if (this.GetGamepadState(i).IsButtonUp(button)) return true; return false; @@ -132,7 +135,7 @@ namespace MLEM.Input { public bool WasGamepadButtonDown(Buttons button, int index = -1) { if (index < 0) { - for (var i = 0; i < GamePad.MaximumGamePadCount; i++) + for (var i = 0; i < this.ConnectedGamepads; i++) if (this.GetLastGamepadState(i).IsButtonDown(button)) return true; return false; @@ -142,7 +145,7 @@ namespace MLEM.Input { public bool WasGamepadButtonUp(Buttons button, int index = -1) { if (index < 0) { - for (var i = 0; i < GamePad.MaximumGamePadCount; i++) + for (var i = 0; i < this.ConnectedGamepads; i++) if (this.GetLastGamepadState(i).IsButtonUp(button)) return true; return false;