SDL: Support exposing an axis directly as the gyro value (closes #2531)

This commit is contained in:
Vicki Pfau 2022-06-14 21:12:53 -07:00
parent 85737f1103
commit a0613e27ab
4 changed files with 27 additions and 2 deletions

View File

@ -341,6 +341,11 @@ void InputController::registerGyroAxisX(int axis) {
#ifdef BUILD_SDL
if (m_playerAttached) {
m_sdlPlayer.rotation.gyroX = axis;
if (m_sdlPlayer.rotation.gyroY == axis) {
m_sdlPlayer.rotation.gyroZ = axis;
} else {
m_sdlPlayer.rotation.gyroZ = -1;
}
}
#endif
}
@ -349,6 +354,11 @@ void InputController::registerGyroAxisY(int axis) {
#ifdef BUILD_SDL
if (m_playerAttached) {
m_sdlPlayer.rotation.gyroY = axis;
if (m_sdlPlayer.rotation.gyroX == axis) {
m_sdlPlayer.rotation.gyroZ = axis;
} else {
m_sdlPlayer.rotation.gyroZ = -1;
}
}
#endif
}

View File

@ -284,10 +284,10 @@
<bool>false</bool>
</property>
<property name="minimum">
<number>-2147483647</number>
<number>-1073741823</number>
</property>
<property name="maximum">
<number>2147483647</number>
<number>1073741823</number>
</property>
<property name="value">
<number>0</number>

View File

@ -199,6 +199,7 @@ bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
player->rotation.gyroSensitivity = 2.2e9f;
player->rotation.gyroX = 0;
player->rotation.gyroY = 1;
player->rotation.gyroZ = -1;
player->rotation.zDelta = 0;
CircleBufferInit(&player->rotation.zHistory, sizeof(float) * GYRO_STEPS);
player->rotation.p = player;
@ -327,6 +328,13 @@ void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration
context->rotation.gyroY = axis;
}
}
value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisZ", name);
if (value) {
axis = strtol(value, &end, 0);
if (axis >= 0 && axis < numAxes && end && !*end) {
context->rotation.gyroZ = axis;
}
}
value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
if (value) {
float sensitivity = strtof_u(value, &end);
@ -357,6 +365,8 @@ void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration
mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
snprintf(value, sizeof(value), "%i", context->rotation.gyroZ);
mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisZ", value, name);
snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
}
@ -750,6 +760,10 @@ static void _mSDLRotationSample(struct mRotationSource* source) {
}
}
#endif
if (rotation->gyroZ >= 0) {
rotation->zDelta = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroZ) / 1.e5f;
return;
}
int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);

View File

@ -95,6 +95,7 @@ struct mSDLPlayer {
// Gyro
int gyroX;
int gyroY;
int gyroZ;
float gyroSensitivity;
struct CircleBuffer zHistory;
int oldX;