diff --git a/cocos/2d/CCDeprecated.h b/cocos/2d/CCDeprecated.h index b2710848bf..c894f7988c 100644 --- a/cocos/2d/CCDeprecated.h +++ b/cocos/2d/CCDeprecated.h @@ -545,7 +545,8 @@ CC_DEPRECATED_ATTRIBUTE typedef IMEDelegate CCIMEDelegate; CC_DEPRECATED_ATTRIBUTE typedef IMEKeyboardNotificationInfo CCIMEKeyboardNotificationInfo; CC_DEPRECATED_ATTRIBUTE typedef TextFieldDelegate CCTextFieldDelegate; 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 StandardTouchDelegate CCStandardTouchDelegate; //CC_DEPRECATED_ATTRIBUTE typedef TouchDelegate CCTouchDelegate; diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index b0ecc02c4a..097d89b433 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -37,7 +37,6 @@ THE SOFTWARE. #include "CCArray.h" #include "CCScheduler.h" #include "ccMacros.h" -#include "CCNotificationCenter.h" #include "CCTransition.h" #include "CCTextureCache.h" #include "CCSpriteFrameCache.h" @@ -768,7 +767,6 @@ void Director::purgeDirector() // cocos2d-x specific data structures UserDefault::destroyInstance(); - NotificationCenter::destroyInstance(); GL::invalidateStateCache(); diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index ee4f218337..1580d0af23 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -23,12 +23,13 @@ #include "CCDrawNode.h" #include "CCShaderCache.h" #include "CCGL.h" -#include "CCNotificationCenter.h" #include "CCEventType.h" #include "CCConfiguration.h" #include "CCCustomCommand.h" #include "CCDirector.h" #include "CCRenderer.h" +#include "CCEventListenerCustom.h" +#include "CCEventDispatcher.h" NS_CC_BEGIN @@ -124,10 +125,6 @@ DrawNode::~DrawNode() GL::bindVAO(0); _vao = 0; } - -#if CC_ENABLE_CACHE_TEXTURE_DATA - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); -#endif } DrawNode* DrawNode::create() @@ -196,10 +193,12 @@ bool DrawNode::init() #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(DrawNode::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - nullptr); + auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ + /** listen the event that coming to foreground on Android */ + this->init(); + }); + + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif return true; @@ -472,11 +471,4 @@ void DrawNode::setBlendFunc(const BlendFunc &blendFunc) _blendFunc = blendFunc; } -/** listen the event that coming to foreground on Android - */ -void DrawNode::listenBackToForeground(Object *obj) -{ - init(); -} - NS_CC_END diff --git a/cocos/2d/CCDrawNode.h b/cocos/2d/CCDrawNode.h index e91f41e8ca..a5dbcfe76b 100644 --- a/cocos/2d/CCDrawNode.h +++ b/cocos/2d/CCDrawNode.h @@ -78,12 +78,6 @@ public: * @lua NA */ void setBlendFunc(const BlendFunc &blendFunc); - - /** listen the event that coming to foreground on Android - * @js NA - * @lua NA - */ - void listenBackToForeground(Object *obj); void onDraw(); diff --git a/cocos/2d/CCEventType.h b/cocos/2d/CCEventType.h index c00c62e456..46cb1a1985 100644 --- a/cocos/2d/CCEventType.h +++ b/cocos/2d/CCEventType.h @@ -9,7 +9,7 @@ // The application will come to foreground. // This message is used for reloading resources before come to foreground on Android. // 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. // This message is used for doing something before coming to background, such as save RenderTexture. diff --git a/cocos/2d/CCNotificationCenter.cpp b/cocos/2d/CCNotificationCenter.cpp index 9e544f1ffd..081d699ea1 100644 --- a/cocos/2d/CCNotificationCenter.cpp +++ b/cocos/2d/CCNotificationCenter.cpp @@ -31,50 +31,50 @@ using namespace std; NS_CC_BEGIN -static NotificationCenter *s_sharedNotifCenter = nullptr; +static __NotificationCenter *s_sharedNotifCenter = nullptr; -NotificationCenter::NotificationCenter() +__NotificationCenter::__NotificationCenter() : _scriptHandler(0) { _observers = __Array::createWithCapacity(3); _observers->retain(); } -NotificationCenter::~NotificationCenter() +__NotificationCenter::~__NotificationCenter() { _observers->release(); } -NotificationCenter *NotificationCenter::getInstance() +__NotificationCenter *__NotificationCenter::getInstance() { if (!s_sharedNotifCenter) { - s_sharedNotifCenter = new NotificationCenter; + s_sharedNotifCenter = new __NotificationCenter; } return s_sharedNotifCenter; } -void NotificationCenter::destroyInstance() +void __NotificationCenter::destroyInstance() { CC_SAFE_RELEASE_NULL(s_sharedNotifCenter); } // XXX: deprecated -NotificationCenter *NotificationCenter::sharedNotificationCenter(void) +__NotificationCenter *__NotificationCenter::sharedNotificationCenter(void) { - return NotificationCenter::getInstance(); + return __NotificationCenter::getInstance(); } // XXX: deprecated -void NotificationCenter::purgeNotificationCenter(void) +void __NotificationCenter::purgeNotificationCenter(void) { - NotificationCenter::destroyInstance(); + __NotificationCenter::destroyInstance(); } // // 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; CCARRAY_FOREACH(_observers, obj) @@ -92,7 +92,7 @@ bool NotificationCenter::observerExisted(Object *target, const std::string& name // // observer functions // -void NotificationCenter::addObserver(Object *target, +void __NotificationCenter::addObserver(Object *target, SEL_CallFuncO selector, const std::string& name, Object *sender) @@ -108,7 +108,7 @@ void NotificationCenter::addObserver(Object *target, _observers->addObject(observer); } -void NotificationCenter::removeObserver(Object *target, const std::string& name) +void __NotificationCenter::removeObserver(Object *target, const std::string& name) { Object* obj = nullptr; 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; __Array *toRemove = __Array::create(); @@ -146,7 +146,7 @@ int NotificationCenter::removeAllObservers(Object *target) return static_cast(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)) @@ -161,7 +161,7 @@ void NotificationCenter::registerScriptObserver( Object *target, int handler,con _observers->addObject(observer); } -void NotificationCenter::unregisterScriptObserver(Object *target,const std::string& name) +void __NotificationCenter::unregisterScriptObserver(Object *target,const std::string& name) { Object* obj = nullptr; 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()); 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); } -int NotificationCenter::getObserverHandlerByName(const std::string& name) +int __NotificationCenter::getObserverHandlerByName(const std::string& name) { if (name.empty()) { diff --git a/cocos/2d/CCNotificationCenter.h b/cocos/2d/CCNotificationCenter.h index f2265b1518..8a1b8694ae 100644 --- a/cocos/2d/CCNotificationCenter.h +++ b/cocos/2d/CCNotificationCenter.h @@ -31,29 +31,29 @@ THE SOFTWARE. NS_CC_BEGIN class ScriptHandlerMgr; -class CC_DLL NotificationCenter : public Object +class CC_DLL __NotificationCenter : public Object { friend class ScriptHandlerMgr; public: - /** NotificationCenter constructor + /** __NotificationCenter constructor * @js ctor */ - NotificationCenter(); + __NotificationCenter(); - /** NotificationCenter destructor + /** __NotificationCenter destructor * @js NA * @lua NA */ - ~NotificationCenter(); + ~__NotificationCenter(); - /** Gets the single instance of NotificationCenter. */ - static NotificationCenter *getInstance(); + /** Gets the single instance of __NotificationCenter. */ + static __NotificationCenter *getInstance(); - /** Destroys the single instance of NotificationCenter. */ + /** Destroys the single instance of __NotificationCenter. */ static void destroyInstance(); /** @deprecated use getInstance() instead */ - CC_DEPRECATED_ATTRIBUTE static NotificationCenter *sharedNotificationCenter(void); + CC_DEPRECATED_ATTRIBUTE static __NotificationCenter *sharedNotificationCenter(void); /** @deprecated use destroyInstance() instead */ CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void); diff --git a/cocos/2d/CCParticleSystemQuad.cpp b/cocos/2d/CCParticleSystemQuad.cpp index 30a45644e0..bf589d85b9 100644 --- a/cocos/2d/CCParticleSystemQuad.cpp +++ b/cocos/2d/CCParticleSystemQuad.cpp @@ -35,7 +35,6 @@ THE SOFTWARE. #include "ccGLStateCache.h" #include "CCGLProgram.h" #include "TransformUtils.h" -#include "CCNotificationCenter.h" #include "CCEventType.h" #include "CCConfiguration.h" #include "CCRenderer.h" @@ -44,6 +43,8 @@ THE SOFTWARE. // extern #include "kazmath/GL/matrix.h" +#include "CCEventListenerCustom.h" +#include "CCEventDispatcher.h" NS_CC_BEGIN @@ -74,10 +75,8 @@ bool ParticleSystemQuad::initWithTotalParticles(int numberOfParticles) #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(ParticleSystemQuad::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - nullptr); + auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(ParticleSystemQuad::listenBackToForeground, this)); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif return true; @@ -106,10 +105,6 @@ ParticleSystemQuad::~ParticleSystemQuad() GL::bindVAO(0); } } - -#if CC_ENABLE_CACHE_TEXTURE_DATA - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); -#endif } // implementation ParticleSystemQuad @@ -576,7 +571,7 @@ void ParticleSystemQuad::setupVBO() CHECK_GL_ERROR_DEBUG(); } -void ParticleSystemQuad::listenBackToForeground(Object *obj) +void ParticleSystemQuad::listenBackToForeground(EventCustom* event) { if (Configuration::getInstance()->supportsShareableVAO()) { diff --git a/cocos/2d/CCParticleSystemQuad.h b/cocos/2d/CCParticleSystemQuad.h index e5f54d0837..93e7a1f948 100644 --- a/cocos/2d/CCParticleSystemQuad.h +++ b/cocos/2d/CCParticleSystemQuad.h @@ -33,6 +33,7 @@ THE SOFTWARE. NS_CC_BEGIN class SpriteFrame; +class EventCustom; /** * @addtogroup particle_nodes @@ -81,7 +82,7 @@ public: * @js NA * @lua NA */ - void listenBackToForeground(Object *obj); + void listenBackToForeground(EventCustom* event); // Overrides /** diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 146c39e050..9d3078dbfa 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -34,7 +34,6 @@ THE SOFTWARE. #include "CCTextureCache.h" #include "platform/CCFileUtils.h" #include "CCGL.h" -#include "CCNotificationCenter.h" #include "CCEventType.h" #include "CCGrid.h" @@ -44,6 +43,8 @@ THE SOFTWARE. // extern #include "kazmath/GL/matrix.h" +#include "CCEventListenerCustom.h" +#include "CCEventDispatcher.h" NS_CC_BEGIN @@ -66,15 +67,11 @@ RenderTexture::RenderTexture() #if CC_ENABLE_CACHE_TEXTURE_DATA // Listen this event to save render texture before come to background. // Then it can be restored after coming to foreground on Android. - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(RenderTexture::listenToBackground), - EVENT_COME_TO_BACKGROUND, - nullptr); - - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(RenderTexture::listenToForeground), - EVNET_COME_TO_FOREGROUND, // this is misspelt - nullptr); + auto toBackgroundListener = EventListenerCustom::create(EVENT_COME_TO_BACKGROUND, CC_CALLBACK_1(RenderTexture::listenToBackground, this)); + _eventDispatcher->addEventListenerWithSceneGraphPriority(toBackgroundListener, this); + + auto toForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(RenderTexture::listenToForeground, this)); + _eventDispatcher->addEventListenerWithSceneGraphPriority(toForegroundListener, this); #endif } @@ -89,14 +86,9 @@ RenderTexture::~RenderTexture() glDeleteRenderbuffers(1, &_depthRenderBufffer); } 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 CC_SAFE_DELETE(_UITextureImage); @@ -124,7 +116,7 @@ void RenderTexture::listenToBackground(cocos2d::Object *obj) #endif } -void RenderTexture::listenToForeground(cocos2d::Object *obj) +void RenderTexture::listenToForeground(EventCustom *event) { #if CC_ENABLE_CACHE_TEXTURE_DATA // -- regenerate frame buffer object and attach the texture diff --git a/cocos/2d/CCRenderTexture.h b/cocos/2d/CCRenderTexture.h index fdf4910d4e..58b38787a5 100644 --- a/cocos/2d/CCRenderTexture.h +++ b/cocos/2d/CCRenderTexture.h @@ -34,6 +34,8 @@ THE SOFTWARE. NS_CC_BEGIN +class EventCustom; + /** * @addtogroup textures * @{ @@ -111,12 +113,12 @@ public: /** Listen "come to background" message, and save render texture. 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 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. */ inline unsigned int getClearFlags() const { return _clearFlags; }; diff --git a/cocos/2d/CCScriptSupport.h b/cocos/2d/CCScriptSupport.h index 8fcfba4a11..c67e5350bc 100644 --- a/cocos/2d/CCScriptSupport.h +++ b/cocos/2d/CCScriptSupport.h @@ -41,7 +41,6 @@ NS_CC_BEGIN class Timer; class Layer; class MenuItem; -class NotificationCenter; class CallFunc; class Acceleration; diff --git a/cocos/2d/CCTextureAtlas.cpp b/cocos/2d/CCTextureAtlas.cpp index f4f6422bbe..bc686d0521 100644 --- a/cocos/2d/CCTextureAtlas.cpp +++ b/cocos/2d/CCTextureAtlas.cpp @@ -30,7 +30,6 @@ THE SOFTWARE. #include "ccMacros.h" #include "CCGLProgram.h" #include "ccGLStateCache.h" -#include "CCNotificationCenter.h" #include "CCEventType.h" #include "CCDirector.h" #include "CCGL.h" @@ -39,6 +38,8 @@ THE SOFTWARE. #include "CCTexture2D.h" #include "CCString.h" #include +#include "CCEventDispatcher.h" +#include "CCEventListenerCustom.h" //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); #if CC_ENABLE_CACHE_TEXTURE_DATA - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); + Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener); #endif } @@ -185,10 +186,8 @@ bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity) #if CC_ENABLE_CACHE_TEXTURE_DATA // listen the event when app go to background - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(TextureAtlas::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - nullptr); + _backToForegroundlistener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(TextureAtlas::listenBackToForeground, this)); + Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundlistener, -1); #endif this->setupIndices(); @@ -207,7 +206,7 @@ bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity) return true; } -void TextureAtlas::listenBackToForeground(Object *obj) +void TextureAtlas::listenBackToForeground(EventCustom* event) { if (Configuration::getInstance()->supportsShareableVAO()) { diff --git a/cocos/2d/CCTextureAtlas.h b/cocos/2d/CCTextureAtlas.h index 2862b13eb0..a20a41542a 100644 --- a/cocos/2d/CCTextureAtlas.h +++ b/cocos/2d/CCTextureAtlas.h @@ -35,6 +35,8 @@ THE SOFTWARE. NS_CC_BEGIN class Texture2D; +class EventCustom; +class EventListenerCustom; /** * @addtogroup textures @@ -184,7 +186,7 @@ public: void drawQuads(); /** 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*/ inline bool isDirty(void) { return _dirty; } @@ -235,6 +237,8 @@ protected: Texture2D* _texture; /** Quads that are going to be rendered */ V3F_C4B_T2F_Quad* _quads; + + EventListenerCustom* _backToForegroundlistener; }; // end of textures group diff --git a/cocos/2d/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp b/cocos/2d/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp index 5ed0fc7279..0e8f28a580 100644 --- a/cocos/2d/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp +++ b/cocos/2d/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp @@ -3,8 +3,6 @@ #include "CCDirector.h" #include "../CCApplication.h" #include "platform/CCFileUtils.h" -#include "CCEventType.h" -#include "CCNotificationCenter.h" #include using namespace cocos2d; diff --git a/cocos/2d/platform/android/nativeactivity.cpp b/cocos/2d/platform/android/nativeactivity.cpp index 7a0416188a..1ec481c9d2 100644 --- a/cocos/2d/platform/android/nativeactivity.cpp +++ b/cocos/2d/platform/android/nativeactivity.cpp @@ -17,7 +17,6 @@ #include "CCDirector.h" #include "CCApplication.h" #include "CCEventType.h" -#include "CCNotificationCenter.h" #include "CCFileUtilsAndroid.h" #include "jni/JniHelper.h" @@ -28,6 +27,7 @@ #include "CCEventDispatcher.h" #include "CCEventAcceleration.h" #include "CCEventKeyboard.h" +#include "CCEventCustom.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::DrawPrimitives::init(); 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(); } } @@ -557,12 +558,14 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { break; case APP_CMD_LOST_FOCUS: - cocos2d::Application::getInstance()->applicationDidEnterBackground(); - cocos2d::NotificationCenter::getInstance()->postNotification(EVENT_COME_TO_BACKGROUND, NULL); - - // Also stop animating. - engine->animating = 0; - engine_draw_frame(engine); + { + cocos2d::Application::getInstance()->applicationDidEnterBackground(); + cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND); + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&backgroundEvent); + // Also stop animating. + engine->animating = 0; + engine_draw_frame(engine); + } break; } } diff --git a/cocos/2d/renderer/CCRenderer.cpp b/cocos/2d/renderer/CCRenderer.cpp index 8dbe93557e..9d621cad05 100644 --- a/cocos/2d/renderer/CCRenderer.cpp +++ b/cocos/2d/renderer/CCRenderer.cpp @@ -29,7 +29,9 @@ #include "renderer/CCQuadCommand.h" #include "CCGroupCommand.h" #include "CCConfiguration.h" -#include "CCNotificationCenter.h" +#include "CCDirector.h" +#include "CCEventDispatcher.h" +#include "CCEventListenerCustom.h" #include "CCEventType.h" #include // for std::stable_sort @@ -45,6 +47,7 @@ Renderer::Renderer() ,_lastCommand(0) ,_numQuads(0) ,_glViewAssigned(false) +,_cacheTextureListener(nullptr) { _commandGroupStack.push(DEFAULT_RENDER_QUEUE); @@ -66,18 +69,19 @@ Renderer::~Renderer() GL::bindVAO(0); } #if CC_ENABLE_CACHE_TEXTURE_DATA - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); + Director::getInstance()->getEventDispatcher()->removeEventListener(_cacheTextureListener); #endif } void Renderer::initGLView() { #if CC_ENABLE_CACHE_TEXTURE_DATA - // listen the event when app go to background - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(Renderer::onBackToForeground), - EVNET_COME_TO_FOREGROUND, - nullptr); + _cacheTextureListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ + /** listen the event that coming to foreground on Android */ + this->setupBuffer(); + }); + + Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_cacheTextureListener, -1); #endif setupIndices(); @@ -87,12 +91,6 @@ void Renderer::initGLView() _glViewAssigned = true; } -void Renderer::onBackToForeground(Object* obj) -{ - CC_UNUSED_PARAM(obj); - setupBuffer(); -} - void Renderer::setupIndices() { for( int i=0; i < VBO_SIZE; i++) diff --git a/cocos/2d/renderer/CCRenderer.h b/cocos/2d/renderer/CCRenderer.h index 7cfa3ac707..f6b317d917 100644 --- a/cocos/2d/renderer/CCRenderer.h +++ b/cocos/2d/renderer/CCRenderer.h @@ -35,6 +35,8 @@ NS_CC_BEGIN +class EventListenerCustom; + typedef std::vector RenderQueue; struct RenderStackElement @@ -43,7 +45,7 @@ struct RenderStackElement size_t currentIndex; }; -class Renderer : public Object +class Renderer { public: static const int VBO_SIZE = 65536 / 6; @@ -76,8 +78,6 @@ protected: //Draw the previews queued quads and flush previous context void flush(); - void onBackToForeground(Object* obj); - std::stack _commandGroupStack; std::stack _renderStack; @@ -96,6 +96,10 @@ protected: int _numQuads; bool _glViewAssigned; + +#if CC_ENABLE_CACHE_TEXTURE_DATA + EventListenerCustom* _cacheTextureListener; +#endif }; NS_CC_END diff --git a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp index 4f69133030..ba6c1f01c1 100644 --- a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp @@ -122,7 +122,6 @@ ShaderNode::ShaderNode() ShaderNode::~ShaderNode() { - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); } 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) { - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(ShaderNode::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - NULL); +#if CC_ENABLE_CACHE_TEXTURE_DATA + auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ + this->setShaderProgram(NULL); + loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str()); + }); + + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); +#endif loadShaderVertex(vert, frag); @@ -157,12 +160,6 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag) 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) { auto shader = new GLProgram(); @@ -437,7 +434,6 @@ public: bool initWithTexture(Texture2D* texture, const Rect& rect); void draw(); void initProgram(); - void listenBackToForeground(Object *obj); static SpriteBlur* create(const char *pszFileName); @@ -454,7 +450,6 @@ private: SpriteBlur::~SpriteBlur() { - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); } SpriteBlur* SpriteBlur::create(const char *pszFileName) @@ -472,20 +467,18 @@ SpriteBlur* SpriteBlur::create(const char *pszFileName) return pRet; } -void SpriteBlur::listenBackToForeground(Object *obj) -{ - setShaderProgram(NULL); - initProgram(); -} - bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect) { if( Sprite::initWithTexture(texture, rect) ) { - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(SpriteBlur::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - NULL); +#if CC_ENABLE_CACHE_TEXTURE_DATA + auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ + setShaderProgram(NULL); + initProgram(); + }); + + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); +#endif auto s = getTexture()->getContentSizeInPixels(); diff --git a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.h b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.h index 9459bd584a..741b0bb66f 100644 --- a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.h +++ b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.h @@ -118,7 +118,6 @@ public: bool initWithVertex(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 setPosition(const Point &newPosition); diff --git a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest2.cpp b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest2.cpp index fd35fbfbcc..569bef3915 100644 --- a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest2.cpp +++ b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest2.cpp @@ -133,21 +133,17 @@ ShaderSprite::ShaderSprite() ShaderSprite::~ShaderSprite() { - NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); -} - -void ShaderSprite::listenBackToForeground(Object *obj) -{ - setShaderProgram(NULL); - initShader(); } void ShaderSprite::setBackgroundNotification() { - NotificationCenter::getInstance()->addObserver(this, - callfuncO_selector(ShaderSprite::listenBackToForeground), - EVNET_COME_TO_FOREGROUND, - NULL); +#if CC_ENABLE_CACHE_TEXTURE_DATA + auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ + this->setShaderProgram(nullptr); + this->initShader(); + }); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); +#endif } void ShaderSprite::initShader() diff --git a/tools/tojs/cocos2dx.ini b/tools/tojs/cocos2dx.ini index e5dc054b3a..da6a3a0ae1 100644 --- a/tools/tojs/cocos2dx.ini +++ b/tools/tojs/cocos2dx.ini @@ -108,7 +108,8 @@ skip = Node::[^setPosition$ setGrid setGLServerState description getUserObject . Application::[^application.* ^run$], Camera::[getEyeXYZ getCenterXYZ getUpXYZ], ccFontDefinition::[*], - NewTextureAtlas::[*] + NewTextureAtlas::[*], + RenderTexture::[listenToBackground listenToForeground] rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName], diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index 65487bdcce..fbd9fbd161 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -112,7 +112,8 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS EGLViewProtocol::[setTouchDelegate], EGLView::[end swapBuffers], NewTextureAtlas::[*], - DisplayLinkDirector::[mainLoop setAnimationInterval startAnimation stopAnimation] + DisplayLinkDirector::[mainLoop setAnimationInterval startAnimation stopAnimation], + RenderTexture::[listenToBackground listenToForeground] rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], ProgressTimer::[setReverseProgress=setReverseDirection],