issue #1056: Move some singletons to CCDirector. It is easily managed by CCDirector.

This commit is contained in:
James Chen 2012-03-21 17:35:20 +08:00
parent 159b677dc0
commit f529bcd659
36 changed files with 148 additions and 195 deletions

View File

@ -44,6 +44,7 @@ THE SOFTWARE.
#include "CCLabelTTF.h" #include "CCLabelTTF.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "CCKeypadDispatcher.h" #include "CCKeypadDispatcher.h"
#include "CCAccelerometer.h"
#include "CCGL.h" #include "CCGL.h"
#include "CCAnimationCache.h" #include "CCAnimationCache.h"
#include "CCTouch.h" #include "CCTouch.h"
@ -85,6 +86,11 @@ CCDirector* CCDirector::sharedDirector(void)
return &s_sharedDirector; return &s_sharedDirector;
} }
CCDirector::CCDirector(void)
{
}
bool CCDirector::init(void) bool CCDirector::init(void)
{ {
CCLOG("cocos2d: %s", cocos2dVersion()); CCLOG("cocos2d: %s", cocos2dVersion());
@ -133,6 +139,15 @@ bool CCDirector::init(void)
// action manager // action manager
m_pActionManager = new CCActionManager(); m_pActionManager = new CCActionManager();
m_pScheduler->scheduleUpdateForTarget(m_pActionManager, kCCActionManagerPriority, false); m_pScheduler->scheduleUpdateForTarget(m_pActionManager, kCCActionManagerPriority, false);
// touchDispatcher
m_pTouchDispatcher = new CCTouchDispatcher();
m_pTouchDispatcher->init();
// KeypadDispatcher
m_pKeypadDispatcher = new CCKeypadDispatcher();
// Accelerometer
m_pAccelerometer = new CCAccelerometer();
// create autorelease pool // create autorelease pool
CCPoolManager::getInstance()->push(); CCPoolManager::getInstance()->push();
@ -153,15 +168,14 @@ CCDirector::~CCDirector(void)
CC_SAFE_RELEASE(m_pobScenesStack); CC_SAFE_RELEASE(m_pobScenesStack);
CC_SAFE_RELEASE(m_pScheduler); CC_SAFE_RELEASE(m_pScheduler);
CC_SAFE_RELEASE(m_pActionManager); CC_SAFE_RELEASE(m_pActionManager);
CC_SAFE_RELEASE(m_pTouchDispatcher);
CC_SAFE_RELEASE(m_pKeypadDispatcher);
CC_SAFE_DELETE(m_pAccelerometer);
// pop the autorelease pool // pop the autorelease pool
CCPoolManager::getInstance()->pop(); CCPoolManager::getInstance()->pop();
// delete m_pLastUpdate // delete m_pLastUpdate
CC_SAFE_DELETE(m_pLastUpdate); CC_SAFE_DELETE(m_pLastUpdate);
CCKeypadDispatcher::purgeSharedDispatcher();
// delete fps string // delete fps string
delete []m_pszFPS; delete []m_pszFPS;
} }
@ -300,9 +314,8 @@ void CCDirector::setOpenGLView(CC_GLVIEW *pobOpenGLView)
updateContentScaleFactor(); updateContentScaleFactor();
} }
CCTouchDispatcher *pTouchDispatcher = CCTouchDispatcher::sharedDispatcher(); m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
m_pobOpenGLView->setTouchDelegate(pTouchDispatcher); m_pTouchDispatcher->setDispatchEvents(true);
pTouchDispatcher->setDispatchEvents(true);
} }
} }
@ -577,7 +590,7 @@ void CCDirector::resetDirector()
{ {
// don't release the event handlers // don't release the event handlers
// They are needed in case the director is run again // They are needed in case the director is run again
CCTouchDispatcher::sharedDispatcher()->removeAllDelegates(); m_pTouchDispatcher->removeAllDelegates();
if (m_pRunningScene) if (m_pRunningScene)
{ {
@ -613,7 +626,7 @@ void CCDirector::purgeDirector()
{ {
// don't release the event handlers // don't release the event handlers
// They are needed in case the director is run again // They are needed in case the director is run again
CCTouchDispatcher::sharedDispatcher()->removeAllDelegates(); m_pTouchDispatcher->removeAllDelegates();
if (m_pRunningScene) if (m_pRunningScene)
{ {
@ -927,6 +940,44 @@ CCActionManager* CCDirector::getActionManager()
return m_pActionManager; return m_pActionManager;
} }
void CCDirector::setTouchDispatcher(CCTouchDispatcher* pTouchDispatcher)
{
CC_SAFE_RETAIN(pTouchDispatcher);
CC_SAFE_RELEASE(m_pTouchDispatcher);
m_pTouchDispatcher = pTouchDispatcher;
}
CCTouchDispatcher* CCDirector::getTouchDispatcher()
{
return m_pTouchDispatcher;
}
void CCDirector::setKeypadDispatcher(CCKeypadDispatcher* pKeypadDispatcher)
{
CC_SAFE_RETAIN(pKeypadDispatcher);
CC_SAFE_RELEASE(m_pKeypadDispatcher);
m_pKeypadDispatcher = pKeypadDispatcher;
}
CCKeypadDispatcher* CCDirector::getKeypadDispatcher()
{
return m_pKeypadDispatcher;
}
void CCDirector::setAccelerometer(CCAccelerometer* pAccelerometer)
{
if (m_pAccelerometer != pAccelerometer)
{
CC_SAFE_DELETE(m_pAccelerometer);
m_pAccelerometer = pAccelerometer;
}
}
CCAccelerometer* CCDirector::getAccelerometer()
{
return m_pAccelerometer;
}
/*************************************************** /***************************************************
* implementation of DisplayLinkDirector * implementation of DisplayLinkDirector
**************************************************/ **************************************************/

View File

@ -142,6 +142,10 @@ class CCDirectorDelegate;
class CCNode; class CCNode;
class CCScheduler; class CCScheduler;
class CCActionManager; class CCActionManager;
class CCTouchDispatcher;
class CCKeypadDispatcher;
class CCAccelerometer;
/** /**
@brief Class that creates and handle the main Window and manages how @brief Class that creates and handle the main Window and manages how
and when to execute the Scenes. and when to execute the Scenes.
@ -164,10 +168,10 @@ and when to execute the Scenes.
*/ */
class CC_DLL CCDirector : public CCObject class CC_DLL CCDirector : public CCObject
{ {
public: public:
virtual bool init(void); CCDirector(void);
virtual ~CCDirector(void); virtual ~CCDirector(void);
CCDirector(void) {} virtual bool init(void);
// attribute // attribute
@ -393,6 +397,21 @@ public:
*/ */
CC_PROPERTY(CCActionManager*, m_pActionManager, ActionManager); CC_PROPERTY(CCActionManager*, m_pActionManager, ActionManager);
/** CCTouchDispatcher associated with this director
@since v2.0
*/
CC_PROPERTY(CCTouchDispatcher*, m_pTouchDispatcher, TouchDispatcher);
/** CCKeypadDispatcher associated with this director
@since v2.0
*/
CC_PROPERTY(CCKeypadDispatcher*, m_pKeypadDispatcher, KeypadDispatcher);
/** CCAccelerometer associated with this director
@since v2.0
*/
CC_PROPERTY(CCAccelerometer*, m_pAccelerometer, Accelerometer);
/** returns a shared instance of the director */ /** returns a shared instance of the director */
static CCDirector* sharedDirector(void); static CCDirector* sharedDirector(void);
void resetDirector(); void resetDirector();

View File

@ -47,16 +47,6 @@ public:
CCKeypadDispatcher(); CCKeypadDispatcher();
~CCKeypadDispatcher(); ~CCKeypadDispatcher();
/**
@brief Returns the shared CCKeypadDispatcher object for the system.
*/
static CCKeypadDispatcher* sharedDispatcher();
/**
@brief Release the shared CCKeypadDispatcher object from the system.
*/
static void purgeSharedDispatcher();
/** /**
@brief add delegate to concern keypad msg @brief add delegate to concern keypad msg
*/ */

View File

@ -140,10 +140,7 @@ public:
virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent); virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent);
public: public:
/** singleton of the CCTouchDispatcher */
static CCTouchDispatcher* sharedDispatcher();
CCTouchHandler* findHandler(CCTouchDelegate *pDelegate); CCTouchHandler* findHandler(CCTouchDelegate *pDelegate);
protected: protected:
void forceRemoveDelegate(CCTouchDelegate *pDelegate); void forceRemoveDelegate(CCTouchDelegate *pDelegate);
void forceAddHandler(CCTouchHandler *pHandler, CCArray* pArray); void forceAddHandler(CCTouchHandler *pHandler, CCArray* pArray);

View File

@ -27,7 +27,6 @@ THE SOFTWARE.
namespace cocos2d { namespace cocos2d {
static CCKeypadDispatcher* s_KeypadDispatcher = NULL;
//------------------------------------------------------------------ //------------------------------------------------------------------
// //
// CCKeypadDispatcher // CCKeypadDispatcher
@ -59,25 +58,6 @@ CCKeypadDispatcher::~CCKeypadDispatcher()
} }
} }
CCKeypadDispatcher* CCKeypadDispatcher::sharedDispatcher()
{
if (! s_KeypadDispatcher)
{
s_KeypadDispatcher = new CCKeypadDispatcher;
}
return s_KeypadDispatcher;
}
void CCKeypadDispatcher::purgeSharedDispatcher()
{
if (s_KeypadDispatcher)
{
s_KeypadDispatcher->release();
s_KeypadDispatcher = NULL;
}
}
void CCKeypadDispatcher::removeDelegate(CCKeypadDelegate* pDelegate) void CCKeypadDispatcher::removeDelegate(CCKeypadDelegate* pDelegate)
{ {
if (!pDelegate) if (!pDelegate)

View File

@ -92,23 +92,24 @@ CCLayer *CCLayer::node()
void CCLayer::registerWithTouchDispatcher() void CCLayer::registerWithTouchDispatcher()
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
if (m_pScriptHandlerEntry) if (m_pScriptHandlerEntry)
{ {
if (m_pScriptHandlerEntry->getIsMultiTouches()) if (m_pScriptHandlerEntry->getIsMultiTouches())
{ {
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0); pDirector->getTouchDispatcher()->addStandardDelegate(this, 0);
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler()); LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler());
} }
else else
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, pDirector->getTouchDispatcher()->addTargetedDelegate(this,
m_pScriptHandlerEntry->getPriority(), m_pScriptHandlerEntry->getPriority(),
m_pScriptHandlerEntry->getSwallowsTouches()); m_pScriptHandlerEntry->getSwallowsTouches());
LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler()); LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler());
} }
return; return;
} }
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); pDirector->getTouchDispatcher()->addStandardDelegate(this,0);
} }
void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches)
@ -157,7 +158,8 @@ void CCLayer::setIsTouchEnabled(bool enabled)
else else
{ {
// have problems? // have problems?
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
} }
} }
} }
@ -177,13 +179,14 @@ void CCLayer::setIsAccelerometerEnabled(bool enabled)
if (m_bIsRunning) if (m_bIsRunning)
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled) if (enabled)
{ {
CCAccelerometer::sharedAccelerometer()->setDelegate(this); pDirector->getAccelerometer()->setDelegate(this);
} }
else else
{ {
CCAccelerometer::sharedAccelerometer()->setDelegate(NULL); pDirector->getAccelerometer()->setDelegate(NULL);
} }
} }
} }
@ -203,13 +206,14 @@ void CCLayer::setIsKeypadEnabled(bool enabled)
if (m_bIsRunning) if (m_bIsRunning)
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled) if (enabled)
{ {
CCKeypadDispatcher::sharedDispatcher()->addDelegate(this); pDirector->getKeypadDispatcher()->addDelegate(this);
} }
else else
{ {
CCKeypadDispatcher::sharedDispatcher()->removeDelegate(this); pDirector->getKeypadDispatcher()->removeDelegate(this);
} }
} }
} }
@ -218,6 +222,7 @@ void CCLayer::setIsKeypadEnabled(bool enabled)
/// Callbacks /// Callbacks
void CCLayer::onEnter() void CCLayer::onEnter()
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
// register 'parent' nodes first // register 'parent' nodes first
// since events are propagated in reverse order // since events are propagated in reverse order
if (m_bIsTouchEnabled) if (m_bIsTouchEnabled)
@ -231,34 +236,35 @@ void CCLayer::onEnter()
// add this layer to concern the Accelerometer Sensor // add this layer to concern the Accelerometer Sensor
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->setDelegate(this); pDirector->getAccelerometer()->setDelegate(this);
} }
// add this layer to concern the kaypad msg // add this layer to concern the kaypad msg
if (m_bIsKeypadEnabled) if (m_bIsKeypadEnabled)
{ {
CCKeypadDispatcher::sharedDispatcher()->addDelegate(this); pDirector->getKeypadDispatcher()->addDelegate(this);
} }
} }
void CCLayer::onExit() void CCLayer::onExit()
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
if( m_bIsTouchEnabled ) if( m_bIsTouchEnabled )
{ {
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); pDirector->getTouchDispatcher()->removeDelegate(this);
unregisterScriptTouchHandler(); unregisterScriptTouchHandler();
} }
// remove this layer from the delegates who concern Accelerometer Sensor // remove this layer from the delegates who concern Accelerometer Sensor
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->setDelegate(NULL); pDirector->getAccelerometer()->setDelegate(NULL);
} }
// remove this layer from the delegates who concern the kaypad msg // remove this layer from the delegates who concern the kaypad msg
if (m_bIsKeypadEnabled) if (m_bIsKeypadEnabled)
{ {
CCKeypadDispatcher::sharedDispatcher()->removeDelegate(this); pDirector->getKeypadDispatcher()->removeDelegate(this);
} }
CCNode::onExit(); CCNode::onExit();
@ -268,7 +274,8 @@ void CCLayer::onEnterTransitionDidFinish()
{ {
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->setDelegate(this); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getAccelerometer()->setDelegate(this);
} }
CCNode::onEnterTransitionDidFinish(); CCNode::onEnterTransitionDidFinish();

