www.xbdev.net
xbdev - software development
Thursday March 28, 2024
home | contact | Support | XBOX Programming.. More than just a toy...

     
 

XBOX Programming..

More than just a toy...

 

Prt8 - "GamePad Basics..."

Author: bkenwright@xbdev.net

 


 

It took a while for this tutorial to reach the world....but its worth waiting for....as with this knowledge you'll be putting games together before you know it....as what use is it to display wonderful 3D characters and images if you can't interact with them.  So hold on to your hair and we'll show you the basics...

 

So what does the xbox game pad look like?...heheh...well I've put a sketch of it together below, so you an see it...just remember there only buttons...so we keep checking the gamepad to see if you've pressed a button, and if you have, respond accordingly.

 

 

Gampad xbdev.net

 

 

What a great looking game pad...heheh...but how do we start?  How are you going to get input from this gamepad and use it in your code?  Well follow us step by step and you'll be a gamepad guru in no time.

 

Now if you look at the xdk examples, you'll get lost before you get started...so many wrapper classes hiding the true simplisity of the gamepad!  "Why can't things be easy!" your saying...well they are now...read on...

 

For this first part, make sure your gamepad is plugged into the left port...as this is input port 0...the far right one is port 3.

 

 

The code...use the code...

 

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

/*                                                                         */

/* FileName: main.cpp                                                      */

/*                                                                         */

/* Details: gamepad basics                                                 */

/*                                                                         */

/* Author: Ben_3D@xbdev.net                                           */

/*                                                                         */

/*                                                                         */

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

 

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

 

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

/*                                                                         */

/*                                                                         */

/* Details: Determines if the keypad buttons have been pressed             */

/*                                                                         */

/* Auth: x-factor (http://www.xbdev.net)                              */

/*                                                                         */

/* Main functions:                                                         */

/* Allows you to understand how the gamepad works, and give you a chance   */

/* to build a self contained gamepad class that will meet all of your      */

/* gameing needs.....                                                      */

/*                                                                         */

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

 

/*

     *****************************************************************

  ***                                                                 *** 

***        | 1                                             Y   B        ***

*       ___|___               xbdev.net                               *

*          |                                             X    A            *

  *        |                                                              *

  *                                                                      *

   *          x 3                                           |           *

    *         x                                          ___|___       *

     *    xxxxxxxxx    *****************************        |         *

      *       x       *                             *       | 2      *

        *       x      *    start         back         *              *

         *             *                               *             *

          *           *                                 *           *

            *********                                     *********

*/

 

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

 

 

#include <xtl.h>  // this is the xdk..xbox libraries

 

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

/*                                                                         */

/* Well the function name should say it all, but its so that we can        */

/* see if the gamepad has been pressed, so when we do press a button, we   */

/* need some feedback, so I added this function....its nothing to do with  */

/* the gamepad..heheh ..just incase you didn't know..                      */

/*                                                                         */

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

 

void ReBoot()

{

   LD_LAUNCH_DASHBOARD LaunchData = { XLD_LAUNCH_DASHBOARD_MAIN_MENU };

   XLaunchNewImage( NULL, (LAUNCH_DATA*)&LaunchData );

}

 

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

/*                                                                         */

/* This is it...the code that explains how the gamepad works...its not to  */

/* long I didnt' think...coudlnt really make it any smaller than this....  */

/* But hopefully after looking at it for a second or to you can see what is*/

/* happening..                                                             */

/*                                                                         */

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

 

 

bool GamePad()

{

      XINPUT_GAMEPAD myGamePad;

 

     

      // Definition of what XINPUT_GAMEPAD looks like:

      /*

            struct

            {

                  WORD    wButtons;

                  BYTE    bAnalogButtons[8];

                  SHORT   sThumbLX;

                  SHORT   sThumbLY;

                  SHORT   sThumbRX;

                  SHORT   sThumbRY;

            } XINPUT_GAMEPAD;

      */

 

     

 

    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 )

    {

            XINPUT_STATE myInputStates;

            XInputGetState( pGamePd, &myInputStates );

 

            memcpy( &myGamePad, &myInputStates.Gamepad, sizeof(XINPUT_GAMEPAD) );

 

    }

     

      // Okay simple but effective in this example, but you press the gamepad

      // up button, and it returns true!

      if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_UP )

      {

            return true;

      }

 

      return false;

}

 

//Application entry point

void __cdecl main()

{

 

      XInitDevices(0, 0);

 

      // We keep checking the gampad, when it return true, the up button on the gamepad

      // was pressed and it returns true, and we leave the loop and reboot our xbox.

      while( true )

      {

            bool bb = GamePad();

            if( bb )

            {

                  break;

            }

      }

     

      ReBoot();

}

 

 

 

So what are the main pieces...the parts that make it tick and why? If you compile and run this code, all that happens is that the console will sort of freeze, it wont' do anything ....then when you press the up key it will reboot your xbox....reacting to an input from your xbox.

 

Code explained...now before we can use the gamepad, or any input device with the xbox, we first need to call 'InitDevices(0,0)', if you don't call this function first then your xbox won't respond...just stay in the loop....you call this function once and only once.

 

'XGetDeviceChanges(..)' is called to see which devices are available...as you can unplug and plug in gamepad's whenever you want...but if you don't test to make sure that its there it can cause your xbox to crash!  It might have a gamepad plugged in, then you unplug it in the middle of the loop, and it still things it's there...so the xbox gets excited and crashes!

 

'HANDLE pGamePd = XInputOpen( XDEVICE_TYPE_GAMEPAD, 0, XDEVICE_NO_SLOT, NULL );'  is what connects to your gamepad...when you call this, you specify 0, which means gamepad 0 (far left one) and it returns a handle to it.... you can use this handle to test for button presses in another function.

 

 

XINPUT_STATE myInputStates;
XInputGetState( pGamePd, &myInputStates ); ... is not to hard to understand, we pass this function our valid handle, and a reference to our XINPUT_STATE structure, the XInputGetState function fills in the structure with the gamepad information, key presses etc.

 

We can now use the myInputStates structure...see if anything has happened...as if you look at it, its got some members in it which are defined as:

 

      // Definition of what XINPUT_GAMEPAD looks like:

      /*

            struct

            {

                  WORD    wButtons;

                  BYTE    bAnalogButtons[8];

                  SHORT   sThumbLX;

                  SHORT   sThumbLY;

                  SHORT   sThumbRX;

                  SHORT   sThumbRY;

            } XINPUT_GAMEPAD;

      */

 

 

So to check for various buttons we can do this:

 

if ( myInputStates.wButtons & XINPUT_GAMEPAD_DPAD_UP )

    /*Code to move up */

 

if (  myInputStates.wButtons & XINPUT_GAMEPAD_DPAD_DOWN )

   /*Code to move down*/

 

if ( myInputStates.wButtons & XINPUT_GAMEPAD_DPAD_LEFT )

   /*Code to move left*/

 

if ( myInputStates.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT )

   /*Code to move right*/

 

 

.... you get the idea... so to test for button presses ..

 

if( myInputStates.bPressedAnalogButtons[XINPUT_GAMEPAD_BLACK] )

    /*Button pressed*/

 

if( myInputStates.bPressedAnalogButtons[XINPUT_GAMEPAD_WHITE] )

 

 

This should lead you down the road to creating a CGamePad class...which has an initilisation function and a polling function, so you can test for gamepad presses repeatedly....I've shown how to test for game input for a single gamepad ( 0 ), but you can test for the other 3, just create an array of XINPUT_GAMEPAD[4] structure and put your test code in a loop, test each one.

 

(Back Tutorials Page)

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.