xboxscene.org forums

Author Topic: Getting Input From A Controller  (Read 220 times)

razorrifh

  • Archived User
  • Sr. Member
  • *
  • Posts: 329
Getting Input From A Controller
« on: May 03, 2003, 02:41:00 PM »

CODE
   if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN & XINPUT_GAMEPAD_A )
   {
 ReBoot();
   }


this isnt working. i'm pretty sure its because i'm not handling how hard the a button is pushed, but i cant really find anything on this i understand on the xdk help file. some input would be nice. (also the x2 igr code doesnt work on this, so i'm wondering why...)

*edit* it works with just the XINPUT_GAMEPAD_DPAD_DOWN, but not the a button
Logged

razorrifh

  • Archived User
  • Sr. Member
  • *
  • Posts: 329
Getting Input From A Controller
« Reply #1 on: May 04, 2003, 10:36:00 PM »

biggrin.gif
CODE
int 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 1;
   }

   if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN )
   {
 return 2;
   }

   if( (myGamePad.wButtons & XINPUT_GAMEPAD_START ) && (myGamePad.wButtons & XINPUT_GAMEPAD_BACK ))
   {
   //   ReBoot();
 return 3;
   }

   return 0;
}



but when i add this line, the code instantly reboots to evox
CODE
int 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 1;
   }

   if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN )
   {
 return 2;
   }

   if( (myGamePad.wButtons & XINPUT_GAMEPAD_START ) && (myGamePad.wButtons & XINPUT_GAMEPAD_BACK ))
   {
   //   ReBoot();
 return 3;
   }
   

// REBOOTS TO DASH ************************************************
   if( (myGamePad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) > 0 )
   {
   //   ReBoot();
 //return 3;
   }

// REBOOTS TO DASH ************************************************

   return 0;
}


i have no idea why this isnt working. ive been looking at all the xbox tut sources and they're no help.  huh.gif  blink.gif
Logged

Wishi

  • Archived User
  • Full Member
  • *
  • Posts: 191
Getting Input From A Controller
« Reply #2 on: May 06, 2003, 01:41:00 PM »

There's a good chance that it's rebooting instantly due to the fact that the triggers are analog and currently you're checking for the slightest movement (anything > 0) so any press of the right trigger no matter how small would cause a reboot.
Logged

razorrifh

  • Archived User
  • Sr. Member
  • *
  • Posts: 329
Getting Input From A Controller
« Reply #3 on: May 06, 2003, 02:57:00 PM »

sad.gif )
Logged

razorrifh

  • Archived User
  • Sr. Member
  • *
  • Posts: 329
Getting Input From A Controller
« Reply #4 on: May 07, 2003, 03:11:00 PM »

CODE
/***************************************************************************/
/*                                                                         */
/* FileName: main.cpp                                                      */
/*                                                                         */
/* Details: gamepad basics                                                 */
/*                                                                         */
/* Author: [email protected]                                           */
/*                                                                         */
/*                                                                         */
/***************************************************************************/

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

/***************************************************************************/
/*                                                                         */
/*                                                                         */
/* Details: Determines if the keypad buttons have been pressed             */
/*                                                                         */
/* Auth: x-factor (http://www.xfactordev.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        ***
*       ___|___               xfactordev.net                               *
*          |                                             X    A            *
 *        |                                                              *
 *                                                                      *
  *          x 3                                           |           *
   *         x                                          ___|___       *
    *    xxxxxxxxx    *****************************        |         *
     *       x       *                             *       | 2      *
  *       x      *    start         back         *              *
   *             *                               *             *
    *           *                                 *           *
      *********                                     *********
*/

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


#include  // this is the xdk..xbox libraries


LPDIRECT3D8 g_pD3D = NULL;                      // DirectX Object
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;          // Screen Object

const SHORT XINPUT_DEADZONE = (SHORT)( 0.24f * FLOAT(0x7FFF) ); // Deadzone for the gamepad inputs

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

void CleanUp()
{
     g_pD3DDevice->Release();
     g_pD3D->Release();
}

/***************************************************************************/
/*                                                                         */
/* 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..                                                             */
/*                                                                         */
/***************************************************************************/


int 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 1;
   }

   if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN )
   {
 return 2;
   }

   if( (myGamePad.wButtons & XINPUT_GAMEPAD_START ) &&(myGamePad.wButtons & XINPUT_GAMEPAD_BACK ))
   {
   //   ReBoot();
 return 3;
   }
   
   if( (myGamePad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) > 50 )
   {
   //   ReBoot();
 //return 3;
   }

   return false;
}






LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Vertices Buffer

struct CUSTOMVERTEX
{
   FLOAT x, y, z, rhw; // The transformed position for the vertex.
   DWORD colour; // The vertex colour.
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)


void DrawTriangle(int tx)
{
   VOID* pVertices;
   
   //Store each point of the triangle together with it's colour
   CUSTOMVERTEX cvVertices[] =
   {
       {float(tx)+250.0f, 100.0f, 200.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
      {float(tx)+120.0f, 180.0f, 20.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
      {float(tx)+750.0f, 10.0f, 100.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
     
   };

   //Create the vertex buffer from our device
   g_pD3DDevice->CreateVertexBuffer(1 * sizeof(CUSTOMVERTEX),
                                              0,
             D3DFVF_CUSTOMVERTEX,
                                              D3DPOOL_DEFAULT,
             &g_pVertexBuffer);

   //Get a pointer to the vertex buffer vertices and lock the vertex buffer
   g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);

   //Copy our stored vertices values into the vertex buffer
   memcpy(pVertices, cvVertices, sizeof(cvVertices));

   //Unlock the vertex buffer
   g_pVertexBuffer->Unlock();

   //Rendering our triangle
   g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
   g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
   g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
}







//Application entry point
void __cdecl main()
{
   InitialiseD3D();
   XInitDevices(0, 0);
   int xval = 9;

   // 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 )
   {
   
   

     // Clear the backbuffer to black
           g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                               D3DCOLOR_XRGB(0,  0, 255), 1.0f, 0);
           //                                r   g   b
   
           // Begin the scene
           g_pD3DDevice->BeginScene();
   
   int bb = GamePad();
 if( bb == 1 )
 {
    if(xval>638){
   
    }else{
   xval++;
   xval++;
    }
 }


 if( bb == 2 )
 {
    if(xval<2){
   
    }else{
   xval--;
   xval--;
    }
 }

 if( bb == 3 )
 {
    ReBoot();
 }

   //DrawTriangle(xval);   //commented out to focus on the real code error



           // End the scene
           g_pD3DDevice->EndScene();
   
           // Flip 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();

   ReBoot();
}


Logged