closed #2865: Deprecates CCNotificationCenter, uses EventDispatcher instead.

This commit is contained in:
James Chen 2013-12-31 10:54:37 +08:00
parent 1d8ee016e5
commit cf006df9ff
23 changed files with 126 additions and 156 deletions

View File

@ -545,7 +545,8 @@ CC_DEPRECATED_ATTRIBUTE typedef IMEDelegate CCIMEDelegate;
CC_DEPRECATED_ATTRIBUTE typedef IMEKeyboardNotificationInfo CCIMEKeyboardNotificationInfo; CC_DEPRECATED_ATTRIBUTE typedef IMEKeyboardNotificationInfo CCIMEKeyboardNotificationInfo;
CC_DEPRECATED_ATTRIBUTE typedef TextFieldDelegate CCTextFieldDelegate; CC_DEPRECATED_ATTRIBUTE typedef TextFieldDelegate CCTextFieldDelegate;
CC_DEPRECATED_ATTRIBUTE typedef TextFieldTTF CCTextFieldTTF; CC_DEPRECATED_ATTRIBUTE typedef TextFieldTTF CCTextFieldTTF;
CC_DEPRECATED_ATTRIBUTE typedef NotificationCenter CCNotificationCenter; CC_DEPRECATED_ATTRIBUTE typedef __NotificationCenter CCNotificationCenter;
CC_DEPRECATED_ATTRIBUTE typedef __NotificationCenter NotificationCenter;
//CC_DEPRECATED_ATTRIBUTE typedef TargetedTouchDelegate CCTargetedTouchDelegate; //CC_DEPRECATED_ATTRIBUTE typedef TargetedTouchDelegate CCTargetedTouchDelegate;
//CC_DEPRECATED_ATTRIBUTE typedef StandardTouchDelegate CCStandardTouchDelegate; //CC_DEPRECATED_ATTRIBUTE typedef StandardTouchDelegate CCStandardTouchDelegate;
//CC_DEPRECATED_ATTRIBUTE typedef TouchDelegate CCTouchDelegate; //CC_DEPRECATED_ATTRIBUTE typedef TouchDelegate CCTouchDelegate;

View File

@ -37,7 +37,6 @@ THE SOFTWARE.
#include "CCArray.h" #include "CCArray.h"
#include "CCScheduler.h" #include "CCScheduler.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "CCNotificationCenter.h"
#include "CCTransition.h" #include "CCTransition.h"
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "CCSpriteFrameCache.h" #include "CCSpriteFrameCache.h"
@ -768,7 +767,6 @@ void Director::purgeDirector()
// cocos2d-x specific data structures // cocos2d-x specific data structures
UserDefault::destroyInstance(); UserDefault::destroyInstance();
NotificationCenter::destroyInstance();
GL::invalidateStateCache(); GL::invalidateStateCache();

View File

@ -23,12 +23,13 @@
#include "CCDrawNode.h" #include "CCDrawNode.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGL.h" #include "CCGL.h"
#include "CCNotificationCenter.h"
#include "CCEventType.h" #include "CCEventType.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "CCCustomCommand.h" #include "CCCustomCommand.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "CCRenderer.h" #include "CCRenderer.h"
#include "CCEventListenerCustom.h"
#include "CCEventDispatcher.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -124,10 +125,6 @@ DrawNode::~DrawNode()
GL::bindVAO(0); GL::bindVAO(0);
_vao = 0; _vao = 0;
} }
#if CC_ENABLE_CACHE_TEXTURE_DATA
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
#endif
} }
DrawNode* DrawNode::create() DrawNode* DrawNode::create()
@ -196,10 +193,12 @@ bool DrawNode::init()
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// Need to listen the event only when not use batchnode, because it will use VBO // Need to listen the event only when not use batchnode, because it will use VBO
NotificationCenter::getInstance()->addObserver(this, auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
callfuncO_selector(DrawNode::listenBackToForeground), /** listen the event that coming to foreground on Android */
EVNET_COME_TO_FOREGROUND, this->init();
nullptr); });
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif #endif
return true; return true;
@ -472,11 +471,4 @@ void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
_blendFunc = blendFunc; _blendFunc = blendFunc;
} }
/** listen the event that coming to foreground on Android
*/
void DrawNode::listenBackToForeground(Object *obj)
{
init();
}
NS_CC_END NS_CC_END

