mirror of
https://github.com/encounter/aurora.git
synced 2026-07-09 18:19:33 -07:00
Add device accelerometer (#214)
This commit is contained in:
+34
-19
@@ -14,6 +14,7 @@ void shutdown_rumble() noexcept;
|
||||
|
||||
namespace {
|
||||
SDL_Sensor* g_gyro = nullptr;
|
||||
SDL_Sensor* g_accel = nullptr;
|
||||
#if defined(SDL_PLATFORM_ANDROID)
|
||||
SDL_Haptic* g_haptic = nullptr;
|
||||
|
||||
@@ -142,6 +143,31 @@ void rotate_sensor_to_display(float* data, const int n_values) noexcept {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool read_sensor_data(const SDL_SensorType type, SDL_Sensor*& cached_sensor, float* data, const int n_values) noexcept {
|
||||
if (data == nullptr || n_values <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Sensor* sensor = open_sensor(type, cached_sensor);
|
||||
if (sensor == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_UpdateSensors();
|
||||
if (!SDL_GetSensorData(sensor, data, n_values)) {
|
||||
return false;
|
||||
}
|
||||
rotate_sensor_to_display(data, n_values);
|
||||
return true;
|
||||
}
|
||||
|
||||
void close_sensor(SDL_Sensor*& sensor) noexcept {
|
||||
if (sensor != nullptr) {
|
||||
SDL_CloseSensor(sensor);
|
||||
sensor = nullptr;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
#if defined(SDL_PLATFORM_ANDROID)
|
||||
@@ -175,8 +201,7 @@ void shutdown_rumble() noexcept {
|
||||
#elif !defined(SDL_PLATFORM_IOS)
|
||||
bool rumble_available() noexcept { return false; }
|
||||
|
||||
void rumble(const uint16_t lowFreq, const uint16_t highFreq,
|
||||
const uint16_t durationMs) noexcept {
|
||||
void rumble(const uint16_t lowFreq, const uint16_t highFreq, const uint16_t durationMs) noexcept {
|
||||
static_cast<void>(lowFreq);
|
||||
static_cast<void>(highFreq);
|
||||
static_cast<void>(durationMs);
|
||||
@@ -190,28 +215,18 @@ void shutdown_rumble() noexcept {}
|
||||
bool gyro_available() noexcept { return open_sensor(SDL_SENSOR_GYRO, g_gyro) != nullptr; }
|
||||
|
||||
bool gyro(float* data, const int n_values) noexcept {
|
||||
if (data == nullptr || n_values <= 0) {
|
||||
return false;
|
||||
}
|
||||
return read_sensor_data(SDL_SENSOR_GYRO, g_gyro, data, n_values);
|
||||
}
|
||||
|
||||
SDL_Sensor* sensor = open_sensor(SDL_SENSOR_GYRO, g_gyro);
|
||||
if (sensor == nullptr) {
|
||||
return false;
|
||||
}
|
||||
bool accel_available() noexcept { return open_sensor(SDL_SENSOR_ACCEL, g_accel) != nullptr; }
|
||||
|
||||
SDL_UpdateSensors();
|
||||
if (!SDL_GetSensorData(sensor, data, n_values)) {
|
||||
return false;
|
||||
}
|
||||
rotate_sensor_to_display(data, n_values);
|
||||
return true;
|
||||
bool accel(float* data, const int n_values) noexcept {
|
||||
return read_sensor_data(SDL_SENSOR_ACCEL, g_accel, data, n_values);
|
||||
}
|
||||
|
||||
void shutdown() noexcept {
|
||||
detail::shutdown_rumble();
|
||||
if (g_gyro != nullptr) {
|
||||
SDL_CloseSensor(g_gyro);
|
||||
g_gyro = nullptr;
|
||||
}
|
||||
close_sensor(g_gyro);
|
||||
close_sensor(g_accel);
|
||||
}
|
||||
} // namespace aurora::device
|
||||
|
||||
@@ -10,6 +10,9 @@ void rumble(uint16_t lowFreq, uint16_t highFreq, uint16_t durationMs) noexcept;
|
||||
bool gyro_available() noexcept;
|
||||
bool gyro(float* data, int n_values) noexcept;
|
||||
|
||||
bool accel_available() noexcept;
|
||||
bool accel(float* data, int n_values) noexcept;
|
||||
|
||||
void shutdown() noexcept;
|
||||
|
||||
} // namespace aurora::device
|
||||
|
||||
+28
-3
@@ -325,14 +325,39 @@ static bool device_gyro_available_for_port(const u32 port) {
|
||||
return port == PAD_CHAN0 && aurora::device::gyro_available();
|
||||
}
|
||||
|
||||
static bool device_accel_available_for_port(const u32 port) {
|
||||
return port == PAD_CHAN0 && aurora::device::accel_available();
|
||||
}
|
||||
|
||||
static bool device_sensor_available_for_port(const u32 port, const PADSensorType sensor) {
|
||||
switch (sensor) {
|
||||
case PAD_SENSOR_ACCEL:
|
||||
return device_accel_available_for_port(port);
|
||||
case PAD_SENSOR_GYRO:
|
||||
return device_gyro_available_for_port(port);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool get_device_sensor_data(const PADSensorType sensor, f32* data, const int nValues) {
|
||||
switch (sensor) {
|
||||
case PAD_SENSOR_ACCEL:
|
||||
return aurora::device::accel(data, nValues);
|
||||
case PAD_SENSOR_GYRO:
|
||||
return aurora::device::gyro(data, nValues);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool controller_has_sensor(const aurora::input::GameController* controller, const PADSensorType sensor) {
|
||||
return controller != nullptr && SDL_GamepadHasSensor(controller->m_controller, static_cast<SDL_SensorType>(sensor));
|
||||
}
|
||||
|
||||
static bool should_use_device_sensor(const u32 port, const aurora::input::GameController* controller,
|
||||
const PADSensorType sensor) {
|
||||
return sensor == PAD_SENSOR_GYRO && !controller_has_sensor(controller, sensor) &&
|
||||
device_gyro_available_for_port(port);
|
||||
return device_sensor_available_for_port(port, sensor) && !controller_has_sensor(controller, sensor);
|
||||
}
|
||||
|
||||
// ReSharper disable once CppDFAConstantFunctionResult
|
||||
@@ -1657,7 +1682,7 @@ BOOL PADGetSensorData(const u32 port, const PADSensorType sensor, f32* data, con
|
||||
}
|
||||
|
||||
if (should_use_device_sensor(port, ctrl, sensor)) {
|
||||
return aurora::device::gyro(data, nValues) ? TRUE : FALSE;
|
||||
return get_device_sensor_data(sensor, data, nValues) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
|
||||
Reference in New Issue
Block a user