Game controller wrapper.
Just finished a nice piece of code to manage gamepads (and potentially joysticks) in an efficient and clean way.
For start, you just ask Input manager to start a game controller
input::InputManager::openGameController(0);
To get the information out of the InputManager, you call:
input::DataFrame input_frame;
input::InputManager::getInputFrame( &input_frame );
To detect change events (equivalent to button pressed), you have to declare the DataFrame as a class parameter. Each time you call getInputFrame, the events are rendered.
To access events and input values, you have to read your input_frame. It contains a vector of GameControllerFrames. Each GameControllerFrame has an id, corresponding to the id you gave in openGameController( ID );
A GameControllerFrame also contains several maps:
- axis: a map of vector2d for 3 axis: left & right sticks + trigger.x (~LT) & trigger.y (~RT). Vectors are normalised and stabilised.
- buttons: a map of boolean, true if the button is pressed
- axis status: indicate if the axis is active, inactive, has just been activated or deactivated
- button status: indicate if the button is active, inactive, has just been activated or deactivated
activated and deactivated are there only for one update. After that the status becomes generally active or inactive.
It is important to understand that these status depends on the frequency of InputManager::getInputFrame( &input_frame ). The previous data frame is stored in the local DataFrame, NOT in the InputManager. The idea is to make a clean data retrieval when needed, and not risking interruption during the main loop.
The InputManager runs on its own thread and does not trigger events in the main application.
The code is available in the example-gamepad.2.0 in bitbucket. See related issue.