View File

@ -78,12 +78,6 @@ public:
* @lua NA * @lua NA
*/ */
void setBlendFunc(const BlendFunc &blendFunc); void setBlendFunc(const BlendFunc &blendFunc);
/** listen the event that coming to foreground on Android
* @js NA
* @lua NA
*/
void listenBackToForeground(Object *obj);
void onDraw(); void onDraw();

View File

@ -9,7 +9,7 @@
// The application will come to foreground. // The application will come to foreground.
// This message is used for reloading resources before come to foreground on Android. // This message is used for reloading resources before come to foreground on Android.
// This message is posted in main.cpp. // This message is posted in main.cpp.
#define EVNET_COME_TO_FOREGROUND "event_come_to_foreground" #define EVENT_COME_TO_FOREGROUND "event_come_to_foreground"
// The application will come to background. // The application will come to background.
// This message is used for doing something before coming to background, such as save RenderTexture. // This message is used for doing something before coming to background, such as save RenderTexture.

View File

@ -31,50 +31,50 @@ using namespace std;
NS_CC_BEGIN NS_CC_BEGIN
static NotificationCenter *s_sharedNotifCenter = nullptr; static __NotificationCenter *s_sharedNotifCenter = nullptr;
NotificationCenter::NotificationCenter() __NotificationCenter::__NotificationCenter()
: _scriptHandler(0) : _scriptHandler(0)
{ {
_observers = __Array::createWithCapacity(3); _observers = __Array::createWithCapacity(3);
_observers->retain(); _observers->retain();
} }
NotificationCenter::~NotificationCenter() __NotificationCenter::~__NotificationCenter()
{ {
_observers->release(); _observers->release();
} }
NotificationCenter *NotificationCenter::getInstance() __NotificationCenter *__NotificationCenter::getInstance()
{ {
if (!s_sharedNotifCenter) if (!s_sharedNotifCenter)
{ {
s_sharedNotifCenter = new NotificationCenter; s_sharedNotifCenter = new __NotificationCenter;
} }
return s_sharedNotifCenter; return s_sharedNotifCenter;
} }
void NotificationCenter::destroyInstance() void __NotificationCenter::destroyInstance()
{ {
CC_SAFE_RELEASE_NULL(s_sharedNotifCenter); CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
} }
// XXX: deprecated // XXX: deprecated
NotificationCenter *NotificationCenter::sharedNotificationCenter(void) __NotificationCenter *__NotificationCenter::sharedNotificationCenter(void)
{ {
return NotificationCenter::getInstance(); return __NotificationCenter::getInstance();
} }
// XXX: deprecated // XXX: deprecated
void NotificationCenter::purgeNotificationCenter(void) void __NotificationCenter::purgeNotificationCenter(void)
{ {
NotificationCenter::destroyInstance(); __NotificationCenter::destroyInstance();
} }
// //
// internal functions // internal functions
// //
bool NotificationCenter::observerExisted(Object *target, const std::string& name, Object *sender) bool __NotificationCenter::observerExisted(Object *target, const std::string& name, Object *sender)
{ {
Object* obj = nullptr; Object* obj = nullptr;
CCARRAY_FOREACH(_observers, obj) CCARRAY_FOREACH(_observers, obj)
@ -92,7 +92,7 @@ bool NotificationCenter::observerExisted(Object *target, const std::string& name
// //
// observer functions // observer functions
// //
void NotificationCenter::addObserver(Object *target, void __NotificationCenter::addObserver(Object *target,
SEL_CallFuncO selector, SEL_CallFuncO selector,
const std::string& name, const std::string& name,
Object *sender) Object *sender)
@ -108,7 +108,7 @@ void NotificationCenter::addObserver(Object *target,
_observers->addObject(observer); _observers->addObject(observer);
} }
void NotificationCenter::removeObserver(Object *target, const std::string& name) void __NotificationCenter::removeObserver(Object *target, const std::string& name)
{ {
Object* obj = nullptr; Object* obj = nullptr;
CCARRAY_FOREACH(_observers, obj) CCARRAY_FOREACH(_observers, obj)
@ -125,7 +125,7 @@ void NotificationCenter::removeObserver(Object *target, const std::string& name)
} }
} }
int NotificationCenter::removeAllObservers(Object *target) int __NotificationCenter::removeAllObservers(Object *target)
{ {
Object *obj = nullptr; Object *obj = nullptr;
__Array *toRemove = __Array::create(); __Array *toRemove = __Array::create();
@ -146,7 +146,7 @@ int NotificationCenter::removeAllObservers(Object *target)
return static_cast<int>(toRemove->count()); return static_cast<int>(toRemove->count());
} }
void NotificationCenter::registerScriptObserver( Object *target, int handler,const std::string& name) void __NotificationCenter::registerScriptObserver( Object *target, int handler,const std::string& name)
{ {
if (this->observerExisted(target, name, nullptr)) if (this->observerExisted(target, name, nullptr))
@ -161,7 +161,7 @@ void NotificationCenter::registerScriptObserver( Object *target, int handler,con
_observers->addObject(observer); _observers->addObject(observer);
} }
void NotificationCenter::unregisterScriptObserver(Object *target,const std::string& name) void __NotificationCenter::unregisterScriptObserver(Object *target,const std::string& name)
{ {
Object* obj = nullptr; Object* obj = nullptr;
CCARRAY_FOREACH(_observers, obj) CCARRAY_FOREACH(_observers, obj)
@ -177,7 +177,7 @@ void NotificationCenter::unregisterScriptObserver(Object *target,const std::stri
} }
} }
void NotificationCenter::postNotification(const std::string& name, Object *sender) void __NotificationCenter::postNotification(const std::string& name, Object *sender)
{ {
__Array* ObserversCopy = __Array::createWithCapacity(_observers->count()); __Array* ObserversCopy = __Array::createWithCapacity(_observers->count());
ObserversCopy->addObjectsFromArray(_observers); ObserversCopy->addObjectsFromArray(_observers);
@ -198,12 +198,12 @@ void NotificationCenter::postNotification(const std::string& name, Object *sende
} }
} }
void NotificationCenter::postNotification(const std::string& name) void __NotificationCenter::postNotification(const std::string& name)
{ {
this->postNotification(name,nullptr); this->postNotification(name,nullptr);
} }
int NotificationCenter::getObserverHandlerByName(const std::string& name) int __NotificationCenter::getObserverHandlerByName(const std::string& name)
{ {
if (name.empty()) if (name.empty())
{ {

View File

@ -31,29 +31,29 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
class ScriptHandlerMgr; class ScriptHandlerMgr;
class CC_DLL NotificationCenter : public Object class CC_DLL __NotificationCenter : public Object
{ {
friend class ScriptHandlerMgr; friend class ScriptHandlerMgr;
public: public:
/** NotificationCenter constructor /** __NotificationCenter constructor
* @js ctor * @js ctor
*/ */
NotificationCenter(); __NotificationCenter();
/** NotificationCenter destructor /** __NotificationCenter destructor
* @js NA * @js NA
* @lua NA * @lua NA
*/ */
~NotificationCenter(); ~__NotificationCenter();
/** Gets the single instance of NotificationCenter. */ /** Gets the single instance of __NotificationCenter. */
static NotificationCenter *getInstance(); static __NotificationCenter *getInstance();
/** Destroys the single instance of NotificationCenter. */ /** Destroys the single instance of __NotificationCenter. */
static void destroyInstance(); static void destroyInstance();
/** @deprecated use getInstance() instead */ /** @deprecated use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static NotificationCenter *sharedNotificationCenter(void); CC_DEPRECATED_ATTRIBUTE static __NotificationCenter *sharedNotificationCenter(void);
/** @deprecated use destroyInstance() instead */ /** @deprecated use destroyInstance() instead */
CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void); CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void);

View File

@ -35,7 +35,6 @@ THE SOFTWARE.
#include "ccGLStateCache.h" #include "ccGLStateCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "TransformUtils.h" #include "TransformUtils.h"
#include "CCNotificationCenter.h"
#include "CCEventType.h" #include "CCEventType.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "CCRenderer.h" #include "CCRenderer.h"
@ -44,6 +43,8 @@ THE SOFTWARE.
// extern // extern
#include "kazmath/GL/matrix.h" #include "kazmath/GL/matrix.h"
#include "CCEventListenerCustom.h"
#include "CCEventDispatcher.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -74,10 +75,8 @@ bool ParticleSystemQuad::initWithTotalParticles(int numberOfParticles)
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// Need to listen the event only when not use batchnode, because it will use VBO // Need to listen the event only when not use batchnode, because it will use VBO
NotificationCenter::getInstance()->addObserver(this, auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(ParticleSystemQuad::listenBackToForeground, this));
callfuncO_selector(ParticleSystemQuad::listenBackToForeground), _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
EVNET_COME_TO_FOREGROUND,
nullptr);
#endif #endif
return true; return true;
@ -106,10 +105,6 @@ ParticleSystemQuad::~ParticleSystemQuad()
GL::bindVAO(0); GL::bindVAO(0);
} }
} }
#if CC_ENABLE_CACHE_TEXTURE_DATA
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
#endif
} }
// implementation ParticleSystemQuad // implementation ParticleSystemQuad
@ -576,7 +571,7 @@ void ParticleSystemQuad::setupVBO()
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
} }
void ParticleSystemQuad::listenBackToForeground(Object *obj) void ParticleSystemQuad::listenBackToForeground(EventCustom* event)
{ {
if (Configuration::getInstance()->supportsShareableVAO()) if (Configuration::getInstance()->supportsShareableVAO())
{ {

View File

@ -33,6 +33,7 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
class SpriteFrame; class SpriteFrame;
class EventCustom;
/** /**
* @addtogroup particle_nodes * @addtogroup particle_nodes
@ -81,7 +82,7 @@ public:
* @js NA * @js NA
* @lua NA * @lua NA
*/ */
void listenBackToForeground(Object *obj); void listenBackToForeground(EventCustom* event);
// Overrides // Overrides
/** /**

View File

@ -34,7 +34,6 @@ THE SOFTWARE.
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "CCGL.h" #include "CCGL.h"
#include "CCNotificationCenter.h"
#include "CCEventType.h" #include "CCEventType.h"
#include "CCGrid.h" #include "CCGrid.h"
@ -44,6 +43,8 @@ THE SOFTWARE.
// extern // extern
#include "kazmath/GL/matrix.h" #include "kazmath/GL/matrix.h"
#include "CCEventListenerCustom.h"
#include "CCEventDispatcher.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -66,15 +67,11 @@ RenderTexture::RenderTexture()
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// Listen this event to save render texture before come to background. // Listen this event to save render texture before come to background.
// Then it can be restored after coming to foreground on Android. // Then it can be restored after coming to foreground on Android.
NotificationCenter::getInstance()->addObserver(this, auto toBackgroundListener = EventListenerCustom::create(EVENT_COME_TO_BACKGROUND, CC_CALLBACK_1(RenderTexture::listenToBackground, this));
callfuncO_selector(RenderTexture::listenToBackground), _eventDispatcher->addEventListenerWithSceneGraphPriority(toBackgroundListener, this);
EVENT_COME_TO_BACKGROUND,
nullptr); auto toForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(RenderTexture::listenToForeground, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(toForegroundListener, this);
NotificationCenter::getInstance()->addObserver(this,
callfuncO_selector(RenderTexture::listenToForeground),
EVNET_COME_TO_FOREGROUND, // this is misspelt
nullptr);
#endif #endif
} }
@ -89,14 +86,9 @@ RenderTexture::~RenderTexture()
glDeleteRenderbuffers(1, &_depthRenderBufffer); glDeleteRenderbuffers(1, &_depthRenderBufffer);
} }
CC_SAFE_DELETE(_UITextureImage); CC_SAFE_DELETE(_UITextureImage);
#if CC_ENABLE_CACHE_TEXTURE_DATA
NotificationCenter::getInstance()->removeObserver(this, EVENT_COME_TO_BACKGROUND);
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
#endif
} }
void RenderTexture::listenToBackground(cocos2d::Object *obj) void RenderTexture::listenToBackground(EventCustom *event)
{ {
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
CC_SAFE_DELETE(_UITextureImage); CC_SAFE_DELETE(_UITextureImage);
@ -124,7 +116,7 @@ void RenderTexture::listenToBackground(cocos2d::Object *obj)
#endif #endif
} }
void RenderTexture::listenToForeground(cocos2d::Object *obj) void RenderTexture::listenToForeground(EventCustom *event)
{ {
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// -- regenerate frame buffer object and attach the texture // -- regenerate frame buffer object and attach the texture

View File

@ -34,6 +34,8 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
class EventCustom;
/** /**
* @addtogroup textures * @addtogroup textures
* @{ * @{
@ -111,12 +113,12 @@ public:
/** Listen "come to background" message, and save render texture. /** Listen "come to background" message, and save render texture.
It only has effect on Android. It only has effect on Android.
*/ */
void listenToBackground(Object *obj); void listenToBackground(EventCustom *event);
/** Listen "come to foreground" message and restore the frame buffer object /** Listen "come to foreground" message and restore the frame buffer object
It only has effect on Android. It only has effect on Android.
*/ */
void listenToForeground(Object *obj); void listenToForeground(EventCustom *event);
/** Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw" is true. */ /** Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw" is true. */
inline unsigned int getClearFlags() const { return _clearFlags; }; inline unsigned int getClearFlags() const { return _clearFlags; };

View File

@ -41,7 +41,6 @@ NS_CC_BEGIN
class Timer; class Timer;
class Layer; class Layer;
class MenuItem; class MenuItem;
class NotificationCenter;
class CallFunc; class CallFunc;
class Acceleration; class Acceleration;

View File

@ -30,7 +30,6 @@ THE SOFTWARE.
#include "ccMacros.h" #include "ccMacros.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLStateCache.h" #include "ccGLStateCache.h"
#include "CCNotificationCenter.h"
#include "CCEventType.h" #include "CCEventType.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "CCGL.h" #include "CCGL.h"
@ -39,6 +38,8 @@ THE SOFTWARE.
#include "CCTexture2D.h" #include "CCTexture2D.h"
#include "CCString.h" #include "CCString.h"
#include <stdlib.h> #include <stdlib.h>
#include "CCEventDispatcher.h"
#include "CCEventListenerCustom.h"
//According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong //According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong
@ -70,7 +71,7 @@ TextureAtlas::~TextureAtlas()
CC_SAFE_RELEASE(_texture); CC_SAFE_RELEASE(_texture);
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener);
#endif #endif
} }
@ -185,10 +186,8 @@ bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity)
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// listen the event when app go to background // listen the event when app go to background
NotificationCenter::getInstance()->addObserver(this, _backToForegroundlistener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(TextureAtlas::listenBackToForeground, this));
callfuncO_selector(TextureAtlas::listenBackToForeground), Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundlistener, -1);
EVNET_COME_TO_FOREGROUND,
nullptr);
#endif #endif
this->setupIndices(); this->setupIndices();
@ -207,7 +206,7 @@ bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity)
return true; return true;
} }
void TextureAtlas::listenBackToForeground(Object *obj) void TextureAtlas::listenBackToForeground(EventCustom* event)
{ {
if (Configuration::getInstance()->supportsShareableVAO()) if (Configuration::getInstance()->supportsShareableVAO())
{ {

View File

@ -35,6 +35,8 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
class Texture2D; class Texture2D;
class EventCustom;
class EventListenerCustom;
/** /**
* @addtogroup textures * @addtogroup textures
@ -184,7 +186,7 @@ public:
void drawQuads(); void drawQuads();
/** listen the event that coming to foreground on Android /** listen the event that coming to foreground on Android
*/ */
void listenBackToForeground(Object *obj); void listenBackToForeground(EventCustom* event);
/** whether or not the array buffer of the VBO needs to be updated*/ /** whether or not the array buffer of the VBO needs to be updated*/
inline bool isDirty(void) { return _dirty; } inline bool isDirty(void) { return _dirty; }
@ -235,6 +237,8 @@ protected:
Texture2D* _texture; Texture2D* _texture;
/** Quads that are going to be rendered */ /** Quads that are going to be rendered */
V3F_C4B_T2F_Quad* _quads; V3F_C4B_T2F_Quad* _quads;
EventListenerCustom* _backToForegroundlistener;
}; };
// end of textures group // end of textures group

View File

@ -3,8 +3,6 @@
#include "CCDirector.h" #include "CCDirector.h"
#include "../CCApplication.h" #include "../CCApplication.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "CCEventType.h"
#include "CCNotificationCenter.h"
#include <jni.h> #include <jni.h>
using namespace cocos2d; using namespace cocos2d;

View File

@ -17,7 +17,6 @@
#include "CCDirector.h" #include "CCDirector.h"
#include "CCApplication.h" #include "CCApplication.h"
#include "CCEventType.h" #include "CCEventType.h"
#include "CCNotificationCenter.h"
#include "CCFileUtilsAndroid.h" #include "CCFileUtilsAndroid.h"
#include "jni/JniHelper.h" #include "jni/JniHelper.h"
@ -28,6 +27,7 @@
#include "CCEventDispatcher.h" #include "CCEventDispatcher.h"
#include "CCEventAcceleration.h" #include "CCEventAcceleration.h"
#include "CCEventKeyboard.h" #include "CCEventKeyboard.h"
#include "CCEventCustom.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" #include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
@ -140,7 +140,8 @@ static void cocos_init(cocos_dimensions d, struct android_app* app) {
cocos2d::ShaderCache::getInstance()->reloadDefaultShaders(); cocos2d::ShaderCache::getInstance()->reloadDefaultShaders();
cocos2d::DrawPrimitives::init(); cocos2d::DrawPrimitives::init();
cocos2d::VolatileTextureMgr::reloadAllTextures(); cocos2d::VolatileTextureMgr::reloadAllTextures();
cocos2d::NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
cocos2d::Director::getInstance()->setGLDefaultValues(); cocos2d::Director::getInstance()->setGLDefaultValues();
} }
} }
@ -557,12 +558,14 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
break; break;
case APP_CMD_LOST_FOCUS: case APP_CMD_LOST_FOCUS:
cocos2d::Application::getInstance()->applicationDidEnterBackground(); {
cocos2d::NotificationCenter::getInstance()->postNotification(EVENT_COME_TO_BACKGROUND, NULL); cocos2d::Application::getInstance()->applicationDidEnterBackground();
cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
// Also stop animating. cocos2d::EventDispatcher::getInstance()->dispatchEvent(&backgroundEvent);
engine->animating = 0; // Also stop animating.
engine_draw_frame(engine); engine->animating = 0;
engine_draw_frame(engine);
}
break; break;
} }
} }

