Ejemplo n.º 1
0
        public GameControllerManager()
        {
            gameControllers = new List <GameController>();
            int  joystickCount = WindowsMultiMedia.GetJoystickDeviceCount();
            byte controllerId  = 0;

            WindowsMultiMedia.JOYINFOEX joyInfoEx = new WindowsMultiMedia.JOYINFOEX();
            joyInfoEx.dwSize  = Marshal.SizeOf(joyInfoEx);
            joyInfoEx.dwFlags = WindowsMultiMedia.JOY_RETURNALL;

            for (Int32 deviceId = 0; deviceId < joystickCount; deviceId++)
            {
                if (WindowsMultiMedia.GetJoystickState(deviceId, ref joyInfoEx) == WindowsMultiMedia.JOYERR_NOERROR)
                {
                    gameControllers.Add(new GameController(controllerId++, deviceId));
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdateState()
        {
            int joyX       = 0;
            int joyY       = 0;
            int joyButtons = 0;

            if (WindowsMultiMedia.GetJoystickState(deviceId, ref joyInfoEx) == WindowsMultiMedia.JOYERR_NOERROR)
            {
                joyX       = joyInfoEx.dwXpos;
                joyY       = joyInfoEx.dwYpos;
                joyButtons = joyInfoEx.dwButtons;
            }

            // detect button changes for event dispatch
            Left  = joyX < JoyCentreMinX;
            Right = joyX > JoyCentreMaxX;
            Up    = joyY < JoyCentreMinY;
            Down  = joyY > JoyCentreMaxY;

            buttonState[(int)Button.Left]  = Left;
            buttonState[(int)Button.Right] = Right;
            buttonState[(int)Button.Up]    = Up;
            buttonState[(int)Button.Down]  = Down;


            for (int bitIndex = 0; bitIndex < 32; bitIndex++)
            {
                buttonState[4 + bitIndex] = (joyButtons & 1) == 1;
                joyButtons >>= 1;
            }

            // copy fire button part of button state into fire button state array
            Array.Copy(buttonState, 4, fireButtonState, 0, fireButtonState.Length);

            // if ButtonPressed event wired, fire it on any presses
            if (ButtonPressed != null)
            {
                for (int index = 0; index < buttonState.Length; index++)
                {
                    if (buttonState[index] && !oldButtonState[index])
                    {
                        ButtonPressed(this, new GameControllerEventArgs(Id, (GameController.Button)index));
                    }
                }
            }

            // if ButtonReleased event wired, fire it on any input releases
            if (ButtonReleased != null)
            {
                for (int index = 0; index < buttonState.Length; index++)
                {
                    if (!buttonState[index] && oldButtonState[index])
                    {
                        ButtonReleased(this, new GameControllerEventArgs(Id, (GameController.Button)index));
                    }
                }
            }

            // copy button states to detect changes in next iteration
            Array.Copy(buttonState, oldButtonState, buttonState.Length);
        }