0

I am trying to write a program in C++ in Visual Studio, to test some of the functionalities of an XBOX controller. I am using xinput and so for can register if a controller is connected to my computer, however, I can work out how to tell if a button has been pressed. So far I have this as the check button function

if (state.Gamepad.wButtons == XINPUT_GAMEPAD_A) {
   cout << "A\n";
}

right now I only want it to print out 'A' but it is not working, is there anything else I need to add?

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Jonski Goldstein
  • 169
  • 5
  • 16
  • You may want to post [some more code](http://sscce.org). – zneak Aug 10 '16 at 02:49
  • I'm guessing wButtons is a bit field for all the buttons, so you may want to test for a single bit. if(...wButtons & XINPUT_GAMEPAD_A) { ... } – PaulHK Aug 10 '16 at 03:44
  • It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Aug 10 '16 at 08:59

1 Answers1

0

This information is helpful if you are familiar with creating object classes and using them and based upon an assignment I had to complete for Uni.

First you may want to start off by creating a class to store data for each individual controller you connect.

class xboxController {
private:

public:
};

Now you have somewhere to start, but still are going to be unable to add multiple controllers? What if you want to do something on one controller and something else on another? You could create an overloaded constructor which you could manually assign an index for the controller.(HINT: you will need something to store this for further usage)(HINT2: index's start at 0).

class xboxController {
private:

public:
   //Default constructor
   xboxController();
   //overloaded constructor
   xboxController(int i){...};
};

Then you want a function that you can use to check on that particular controller, what button has been pressed. In a previous assignment for class, i used a bool as shown below:

//NOTE, this takes in a parameter defined by you and checks it.
bool checkButtonPress(WORD button){...};

But you would also need a member in your class in which you can store the state of the controller (Xbox controllers use the Struct XINPUT_STATE).

XINPUT_STATE controllerState

Now you have somewhere to store the state, you can finish the bool:

bool checkButtonPress(WORD press){
    return (controllerState.Gamepad.wButtons & press) != 0;
}

XInput provides a function that is able to receive information regarding the state of the controller. For simplicity sake, you should create a function in order to call this function whenever you need to update/get the state of the controller.

void getState(){
//Understand what these lines are doing, and how to use them for your own needs.
   ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
   XInputGetState(controllerNumber,&controllerState);
}

Hence, in your main, you will be able to assign a controller to an index for your easy management of that controller by using the overloaded constructor and retrieving the state of the controller). Then you will be able to use an object for each controller, and be able to send commands to each controller. Through the use of the bool you will be able to check which button is being pressed and be able to tell the controller, or the computer what to do with that input.

A little snippet of how to use the code is as follows:

//This program will keep checking and print a message when 
//A is pressed on the controller and will terminate when you press B
int main() {
  xboxController xbox(1);
  bool isFinished = false;
  while (isFinished == false) {
    xbox.getState();
    if(xbox.checkButtonPress(XINPUT_GAMEPAD_A)){
      cout << "Button A was pressed" endl;
    }
    else if(xbox.checkButtonPress(XINPUT_GAMEPAD_B)){
      cout << "Program will now terminate" << endl;
      isFinished = true;
    }
  }
  return 0;
}

Furthermore, if this is for MTRN2500, please be aware that I have demonstrated and submitted my code and this is only for help, and not to be copied. Thanks!