Fixed expansion of joysticks collection

This commit is contained in:
Stefanos A 2013-12-22 23:55:46 +01:00 committed by thefiddler
parent 2f1a81da2c
commit 31ce400a7e

View file

@ -55,7 +55,7 @@ namespace OpenTK.Platform.SDL2
} }
} }
readonly List<JoystickDevice> joysticks = new List<JoystickDevice>(); readonly List<JoystickDevice> joysticks = new List<JoystickDevice>(4);
readonly Dictionary<int, Sdl2GamePad> controllers = new Dictionary<int, Sdl2GamePad>(); readonly Dictionary<int, Sdl2GamePad> controllers = new Dictionary<int, Sdl2GamePad>();
IList<JoystickDevice> joysticks_readonly; IList<JoystickDevice> joysticks_readonly;
@ -131,16 +131,18 @@ namespace OpenTK.Platform.SDL2
case EventType.JOYDEVICEADDED: case EventType.JOYDEVICEADDED:
{ {
// Make sure we have enough space to store this instance // Make sure we have enough space to store this instance
if (joysticks.Count <= id) if (joysticks.Capacity <= id)
{ {
joysticks.Capacity = OpenTK.MathHelper.NextPowerOfTwo(id); joysticks.Capacity = OpenTK.MathHelper.NextPowerOfTwo(id + 1);
for (int i = joysticks.Count; i < joysticks.Capacity; i++)
joysticks.Add(null); // Expand the joysticks list
} }
IntPtr handle = SDL.JoystickOpen(id); IntPtr handle = SDL.JoystickOpen(id);
if (handle != IntPtr.Zero) if (handle != IntPtr.Zero)
{ {
JoystickDevice<Sdl2JoystickDetails> joystick = OpenJoystick(id); JoystickDevice<Sdl2JoystickDetails> joystick = OpenJoystick(id);
if (joysticks != null) if (joystick != null)
{ {
joystick.Details.IsConnected = true; joystick.Details.IsConnected = true;
joysticks[id] = joystick; joysticks[id] = joystick;