Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

C++ Win32 Using XBox Controller For Input With Windows Messages

This topic is 2503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i started thinking... how cool would it be too play my game with a controller? :D

But i have no clue how i would receive window messages from a wired controller... where should i start?

Share this post


Link to post
Share on other sites
Advertisement
You don't use Win32 for that, you use XInput (part of DirectX)

http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014%28v=vs.85%29.aspx

Share this post


Link to post
Share on other sites

Darn... Hopefully i will be able to merge the XInput code with the Win32 message code i have for key-presses... do you think its possible?


Yes it is possible and pretty much recommended. (Microsoft recommends using Win32 for keyboard/mouse, DirectInput for gamepads/joysticks and XInput for xbox360 controllers or other XInput capable devices)

You could treat the xbox360 controller as a DirectInput device if you want, but you will miss out on some functionality, Using joySetcapture is probably not a good idea as its pretty darn ancient and will not work very well with a xbox360 controller. (the old joystick handling code only supports one analog stick and 4 buttons per joystick and no more than 2 joysticks total).

Share this post


Link to post
Share on other sites
Im going to use XInput and try to merge the code for XBox and joystick related items into my key-press functions... wish me luck! tongue.png

Entry 1: 11:04

DX It took me this long too get the library working... so now i can really work! :D

Share this post


Link to post
Share on other sites
Ok, i have it so the controllers can connect and such, but i cant seem so get the presses... what is wrong with this code?

unsigned int ConsUsing=0;
for(unsigned int Con=0;Con<4;Con++)
{
DWORD isController;//Is it connected?
_XINPUT_STATE ConState;//all states of controller
ZeroMemory(&ConState,sizeof(_XINPUT_STATE));
isController=XInputGetState(Con,&ConState);
if(isController==ERROR_SUCCESS)
{
if(!APP.UseXBOX[Con])
{
APP.UseXBOX[Con]=true;
std::stringstream ConRep;ConRep<<"Switched To Using Xbox Controller On Port: "<<Con;
APP.Log.Write(ConRep.str().c_str());
}
ConsUsing++;
}
else if(APP.UseXBOX[Con])
{
APP.UseXBOX[Con]=false;
std::stringstream ConRep;ConRep<<"NOT Using Xbox Controller On Port: "<<Con;
APP.Log.Write(ConRep.str().c_str());
}
}
if(ConsUsing!=0)
{
APP.UsingXbox=true;
}
//after alot of other stuff regarding normal key presses
else if(APP.UsingXbox)
{
for(unsigned int Con=0;Con<4;Con++)
{
_XINPUT_STATE ConState;//all states of controller
ZeroMemory(&ConState,sizeof(_XINPUT_STATE));
XInputGetState(Con,&ConState);
if(ConState.Gamepad.bLeftTrigger>0)
{
std::stringstream ConRep;ConRep<<"XBox Controller Pressed Start On Port: "<<Con;
APP.Quit(ConRep.str().c_str());
APP.Log.Write(ConRep.str().c_str());
}
}
}

Share this post


Link to post
Share on other sites
As it stands there is nothing inherently wrong with your code, but that's given the fact that we know little about the structure of your entire program. A little bit more information and description is needed in order to figure out the problem. Based upon the small sampling above, there is no problem with the logic.

Although I have a bit of a recommendation. It's not necessary to poll the state of all controllers twice. Simply fold the detection of whether a controller is connected into the logic that handles input. It's only necessary to poll a controller's state once per update, seeing as it is the state.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!