From 063c730726536867190ec5ce2cd182be0a6a70c1 Mon Sep 17 00:00:00 2001 From: folecr Date: Thu, 8 Aug 2013 14:38:31 -0700 Subject: [PATCH 1/2] Take orientation into account when reporting accelerometer sensor readout --- cocos2dx/platform/android/nativeactivity.cpp | 31 ++++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index 9dbb995c03..0b593ece2e 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -517,15 +518,33 @@ void android_main(struct android_app* state) { while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { - cocos2d::Director* pDirector = cocos2d::Director::getInstance(); - pDirector->getAccelerometer()->update(event.acceleration.x, - event.acceleration.y, - event.acceleration.z, - 0); - LOG_EVENTS_DEBUG("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); + + AConfiguration* _currentconf = AConfiguration_new(); + AConfiguration_fromAssetManager(_currentconf, + state->activity->assetManager); + static int32_t _orientation; + _orientation = AConfiguration_getOrientation(_currentconf); + + cocos2d::Director* pDirector = cocos2d::Director::getInstance(); + if (ACONFIGURATION_ORIENTATION_LAND != _orientation) { + // ACONFIGURATION_ORIENTATION_ANY + // ACONFIGURATION_ORIENTATION_PORT + // ACONFIGURATION_ORIENTATION_SQUARE + pDirector->getAccelerometer()->update(event.acceleration.x, + -event.acceleration.y, + event.acceleration.z, + 0); + } else { + // ACONFIGURATION_ORIENTATION_LAND + // swap x and y parameters + pDirector->getAccelerometer()->update(-event.acceleration.y, + event.acceleration.x, + event.acceleration.z, + 0); + } } } } From 2ebd400a75584704ab6fa413de01923a7db883a0 Mon Sep 17 00:00:00 2001 From: folecr Date: Fri, 9 Aug 2013 11:39:52 -0700 Subject: [PATCH 2/2] clean up coding style and remove explicit temporary variable --- cocos2dx/platform/android/nativeactivity.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index 0b593ece2e..de53135e3d 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -525,25 +525,23 @@ void android_main(struct android_app* state) { AConfiguration* _currentconf = AConfiguration_new(); AConfiguration_fromAssetManager(_currentconf, state->activity->assetManager); - static int32_t _orientation; - _orientation = AConfiguration_getOrientation(_currentconf); + static int32_t _orientation = AConfiguration_getOrientation(_currentconf); - cocos2d::Director* pDirector = cocos2d::Director::getInstance(); if (ACONFIGURATION_ORIENTATION_LAND != _orientation) { // ACONFIGURATION_ORIENTATION_ANY // ACONFIGURATION_ORIENTATION_PORT // ACONFIGURATION_ORIENTATION_SQUARE - pDirector->getAccelerometer()->update(event.acceleration.x, - -event.acceleration.y, - event.acceleration.z, - 0); + cocos2d::Director::getInstance()->getAccelerometer()->update(event.acceleration.x, + -event.acceleration.y, + event.acceleration.z, + 0); } else { // ACONFIGURATION_ORIENTATION_LAND // swap x and y parameters - pDirector->getAccelerometer()->update(-event.acceleration.y, - event.acceleration.x, - event.acceleration.z, - 0); + cocos2d::Director::getInstance()->getAccelerometer()->update(-event.acceleration.y, + event.acceleration.x, + event.acceleration.z, + 0); } } }