View File

@ -92,7 +92,8 @@ bool CCTransitionScene::initWithDuration(ccTime t, CCScene *scene)
CCAssert( m_pInScene != m_pOutScene, "Incoming scene must be different from the outgoing scene" ); CCAssert( m_pInScene != m_pOutScene, "Incoming scene must be different from the outgoing scene" );
// disable events while transitions // disable events while transitions
CCTouchDispatcher::sharedDispatcher()->setDispatchEvents(false); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->setDispatchEvents(false);
this->sceneOrder(); this->sceneOrder();
return true; return true;
@ -153,7 +154,7 @@ void CCTransitionScene::setNewScene(ccTime dt)
m_bIsSendCleanupToScene = director->isSendCleanupToScene(); m_bIsSendCleanupToScene = director->isSendCleanupToScene();
director->replaceScene(m_pInScene); director->replaceScene(m_pInScene);
// enable events while transitions // enable events while transitions
CCTouchDispatcher::sharedDispatcher()->setDispatchEvents(true); director->getTouchDispatcher()->setDispatchEvents(true);
// issue #267 // issue #267
m_pOutScene->setIsVisible(true); m_pOutScene->setIsVisible(true);
} }

View File

@ -160,7 +160,8 @@ namespace cocos2d{
//Menu - Events //Menu - Events
void CCMenu::registerWithTouchDispatcher() void CCMenu::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority, true);
} }
bool CCMenu::ccTouchBegan(CCTouch* touch, CCEvent* event) bool CCMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)

