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

     
 

XBOX Programming..

More than just a toy...

 

GamePad Information

Author: Ben_3D@xbdev.net

 

Quite a while ago now...I did some simple code on testing for GamePad inputs.... but to give a better feel for the GamePad, I wrote this Second tutorial...which will give you an insight of whats in the "XINPUT_GAMEPAD" which is what the gamepad is!  We check for input over and over again in our main render loop...and if there is an input, the details are put into this structure.  Its details are shown below:

 

 

                                struct XINPUT_GAMEPAD

                                {

                                      WORD    wButtons;

                                      BYTE    bAnalogButtons[8];

                                      SHORT   sThumbLX;

                                      SHORT   sThumbLY;

                                      SHORT   sThumbRX;

                                      SHORT   sThumbRY;

                                };

 

Its not that bad is it?  Well people usually get scared when they read the docs, and it starts to say...this 'bit' is for this...and this 'bit' should be tested for that...eeekKKk...  New programmers always get worried about bit testing.  But believe me, once you run up my simple code sample below, which dumps the contents of this structure to the screen...and will display the values in them while you play around with the gamepad, your eyes will light up...and you really will see how it works.

 

Things you'll notice is which buttons are analog, and which are digital...... and you'll also be able to see just how sensitive they really are.

 

Here is a quick screenshot of what you'll be seeing:

 

 

For this screenshot, I pressed the up button and the Green (A) button both down hard!  As you'll soon see...the Green(A), Red(B), Yellow(Y) and Blue(X) are all analog..even though they look digital on the gamepad :)   ...heheh...well they did to me :)

 

So here is the simple code!

 

Download Source Code
//Main header file for the XDK

#include <xtl.h>

 

#include <xfont.h>                              // used for directX textout

#include <stdio.h>                              // used for swprintf

 

LPDIRECT3D8 g_pD3D = NULL;                      // DirectX Object

LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;          // Screen Object

 

void InitialiseD3D()

{

    //First of all, create the main D3D object. If it is created successfully we

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

 

    //Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

 

    //Fill the structure.

    // Set fullscreen 640x480x32 mode

 

      d3dpp.BackBufferWidth = 640;

      d3dpp.BackBufferHeight = 480;

      d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

 

 

      // Create one backbuffer and a zbuffer

      d3dpp.BackBufferCount = 1;

 

      // Set up how the backbuffer is "presented" to the frontbuffer each time

      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

 

    //Create a Direct3D device.

    g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,

                                   D3DCREATE_HARDWARE_VERTEXPROCESSING,

                                                   &d3dpp, &g_pD3DDevice);

 

    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

}// End InitialiseD3D()

 

 

void CleanUp()

{

    g_pD3DDevice->Release();

    g_pD3D->Release();

}// End CleanUp()

 

 

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

/*                                                                         */

/*  A very simple Text Output Function.                                    */

/*  It uses the basic of code, and you need to include <xfont.h> to use    */

/*  this code - its simple and its totally self contained....quick and     */

/*  simple if you need to check a dynamic value or do some simple          */

/*  debugging :)                                                           */

/*                                                                         */

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

 

void DisplayText(char * szStr, long xpos=100, long ypos=100 )

{

      //Create some DirectX text buffers

      XFONT*                        pFont; 

      LPDIRECT3DSURFACE8      pBackBuffer;

 

    //InitialiseFonts

    g_pD3DDevice->GetBackBuffer( 0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer);

 

      XFONT_OpenDefaultFont( &pFont );

 

      WCHAR szbuff[200] = {0};

      swprintf(szbuff, L"%S", szStr);

 

      pFont->SetTextColor(D3DCOLOR_XRGB(30,255,20));

 

      //Display our text.

      pFont->TextOut( pBackBuffer, szbuff, -1, (long)xpos, (long)ypos );

 

      //Release our TextBuffers

      pFont->Release();

      pBackBuffer->Release();

}// End DisplayText()

 

 

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

/*                                                                         */

/*  This is what here here to see!  Its our GamePad code..we call this     */

/*  From our main render loop, and we check for various inputs.  Then      */

/*  Because this is a tutorial, we display the information on the screen   */

/*  And we follow this by drewling..heheh...wow...you can really see it    */

/*  working you'll be saying :)..hehe                                      */

/*                                                                         */

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

 

void 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) );

 

 

            // Lets display all the information which is given to use in our XINPUT_GAMEPAD

            // structure, and we can monitor how it changes in relation to gamepad

            // input (e.g. button presses..hehe)

 

            long xpos=60;

            long ypos=25;

            long height=25;

 

            char bufA[1000]; // Large temp char buffer for our text

 

            strcpy(bufA, "GamePad One Information Data:");

            DisplayText(bufA, xpos, ypos+=height);

            ypos+=10;

 

            sprintf(bufA, "XINPUT_GAMEPAD::wButtons: 0x%04X", myGamePad.wButtons);

            DisplayText(bufA, xpos, ypos+=height);

                 

            for(int i=0; i<8; i++)

            {

                  sprintf(bufA, "XINPUT_GAMEPAD::bAnalogButtons[%d]: 0x%01X", i, myGamePad.bAnalogButtons[i]);

                  DisplayText(bufA, xpos, ypos+=height);

            }// End for loop

 

            sprintf(bufA, "XINPUT_GAMEPAD::sThumbLX: 0x%02X", myGamePad.sThumbLX);

            DisplayText(bufA, xpos, ypos+=height);

            sprintf(bufA, "XINPUT_GAMEPAD::sThumbLY: 0x%02X", myGamePad.sThumbLY);

            DisplayText(bufA, xpos, ypos+=height);

            sprintf(bufA, "XINPUT_GAMEPAD::sThumbRX: 0x%02X", myGamePad.sThumbRX);

            DisplayText(bufA, xpos, ypos+=height);

            sprintf(bufA, "XINPUT_GAMEPAD::sThumbRY: 0x%02X", myGamePad.sThumbRY);

            DisplayText(bufA, xpos, ypos+=height);

 

      }// End if(pGamePd)

 

 

}// End GamePad()

 

 

//Application entry point

void __cdecl main()

{

      InitialiseD3D();

 

        XInitDevices(0,0);

 

      while(true)

      {

            //Clear the backbuffer to black

            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(55, 55, 100), 1.0f, 0);

            //Begin the scene

            g_pD3DDevice->BeginScene();

 

 

                  // Check our gamepad for data...input...gampad 1 only.

                  GamePad();

 

            //End the scene

            g_pD3DDevice->EndScene();

 

            //Filp the back and front buffers so that whatever has been rendered on the back buffer

            //will now be visible on screen (front buffer).

 

            g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

      }    

      void CleanUp();

 

}// End main()

 

 

 

 

 

 

 
 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.