From ab2a6cb2bb61a2302991284bf291538464a93354 Mon Sep 17 00:00:00 2001 From: liswei Date: Tue, 30 Aug 2011 15:59:06 +0800 Subject: [PATCH] fixed #702 tests crashs at the start on wophone device --- .../android/CCAccelerometer_android.cpp | 13 +++--- .../ios/AccelerometerDelegateWrapper.mm | 5 +++ .../wophone/CCAccelerometer_wophone.cpp | 43 ++++++++++++------- tests/test.wophone/Makefile.ARM | 6 ++- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/cocos2dx/platform/android/CCAccelerometer_android.cpp b/cocos2dx/platform/android/CCAccelerometer_android.cpp index 77798d4027..cba35a71ca 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.cpp +++ b/cocos2dx/platform/android/CCAccelerometer_android.cpp @@ -67,12 +67,15 @@ namespace cocos2d void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp) { - m_obAccelerationValue.x = -((double)x / TG3_GRAVITY_EARTH); - m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH); - m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH); - m_obAccelerationValue.timestamp = (double)sensorTimeStamp; + if (m_pAccelDelegate) + { + m_obAccelerationValue.x = -((double)x / TG3_GRAVITY_EARTH); + m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH); + m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH); + m_obAccelerationValue.timestamp = (double)sensorTimeStamp; - m_pAccelDelegate->didAccelerate(&m_obAccelerationValue); + m_pAccelDelegate->didAccelerate(&m_obAccelerationValue); + } } } // end of namespace cococs2d diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm index 322693aaf2..3c1abc5392 100644 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm +++ b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm @@ -68,6 +68,11 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher; - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { + if (! delegate_) + { + return; + } + acceleration_->x = acceleration.x; acceleration_->y = acceleration.y; acceleration_->z = acceleration.z; diff --git a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp index b50917dee6..7556083843 100644 --- a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp +++ b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp @@ -64,7 +64,6 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer() void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) { m_pDelegate = NULL; - m_pSensor->Release(); m_pSensor = NULL; } @@ -73,27 +72,39 @@ void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) { m_pDelegate = pDelegate; - m_pSensor = TCOM_Sensors_DataType_Client::GetInstance(); - - if (m_pSensor) + do { - m_pSensor->StartUp(); - m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST); + if (m_pSensor) + { + break; + } - TApplication* pApp = TApplication::GetCurrentApplication(); - TWindow* pWnd = pApp->GetActiveWindow(); - m_pSensor->SetWindowCtrlId(pWnd->GetWindowHwndId(), 0); - m_pSensor->Activate(TG3_SENSOR_TYPE_ACCELEROMETER, TRUE); - } - else - { - CCLOG("cocos2d: The Accelerometer Sensor Open failed"); - } + m_pSensor = TCOM_Sensors_DataType_Client::GetInstance(); + + if (m_pSensor) + { + m_pSensor->StartUp(); + m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST); + + TApplication* pApp = TApplication::GetCurrentApplication(); + TWindow* pWnd = pApp->GetActiveWindow(); + m_pSensor->SetWindowCtrlId(pWnd->GetWindowHwndId(), 0); + m_pSensor->Activate(TG3_SENSOR_TYPE_ACCELEROMETER, TRUE); + } + else + { + CCLOG("cocos2d: The Accelerometer Sensor Open failed"); + } + } while (0); + } void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue) { - m_pDelegate->didAccelerate(pAccelerationValue); + if (m_pDelegate) + { + m_pDelegate->didAccelerate(pAccelerationValue); + } } }//namespace cocos2d diff --git a/tests/test.wophone/Makefile.ARM b/tests/test.wophone/Makefile.ARM index 225c8158cb..2e05953ca3 100644 --- a/tests/test.wophone/Makefile.ARM +++ b/tests/test.wophone/Makefile.ARM @@ -120,7 +120,8 @@ OBJECTS = \ $(OBJECTS_DIR)/TransitionsTest.o \ $(OBJECTS_DIR)/UserDefaultTest.o \ $(OBJECTS_DIR)/ZwoptexTest.o \ - $(OBJECTS_DIR)/FontTest.o + $(OBJECTS_DIR)/FontTest.o \ + $(OBJECTS_DIR)/CurrentLanguageTest.o ADD_OBJECTS += @@ -394,3 +395,6 @@ $(OBJECTS_DIR)/ZwoptexTest.o : ../tests/ZwoptexTest/ZwoptexTest.cpp $(OBJECTS_DIR)/FontTest.o : ../tests/FontTest/FontTest.cpp $(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/FontTest.o ../tests/FontTest/FontTest.cpp +$(OBJECTS_DIR)/CurrentLanguageTest.o : ../tests/CurrentLanguageTest/CurrentLanguageTest.cpp + $(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/CurrentLanguageTest.o ../tests/CurrentLanguageTest/CurrentLanguageTest.cpp +