//----------------------------------------------------------------------------- // File: XBInput.cpp // // Desc: Input helper functions for the XBox samples // // Hist: 12.15.00 - Separated from XBUtil.cpp for December XDK release // 01.03.00 - Made changes for real Xbox controller // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include "defines.h" #ifdef INCLUDE_INPUT #include #include "XBInput.h" //----------------------------------------------------------------------------- // Globals //----------------------------------------------------------------------------- // Deadzone for thumbsticks #define XBINPUT_DEADZONE 0.25 // Threshold for analog buttons #define XBINPUT_BUTTONTHRESHOLD 1 // Global instance of gamepad devices XBGAMEPAD g_Gamepads[4]; //----------------------------------------------------------------------------- // Name: XBInput_CreateGamepads() // Desc: Creates the gamepad devices //----------------------------------------------------------------------------- HRESULT XBInput_CreateGamepads( XBGAMEPAD** ppGamepads ) { // Get a mask of all currently available devices DWORD dwDeviceMask = XGetDevices( XDEVICE_TYPE_GAMEPAD ); // Open the devices for( DWORD i=0; i < XGetPortCount(); i++ ) { ZeroMemory( &g_Gamepads[i], sizeof(XBGAMEPAD) ); if( dwDeviceMask & (1<= the threshold. for( DWORD b=0; b<8; b++ ) { // Turn the 8-bit polled value into a boolean value BOOL bPressed = ( pGamepads[i].bAnalogButtons[b] >= XBINPUT_BUTTONTHRESHOLD ); if( bPressed ) pGamepads[i].bPressedAnalogButtons[b] = !pGamepads[i].bLastAnalogButtons[b]; else pGamepads[i].bPressedAnalogButtons[b] = FALSE; // Store the current state for the next time pGamepads[i].bLastAnalogButtons[b] = bPressed; } } } } //----------------------------------------------------------------------------- // Name: XBInput_GetPrimaryController() // Desc: The primary controller is the first controller used by a player. // If no controller has been used or the controller has been removed, // the primary controller is the controller inserted at the lowest // port number. Function returns NULL if no controller is inserted. //----------------------------------------------------------------------------- const XBGAMEPAD* XBInput_GetPrimaryController() { static INT nPrimaryController = -1; // If primary controller has been set and hasn't been removed, use it const XBGAMEPAD* pGamePad = NULL; if( nPrimaryController != -1 ) { pGamePad = &g_Gamepads[ nPrimaryController ]; if( pGamePad->hDevice != NULL ) return pGamePad; } // Primary controller hasn't been set or has been removed... // Examine each inserted controller to see if any is being used INT nFirst = -1; for( DWORD i=0; i < XGetPortCount(); ++i ) { pGamePad = &g_Gamepads[i]; if( pGamePad->hDevice != NULL ) { // Remember the lowest inserted controller ID if( nFirst == -1 ) nFirst = i; // If any button is active, we found the primary controller if( XBInput_IsAnyButtonActive( pGamePad ) ) { nPrimaryController = i; return pGamePad; } } } // No controllers are inserted if( nFirst == -1 ) return NULL; // The primary controller hasn't been set and no controller has been // used yet, so return the controller on the lowest port number pGamePad = &g_Gamepads[ nFirst ]; return pGamePad; } //----------------------------------------------------------------------------- // Name: XBInput_IsAnyButtonActive() // Desc: TRUE if any button depressed or any thumbstick offset on the given // controller. //----------------------------------------------------------------------------- BOOL XBInput_IsAnyButtonActive( const XBGAMEPAD* pGamePad ) { // Check digital buttons if( pGamePad->wButtons ) return TRUE; // Check analog buttons for( DWORD i = 0; i < 8; ++i ) { if( pGamePad->bAnalogButtons[ i ] ) return TRUE; } // Check thumbsticks if( pGamePad->fX1 > XBINPUT_DEADZONE || pGamePad->fX1 < -XBINPUT_DEADZONE || pGamePad->fY1 > XBINPUT_DEADZONE || pGamePad->fY1 < -XBINPUT_DEADZONE ) { return TRUE; } if( pGamePad->fX2 > XBINPUT_DEADZONE || pGamePad->fX2 < -XBINPUT_DEADZONE || pGamePad->fY2 > XBINPUT_DEADZONE || pGamePad->fY2 < -XBINPUT_DEADZONE ) { return TRUE; } // Nothing active return FALSE; } #endif // INCLUDE_INPUT