View File

@ -32,26 +32,13 @@ THE SOFTWARE.
namespace cocos2d namespace cocos2d
{ {
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL) CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL)
{ {
} }
CCAccelerometer::~CCAccelerometer() CCAccelerometer::~CCAccelerometer()
{ {
m_spCCAccelerometer = NULL;
}
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spCCAccelerometer == NULL)
{
m_spCCAccelerometer = new CCAccelerometer();
}
return m_spCCAccelerometer;
} }
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)

View File

@ -36,13 +36,10 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void update(float x, float y, float z, long sensorTimeStamp); void update(float x, float y, float z, long sensorTimeStamp);
private: private:
static CCAccelerometer* m_spCCAccelerometer;
CCAccelerometerDelegate* m_pAccelDelegate; CCAccelerometerDelegate* m_pAccelDelegate;
CCAcceleration m_obAccelerationValue; CCAcceleration m_obAccelerationValue;
}; };

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "JniHelper.h" #include "JniHelper.h"
#include <android/log.h> #include <android/log.h>
#include <jni.h> #include <jni.h>
#include "CCDirector.h"
#if 0 #if 0
#define LOG_TAG "SensorJni" #define LOG_TAG "SensorJni"
@ -56,7 +56,8 @@ extern "C"
// (y - rcRect.origin.y) / fScreenScaleFactor, // (y - rcRect.origin.y) / fScreenScaleFactor,
// z, // z,
// timeStamp); // timeStamp);
cocos2d::CCAccelerometer::sharedAccelerometer()->update(x, y, z, timeStamp); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getAccelerometer()->update(x, y, z, timeStamp);
} }
void enableAccelerometerJNI() void enableAccelerometerJNI()

