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!