View File

@ -29,7 +29,9 @@
#include "renderer/CCQuadCommand.h" #include "renderer/CCQuadCommand.h"
#include "CCGroupCommand.h" #include "CCGroupCommand.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "CCNotificationCenter.h" #include "CCDirector.h"
#include "CCEventDispatcher.h"
#include "CCEventListenerCustom.h"
#include "CCEventType.h" #include "CCEventType.h"
#include <algorithm> // for std::stable_sort #include <algorithm> // for std::stable_sort
@ -45,6 +47,7 @@ Renderer::Renderer()
,_lastCommand(0) ,_lastCommand(0)
,_numQuads(0) ,_numQuads(0)
,_glViewAssigned(false) ,_glViewAssigned(false)
,_cacheTextureListener(nullptr)
{ {
_commandGroupStack.push(DEFAULT_RENDER_QUEUE); _commandGroupStack.push(DEFAULT_RENDER_QUEUE);
@ -66,18 +69,19 @@ Renderer::~Renderer()
GL::bindVAO(0); GL::bindVAO(0);
} }
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); Director::getInstance()->getEventDispatcher()->removeEventListener(_cacheTextureListener);
#endif #endif
} }
void Renderer::initGLView() void Renderer::initGLView()
{ {
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// listen the event when app go to background _cacheTextureListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
NotificationCenter::getInstance()->addObserver(this, /** listen the event that coming to foreground on Android */
callfuncO_selector(Renderer::onBackToForeground), this->setupBuffer();
EVNET_COME_TO_FOREGROUND, });
nullptr);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_cacheTextureListener, -1);
#endif #endif
setupIndices(); setupIndices();
@ -87,12 +91,6 @@ void Renderer::initGLView()
_glViewAssigned = true; _glViewAssigned = true;
} }
void Renderer::onBackToForeground(Object* obj)
{
CC_UNUSED_PARAM(obj);
setupBuffer();
}
void Renderer::setupIndices() void Renderer::setupIndices()
{ {
for( int i=0; i < VBO_SIZE; i++) for( int i=0; i < VBO_SIZE; i++)

View File

@ -35,6 +35,8 @@
NS_CC_BEGIN NS_CC_BEGIN
class EventListenerCustom;
typedef std::vector<RenderCommand*> RenderQueue; typedef std::vector<RenderCommand*> RenderQueue;
struct RenderStackElement struct RenderStackElement
@ -43,7 +45,7 @@ struct RenderStackElement
size_t currentIndex; size_t currentIndex;
}; };
class Renderer : public Object class Renderer
{ {
public: public:
static const int VBO_SIZE = 65536 / 6; static const int VBO_SIZE = 65536 / 6;
@ -76,8 +78,6 @@ protected:
//Draw the previews queued quads and flush previous context //Draw the previews queued quads and flush previous context
void flush(); void flush();
void onBackToForeground(Object* obj);
std::stack<int> _commandGroupStack; std::stack<int> _commandGroupStack;
std::stack<RenderStackElement> _renderStack; std::stack<RenderStackElement> _renderStack;
@ -96,6 +96,10 @@ protected:
int _numQuads; int _numQuads;
bool _glViewAssigned; bool _glViewAssigned;
#if CC_ENABLE_CACHE_TEXTURE_DATA
EventListenerCustom* _cacheTextureListener;
#endif
}; };
NS_CC_END NS_CC_END

