diff --git a/cocos2dx/include/CCAccelerometerDelegate.h b/cocos2dx/include/CCAccelerometerDelegate.h index 1233284f9b..a6ab9718ab 100644 --- a/cocos2dx/include/CCAccelerometerDelegate.h +++ b/cocos2dx/include/CCAccelerometerDelegate.h @@ -49,11 +49,6 @@ class CC_DLL CCAccelerometerDelegate { public: virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} - - //! call the release() in child layer - virtual void AccelerometerDestroy(void) {} - //! call the retain() in child layer - virtual void AccelerometerKeep(void) {} }; } //namespace cocos2d diff --git a/cocos2dx/include/CCLayer.h b/cocos2dx/include/CCLayer.h index 2f62073853..994e5209aa 100644 --- a/cocos2dx/include/CCLayer.h +++ b/cocos2dx/include/CCLayer.h @@ -67,8 +67,6 @@ public: virtual void keep(void); virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} - virtual void AccelerometerDestroy(void); - virtual void AccelerometerKeep(void); virtual void KeypadDestroy(); virtual void KeypadKeep(); diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index bf086c1bcf..a0dce5fe3e 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -96,16 +96,6 @@ void CCLayer::keep(void) this->retain(); } -void CCLayer::AccelerometerDestroy(void) -{ - this->release(); -} - -void CCLayer::AccelerometerKeep(void) -{ - this->retain(); -} - void CCLayer::KeypadDestroy() { this->release(); diff --git a/cocos2dx/platform/android/CCAccelerometer_android.cpp b/cocos2dx/platform/android/CCAccelerometer_android.cpp index 4d04327b0a..77798d4027 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.cpp +++ b/cocos2dx/platform/android/CCAccelerometer_android.cpp @@ -34,18 +34,17 @@ namespace cocos2d { CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL; - CCAccelerometer::CCAccelerometer() { - m_pAccelDelegates = new std::list(); + CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL) + { } - CCAccelerometer::~CCAccelerometer() { - if ( m_pAccelDelegates ) { - delete m_pAccelDelegates; - m_pAccelDelegates = NULL; - } + CCAccelerometer::~CCAccelerometer() + { + m_spCCAccelerometer = NULL; } - CCAccelerometer* CCAccelerometer::sharedAccelerometer() { + CCAccelerometer* CCAccelerometer::sharedAccelerometer() + { if (m_spCCAccelerometer == NULL) { @@ -55,34 +54,25 @@ namespace cocos2d return m_spCCAccelerometer; } - void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) { - m_pAccelDelegates->remove(pDelegate); - - if ( 0 == m_pAccelDelegates->size() ) { - disableAccelerometerJNI(); - } + void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) + { + disableAccelerometerJNI(); } - void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) { - if ( 0 == m_pAccelDelegates->size() ) { - enableAccelerometerJNI(); - } - - m_pAccelDelegates->push_front(pDelegate); + void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) + { + m_pAccelDelegate = pDelegate; + enableAccelerometerJNI(); } - void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp) { - if ( m_pAccelDelegates != NULL && !m_pAccelDelegates->empty() ) { - 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; + 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; - for(std::list::const_iterator it = m_pAccelDelegates->begin(); it != m_pAccelDelegates->end(); ++it) - { - (*it)->didAccelerate(&m_obAccelerationValue); - } - } + m_pAccelDelegate->didAccelerate(&m_obAccelerationValue); } } // end of namespace cococs2d diff --git a/cocos2dx/platform/android/CCAccelerometer_android.h b/cocos2dx/platform/android/CCAccelerometer_android.h index 3bb75a4fad..b34de5b01a 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.h +++ b/cocos2dx/platform/android/CCAccelerometer_android.h @@ -27,7 +27,6 @@ THE SOFTWARE. #include "CCCommon.h" #include "CCAccelerometerDelegate.h" -#include namespace cocos2d { @@ -45,7 +44,7 @@ public: private: static CCAccelerometer* m_spCCAccelerometer; - std::list* m_pAccelDelegates; + CCAccelerometerDelegate* m_pAccelDelegate; CCAcceleration m_obAccelerationValue; }; diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h index 8233b25b6f..f4c7b4621e 100644 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h +++ b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h @@ -28,10 +28,12 @@ @interface AccelerometerDispatcher : NSObject { - NSMutableArray *delegateWrappers; + cocos2d::CCAccelerometerDelegate *delegate_; + cocos2d::CCAcceleration *acceleration_; } -@property(readwrite, retain) NSMutableArray* delegateWrappers; +@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_; +@property(readwrite) cocos2d::CCAcceleration *acceleration_; + (id) sharedAccelerometerDispather; - (id) init; @@ -39,16 +41,3 @@ - (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; @end - - -@interface AccelerometerDelegateWrapper : NSObject { - cocos2d::CCAccelerometerDelegate *delegate_; -} - -@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_; - -+ (id) delegateWrapperWithDelegate:(cocos2d::CCAccelerometerDelegate *)delegate; -- (id) initWithDelegate: (cocos2d::CCAccelerometerDelegate *)delegate; -- (void) didAccelerate: (cocos2d::CCAcceleration *)acceleration; - -@end diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm index 131a73287f..322693aaf2 100644 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm +++ b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm @@ -28,7 +28,8 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher; -@synthesize delegateWrappers; +@synthesize delegate_; +@synthesize acceleration_; + (id) sharedAccelerometerDispather { @@ -41,107 +42,62 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher; - (id) init { - self.delegateWrappers = [NSMutableArray arrayWithCapacity:4]; - [[UIAccelerometer sharedAccelerometer] setDelegate: self]; - + acceleration_ = new cocos2d::CCAcceleration(); return self; } - (void) dealloc { - [[UIAccelerometer sharedAccelerometer] setDelegate: nil]; - [delegateWrappers release]; + s_pAccelerometerDispatcher = 0; + delegate_ = 0; + delete acceleration_; [super dealloc]; } -- (id) findDelegateWrapperByDelegate: (cocos2d::CCAccelerometerDelegate *) delegate -{ - for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) { - if (wrapper.delegate_ == delegate) { - return wrapper; - } - } - - return nil; -} - - (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate { - [delegateWrappers addObject: [AccelerometerDelegateWrapper delegateWrapperWithDelegate:delegate]]; + [[UIAccelerometer sharedAccelerometer] setDelegate:self]; + delegate_ = delegate; } - (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate { - [delegateWrappers removeObject:[self findDelegateWrapperByDelegate:delegate]]; + [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; + delegate_ = 0; } - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration -{ - cocos2d::CCAcceleration accelerationCpp; +{ + acceleration_->x = acceleration.x; + acceleration_->y = acceleration.y; + acceleration_->z = acceleration.z; + acceleration_->timestamp = acceleration.timestamp; - accelerationCpp.x = acceleration.x; - accelerationCpp.y = acceleration.y; - accelerationCpp.z = acceleration.z; - accelerationCpp.timestamp = acceleration.timestamp; - - double tmp = accelerationCpp.x; + double tmp = acceleration_->x; switch ([[UIApplication sharedApplication] statusBarOrientation]) { case UIInterfaceOrientationLandscapeRight: - accelerationCpp.x = -acceleration.y; - accelerationCpp.y = tmp; + acceleration_->x = -acceleration_->y; + acceleration_->y = tmp; break; case UIInterfaceOrientationLandscapeLeft: - accelerationCpp.x = acceleration.y; - accelerationCpp.y = -tmp; + acceleration_->x = acceleration_->y; + acceleration_->y = -tmp; break; case UIInterfaceOrientationPortraitUpsideDown: - accelerationCpp.x = -accelerationCpp.y; - accelerationCpp.y = -tmp; + acceleration_->x = -acceleration_->y; + acceleration_->y = -tmp; break; case UIInterfaceOrientationPortrait: break; } - for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) { - [wrapper didAccelerate: &accelerationCpp]; - } + delegate_->didAccelerate(acceleration_); } @end - -@implementation AccelerometerDelegateWrapper - -@synthesize delegate_; - -+ (id)delegateWrapperWithDelegate:(cocos2d::CCAccelerometerDelegate *)delegate -{ - return [[self alloc] initWithDelegate: delegate]; -} - -- (id) initWithDelegate: (cocos2d::CCAccelerometerDelegate *)delegate -{ - delegate->AccelerometerKeep(); - self.delegate_ = delegate; - - return self; -} - -- (void) didAccelerate: (cocos2d::CCAcceleration *)acceleration -{ - self.delegate_->didAccelerate(acceleration); -} - -- (void) dealloc -{ - self.delegate_->AccelerometerDestroy(); - - [super dealloc]; -} - -@end diff --git a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp index d7ffa388b8..b50917dee6 100644 --- a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp +++ b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp @@ -27,72 +27,13 @@ THE SOFTWARE. #include "TCOM_Generic_Method_IIDs.h" -//只能包含一次 +// Can only include once #include "TCOM_Sensors_IIDs.h" namespace cocos2d { static CCAccelerometer s_Accelerometer; -//------------------------------------------------------------------ -// -// CCAccelerometerHandler -// -//------------------------------------------------------------------ -CCAccelerometerDelegate* CCAccelerometerHandler::getDelegate() -{ - return m_pDelegate; -} - -CCAccelerometerHandler::~CCAccelerometerHandler() -{ - m_pDelegate->AccelerometerDestroy(); -} - -void CCAccelerometerHandler::setDelegate(CCAccelerometerDelegate *pDelegate) -{ - if (pDelegate) - { - pDelegate->AccelerometerKeep(); - } - - if (m_pDelegate) - { - m_pDelegate->AccelerometerDestroy(); - } - m_pDelegate = pDelegate; -} - -bool CCAccelerometerHandler::initWithDelegate(CCAccelerometerDelegate *pDelegate) -{ - assert(pDelegate != NULL); - - m_pDelegate = pDelegate; - pDelegate->AccelerometerKeep(); - - return true; -} - -CCAccelerometerHandler* CCAccelerometerHandler::handlerWithDelegate(CCAccelerometerDelegate *pDelegate) -{ - CCAccelerometerHandler* pHandler = new CCAccelerometerHandler; - - if (pHandler) - { - if (pHandler->initWithDelegate(pDelegate)) - { - pHandler->autorelease(); - } - else - { - CC_SAFE_RELEASE_NULL(pHandler); - } - } - - return pHandler; -} - - //------------------------------------------------------------------ // // CCAccelerometer @@ -100,19 +41,19 @@ CCAccelerometerHandler* CCAccelerometerHandler::handlerWithDelegate(CCAccelerome //------------------------------------------------------------------ CCAccelerometer::CCAccelerometer() : m_pSensor(NULL) +, m_pDelegate(NULL) { - m_pDelegates = new AccDelegateArray; } CCAccelerometer::~CCAccelerometer() { - m_pDelegates->release(); - if (m_pSensor) { m_pSensor->Release(); m_pSensor = NULL; } + + m_pDelegate = NULL; } CCAccelerometer* CCAccelerometer::sharedAccelerometer() @@ -122,90 +63,37 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer() void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) { - CCAccelerometerHandler *pHandler; - CCMutableArray::CCMutableArrayIterator iter; + m_pDelegate = NULL; - if (pDelegate) - { - for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter) - { - pHandler = *iter; - if (pHandler && pHandler->getDelegate() == pDelegate) - { - m_pDelegates->removeObject(pHandler); - break; - } - } - } - - if (0 == m_pDelegates->count()) - { - m_pSensor->Release(); - m_pSensor = NULL; - } + m_pSensor->Release(); + m_pSensor = NULL; } void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) { - CCAccelerometerHandler *pHandlerIter; - CCMutableArray::CCMutableArrayIterator iter; + m_pDelegate = pDelegate; - if (pDelegate) - { - for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter) - { - pHandlerIter = *iter; - if (pHandlerIter && pHandlerIter->getDelegate() == pDelegate) - { - // this delegate have existed - return; - } - } - } + m_pSensor = TCOM_Sensors_DataType_Client::GetInstance(); - CCAccelerometerHandler* pHandler = CCAccelerometerHandler::handlerWithDelegate(pDelegate); + if (m_pSensor) + { + m_pSensor->StartUp(); + m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST); - if (pHandler) - { - m_pDelegates->addObject(pHandler); - - if (!m_pSensor) - { - 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"); - } - } - } + 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"); + } } void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue) { - CCAccelerometerHandler *pHandler; - CCAccelerometerDelegate *pDelegate; - CCMutableArray::CCMutableArrayIterator iter; - - if (m_pDelegates->count() > 0) - { - for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter) - { - pHandler = *iter; - pDelegate = pHandler->getDelegate(); - pDelegate->didAccelerate(pAccelerationValue); - } - } + m_pDelegate->didAccelerate(pAccelerationValue); } }//namespace cocos2d diff --git a/cocos2dx/platform/wophone/CCAccelerometer_wophone.h b/cocos2dx/platform/wophone/CCAccelerometer_wophone.h index e9f69a726b..dd6a7b8d02 100644 --- a/cocos2dx/platform/wophone/CCAccelerometer_wophone.h +++ b/cocos2dx/platform/wophone/CCAccelerometer_wophone.h @@ -27,37 +27,11 @@ THE SOFTWARE. #include "CCAccelerometerDelegate.h" #include "TG3.h" -#include "CCMutableArray.h" #include "CCCommon.h" #include "TCOM_Sensors_Interface.h" namespace cocos2d { -/** -@brief -CCAccelerometerHandler -Object than contains the CCAccelerometerDelegate. -*/ -class CC_DLL CCAccelerometerHandler : public CCObject -{ -public: - virtual ~CCAccelerometerHandler(void); - - /** delegate */ - CCAccelerometerDelegate* getDelegate(); - void setDelegate(CCAccelerometerDelegate *pDelegate); - - /** initializes a CCAccelerometerHandler with a delegate */ - virtual bool initWithDelegate(CCAccelerometerDelegate *pDelegate); - -public: - /** allocates a AccelerometerHandler with a delegate */ - static CCAccelerometerHandler* handlerWithDelegate(CCAccelerometerDelegate *pDelegate); - -protected: - CCAccelerometerDelegate* m_pDelegate; -}; - /** @brief The CCAccelerometer class lets you register to receive @@ -90,9 +64,7 @@ public: void didAccelerate(CCAcceleration* pAccelerationValue); protected: - typedef CCMutableArray AccDelegateArray; - - AccDelegateArray* m_pDelegates; + CCAccelerometerDelegate* m_pDelegate; TCOM_Sensors_DataType_Client* m_pSensor; };