Implementing XInput
|
We have now officially started the final assignment of Game Programming 3! In short, it’s to create a simple 3D game using what we learned during the lectures. There are some things that are required (such as GUI, music, and (obviously) a fully functional game), and also various “advanced” features. The advanced features give varying amounts of points, of which you need 30 to receive a VG grade on the assignment as well as the course. I’m working with Per Johansson and André Åström, and we are planning on making a simple racing game. One of the advanced features was to implement controller support. I took the task upon me, thinking it would end up somewhere between easy and somewhat challenging in terms of difficulity. We decided on using XInput since it was (more or less) what we had heard of in terms of controller API’s. Implementing XInput was way easier than anticipated. In terms of code, the only things you need are:
When you call XInputGetState(), the XINPUT_STATE gets filled with freshly polled data from the controllers, which can then be used however you pleased. On top of this, I added some simple Get-methods in order to actually access this data. By default, the thumbstick axises return values between -32768 and 32767. This felt a bit too precise, and also somewhat unconvenient to work with. Therefore, I divide the actual value with 128 to get a range between -255 and 255, which feels easier to work with. Buttons were slightly more difficult, but not by much. The buttons are stored as a bitmask, where if the corresponding bit is set, the button is pressed. To check if a specific button is pressed, I perform a bitwise AND (&) to check if that bit is indeed set, and return true or false based on that. This was worth 5 out of the 30 points for a VG grade. In my opinion, it was way too easy for even 5 points. My implementation can be found here (header) and here (source).
|