XBox.h
class XBOXController
{
public:
HANDLE mThread;
unsigned int ThreadKillState;
bool InUse;
POINT LAnol,RAnol;
unsigned int LTrig,RTrig;
bool A,B,C,X,Y;
bool Start,Select;
void Init();
void StartChecking();
void StopChecking();
void Check();
};
DWORD WINAPI HandleXBOX(void* nID);
Xbox.cpp
extern myApp APP;
void XBOXController::Init()
{
this->ThreadKillState=0;//started
this->InUse=false;
this->LAnol.x=0;this->LAnol.y=0;this->RAnol.x=0;this->RAnol.y=0;
this->LTrig=0;this->RTrig=0;
this->A=false;this->B=false;this->X=false;this->Y=false;
this->Start=false;this->Select=false;
}
void XBOXController::Check()
{
DWORD isController;//Is it connected?
_XINPUT_STATE ConState;//all states of controller
ZeroMemory(&ConState,sizeof(_XINPUT_STATE));
isController=XInputGetState(0,&ConState);
if(isController==ERROR_SUCCESS)
{
if(!this->InUse)
{
this->InUse=true;
std::stringstream ConRep;ConRep<<"Switched To Using Xbox Controller";
APP.Log.Write(ConRep.str().c_str());
}
if(ConState.Gamepad.bLeftTrigger!=0)
{
this->LTrig=ConState.Gamepad.bLeftTrigger;
}
if(ConState.Gamepad.bRightTrigger!=0)
{
this->RTrig=ConState.Gamepad.bRightTrigger;
}
if(ConState.Gamepad.sThumbLX!=this->LAnol.x)
{
this->LAnol.x=ConState.Gamepad.sThumbLX;
}
if(ConState.Gamepad.sThumbLY!=this->LAnol.y)
{
this->LAnol.y=ConState.Gamepad.sThumbLY;
}
if(ConState.Gamepad.sThumbRX!=this->RAnol.x)
{
this->RAnol.x=ConState.Gamepad.sThumbRX;
}
if(ConState.Gamepad.sThumbRY!=this->RAnol.y)
{
this->RAnol.y=ConState.Gamepad.sThumbRY;
}
if(ConState.Gamepad.wButtons & XINPUT_GAMEPAD_A){this->A=true;APP.KeyLog.Write("XBox A Pressed");}else{this->A=false;APP.KeyLog.Write("XBox A Released");}
if(ConState.Gamepad.wButtons & XINPUT_GAMEPAD_B){this->B=true;APP.KeyLog.Write("XBox B Pressed");}else{this->B=false;APP.KeyLog.Write("XBox B Released");}
if(ConState.Gamepad.wButtons & XINPUT_GAMEPAD_X){this->X=true;APP.KeyLog.Write("XBox X Pressed");}else{this->X=false;APP.KeyLog.Write("XBox X Released");}
if(ConState.Gamepad.wButtons & XINPUT_GAMEPAD_Y){this->Y=true;APP.KeyLog.Write("XBox Y Pressed");}else{this->Y=false;APP.KeyLog.Write("XBox Y Released");}
}
else if(this->InUse)
{
this->InUse=false;
std::stringstream ConRep;ConRep<<"NOT Using Xbox Controller";
APP.Log.Write(ConRep.str().c_str());
}
}
void XBOXController::StartChecking()
{
this->mThread=CreateThread(NULL,0,HandleXBOX,(void*)NULL,0,NULL);
}
void XBOXController::StopChecking()
{
this->ThreadKillState=1;
//while(this->ThreadKillState<2)
//{
//}
TerminateThread(this->mThread,0);
}
DWORD WINAPI HandleXBOX(void* nID)
{
while(APP.XBOX.ThreadKillState==0)
{
APP.XBOX.Check();
}
APP.XBOX.ThreadKillState=2;//Done with thread
while(APP.XBOX.ThreadKillState==1)
{
APP.XBOX.ThreadKillState=2;
}
ExitThread(0);
}
Just make sure to call XBox.Init(), then after creating the window call XBox.StartListening(), then when youre done, call XBox.StopListening()
I have the XBox class in a App class... so this is how i handled it. ;)