SDL: Add ability to control tilt sensor with right analog stick (currently hardcoded)

This commit is contained in:
Jeffrey Pfau 2015-04-19 23:25:05 -07:00
parent 049e3639d1
commit 7fa043cb50
6 changed files with 43 additions and 1 deletions

View File

@ -286,7 +286,7 @@ void GBAHardwareInitGyro(struct GBACartridgeHardware* hw) {
void _gyroReadPins(struct GBACartridgeHardware* hw) {
struct GBARotationSource* gyro = hw->p->rotationSource;
if (!gyro) {
if (!gyro || !gyro->readGyroZ) {
return;
}

View File

@ -100,6 +100,7 @@ GameController::GameController(QObject* parent)
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);
};

View File

@ -157,6 +157,10 @@ void InputController::setPreferredGamepad(uint32_t type, const QString& device)
GBARumble* InputController::rumble() {
return &m_sdlPlayer.rumble.d;
}
GBARotationSource* InputController::rotationSource() {
return &m_sdlPlayer.rotation.d;
}
#endif
GBAKey InputController::mapKeyboard(int key) const {

View File

@ -66,6 +66,7 @@ public:
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:

View File

@ -21,6 +21,9 @@
#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;
@ -146,6 +149,14 @@ bool GBASDLAttachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player
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 @@ static void _GBASDLSetRumble(struct GBARumble* rumble, int enable) {
}
}
#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();
}

View File

@ -47,6 +47,13 @@ struct GBASDLPlayer {
struct GBASDLPlayer* p;
} rumble;
#endif
struct GBASDLRotation {
struct GBARotationSource d;
struct GBASDLPlayer* p;
int axisX;
int axisY;
} rotation;
};
bool GBASDLInitEvents(struct GBASDLEvents*);