From 910099fbe5a6e63f3ffe4004d2f70d2cfdff35e5 Mon Sep 17 00:00:00 2001 From: vision Date: Fri, 23 May 2014 17:08:22 +0800 Subject: [PATCH] add new interfaces for TextureCache to unbind the bound image asynchronous load callbacks. --- cocos/renderer/CCTextureCache.cpp | 22 +++++++++++++++++++ cocos/renderer/CCTextureCache.h | 12 ++++++++++ .../Classes/Texture2dTest/Texture2dTest.cpp | 1 + 3 files changed, 35 insertions(+) diff --git a/cocos/renderer/CCTextureCache.cpp b/cocos/renderer/CCTextureCache.cpp index 99f7dc4624..a851137d16 100644 --- a/cocos/renderer/CCTextureCache.cpp +++ b/cocos/renderer/CCTextureCache.cpp @@ -141,6 +141,28 @@ void TextureCache::addImageAsync(const std::string &path, const std::functionfullPathForFilename(filename); + auto found = std::find_if(_imageInfoQueue->begin(), _imageInfoQueue->end(), [&](ImageInfo* ptr)->bool{ return ptr->asyncStruct->filename == fullpath; }); + if (found != _imageInfoQueue->end()) + { + _imageInfoMutex.lock(); + (*found)->asyncStruct->callback = nullptr; + _imageInfoMutex.unlock(); + } +} + +void TextureCache::unbindAllImageAsync() +{ + if (_imageInfoQueue && !_imageInfoQueue->empty()) + { + _imageInfoMutex.lock(); + std::for_each(_imageInfoQueue->begin(), _imageInfoQueue->end(), [](ImageInfo* ptr) { ptr->asyncStruct->callback = nullptr; }); + _imageInfoMutex.unlock(); + } +} + void TextureCache::loadImage() { AsyncStruct *asyncStruct = nullptr; diff --git a/cocos/renderer/CCTextureCache.h b/cocos/renderer/CCTextureCache.h index 993ceb6b0b..b05eb78b86 100644 --- a/cocos/renderer/CCTextureCache.h +++ b/cocos/renderer/CCTextureCache.h @@ -118,6 +118,18 @@ public: * @since v0.8 */ virtual void addImageAsync(const std::string &filepath, const std::function& callback); + + /* Unbind a specified bound image asynchronous callback + * In the case an object who was bound to an image asynchronous callback was destroyed before the callback is invoked, + * the object always need to unbind this callback manually. + * @since v3.1 + */ + virtual void unbindImageAsync(const std::string &filename); + + /* Unbind all bound image asynchronous load callbacks + * @since v3.1 + */ + virtual void unbindAllImageAsync(); /** Returns a Texture2D object given an Image. * If the image was not previously loaded, it will create a new Texture2D object and it will return it. diff --git a/tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp b/tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp index a3c4c749b9..02e3509ca1 100644 --- a/tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp +++ b/tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp @@ -1533,6 +1533,7 @@ void TextureAsync::onEnter() TextureAsync::~TextureAsync() { + Director::getInstance()->getTextureCache()->unbindAllImageAsync(); Director::getInstance()->getTextureCache()->removeAllTextures(); }