View File

@ -45,12 +45,6 @@ CCAccelerometer::~CCAccelerometer()
CC_SAFE_DELETE(m_pSensor); CC_SAFE_DELETE(m_pSensor);
} }
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
static CCAccelerometer s_CCAccelerometer;
return &s_CCAccelerometer;
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{ {
m_pAccelDelegate = pDelegate; m_pAccelDelegate = pDelegate;

View File

@ -40,7 +40,6 @@ class CC_DLL CCAccelerometer :
public: public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
virtual void OnDataReceived(Osp::Uix::SensorType sensorType, Osp::Uix::SensorData& sensorData , result r); virtual void OnDataReceived(Osp::Uix::SensorType sensorType, Osp::Uix::SensorData& sensorData , result r);

View File

@ -35,12 +35,7 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
private:
static CCAccelerometer* m_spUIAccelerometer;
}; };
}//namespace cocos2d }//namespace cocos2d

View File

@ -27,8 +27,6 @@
namespace cocos2d { namespace cocos2d {
CCAccelerometer* CCAccelerometer::m_spUIAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() CCAccelerometer::CCAccelerometer()
{ {
} }
@ -37,15 +35,6 @@ namespace cocos2d {
{ {
} }
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spUIAccelerometer == NULL) {
m_spUIAccelerometer = new CCAccelerometer();
}
return m_spUIAccelerometer;
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{ {
[[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate]; [[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate];

View File

@ -18,8 +18,6 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer() { return NULL; };
void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);};
void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}; void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);};
void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);} void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}

