Skip to main content
gamedev.net
🔒 Locked

DirectInput Question

Started by gamer_1234 4 replies 900+ views
Original Post
gamer_1234
gamer_1234
I'm using DirectInput for a game that I am working on. I have it working for keyboard, mouse, and gamepad perfectly. My problem is that if someone doesn't plug the mouse or gamepad in until the game is running the device will not work. I was wondering if there was some c++ function to test when someone's plugged a device into the usb, so I know when to initialize it.
rjackets
rjackets
If I were doing it I would check for device presence every second or so (no need to check every frame / 60 times a second or anything [wink]). Also, you only need to check so long as it is not there. If you want a maximum of 4 controllers, then each time you check you would check for all the ones that are not there. If all 4 controllers are plugged in, no need to check. Have a callback process like OnDetectController( BYTE bControllerID) and have it exectute if the check detects the precense of a controller that was not there before. Obviously, you will need a variable/array to store what is plugged in (ex. BYTE bActiveControllers -- I would use a "bitmask" to store it, but BOOL array of 4 would also work). So if on Frame1 you have a BYTE of 0x0 and on the next frame you have 0x1 then you know that someone has plugged in a controller and you need to add it to the available list and check it's activity. Which is another point, you should only be checking the input of controllers that are plugged in. Don't bother updating any structures for non-existant players -- the fastest code is code that is never executed [wink].
I hope that makes sense. Let me know if you need anything cleared up.
Source
Source
You are looking for the WM_DEVICECHANGE windows message. This is called when a new device is plugged into the USB port. Then its a simple process to re-enumerate your DPlay devices and initialise the new controller.

I have implemented this system many times and it works fine. More important is reacting to devices being unplugged (which also triggers this message) - as this can seriously screw with your application =)
gamer_1234
gamer_1234
Thanks for the help, I'll try these and see if it works.
gamer_1234
gamer_1234
When I use the WM_DEVICECHANGE message in the WindowProc how can I figure out what type of device was unplugged or plugged in (mouse, joypad, etc.)
Source
Source
Well the simply answer is: you don't need to. Whenever you process a WM_DEVICECHANGE message, refresh and re-enumerate all of your DirectInput devices as you did when you first started your app. Remember you know something has changed - and it won't be happenening often, so just do a full refresh.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.