This is the final part of my Engine feature series. Previous posts are here and here. My main focus this week is to make my library fast and stable, with less bugs. Most of the code changes I’ve done are under the hood.
The major update this week is the support for specifying the trigger and stick deflection dead zones both in Lua and the launcher. I also added support for multiple controller remapping in the launcher.
Adding multiple controller and dead zone support was not much of a work, but i had to think about the way i have to arrange my binary data, so that the file size is not large and it is also easy to retrieve data from it. Since reading binary data is reading from the memory and incrementing the pointer to the next address to read that data, we can inadvertently access data that might not belong to the file. Also, in the case of deadzones, if the player does not enter any data, we do not write anything to the file, but we must have some way of checking that while reading, otherwise we would be reading garbage values. Keeping in mind these things, i’ve decided to add the controller number in front of the controller data and have 1 byte before each deadzone to store where there is data or not in that.
The 0 above yellow line is the controller number and the 0’s above red line indicate that there is no dead zone data.
The updated launcher looks like below.
What the project does:
The project allows you to
- Check when a button is pressed on a controller.
- Get the actual and normalized deflections for both triggers and sticks
- Register and deregister functions that are assigned to specific buttons, which are called when that button is pressed.
- Set vibration effects for the motors in the buttons.
How to use the library:
-
- Download the project files for controller input and launcher.
- Add code to initialize and clean up the controller input in cbApplication.cpp
- Include the header “Engine/ControllerInput/ControllerInput.h”
- All the functions are present in eae6320::UserInput::ControllerInput namespace
- All the functions require passing of the controller keys which are an enum.
- Some examples of using the library
if (GetNormalizedStickDeflection(ControllerKeyCodes::LEFT_STICK, 0).x != 0) { m_Camera->SetAngularSpeed(GetNormalizedStickDeflection(ControllerKeyCodes::LEFT_STICK, 0).x); } if (IsKeyPressed(ControllerKeyCodes::RIGHT_TRIGGER)) { m_Camera->SetCameraVelocity(Math::sVector(0, GetNormalizedTriggerDeflection(ControllerKeyCodes::RIGHT_TRIGGER), 0)); } if (IsKeyPressed(ControllerKeyCodes::LEFT_TRIGGER)) { m_Camera->SetCameraVelocity(Math::sVector(0, -GetNormalizedTriggerDeflection(ControllerKeyCodes::LEFT_TRIGGER), 0)); }
-
- To register and deregister functions:
RegisterFunctionForCallback(ControllerKeyCodes::B, [this] {return Test(); }, 0); RemoveKeyFromCallback(ControllerKeyCodes::B);
- To Use the launcher:
- Add it as a build dependency to the Game project so that it will build before the game does in the same folder.
- If your game name is different from “MyGame.exe”, you need to change it in “MainWindow.xaml.cs” at this line
-
gameProcess.StartInfo.FileName = "MyGame.exe";
- Enjoy!!
- Contact me at my email if you encounter any bugs!
What I’ve learned from this project/What I have used in this project that I have learned over the past 6 months:
- Creating platform independent interfaces for others to use.
- Working with a third party library. In this case XInput.
- Working with both Lua and binary files.
- Working on binary files in C# and WPF and calling the game from the launcher.
What gave me lot of trouble ;(
- Working with function pointers and std::function to use with the callback feature.
- Creating separate paths for different builds in WPF. Since WPF by default builds in a platform agnostic way. I had to make a lot of changes to the project settings to get the launcher to build in the same folder as the game.
Where to go from here:
I am planning to continue working on this project in the future. Since me and hopefully some others in my class have to use this project in the upcoming tech demo game, I might encounter some bugs which i will be patching and updating here. I am also planning on continuing the work i was doing with audio during the winter break whose progress I will updating in future blog posts.
Downloads:
The code provided below is released under the 2-Clause BSD license.