View File

@ -122,7 +122,6 @@ ShaderNode::ShaderNode()
ShaderNode::~ShaderNode() ShaderNode::~ShaderNode()
{ {
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
} }
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
@ -136,10 +135,14 @@ ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
bool ShaderNode::initWithVertex(const char *vert, const char *frag) bool ShaderNode::initWithVertex(const char *vert, const char *frag)
{ {
NotificationCenter::getInstance()->addObserver(this, #if CC_ENABLE_CACHE_TEXTURE_DATA
callfuncO_selector(ShaderNode::listenBackToForeground), auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
EVNET_COME_TO_FOREGROUND, this->setShaderProgram(NULL);
NULL); loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str());
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
loadShaderVertex(vert, frag); loadShaderVertex(vert, frag);
@ -157,12 +160,6 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
return true; return true;
} }
void ShaderNode::listenBackToForeground(Object *obj)
{
this->setShaderProgram(NULL);
loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str());
}
void ShaderNode::loadShaderVertex(const char *vert, const char *frag) void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{ {
auto shader = new GLProgram(); auto shader = new GLProgram();
@ -437,7 +434,6 @@ public:
bool initWithTexture(Texture2D* texture, const Rect& rect); bool initWithTexture(Texture2D* texture, const Rect& rect);
void draw(); void draw();
void initProgram(); void initProgram();
void listenBackToForeground(Object *obj);
static SpriteBlur* create(const char *pszFileName); static SpriteBlur* create(const char *pszFileName);
@ -454,7 +450,6 @@ private:
SpriteBlur::~SpriteBlur() SpriteBlur::~SpriteBlur()
{ {
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
} }
SpriteBlur* SpriteBlur::create(const char *pszFileName) SpriteBlur* SpriteBlur::create(const char *pszFileName)
@ -472,20 +467,18 @@ SpriteBlur* SpriteBlur::create(const char *pszFileName)
return pRet; return pRet;
} }
void SpriteBlur::listenBackToForeground(Object *obj)
{
setShaderProgram(NULL);
initProgram();
}
bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect) bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
{ {
if( Sprite::initWithTexture(texture, rect) ) if( Sprite::initWithTexture(texture, rect) )
{ {
NotificationCenter::getInstance()->addObserver(this, #if CC_ENABLE_CACHE_TEXTURE_DATA
callfuncO_selector(SpriteBlur::listenBackToForeground), auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
EVNET_COME_TO_FOREGROUND, setShaderProgram(NULL);
NULL); initProgram();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
auto s = getTexture()->getContentSizeInPixels(); auto s = getTexture()->getContentSizeInPixels();

View File

@ -118,7 +118,6 @@ public:
bool initWithVertex(const char *vert, const char *frag); bool initWithVertex(const char *vert, const char *frag);
void loadShaderVertex(const char *vert, const char *frag); void loadShaderVertex(const char *vert, const char *frag);
void listenBackToForeground(Object *obj);
virtual void update(float dt); virtual void update(float dt);
virtual void setPosition(const Point &newPosition); virtual void setPosition(const Point &newPosition);

View File

@ -133,21 +133,17 @@ ShaderSprite::ShaderSprite()
ShaderSprite::~ShaderSprite() ShaderSprite::~ShaderSprite()
{ {
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
}
void ShaderSprite::listenBackToForeground(Object *obj)
{
setShaderProgram(NULL);
initShader();
} }
void ShaderSprite::setBackgroundNotification() void ShaderSprite::setBackgroundNotification()
{ {
NotificationCenter::getInstance()->addObserver(this, #if CC_ENABLE_CACHE_TEXTURE_DATA
callfuncO_selector(ShaderSprite::listenBackToForeground), auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
EVNET_COME_TO_FOREGROUND, this->setShaderProgram(nullptr);
NULL); this->initShader();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
} }
void ShaderSprite::initShader() void ShaderSprite::initShader()

View File

@ -108,7 +108,8 @@ skip = Node::[^setPosition$ setGrid setGLServerState description getUserObject .
Application::[^application.* ^run$], Application::[^application.* ^run$],
Camera::[getEyeXYZ getCenterXYZ getUpXYZ], Camera::[getEyeXYZ getCenterXYZ getUpXYZ],
ccFontDefinition::[*], ccFontDefinition::[*],
NewTextureAtlas::[*] NewTextureAtlas::[*],
RenderTexture::[listenToBackground listenToForeground]
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName], MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName],

View File

@ -112,7 +112,8 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
EGLViewProtocol::[setTouchDelegate], EGLViewProtocol::[setTouchDelegate],
EGLView::[end swapBuffers], EGLView::[end swapBuffers],
NewTextureAtlas::[*], NewTextureAtlas::[*],
DisplayLinkDirector::[mainLoop setAnimationInterval startAnimation stopAnimation] DisplayLinkDirector::[mainLoop setAnimationInterval startAnimation stopAnimation],
RenderTexture::[listenToBackground listenToForeground]
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
ProgressTimer::[setReverseProgress=setReverseDirection], ProgressTimer::[setReverseProgress=setReverseDirection],