From ccbd0a831d8d2552e32a673d57b690f50e066d66 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 19 Jun 2013 16:10:53 +0800 Subject: [PATCH] issue #2304:use function to simplify acceremeter call back --- .../CCLayer.cpp | 8 +- .../layers_scenes_transitions_nodes/CCLayer.h | 2 +- cocos2dx/platform/CCAccelerometerDelegate.h | 11 -- cocos2dx/platform/android/CCAccelerometer.cpp | 12 +- cocos2dx/platform/android/CCAccelerometer.h | 7 +- .../platform/blackberry/CCAccelerometer.cpp | 10 +- .../platform/blackberry/CCAccelerometer.h | 5 +- .../platform/emscripten/CCAccelerometer.h | 7 +- .../ios/AccelerometerDelegateWrapper.h | 43 ------ .../ios/AccelerometerDelegateWrapper.mm | 115 ---------------- cocos2dx/platform/ios/CCAccelerometer.h | 7 +- cocos2dx/platform/ios/CCAccelerometer.mm | 124 ++++++++++++++++-- cocos2dx/platform/ios/CCImage.mm | 27 +--- cocos2dx/platform/linux/CCAccelerometer.h | 7 +- .../platform/marmalade/CCAccelerometer.cpp | 12 +- cocos2dx/platform/marmalade/CCAccelerometer.h | 6 +- cocos2dx/platform/nacl/CCAccelerometer.h | 5 +- cocos2dx/platform/tizen/CCAccelerometer.cpp | 14 +- cocos2dx/platform/tizen/CCAccelerometer.h | 5 +- cocos2dx/platform/win32/CCAccelerometer.cpp | 12 +- cocos2dx/platform/win32/CCAccelerometer.h | 5 +- .../project.pbxproj.REMOVED.git-id | 2 +- extensions/Components/CCInputDelegate.cpp | 4 +- extensions/Components/CCInputDelegate.h | 2 +- 24 files changed, 185 insertions(+), 267 deletions(-) delete mode 100644 cocos2dx/platform/ios/AccelerometerDelegateWrapper.h delete mode 100644 cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 97a4c93d41..5ecbe61e74 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -235,11 +235,11 @@ void CCLayer::setAccelerometerEnabled(bool enabled) CCDirector* pDirector = CCDirector::sharedDirector(); if (enabled) { - pDirector->getAccelerometer()->setDelegate(this); + pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this)); } else { - pDirector->getAccelerometer()->setDelegate(NULL); + pDirector->getAccelerometer()->setDelegate(nullptr); } } } @@ -379,7 +379,7 @@ void CCLayer::onEnter() // add this layer to concern the Accelerometer Sensor if (_accelerometerEnabled) { - pDirector->getAccelerometer()->setDelegate(this); + pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this)); } // add this layer to concern the keypad msg @@ -419,7 +419,7 @@ void CCLayer::onEnterTransitionDidFinish() if (_accelerometerEnabled) { CCDirector* pDirector = CCDirector::sharedDirector(); - pDirector->getAccelerometer()->setDelegate(this); + pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this)); } CCNode::onEnterTransitionDidFinish(); diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index 08575fa9ce..0b70a8993d 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -60,7 +60,7 @@ All features from CCNode are valid, plus the following new features: - It can receive iPhone Touches - It can receive Accelerometer input */ -class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate +class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCKeypadDelegate { public: CCLayer(); diff --git a/cocos2dx/platform/CCAccelerometerDelegate.h b/cocos2dx/platform/CCAccelerometerDelegate.h index 584c651cff..aff3ea3629 100644 --- a/cocos2dx/platform/CCAccelerometerDelegate.h +++ b/cocos2dx/platform/CCAccelerometerDelegate.h @@ -43,17 +43,6 @@ public: CCAcceleration(): x(0), y(0), z(0), timestamp(0) {} }; -/** -@brief -The CCAccelerometerDelegate defines a single method for -receiving acceleration-related data from the system. -*/ -class CC_DLL CCAccelerometerDelegate -{ -public: - virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} -}; - NS_CC_END #endif diff --git a/cocos2dx/platform/android/CCAccelerometer.cpp b/cocos2dx/platform/android/CCAccelerometer.cpp index ff367e7e2f..9f52331c9f 100644 --- a/cocos2dx/platform/android/CCAccelerometer.cpp +++ b/cocos2dx/platform/android/CCAccelerometer.cpp @@ -32,7 +32,7 @@ THE SOFTWARE. namespace cocos2d { - CCAccelerometer::CCAccelerometer() : _accelDelegate(NULL) + CCAccelerometer::CCAccelerometer() : _function(nullptr) { } @@ -41,11 +41,11 @@ namespace cocos2d } - void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) + void CCAccelerometer::setDelegate(std::function function) { - _accelDelegate = pDelegate; + _function = function; - if (pDelegate) + if (_function) { enableAccelerometerJNI(); } @@ -63,14 +63,14 @@ namespace cocos2d void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp) { - if (_accelDelegate) + if (_function) { _accelerationValue.x = -((double)x / TG3_GRAVITY_EARTH); _accelerationValue.y = -((double)y / TG3_GRAVITY_EARTH); _accelerationValue.z = -((double)z / TG3_GRAVITY_EARTH); _accelerationValue.timestamp = (double)sensorTimeStamp; - _accelDelegate->didAccelerate(&_accelerationValue); + _function(&_accelerationValue); } } } // end of namespace cococs2d diff --git a/cocos2dx/platform/android/CCAccelerometer.h b/cocos2dx/platform/android/CCAccelerometer.h index f9b1597676..6bffa3b88a 100644 --- a/cocos2dx/platform/android/CCAccelerometer.h +++ b/cocos2dx/platform/android/CCAccelerometer.h @@ -27,21 +27,22 @@ THE SOFTWARE. #include "platform/CCCommon.h" #include "platform/CCAccelerometerDelegate.h" +#include namespace cocos2d { -class CC_DLL CCAccelerometer +class CCAccelerometer { public: CCAccelerometer(); ~CCAccelerometer(); - void setDelegate(CCAccelerometerDelegate* pDelegate); + void setDelegate(std::function function); void setAccelerometerInterval(float interval); void update(float x, float y, float z, long sensorTimeStamp); private: - CCAccelerometerDelegate* _accelDelegate; + std::function _function; CCAcceleration _accelerationValue; }; diff --git a/cocos2dx/platform/blackberry/CCAccelerometer.cpp b/cocos2dx/platform/blackberry/CCAccelerometer.cpp index 0625e41432..0e43237697 100644 --- a/cocos2dx/platform/blackberry/CCAccelerometer.cpp +++ b/cocos2dx/platform/blackberry/CCAccelerometer.cpp @@ -31,7 +31,7 @@ int CCAccelerometer::_initialOrientationAngle = 0; CCAccelerometer::CCAccelerometer() { - _accelDelegate = NULL; + _function = nullptr; _initialOrientationAngle = atoi(getenv("ORIENTATION")); } @@ -40,14 +40,14 @@ CCAccelerometer::~CCAccelerometer() } -void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) +void CCAccelerometer::setDelegate(std::function function) { - _accelDelegate = pDelegate; + _function = function; } void CCAccelerometer::update(long timeStamp, double x, double y, double z) { - if ( _accelDelegate != NULL) + if ( _function != NULL) { if (getenv("WIDTH")) { @@ -81,7 +81,7 @@ void CCAccelerometer::update(long timeStamp, double x, double y, double z) _accelerationValue.z = z; _accelerationValue.timestamp = (double)timeStamp; - _accelDelegate->didAccelerate(&_accelerationValue); + _function(&_accelerationValue); } } diff --git a/cocos2dx/platform/blackberry/CCAccelerometer.h b/cocos2dx/platform/blackberry/CCAccelerometer.h index f84683ade3..37af3981b2 100644 --- a/cocos2dx/platform/blackberry/CCAccelerometer.h +++ b/cocos2dx/platform/blackberry/CCAccelerometer.h @@ -25,6 +25,7 @@ THE SOFTWARE. #ifndef __PLATFORM_CCACCELEROMETER_BLACKBERRY_H__ #define __PLATFORM_CCACCELEROMETER_BLACKBERRY_H__ +#include #include "platform/CCAccelerometerDelegate.h" NS_CC_BEGIN @@ -35,12 +36,12 @@ public: CCAccelerometer(); ~CCAccelerometer(); - void setDelegate(CCAccelerometerDelegate* pDelegate); + void setDelegate(std::function function); void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);}; void update(long sensorTimeStamp, double x, double y, double z); private: - CCAccelerometerDelegate* _accelDelegate; + std::function _function; CCAcceleration _accelerationValue; static int _initialOrientationAngle; }; diff --git a/cocos2dx/platform/emscripten/CCAccelerometer.h b/cocos2dx/platform/emscripten/CCAccelerometer.h index 0a738fc5de..be806a06ef 100644 --- a/cocos2dx/platform/emscripten/CCAccelerometer.h +++ b/cocos2dx/platform/emscripten/CCAccelerometer.h @@ -9,6 +9,7 @@ #define CCACCELEROMETER_H_ #include "platform/CCAccelerometerDelegate.h" +#include namespace cocos2d { @@ -20,9 +21,9 @@ public: static CCAccelerometer* sharedAccelerometer() { return NULL; }; - void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; - void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; - void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; + void removeDelegate(std::function function) {CC_UNUSED_PARAM(function);}; + void addDelegate(std::function function) {CC_UNUSED_PARAM(function);}; + void setDelegate(std::function function) {CC_UNUSED_PARAM(function);}; void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);}; }; diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h deleted file mode 100644 index fc0c18bd29..0000000000 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h +++ /dev/null @@ -1,43 +0,0 @@ -/**************************************************************************** - Copyright (c) 2010 cocos2d-x.org - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -#import -#import "CCAccelerometerDelegate.h" -#import - -@interface AccelerometerDispatcher : NSObject -{ - cocos2d::CCAccelerometerDelegate *delegate_; - cocos2d::CCAcceleration *acceleration_; -} - -@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_; -@property(readwrite) cocos2d::CCAcceleration *acceleration_; - -+ (id) sharedAccelerometerDispather; -- (id) init; -- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; -- (void) setAccelerometerInterval:(float)interval; - -@end diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm deleted file mode 100644 index f219e4cdb6..0000000000 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm +++ /dev/null @@ -1,115 +0,0 @@ -/**************************************************************************** - Copyright (c) 2010 cocos2d-x.org - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -#import "AccelerometerDelegateWrapper.h" - -@implementation AccelerometerDispatcher - -static AccelerometerDispatcher* s_pAccelerometerDispatcher; - -@synthesize delegate_; -@synthesize acceleration_; - -+ (id) sharedAccelerometerDispather -{ - if (s_pAccelerometerDispatcher == nil) { - s_pAccelerometerDispatcher = [[self alloc] init]; - } - - return s_pAccelerometerDispatcher; -} - -- (id) init -{ - acceleration_ = new cocos2d::CCAcceleration(); - return self; -} - -- (void) dealloc -{ - s_pAccelerometerDispatcher = 0; - delegate_ = 0; - delete acceleration_; - [super dealloc]; -} - -- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate -{ - delegate_ = delegate; - - if (delegate_) - { - [[UIAccelerometer sharedAccelerometer] setDelegate:self]; - } - else - { - [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; - } -} - --(void) setAccelerometerInterval:(float)interval -{ - [[UIAccelerometer sharedAccelerometer] setUpdateInterval:interval]; -} - -- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration -{ - if (! delegate_) - { - return; - } - - acceleration_->x = acceleration.x; - acceleration_->y = acceleration.y; - acceleration_->z = acceleration.z; - acceleration_->timestamp = acceleration.timestamp; - - double tmp = acceleration_->x; - - switch ([[UIApplication sharedApplication] statusBarOrientation]) - { - case UIInterfaceOrientationLandscapeRight: - acceleration_->x = -acceleration_->y; - acceleration_->y = tmp; - break; - - case UIInterfaceOrientationLandscapeLeft: - acceleration_->x = acceleration_->y; - acceleration_->y = -tmp; - break; - - case UIInterfaceOrientationPortraitUpsideDown: - acceleration_->x = -acceleration_->y; - acceleration_->y = -tmp; - break; - - case UIInterfaceOrientationPortrait: - break; - } - - delegate_->didAccelerate(acceleration_); -} - -@end - diff --git a/cocos2dx/platform/ios/CCAccelerometer.h b/cocos2dx/platform/ios/CCAccelerometer.h index c5274fb7cb..c8011bc169 100644 --- a/cocos2dx/platform/ios/CCAccelerometer.h +++ b/cocos2dx/platform/ios/CCAccelerometer.h @@ -25,17 +25,18 @@ THE SOFTWARE. #ifndef __PLATFORM_IPHONE_CCACCELEROMETER_H__ #define __PLATFORM_IPHONE_CCACCELEROMETER_H__ +#include #include "platform/CCAccelerometerDelegate.h" NS_CC_BEGIN -class CC_DLL CCAccelerometer +class CCAccelerometer { public: CCAccelerometer(); ~CCAccelerometer(); - - void setDelegate(CCAccelerometerDelegate* pDelegate); + + void setDelegate(std::function function); void setAccelerometerInterval(float interval); }; diff --git a/cocos2dx/platform/ios/CCAccelerometer.mm b/cocos2dx/platform/ios/CCAccelerometer.mm index 8ffaa4f2c4..d87ffdfbca 100644 --- a/cocos2dx/platform/ios/CCAccelerometer.mm +++ b/cocos2dx/platform/ios/CCAccelerometer.mm @@ -23,21 +23,124 @@ ****************************************************************************/ #include "CCAccelerometer.h" -#include "AccelerometerDelegateWrapper.h" + +#import +#import "CCAccelerometerDelegate.h" +#import +#import + +@interface AccelerometerDispatcher : NSObject +{ + std::function _function; + cocos2d::CCAcceleration *_acceleration; +} + ++ (id) sharedAccelerometerDispather; +- (id) init; +- (void) addDelegate: (std::function) function; +- (void) setAccelerometerInterval:(float)interval; + +@end + +@implementation AccelerometerDispatcher + +static AccelerometerDispatcher* s_pAccelerometerDispatcher; + ++ (id) sharedAccelerometerDispather +{ + if (s_pAccelerometerDispatcher == nil) { + s_pAccelerometerDispatcher = [[self alloc] init]; + } + + return s_pAccelerometerDispatcher; +} + +- (id) init +{ + _acceleration = new cocos2d::CCAcceleration(); + return self; +} + +- (void) dealloc +{ + s_pAccelerometerDispatcher = nullptr; + _function = nullptr; + delete _acceleration; + [super dealloc]; +} + +- (void) addDelegate: (std::function) function +{ + _function = function; + + if (_function) + { + [[UIAccelerometer sharedAccelerometer] setDelegate:self]; + } + else + { + [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; + } +} + +-(void) setAccelerometerInterval:(float)interval +{ + [[UIAccelerometer sharedAccelerometer] setUpdateInterval:interval]; +} + +- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration +{ + if (! _function) + { + return; + } + + _acceleration->x = acceleration.x; + _acceleration->y = acceleration.y; + _acceleration->z = acceleration.z; + _acceleration->timestamp = acceleration.timestamp; + + double tmp = _acceleration->x; + + switch ([[UIApplication sharedApplication] statusBarOrientation]) + { + case UIInterfaceOrientationLandscapeRight: + _acceleration->x = -_acceleration->y; + _acceleration->y = tmp; + break; + + case UIInterfaceOrientationLandscapeLeft: + _acceleration->x = _acceleration->y; + _acceleration->y = -tmp; + break; + + case UIInterfaceOrientationPortraitUpsideDown: + _acceleration->x = -_acceleration->y; + _acceleration->y = -tmp; + break; + + case UIInterfaceOrientationPortrait: + break; + } + + _function(_acceleration); +} + +@end + +/** Implementation of CCAccelerometer + */ NS_CC_BEGIN - -CCAccelerometer::CCAccelerometer() -{ -} -CCAccelerometer::~CCAccelerometer() -{ -} -void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) +CCAccelerometer::CCAccelerometer() {} + +CCAccelerometer::~CCAccelerometer() {} + +void CCAccelerometer::setDelegate(std::function function) { - [[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate]; + [[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:function]; } void CCAccelerometer::setAccelerometerInterval(float interval) @@ -46,4 +149,3 @@ void CCAccelerometer::setAccelerometerInterval(float interval) } NS_CC_END - diff --git a/cocos2dx/platform/ios/CCImage.mm b/cocos2dx/platform/ios/CCImage.mm index dd996746f6..a07b4a363b 100644 --- a/cocos2dx/platform/ios/CCImage.mm +++ b/cocos2dx/platform/ios/CCImage.mm @@ -123,29 +123,6 @@ static bool _initWithImage(CGImageRef cgImage, tImageInfo *pImageinfo) return true; } -static bool _initWithFile(const char* path, tImageInfo *pImageinfo) -{ - CGImageRef CGImage; - UIImage *jpg; - UIImage *png; - bool ret; - - // convert jpg to png before loading the texture - - NSString *fullPath = [NSString stringWithUTF8String:path]; - jpg = [[UIImage alloc] initWithContentsOfFile: fullPath]; - png = [[UIImage alloc] initWithData:UIImagePNGRepresentation(jpg)]; - CGImage = png.CGImage; - - ret = _initWithImage(CGImage, pImageinfo); - - [png release]; - [jpg release]; - - return ret; -} - - static bool _initWithData(void * pBuffer, int length, tImageInfo *pImageinfo) { bool ret = false; @@ -482,7 +459,7 @@ bool CCImage::initWithImageData(void * pData, CC_BREAK_IF(! pData || nDataLen <= 0); if (eFmt == kFmtRawData) { - bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent, false); + bRet = initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent, false); } else if (eFmt == kFmtWebp) { @@ -506,7 +483,7 @@ bool CCImage::initWithImageData(void * pData, return bRet; } -bool CCImage::_initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti) +bool CCImage::initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti) { bool bRet = false; do diff --git a/cocos2dx/platform/linux/CCAccelerometer.h b/cocos2dx/platform/linux/CCAccelerometer.h index 0a738fc5de..be806a06ef 100644 --- a/cocos2dx/platform/linux/CCAccelerometer.h +++ b/cocos2dx/platform/linux/CCAccelerometer.h @@ -9,6 +9,7 @@ #define CCACCELEROMETER_H_ #include "platform/CCAccelerometerDelegate.h" +#include namespace cocos2d { @@ -20,9 +21,9 @@ public: static CCAccelerometer* sharedAccelerometer() { return NULL; }; - void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; - void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; - void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; + void removeDelegate(std::function function) {CC_UNUSED_PARAM(function);}; + void addDelegate(std::function function) {CC_UNUSED_PARAM(function);}; + void setDelegate(std::function function) {CC_UNUSED_PARAM(function);}; void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);}; }; diff --git a/cocos2dx/platform/marmalade/CCAccelerometer.cpp b/cocos2dx/platform/marmalade/CCAccelerometer.cpp index 7017cf6d9e..14fb5a7302 100644 --- a/cocos2dx/platform/marmalade/CCAccelerometer.cpp +++ b/cocos2dx/platform/marmalade/CCAccelerometer.cpp @@ -30,7 +30,7 @@ namespace cocos2d CCAccelerometer* CCAccelerometer::_spCCAccelerometer = NULL; -CCAccelerometer::CCAccelerometer() : _accelDelegate(NULL) +CCAccelerometer::CCAccelerometer() : _function(nullptr) { } @@ -53,11 +53,11 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer() return _spCCAccelerometer; } -void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) +void CCAccelerometer::setDelegate(std::function function) { - _accelDelegate = pDelegate; + _function = function; - if (pDelegate) + if (_function) { if (s3eAccelerometerStart() != S3E_RESULT_SUCCESS) { @@ -72,14 +72,14 @@ void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::update(float x, float y, float z, uint64 sensorTimeStamp) { - if (_accelDelegate) + if (_function) { _accelerationValue.x = ((double)x)/S3E_ACCELEROMETER_1G ; _accelerationValue.y = ((double)y)/S3E_ACCELEROMETER_1G ; _accelerationValue.z = ((double)z)/S3E_ACCELEROMETER_1G ; _accelerationValue.timestamp = (double)(sensorTimeStamp / 1000.0); - _accelDelegate->didAccelerate(&_accelerationValue); + _function(&_accelerationValue); } } diff --git a/cocos2dx/platform/marmalade/CCAccelerometer.h b/cocos2dx/platform/marmalade/CCAccelerometer.h index b2bd704e9b..0a4ea0a9d3 100644 --- a/cocos2dx/platform/marmalade/CCAccelerometer.h +++ b/cocos2dx/platform/marmalade/CCAccelerometer.h @@ -27,7 +27,7 @@ #include "CCAccelerometerDelegate.h" //#include "CCMutableArray.h" #include "ccCommon.h" - +#include namespace cocos2d { @@ -47,13 +47,13 @@ public: */ static CCAccelerometer* sharedAccelerometer(); - void setDelegate(CCAccelerometerDelegate* pDelegate); + void setDelegate(std::function function); void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);}; void update(float x, float y, float z, uint64 sensorTimeStamp); private: static CCAccelerometer* _spCCAccelerometer; - CCAccelerometerDelegate* _accelDelegate; + std::function _function; CCAcceleration _accelerationValue; }; diff --git a/cocos2dx/platform/nacl/CCAccelerometer.h b/cocos2dx/platform/nacl/CCAccelerometer.h index a64e33cdeb..7a477a7e20 100644 --- a/cocos2dx/platform/nacl/CCAccelerometer.h +++ b/cocos2dx/platform/nacl/CCAccelerometer.h @@ -26,6 +26,7 @@ THE SOFTWARE. #define __CCACCELEROMETER_H__ #include "platform/CCAccelerometerDelegate.h" +#include namespace cocos2d { @@ -37,8 +38,8 @@ public: static CCAccelerometer* sharedAccelerometer() { return NULL; }; - void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; - void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; + void removeDelegate(std::function function) {CC_UNUSED_PARAM(function);}; + void addDelegate(std::function function) {CC_UNUSED_PARAM(function);}; void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);}; }; diff --git a/cocos2dx/platform/tizen/CCAccelerometer.cpp b/cocos2dx/platform/tizen/CCAccelerometer.cpp index 71e282907d..5f1d4b97c5 100644 --- a/cocos2dx/platform/tizen/CCAccelerometer.cpp +++ b/cocos2dx/platform/tizen/CCAccelerometer.cpp @@ -30,8 +30,8 @@ using namespace Tizen::Uix::Sensor; NS_CC_BEGIN CCAccelerometer::CCAccelerometer() - : _accelDelegate(NULL) - , __sensorMgr(NULL) + : _function(nullptr) + , __sensorMgr(nullptr) { } @@ -40,11 +40,11 @@ CCAccelerometer::~CCAccelerometer() } -void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) +void CCAccelerometer::setDelegate(std::function function) { - _accelDelegate = pDelegate; + _function = function; - if (pDelegate) + if (_function) { startSensor(); } @@ -97,7 +97,7 @@ void CCAccelerometer::stopSensor() void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorData , result r) { - if (_accelDelegate) + if (_function) { AccelerationSensorData& data = static_cast(sensorData); AppLog("AccelerationSensorData x = %5.4f , y = %5.4f, z = %5.4f ", data.x,data.y,data.z); @@ -107,7 +107,7 @@ void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorDa _accelerationValue.z = -data.z; _accelerationValue.timestamp = data.timestamp; - _accelDelegate->didAccelerate(&_accelerationValue); + _function(&_accelerationValue); } } diff --git a/cocos2dx/platform/tizen/CCAccelerometer.h b/cocos2dx/platform/tizen/CCAccelerometer.h index 744a982ad7..9e225e33f2 100644 --- a/cocos2dx/platform/tizen/CCAccelerometer.h +++ b/cocos2dx/platform/tizen/CCAccelerometer.h @@ -29,6 +29,7 @@ THE SOFTWARE. #include "platform/CCCommon.h" #include "platform/CCAccelerometerDelegate.h" #include +#include NS_CC_BEGIN @@ -38,7 +39,7 @@ public: CCAccelerometer(); ~CCAccelerometer(); - void setDelegate(CCAccelerometerDelegate* pDelegate); + void setDelegate(std::function function); void setAccelerometerInterval(float interval); void startSensor(); void stopSensor(); @@ -46,7 +47,7 @@ public: virtual void OnDataReceived(Tizen::Uix::Sensor::SensorType sensorType, Tizen::Uix::Sensor::SensorData& sensorData , result r); private: - CCAccelerometerDelegate* _accelDelegate; + std::function _function; CCAcceleration _accelerationValue; Tizen::Uix::Sensor::SensorManager* __sensorMgr; }; diff --git a/cocos2dx/platform/win32/CCAccelerometer.cpp b/cocos2dx/platform/win32/CCAccelerometer.cpp index d4461c56f1..b8757c359a 100644 --- a/cocos2dx/platform/win32/CCAccelerometer.cpp +++ b/cocos2dx/platform/win32/CCAccelerometer.cpp @@ -148,7 +148,7 @@ namespace NS_CC_BEGIN CCAccelerometer::CCAccelerometer() : - _accelDelegate(NULL) + _function(nullptr) { memset(&_accelerationValue, 0, sizeof(_accelerationValue)); } @@ -158,14 +158,14 @@ CCAccelerometer::~CCAccelerometer() } -void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) +void CCAccelerometer::setDelegate(std::function function) { - _accelDelegate = pDelegate; + _function = function; // Enable/disable the accelerometer. // Well, there isn't one on Win32 so we don't do anything other than register // and deregister ourselves from the Windows Key handler. - if (pDelegate) + if (_function) { // Register our handler CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( &myAccelerometerKeyHook ); @@ -185,7 +185,7 @@ void CCAccelerometer::setAccelerometerInterval(float interval) void CCAccelerometer::update( double x,double y,double z,double timestamp ) { - if (_accelDelegate) + if (_function) { _accelerationValue.x = x; _accelerationValue.y = y; @@ -193,7 +193,7 @@ void CCAccelerometer::update( double x,double y,double z,double timestamp ) _accelerationValue.timestamp = timestamp; // Delegate - _accelDelegate->didAccelerate(&_accelerationValue); + _function(&_accelerationValue); } } diff --git a/cocos2dx/platform/win32/CCAccelerometer.h b/cocos2dx/platform/win32/CCAccelerometer.h index 66dec38a41..5de77a7228 100644 --- a/cocos2dx/platform/win32/CCAccelerometer.h +++ b/cocos2dx/platform/win32/CCAccelerometer.h @@ -26,6 +26,7 @@ THE SOFTWARE. #define __PLATFORM_WIN32_UIACCELEROMETER_H__ #include "platform/CCAccelerometerDelegate.h" +#include NS_CC_BEGIN @@ -35,12 +36,12 @@ public: CCAccelerometer(); ~CCAccelerometer(); - void setDelegate(CCAccelerometerDelegate* pDelegate); + void setDelegate(std::function function); void setAccelerometerInterval(float interval); void update( double x,double y,double z,double timestamp ); private: CCAcceleration _accelerationValue; - CCAccelerometerDelegate* _accelDelegate; + std::function _function; }; NS_CC_END diff --git a/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id b/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id index 481079f24d..96fb4148fa 100644 --- a/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -06a3716ff2fdcaa883bed71ffe53793daf0f3980 \ No newline at end of file +08c5c5cd8dcd5b6d72678c72cd156682e33f8553 \ No newline at end of file diff --git a/extensions/Components/CCInputDelegate.cpp b/extensions/Components/CCInputDelegate.cpp index e92d3f1862..5a58b70b42 100644 --- a/extensions/Components/CCInputDelegate.cpp +++ b/extensions/Components/CCInputDelegate.cpp @@ -174,11 +174,11 @@ void CCInputDelegate::setAccelerometerEnabled(bool enabled) CCDirector* pDirector = CCDirector::sharedDirector(); if (enabled) { - pDirector->getAccelerometer()->setDelegate(this); + pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCInputDelegate::didAccelerate, this)); } else { - pDirector->getAccelerometer()->setDelegate(NULL); + pDirector->getAccelerometer()->setDelegate(nullptr); } } } diff --git a/extensions/Components/CCInputDelegate.h b/extensions/Components/CCInputDelegate.h index c3758bbe88..b067de5d24 100644 --- a/extensions/Components/CCInputDelegate.h +++ b/extensions/Components/CCInputDelegate.h @@ -30,7 +30,7 @@ THE SOFTWARE. NS_CC_EXT_BEGIN -class CCInputDelegate : public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate +class CCInputDelegate : public CCTouchDelegate, public CCKeypadDelegate { protected: CCInputDelegate(void);