View File

@ -28,29 +28,12 @@
namespace cocos2d namespace cocos2d
{ {
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL) CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL)
{ {
} }
CCAccelerometer::~CCAccelerometer() CCAccelerometer::~CCAccelerometer()
{ {
if( m_spCCAccelerometer ) {
delete m_spCCAccelerometer ;
m_spCCAccelerometer = NULL;
}
}
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spCCAccelerometer == NULL)
{
m_spCCAccelerometer = new CCAccelerometer();
}
return m_spCCAccelerometer;
} }
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)

View File

@ -42,16 +42,11 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
/**
@brief Returns the shared accelerometer object for the system.
*/
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void update(float x, float y, float z, uint64 sensorTimeStamp); void update(float x, float y, float z, uint64 sensorTimeStamp);
private: private:
static CCAccelerometer* m_spCCAccelerometer;
CCAccelerometerDelegate* m_pAccelDelegate; CCAccelerometerDelegate* m_pAccelDelegate;
CCAcceleration m_obAccelerationValue; CCAcceleration m_obAccelerationValue;
}; };

View File

@ -126,7 +126,8 @@ void CCApplication::ccAccelerationUpdate()
{ {
// Accelerometer doesn't work on Marmalade X86 MacOS-X simulator // Accelerometer doesn't work on Marmalade X86 MacOS-X simulator
#if !(defined(__APPLE__) && defined(I3D_ARCH_X86)) #if !(defined(__APPLE__) && defined(I3D_ARCH_X86))
CCAccelerometer::sharedAccelerometer()->update(s3eAccelerometerGetX(),s3eAccelerometerGetY(),s3eAccelerometerGetZ(),s3eTimerGetMs()); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getAccelerometer()->update(s3eAccelerometerGetX(),s3eAccelerometerGetY(),s3eAccelerometerGetZ(),s3eTimerGetMs());
#endif #endif
} }

