Libretro: Lazy-load sensors

This commit is contained in:
Vicki Pfau 2021-03-20 22:58:52 -07:00
parent 9e251c54e7
commit 49b12139ec
1 changed files with 17 additions and 8 deletions

View File

@ -69,7 +69,8 @@ static int rumbleDown;
static struct mRumble rumble;
static struct GBALuminanceSource lux;
static struct mRotationSource rotation;
static bool rotationEnabled;
static bool tiltEnabled;
static bool gyroEnabled;
static int luxLevelIndex;
static uint8_t luxLevel;
static bool luxSensorEnabled;
@ -100,9 +101,12 @@ static void _initSensors(void) {
sensorStateCallback = sensorInterface.set_sensor_state;
if (sensorStateCallback && sensorGetCallback) {
if (sensorStateCallback(0, RETRO_SENSOR_ACCELEROMETER_ENABLE, EVENT_RATE)
&& sensorStateCallback(0, RETRO_SENSOR_GYROSCOPE_ENABLE, EVENT_RATE)) {
rotationEnabled = true;
if (sensorStateCallback(0, RETRO_SENSOR_ACCELEROMETER_ENABLE, EVENT_RATE)) {
tiltEnabled = true;
}
if (sensorStateCallback(0, RETRO_SENSOR_GYROSCOPE_ENABLE, EVENT_RATE)) {
gyroEnabled = true;
}
if (sensorStateCallback(0, RETRO_SENSOR_ILLUMINANCE_ENABLE, EVENT_RATE)) {
@ -318,7 +322,8 @@ void retro_init(void) {
sensorGetCallback = 0;
sensorStateCallback = 0;
rotationEnabled = false;
tiltEnabled = false;
gyroEnabled = false;
rotation.sample = _updateRotation;
rotation.readTiltX = _readTiltX;
rotation.readTiltY = _readTiltY;
@ -363,7 +368,8 @@ void retro_deinit(void) {
sensorStateCallback = NULL;
}
rotationEnabled = false;
tiltEnabled = false;
gyroEnabled = false;
luxSensorEnabled = false;
sensorsInitDone = false;
}
@ -374,7 +380,6 @@ void retro_run(void) {
}
uint16_t keys;
_initSensors();
inputPollCallback();
bool updated = false;
@ -1023,6 +1028,7 @@ static void _updateLux(struct GBALuminanceSource* lux) {
}
if (luxSensorUsed) {
_initSensors();
float fLux = luxSensorEnabled ? sensorGetCallback(0, RETRO_SENSOR_ILLUMINANCE) : 0.0f;
luxLevel = cbrtf(fLux) * 8;
} else {
@ -1124,9 +1130,12 @@ static void _updateRotation(struct mRotationSource* source) {
tiltX = 0;
tiltY = 0;
gyroZ = 0;
if (rotationEnabled) {
_initSensors();
if (tiltEnabled) {
tiltX = sensorGetCallback(0, RETRO_SENSOR_ACCELEROMETER_X) * 3e8f;
tiltY = sensorGetCallback(0, RETRO_SENSOR_ACCELEROMETER_Y) * -3e8f;
}
if (gyroEnabled) {
gyroZ = sensorGetCallback(0, RETRO_SENSOR_GYROSCOPE_Z) * -1.1e9f;
}
}