SDL: Add ability to control tilt sensor with right analog stick (currently hardcoded)
Jeffrey Pfau jeffrey@endrift.com
Sun, 19 Apr 2015 23:25:05 -0700
6 files changed,
43 insertions(+),
1 deletions(-)
M
src/gba/hardware.c
→
src/gba/hardware.c
@@ -286,7 +286,7 @@ }
void _gyroReadPins(struct GBACartridgeHardware* hw) { struct GBARotationSource* gyro = hw->p->rotationSource; - if (!gyro) { + if (!gyro || !gyro->readGyroZ) { return; }
M
src/platform/qt/GameController.cpp
→
src/platform/qt/GameController.cpp
@@ -100,6 +100,7 @@ context->gba->luminanceSource = &controller->m_lux;
context->gba->rtcSource = &controller->m_rtc; #ifdef BUILD_SDL context->gba->rumble = controller->m_inputController->rumble(); + context->gba->rotationSource = controller->m_inputController->rotationSource(); #endif controller->gameStarted(context); };
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -157,6 +157,10 @@
GBARumble* InputController::rumble() { return &m_sdlPlayer.rumble.d; } + +GBARotationSource* InputController::rotationSource() { + return &m_sdlPlayer.rotation.d; +} #endif GBAKey InputController::mapKeyboard(int key) const {
M
src/platform/qt/InputController.h
→
src/platform/qt/InputController.h
@@ -66,6 +66,7 @@ int gamepad(uint32_t type) const { return m_sdlPlayer.joystickIndex; }
void setGamepad(uint32_t type, int index) { GBASDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index); } void setPreferredGamepad(uint32_t type, const QString& device); GBARumble* rumble(); + GBARotationSource* rotationSource(); #endif public slots:
M
src/platform/sdl/sdl-events.c
→
src/platform/sdl/sdl-events.c
@@ -21,6 +21,9 @@ #define GUI_MOD KMOD_CTRL
#endif static void _GBASDLSetRumble(struct GBARumble* rumble, int enable); +static int32_t _GBASDLReadTiltX(struct GBARotationSource* rumble); +static int32_t _GBASDLReadTiltY(struct GBARotationSource* rumble); +static void _GBASDLRotationSample(struct GBARotationSource* source); bool GBASDLInitEvents(struct GBASDLEvents* context) { int subsystem = SDL_INIT_JOYSTICK;@@ -145,6 +148,14 @@ #if SDL_VERSION_ATLEAST(2, 0, 0)
player->rumble.d.setRumble = _GBASDLSetRumble; player->rumble.p = player; #endif + + player->rotation.d.readTiltX = _GBASDLReadTiltX; + player->rotation.d.readTiltY = _GBASDLReadTiltY; + player->rotation.d.readGyroZ = 0; + player->rotation.d.sample = _GBASDLRotationSample; + player->rotation.axisX = 2; + player->rotation.axisY = 3; + player->rotation.p = player; if (events->playersAttached >= MAX_PLAYERS) { return false;@@ -447,3 +458,21 @@ SDL_HapticRumbleStop(sdlRumble->p->haptic);
} } #endif + +static int32_t _readTilt(struct GBASDLPlayer* player, int axis) { + return SDL_JoystickGetAxis(player->joystick, axis) * 0x3800; +} + +static int32_t _GBASDLReadTiltX(struct GBARotationSource* source) { + struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; + return _readTilt(rotation->p, rotation->axisX); +} + +static int32_t _GBASDLReadTiltY(struct GBARotationSource* source) { + struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; + return _readTilt(rotation->p, rotation->axisY); +} +static void _GBASDLRotationSample(struct GBARotationSource* source) { + UNUSED(source); + SDL_JoystickUpdate(); +}
M
src/platform/sdl/sdl-events.h
→
src/platform/sdl/sdl-events.h
@@ -47,6 +47,13 @@ struct GBARumble d;
struct GBASDLPlayer* p; } rumble; #endif + + struct GBASDLRotation { + struct GBARotationSource d; + struct GBASDLPlayer* p; + int axisX; + int axisY; + } rotation; }; bool GBASDLInitEvents(struct GBASDLEvents*);