View File

@ -33,7 +33,6 @@ THE SOFTWARE.
namespace cocos2d namespace cocos2d
{ {
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
int CCAccelerometer::m_initialOrientationAngle = 0; int CCAccelerometer::m_initialOrientationAngle = 0;
CCAccelerometer::CCAccelerometer() CCAccelerometer::CCAccelerometer()
@ -49,14 +48,6 @@ namespace cocos2d
} }
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spCCAccelerometer == NULL)
m_spCCAccelerometer = new CCAccelerometer();
return m_spCCAccelerometer;
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{ {
m_pAccelDelegate = pDelegate; m_pAccelDelegate = pDelegate;

View File

@ -37,14 +37,10 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void update(long sensorTimeStamp); void update(long sensorTimeStamp);
private: private:
static CCAccelerometer* m_spCCAccelerometer;
CCAccelerometerDelegate* m_pAccelDelegate; CCAccelerometerDelegate* m_pAccelDelegate;
CCAcceleration m_accelerationValue; CCAcceleration m_accelerationValue;
static int m_initialOrientationAngle; static int m_initialOrientationAngle;

View File

@ -60,8 +60,9 @@ int CCApplication::run()
{ {
update_time = current_time; update_time = current_time;
CCAccelerometer::sharedAccelerometer()->update(current_time); CCDirector* pDirector = CCDirector::sharedDirector();
CCDirector::sharedDirector()->mainLoop(); pDirector->getAccelerometer()->update(current_time);
pDirector->mainLoop();
} }
else else
{ {

View File

@ -109,7 +109,7 @@ namespace
void myAccelerometerKeyHook( UINT message,WPARAM wParam,LPARAM lParam ) void myAccelerometerKeyHook( UINT message,WPARAM wParam,LPARAM lParam )
{ {
cocos2d::CCAccelerometer *pAccelerometer=cocos2d::CCAccelerometer::sharedAccelerometer(); cocos2d::CCAccelerometer *pAccelerometer = cocos2d::CCDirector::sharedDirector()->getAccelerometer();
bool sendUpdate=false; bool sendUpdate=false;
switch( message ) switch( message )
{ {
@ -148,9 +148,6 @@ namespace
namespace cocos2d namespace cocos2d
{ {
// static members
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() : CCAccelerometer::CCAccelerometer() :
m_pAccelDelegate(NULL) m_pAccelDelegate(NULL)
{ {
@ -158,21 +155,7 @@ CCAccelerometer::CCAccelerometer() :
CCAccelerometer::~CCAccelerometer() CCAccelerometer::~CCAccelerometer()
{ {
if( m_spCCAccelerometer )
{
delete m_spCCAccelerometer ;
m_spCCAccelerometer = NULL;
}
}
// static
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spCCAccelerometer == NULL)
{
m_spCCAccelerometer = new CCAccelerometer();
}
return m_spCCAccelerometer;
} }
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)

View File

@ -35,11 +35,9 @@ public:
CCAccelerometer(); CCAccelerometer();
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void update( double x,double y,double z,double timestamp ); void update( double x,double y,double z,double timestamp );
private: private:
static CCAccelerometer* m_spCCAccelerometer;
CCAccelerometerDelegate* m_pAccelDelegate; CCAccelerometerDelegate* m_pAccelDelegate;
CCAcceleration m_obAccelerationValue; CCAcceleration m_obAccelerationValue;
}; };

View File

@ -327,8 +327,9 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
case WM_KEYDOWN: case WM_KEYDOWN:
if (wParam == VK_F1 || wParam == VK_F2) if (wParam == VK_F1 || wParam == VK_F2)
{ {
CCDirector* pDirector = CCDirector::sharedDirector();
if (GetKeyState(VK_LSHIFT) < 0 || GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0) if (GetKeyState(VK_LSHIFT) < 0 || GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0)
CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked); pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
} }
if ( m_lpfnAccelerometerKeyHook!=NULL ) if ( m_lpfnAccelerometerKeyHook!=NULL )
{ {

View File

@ -53,20 +53,6 @@ void CCTouchDispatcher::setDispatchEvents(bool bDispatchEvents)
m_bDispatchEvents = bDispatchEvents; m_bDispatchEvents = bDispatchEvents;
} }
static CCTouchDispatcher *pSharedDispatcher = NULL;
CCTouchDispatcher* CCTouchDispatcher::sharedDispatcher(void)
{
// synchronized ??
if (pSharedDispatcher == NULL)
{
pSharedDispatcher = new CCTouchDispatcher();
pSharedDispatcher->init();
}
return pSharedDispatcher;
}
/* /*
+(id) allocWithZone:(CCZone *)zone +(id) allocWithZone:(CCZone *)zone
{ {

View File

@ -119,7 +119,8 @@ void MenuLayer::backCallback(CCObject* sender)
void MenuLayer::registerWithTouchDispatcher() void MenuLayer::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
} }
bool MenuLayer::ccTouchBegan(CCTouch* touch, CCEvent* event) bool MenuLayer::ccTouchBegan(CCTouch* touch, CCEvent* event)
@ -214,7 +215,8 @@ Box2DView::~Box2DView()
void Box2DView::registerWithTouchDispatcher() void Box2DView::registerWithTouchDispatcher()
{ {
// higher priority than dragging // higher priority than dragging
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, -10, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, -10, true);
} }
bool Box2DView::ccTouchBegan(CCTouch* touch, CCEvent* event) bool Box2DView::ccTouchBegan(CCTouch* touch, CCEvent* event)

View File

@ -224,8 +224,6 @@ void ChipmunkAccelTouchTestLayer::addNewSpriteAtPosition(CCPoint pos)
void ChipmunkAccelTouchTestLayer::onEnter() void ChipmunkAccelTouchTestLayer::onEnter()
{ {
CCLayer::onEnter(); CCLayer::onEnter();
// CCAccelerometer::sharedAccelerometer()->setUpdateInterval:(1.0 / 60)];
} }
void ChipmunkAccelTouchTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* event) void ChipmunkAccelTouchTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* event)

View File

@ -161,7 +161,8 @@ void LayerTest1::onEnter()
void LayerTest1::registerWithTouchDispatcher() void LayerTest1::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority + 1, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority + 1, true);
} }
void LayerTest1::updateSize(CCTouch*touch) void LayerTest1::updateSize(CCTouch*touch)

View File

@ -111,7 +111,8 @@ MenuLayer1::MenuLayer1()
void MenuLayer1::registerWithTouchDispatcher() void MenuLayer1::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority+1, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority+1, true);
} }
bool MenuLayer1::ccTouchBegan(CCTouch *touch, CCEvent * pEvent) bool MenuLayer1::ccTouchBegan(CCTouch *touch, CCEvent * pEvent)
@ -148,7 +149,8 @@ void MenuLayer1::menuCallbackConfig(CCObject* sender)
void MenuLayer1::allowTouches(ccTime dt) void MenuLayer1::allowTouches(ccTime dt)
{ {
CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority+1, this); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->setPriority(kCCMenuTouchPriority+1, this);
unscheduleAllSelectors(); unscheduleAllSelectors();
CCLog("TOUCHES ALLOWED AGAIN"); CCLog("TOUCHES ALLOWED AGAIN");
} }
@ -156,7 +158,8 @@ void MenuLayer1::allowTouches(ccTime dt)
void MenuLayer1::menuCallbackDisabled(CCObject* sender) void MenuLayer1::menuCallbackDisabled(CCObject* sender)
{ {
// hijack all touch events for 5 seconds // hijack all touch events for 5 seconds
CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority-1, this); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->setPriority(kCCMenuTouchPriority-1, this);
schedule(schedule_selector(MenuLayer1::allowTouches), 5.0f); schedule(schedule_selector(MenuLayer1::allowTouches), 5.0f);
CCLog("TOUCHES DISABLED FOR 5 SECONDS"); CCLog("TOUCHES DISABLED FOR 5 SECONDS");
} }

View File

@ -134,7 +134,8 @@ Parallax2::Parallax2()
void Parallax2::registerWithTouchDispatcher() void Parallax2::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
} }
bool Parallax2::ccTouchBegan(CCTouch* touch, CCEvent* event) bool Parallax2::ccTouchBegan(CCTouch* touch, CCEvent* event)

View File

@ -1125,7 +1125,8 @@ std::string ParticleDemo::title()
void ParticleDemo::registerWithTouchDispatcher() void ParticleDemo::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
} }
bool ParticleDemo::ccTouchBegan(CCTouch* touch, CCEvent* event) bool ParticleDemo::ccTouchBegan(CCTouch* touch, CCEvent* event)

View File

@ -99,7 +99,8 @@ std::string TouchesPerformTest1::title()
void TouchesPerformTest1::registerWithTouchDispatcher() void TouchesPerformTest1::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
} }
bool TouchesPerformTest1::ccTouchBegan(CCTouch* touch, CCEvent* event) bool TouchesPerformTest1::ccTouchBegan(CCTouch* touch, CCEvent* event)
@ -141,7 +142,8 @@ std::string TouchesPerformTest2::title()
void TouchesPerformTest2::registerWithTouchDispatcher() void TouchesPerformTest2::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addStandardDelegate(this, 0);
} }
void TouchesPerformTest2::ccTouchesBegan(CCSet* touches, CCEvent* event) void TouchesPerformTest2::ccTouchesBegan(CCSet* touches, CCEvent* event)

View File

@ -157,7 +157,8 @@ KeyboardNotificationLayer::KeyboardNotificationLayer()
void KeyboardNotificationLayer::registerWithTouchDispatcher() void KeyboardNotificationLayer::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
} }
void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info) void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)

View File

@ -1346,7 +1346,8 @@ void TileDemo::backCallback(CCObject* pSender)
void TileDemo::registerWithTouchDispatcher() void TileDemo::registerWithTouchDispatcher()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
} }
bool TileDemo::ccTouchBegan(CCTouch* touch, CCEvent* event) bool TileDemo::ccTouchBegan(CCTouch* touch, CCEvent* event)

View File

@ -35,13 +35,15 @@ bool Paddle::initWithTexture(CCTexture2D* aTexture)
void Paddle::onEnter() void Paddle::onEnter()
{ {
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCSprite::onEnter(); CCSprite::onEnter();
} }
void Paddle::onExit() void Paddle::onExit()
{ {
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit(); CCSprite::onExit();
} }