summaryrefslogtreecommitdiff
path: root/platform/android/godot_android.cpp
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2016-07-18 00:52:15 +0200
committerGitHub <noreply@github.com>2016-07-18 00:52:15 +0200
commit8de5aedb9ef7d4c17027b41b40677d1892f4158c (patch)
tree88f3cf2753bc2f2033ed5e80eae43058dea63fa5 /platform/android/godot_android.cpp
parent3183834c5d5dd27e1be4f581c0fe1a8bef3c4a6e (diff)
parent370ae3512deda40b2659a1192f302298e269305f (diff)
Merge pull request #5718 from jay3d/master
Added gyroscope support to Godot and Android
Diffstat (limited to 'platform/android/godot_android.cpp')
-rw-r--r--platform/android/godot_android.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/platform/android/godot_android.cpp b/platform/android/godot_android.cpp
index 9f909d7041..2371274d9d 100644
--- a/platform/android/godot_android.cpp
+++ b/platform/android/godot_android.cpp
@@ -314,6 +314,7 @@ struct engine {
ASensorManager* sensorManager;
const ASensor* accelerometerSensor;
const ASensor* magnetometerSensor;
+ const ASensor* gyroscopeSensor;
ASensorEventQueue* sensorEventQueue;
bool display_active;
@@ -746,6 +747,15 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
engine->magnetometerSensor, (1000L/60)*1000);
}
+ // And the gyroscope.
+ if (engine->gyroscopeSensor != NULL) {
+ ASensorEventQueue_enableSensor(engine->sensorEventQueue,
+ engine->gyroscopeSensor);
+ // We'd like to get 60 events per second (in us).
+ ASensorEventQueue_setEventRate(engine->sensorEventQueue,
+ engine->gyroscopeSensor, (1000L/60)*1000);
+
+ }
engine->animating = 1;
break;
case APP_CMD_LOST_FOCUS:
@@ -759,6 +769,10 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->magnetometerSensor);
}
+ if (engine->gyroscopeSensor != NULL) {
+ ASensorEventQueue_disableSensor(engine->sensorEventQueue,
+ engine->gyroscopeSensor);
+ }
// Also stop animating.
engine->animating = 0;
engine_draw_frame(engine);
@@ -788,6 +802,8 @@ void android_main(struct android_app* state) {
ASENSOR_TYPE_ACCELEROMETER);
engine.magnetometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_MAGNETIC_FIELD);
+ engine.gyroscopeSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
+ ASENSOR_TYPE_GYROSCOPE);
engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
state->looper, LOOPER_ID_USER, NULL, NULL);
@@ -828,7 +844,7 @@ void android_main(struct android_app* state) {
// If a sensor has data, process it now.
// LOGI("events\n");
if (ident == LOOPER_ID_USER) {
- if (engine.accelerometerSensor != NULL || engine.magnetometerSensor != NULL) {
+ if (engine.accelerometerSensor != NULL || engine.magnetometerSensor != NULL || engine.gyroscopeSensor != NULL) {
ASensorEvent event;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
&event, 1) > 0) {
@@ -843,6 +859,10 @@ void android_main(struct android_app* state) {
engine.os->process_magnetometer(Vector3(event.magnetic.x, event.magnetic.y,
event.magnetic.z));
}
+ if (event.vector != NULL) {
+ engine.os->process_gyroscope(Vector3(event.vector.x, event.vector.y,
+ event.vector.z));
+ }
}
}