Skip to main content
gamedev.net
🔒 Locked

XInput Question

Started by lukabratzi 5 replies 3k views
Original Post
lukabratzi
lukabratzi
Hi everyone,

I'm working on my first win32 game and I'm currently programming the Input class. Now, the input class consists of a listener class that distributes messages and events to the classes that are attached to it. Basically, I have it so that my WndProc function passes keyboard and mouse events to the Input class, which then then distributes them to each listener attached to the input class. So in this regard, my program is complete and has full use of keyboard and mouse inputs.

My problem is that I'm trying to figure out a way to get Xbox 360 controller input using the XInput api. What I'd like to do is find a way to detect changes to my xbox 360 controller in my WndProc method just like I do for keyboard and mouse changes, but I'm not sure that this is even possible. Is there some sort of message like WM_KEYDOWN that the XInput api or Xbox 360 controller will send to WndProc? That way I can just pass the message to my Input class and listeners like I do other types of messages.

If this is not possible, is there some other way that I can detect when the state of the controller changes? I'd prefer to not have to poll each button on the face of the controller for each update, I'd much rather have the actual input to the controller drive the input process. I hope that my question is clear, if anyone has any questions I'd be happy to answer them. Unfortunately, the code project is quite big so posting samples is going to be very difficult.

Thank you in advance.
Dawoodoz
Dawoodoz
I make the binary derivarion of buttons after mapping them to different actions in the game. Then it is safe from double key down events of the same type before the first key up event. There should be a separate thread to handle controllers so that low framerates don't make the game miss a fast key event.
lukabratzi
lukabratzi

I make the binary derivarion of buttons after mapping them to different actions in the game. Then it is safe from double key down events of the same type before the first key up event. There should be a separate thread to handle controllers so that low framerates don't make the game miss a fast key event.


I do map the keys to the actions that they perform, I just don't use the mapping until I'm ready to send the action to the listeners. But I still don't understand how to get Xbox 360 inputs to the listener though.
MJP
MJP
As far as I know, you can only query XInput via polling. It won't send messages or raise events or anything like that. If you wanted to, there's no reason you couldn't poll XInput and then post your own custom messages to your message queue indicating the input action that occurred.
lukabratzi
lukabratzi
Okay great, thank you for your help. I was able to get it to work via polling pretty easily, but I just wanted to make sure there wasn't an easier or cleaner method.

I do have one small problem though. I've rigged the code so that when I hit the A button, the text "A button pressed" displays to the console window. Right now, a single press generates multiple button pressed messages. I know this means that I'm polling multiple times while the button is down. Meaning that from when I begin pressing until when I let go, the button registers as a press. What I'd like to do is figure out a way to make it so that each time I press a button on the controller, it only registers once. Is this possible?
SiCrane
SiCrane
When you use XInputGetState() you fill a XINPUT_STATE structure. If between two calls the XINPUT_STATE packet number is the same then there's been no change to the controller state.
Noplace
Noplace
[font="Arial"]Sorry for the late reply but this might help, if you're interested in doing event-based input with xinput you can try calling

X

Input

GetState from WM_INPUT window message by registering the controller device as required by raw input API, however I'm not 100% sure it will work though although from my search on the internet it seems raw input API is capable of handling xinput devices.

[/font]

[font="Arial"]
[/font]


[font="Arial"]on a side note if anyone care to help me with this topic :[/font]

[font="Arial"]
[/font]

[font="Arial"]I've been using XInput[/font][font="Arial"]GetState in my main loop however i have noticed it has a lot of overhead plus I dont think I need to call it every iteration so i thought of setting up kind of a polling rate for that function, do you think it's better to have that function get called say 100hz or even less[/font]? [size=2]and then only within that limit ill be checking for state changes, etc.


Topic Locked

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

Sign in to reply to this topic.