Qt: Add controller gyroscope

This commit is contained in:
wheremyfoodat 2024-08-14 22:57:45 +03:00
parent d208c24c0c
commit 520e00c553
3 changed files with 30 additions and 1 deletions

View file

@ -122,6 +122,7 @@ class MainWindow : public QMainWindow {
void showAboutMenu(); void showAboutMenu();
void initControllers(); void initControllers();
void pollControllers(); void pollControllers();
void setupControllerSensors(SDL_GameController* controller);
void sendMessage(const EmulatorMessage& message); void sendMessage(const EmulatorMessage& message);
void dispatchMessage(const EmulatorMessage& message); void dispatchMessage(const EmulatorMessage& message);

View file

@ -9,6 +9,7 @@
#include "cheats.hpp" #include "cheats.hpp"
#include "input_mappings.hpp" #include "input_mappings.hpp"
#include "sdl_gyro.hpp"
#include "services/dsp.hpp" #include "services/dsp.hpp"
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()) { MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()) {
@ -521,6 +522,8 @@ void MainWindow::initControllers() {
SDL_Joystick* stick = SDL_GameControllerGetJoystick(gameController); SDL_Joystick* stick = SDL_GameControllerGetJoystick(gameController);
gameControllerID = SDL_JoystickInstanceID(stick); gameControllerID = SDL_JoystickInstanceID(stick);
} }
setupControllerSensors(gameController);
} }
} }
@ -558,6 +561,8 @@ void MainWindow::pollControllers() {
if (gameController == nullptr) { if (gameController == nullptr) {
gameController = SDL_GameControllerOpen(event.cdevice.which); gameController = SDL_GameControllerOpen(event.cdevice.which);
gameControllerID = event.cdevice.which; gameControllerID = event.cdevice.which;
setupControllerSensors(gameController);
} }
break; break;
@ -598,6 +603,29 @@ void MainWindow::pollControllers() {
} }
break; break;
} }
case SDL_CONTROLLERSENSORUPDATE: {
if (event.csensor.sensor == SDL_SENSOR_GYRO) {
auto 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;
}
} }
} }
} }
void MainWindow::setupControllerSensors(SDL_GameController* controller) {
bool haveGyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE;
if (haveGyro) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE);
}
}

View file

@ -289,7 +289,7 @@ void FrontendSDL::run() {
case SDL_CONTROLLERSENSORUPDATE: { case SDL_CONTROLLERSENSORUPDATE: {
if (event.csensor.sensor == SDL_SENSOR_GYRO) { if (event.csensor.sensor == SDL_SENSOR_GYRO) {
glm::vec3 rotation = Gyro::SDL::convertRotation({ auto rotation = Gyro::SDL::convertRotation({
event.csensor.data[0], event.csensor.data[0],
event.csensor.data[1], event.csensor.data[1],
event.csensor.data[2], event.csensor.data[2],