Implement controller gyroscope in SDL

This commit is contained in:
wheremyfoodat 2024-08-14 22:35:02 +03:00
parent 88e0782f71
commit d208c24c0c
6 changed files with 54 additions and 1 deletions

View file

@ -2,6 +2,8 @@
#include <glad/gl.h>
#include "sdl_gyro.hpp"
FrontendSDL::FrontendSDL() : keyboardMappings(InputMappings::defaultKeyboardMappings()) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
Helpers::panic("Failed to initialize SDL2");
@ -20,6 +22,8 @@ FrontendSDL::FrontendSDL() : keyboardMappings(InputMappings::defaultKeyboardMapp
SDL_Joystick* stick = SDL_GameControllerGetJoystick(gameController);
gameControllerID = SDL_JoystickInstanceID(stick);
}
setupControllerSensors(gameController);
}
const EmulatorConfig& config = emu.getConfig();
@ -200,6 +204,8 @@ void FrontendSDL::run() {
if (gameController == nullptr) {
gameController = SDL_GameControllerOpen(event.cdevice.which);
gameControllerID = event.cdevice.which;
setupControllerSensors(gameController);
}
break;
@ -280,6 +286,21 @@ void FrontendSDL::run() {
}
break;
}
case SDL_CONTROLLERSENSORUPDATE: {
if (event.csensor.sensor == SDL_SENSOR_GYRO) {
glm::vec3 rotation = Gyro::SDL::convertRotation({
event.csensor.data[0],
event.csensor.data[1],
event.csensor.data[2],
});
hid.setPitch(s16(rotation.x));
hid.setRoll(s16(rotation.y));
hid.setYaw(s16(rotation.z));
}
break;
}
case SDL_DROPFILE: {
char* droppedDir = event.drop.file;
@ -342,3 +363,11 @@ void FrontendSDL::run() {
SDL_GL_SwapWindow(window);
}
}
void FrontendSDL::setupControllerSensors(SDL_GameController* controller) {
bool haveGyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE;
if (haveGyro) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE);
}
}