www.xbdev.net xbdev - software development
Monday December 10, 2018
home | about | contact | Donations

     
 

XBOX Programming..

More than just a toy...

 

GamePad Rumble

Author: Ben_3D@xbdev.net

 

This code will give you a shock when you first run it....you run the code...you sit there and think...nothings happening...then..."GGrrrGggRRGgg"...and the desk is shaking...and you jump..!  Its the gamepad..hehe   Sweet, really allows you to add that little extra touch of fun to your games or demos.  And the best thing of all, is that the code is really really simple.

 

 

Code: DownloadSource
//Main header file for the XDK

#include <xtl.h>

 

 

/***************************************************************************/

/*                                                                         */

/*  The whole gamepad rumble code has been packed in here!  We can see     */

/*  thats its nice and small and simple :)  I did it so that gamepad 1     */

/*  will start rumbling....nice and easy..hehe                             */

/*                                                                         */

/***************************************************************************/

 

// I copied and pasted the structure for XINPUT_FEEDBACK below, so you could

// see what it looks like.  As you might want to know whats inside it.

 

/*

struct XINPUT_RUMBLE

{

   WORD   wLeftMotorSpeed;

   WORD   wRightMotorSpeed;

};

 

struct XINPUT_STATE

{

      DWORD dwPacketNumber;

      union

      {

            XINPUT_GAMEPAD Gamepad;

      };

};

 

 

#define XINPUT_FEEDBACK_HEADER_INTERNAL_SIZE 58

 

struct XINPUT_FEEDBACK_HEADER

{

      DWORD           dwStatus;

      HANDLE OPTIONAL hEvent;

      BYTE            Reserved[XINPUT_FEEDBACK_HEADER_INTERNAL_SIZE];

};

 

struct  XINPUT_FEEDBACK

{

      XINPUT_FEEDBACK_HEADER Header;

      union

      {

        XINPUT_RUMBLE Rumble;

      };

};

*/

 

 

XINPUT_FEEDBACK Feedback = {0};

 

void GamePadRumble()

{

 

      DWORD dwInsertions, dwRemovals;

      XGetDeviceChanges( XDEVICE_TYPE_GAMEPAD, &dwInsertions, &dwRemovals );

 

 

      static HANDLE pGamePd;

 

      // dwInsertion contains the 'bitwise' information of inserted gamepads

      //

      //     etc

      //         GamePad 2

      //         | GamePad 1

      //         | | GamePad 0

      //         | | |

      // 0 0 ... 0 0 1

      if( dwInsertions & 1 )

      {

            pGamePd = XInputOpen( XDEVICE_TYPE_GAMEPAD, 0,

                                                            XDEVICE_NO_SLOT, NULL );

      }

 

      if( pGamePd )

      {

 

            Feedback.Rumble.wLeftMotorSpeed  = (WORD)64000;  // 0 to 65535

            Feedback.Rumble.wRightMotorSpeed = (WORD)0;      // 0 to 65535

 

            // Send the feedback state

            XInputSetState( pGamePd, &Feedback);

 

            while( Feedback.Header.dwStatus == ERROR_IO_PENDING)

            {

                  // Do nothing

            }

 

 

      }// End if(pGamePd)

 

}// End GamePadRumble()

 

 

//Application entry point

void __cdecl main()

{

      //--1-- Init our USB Devices, you only ever need to do this once at startup

      XInitDevices(0,0);

 

      while(true)

      {

            // This is where we call our gamepad code

            // --2-- Call our Gamepad Rumble function!  where we make it rumble!

            GamePadRumble();

 

      }// End while loop

 

}// End main()

 

 

Yup its that simple.  All the rumble code is in that single function ...'GamePadRumble()'

 

 

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.