|
In the latest download of our shoot em up pc game TWTPB, iteration 23, I implemented basic game pad support. There are a number of issues with this implementation maybe the most prominent one being that it does not handle plugging in and out the game pad during play. I think that it is probably a common scenario to plug in a game pad after the game has started. This is a short tutorial on how to get your game to react when an usb device, such as a game pad, is plugged in or removed from the computer. This is handy when using the DirectInput api. XInput handles this in a transparent way which is nice, but alas there are many game controls that are not XInput compatible so when developing a pc game you probably need support for both plain DirectInput devices and XInput devices. This tutorial is Windows specific and in the C++ language. It is not that hard to get a message when a device is added or removed, but it involves some rarely used win32 code that I spent a good few hour trying to get to work. To start things off Windows sends you the handy
The there are also a number of other messages sent in the You will have to tell Windows that you want additional information when a device is added. You can do this with the RegisterDeviceNotification function. This function is quite complicated and takes a number of strange parameters. I for one love when a function has a void pointer as a parameter, it's great, you can just send anything down there Anyway the thing we want is to listen to in our game is the device broadcast messages. The following code sets that up. I added this to my window creation function since you need a window handle to the main window, declared as
By using the handy Also for this code to even compile you also need to have WINVER defined >= 0x0500 like
Now Windows will send you all messages when a game pad is attached or removed from the pc and you can take the appropriate action in your game code. Hope you'll find this short article/tutorial handy! Feel free to comment or suggest improvements. |
Submitted by hObbE
Sun, 10/12/2008 - 15:46
|




May I ask you what language
May I ask you what language it is? Looks a bit like c++, but i can't tell since I don't use it *hinting that you maybe should tell in the tut*
______________________________________________
"The time you enjoy wasting isn't wasted time"
Yepp it's C++ alright. Good
Yepp it's C++ alright. Good point
Added some more color coding
Added some more color coding and clarified the m_hWnd parameter some more...
All interfaces?
Using all interfaces will set up listening to hard disks and all kinds of other stuff.
It's not so hard to set this to just HID using a simple little constant array you set up.
Just set the notification filter's dbcc_classguid to this value:
static const GUID GuidDevInterfaceHID =
{0x745a17a0, 0x74d3, 0x11d0,
{ 0xb6, 0xfe, 0x00, 0xa0, 0xc9, 0x0f, 0x57, 0xda }};
notificationFilter.dbcc_classguid = GuidDevInterfaceHID;
There are two class GUIDs that both work for HIDs, this is just one of them (they both work exactly the same, not sure why there are two available).
Neat! Thanks for the info
Neat!
Thanks for the info I'll make sure to check this out when I get back to coding after the summer
http://www.packersmall.com
Packers Super Bowl Jersey
Aaron Rodgers Jersey
Charles Woodson Jersey
Clay Matthews Jersey
Jermichael Finley Jersey
Jordy Nelson Jersey
Greg Jennings Jersey
A.J. Hawk Jersey
Aaron Kampman Jersey
Al Harris Jersey
B.J. Raji Jersey
Bart Starr Jersey
Brandon Jackson Jersey
Donald Driver Jersey
James Starks Jersey
Nick Barnett Jersey
Paul Hornung Jersey
Ray Nitschke Jersey
Reggie White Jersey
Ryan Grant Jersey
William Henderson Jersey
Women's Aaron Rodgers Jersey
Women's Charles Woodson Jersey
Women's Clay Matthews Jersey
Women's Jermichael Finley Jersey
Aaron Rodgers Youth Jersey
Clay Matthews Youth Jersey
Jermichael Finley Youth Jersey
Authentic Aaron Rodgers Jersey
Authentic Clay Matthews Jersey
Authentic Charles Woodson Jersey
Authentic Jermichael Finley Jersey
Authentic Greg Jennings Jersey
Authentic Jordy Nelson Jersey
Post new comment