Solved

does this code for XInput look correct?

Posted on 2008-10-27
5
630 Views
Last Modified: 2012-05-05
I'm trying to get an XBOX 360 controller to work with a small game engine which has certain requirements for what the function can return and the parameters it can take. Therefore, the below code has to return *char.

No matter what I seem to do, I get a report in from the game engine which says that the controller is disconnected. Worse, I always get "160" back from this:

DWORD Result = XInputGetState(controllerNum, &controllerState);

Does anything look obviously wrong in the code below?

The correct controller drivers are installed, and the controllers work fine with other programs.

extern "C" R_CONTROLLER_API char *GetButtonPress(int n,char *v[])
{
   static char buf[500];
   *buf=0;
   ostringstream SS;
   XINPUT_STATE controllerState;
   DWORD dwResult;
   ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
   int controllerNum = *(int *)v;
      
   
   if( (controllerState.Gamepad.sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && 
     controllerState.Gamepad.sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) && 
    (controllerState.Gamepad.sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && 
     controllerState.Gamepad.sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) )
    {	
        controllerState.Gamepad.sThumbLX = 0;
        controllerState.Gamepad.sThumbLY = 0;
    }
 
    if( (controllerState.Gamepad.sThumbRX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && 
     controllerState.Gamepad.sThumbRX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) && 
    (controllerState.Gamepad.sThumbRY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && 
    controllerState.Gamepad.sThumbRY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) ) 
    {
        controllerState.Gamepad.sThumbRX = 0;
        controllerState.Gamepad.sThumbRY = 0;
    }
   dwResult = XInputGetState(controllerNum, &controllerState);
 
/* For debugging, this snippet never finds any of the key presses
   if((XINPUT_GAMEPAD_A & dwResult)==XINPUT_GAMEPAD_A)
	Log2File("A");
   if((XINPUT_GAMEPAD_B & dwResult)==XINPUT_GAMEPAD_B)
	Log2File("B");
   if((XINPUT_GAMEPAD_X & dwResult)==XINPUT_GAMEPAD_X)
	Log2File("X");
   if((XINPUT_GAMEPAD_Y & dwResult)==XINPUT_GAMEPAD_Y)
	Log2File("Y");
*/
 
 
   SS << dwResult;
   strcat(buf,SS.str().c_str());
   Log2File(buf);
   return buf;
  
}
 
 
 
 
extern "C" __declspec(dllexport) char *IsConnected(int n,char *v[])
{
    XINPUT_STATE controllerState;
    int controllerNum = *(int *)v;
	ZeroMemory(&controllerState, sizeof(XINPUT_STATE)); 
 
    // Get the state
    DWORD Result = XInputGetState(controllerNum, &controllerState);
 
    if(Result == ERROR_SUCCESS)
    {
        return "true"; 
    }
    else
    {
        return "false";
    }
}

Open in new window

0
Comment
Question by:WriterOfCode
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
jgordos earned 250 total points
ID: 22812567
No, it doesn't.

Couple of questions...

This looks intentional, but I'm not sure if it's really what you meant to do, so I'll ask...

The routines you supplied both take two parameters: n and v
n is an integer
v is an array of char pointers...

You're doing this all over the place...

int controllerNum = *(int *)v;

But you never use 'n' that's supplied to the routine.  Is that really what you want?

Also, the routine always (ALWAYS) returns either ERROR_SUCCESS, ERROR_DEVICE_NOT_CONNECTED, or some error value.  The actual STATE of the buttons is not in the return code... it's in the data structure you passed in, so your code for button presses isn't correct.

In other words, you need to test for button A something like this....

if( ( controllerState.wButtons & XINPUT_GAMEPAD_A  ) == true )
{
// do something very profound here.
}

-john


0
 

Author Comment

by:WriterOfCode
ID: 22828326
Well there seems to be more to it, because
 if( ( controllerState.wButtons & XINPUT_GAMEPAD_A  ) == true

or any of its equivalents, doesn't register with the wired controllers. That said, microsoft just released an update for the xbox 360 controllers on windows, so hopefully that'll help.
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22831849
Yeah, I think there is more to it.

I don't think you're calculating the controller number correctly..

-j
0
 

Author Comment

by:WriterOfCode
ID: 22837890
Well, I'll accept your first answer as correct because I wouldn't believe it myself hadn't I tested it.

Apparently, XInputGetState(controllerNum, &controllerState); needs to precede a Sleep() type function because it takes a while to initialize. *sigh*

I also added a while loop to monitor the if statements until something is pressed.
0
 

Author Closing Comment

by:WriterOfCode
ID: 31510209
It was a good answer, it was also helpful in terms of understanding how some bits of the code worked. However, the true solution I stumbled across my self, because it was so inane.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
amazon fire stick 3 82
Laptop Using too much memory when no apps loaded 7 66
CD-RW/DVD-ROM DRIVE 3 56
ESXi 5.5 incompatibility when migrating VM 9 53
The Rasberry PI is a low cost piece of hardware that you can have a lot of fun with through experimenting and building/working on projects like media players, running a low cost computer, build data loggers etc. - see: https://www.raspberrypi.org
What do we know about Legacy Video Conferencing? - Full IT support needed! - Complicated systems at outrageous prices! - Intense training required! Highfive believes we need to embrace a new alternative.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question