2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stack>
|
|
|
|
#include <cctype>
|
|
|
|
#include <list>
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
#include <emscripten/emscripten.h>
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/emscripten/CCTextureCacheEmscripten.h"
|
2013-06-13 05:46:32 +08:00
|
|
|
#endif // EMSCRIPTEN
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation TextureCache
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
TextureCache * TextureCache::getInstance()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-11-07 18:52:36 +08:00
|
|
|
return Director::getInstance()->getTextureCache();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCache::TextureCache()
|
2013-06-26 14:48:19 +08:00
|
|
|
: _loadingThread(nullptr)
|
|
|
|
, _asyncStructQueue(nullptr)
|
2013-06-21 15:29:21 +08:00
|
|
|
, _imageInfoQueue(nullptr)
|
|
|
|
, _needQuit(false)
|
|
|
|
, _asyncRefCount(0)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCache::~TextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
CCLOGINFO("deallocing TextureCache: %p", this);
|
2013-04-27 00:55:36 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it=_textures.begin(); it!=_textures.end(); ++it)
|
|
|
|
(it->second)->release();
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-26 14:48:19 +08:00
|
|
|
CC_SAFE_DELETE(_loadingThread);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
void TextureCache::destroyInstance()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-11 15:40:12 +08:00
|
|
|
TextureCache * TextureCache::sharedTextureCache()
|
|
|
|
{
|
2013-11-11 16:04:34 +08:00
|
|
|
return Director::getInstance()->getTextureCache();
|
2013-11-11 15:40:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::purgeSharedTextureCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:38:12 +08:00
|
|
|
std::string TextureCache::getDescription() const
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2014-01-05 11:59:32 +08:00
|
|
|
return StringUtils::format("<TextureCache | Number of textures = %d>", static_cast<int>(_textures.size()));
|
2012-04-27 18:47:49 +08:00
|
|
|
}
|
|
|
|
|
2014-04-14 10:01:17 +08:00
|
|
|
void TextureCache::addImageAsync(const std::string &path, const std::function<void(Texture2D*)>& callback)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D *texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if( it != _textures.end() )
|
|
|
|
texture = it->second;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-02 17:23:00 +08:00
|
|
|
if (texture != nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-02 17:23:00 +08:00
|
|
|
callback(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy init
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_asyncStructQueue == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-21 15:29:21 +08:00
|
|
|
_asyncStructQueue = new queue<AsyncStruct*>();
|
2013-12-16 17:12:53 +08:00
|
|
|
_imageInfoQueue = new deque<ImageInfo*>();
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
// create a new thread to load images
|
2013-06-26 14:48:19 +08:00
|
|
|
_loadingThread = new std::thread(&TextureCache::loadImage, this);
|
2013-06-21 15:29:21 +08:00
|
|
|
|
|
|
|
_needQuit = false;
|
2012-05-28 02:41:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
if (0 == _asyncRefCount)
|
2012-05-30 14:24:59 +08:00
|
|
|
{
|
2014-10-04 00:38:36 +08:00
|
|
|
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack), this, 0, false);
|
2012-05-30 14:24:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
++_asyncRefCount;
|
2012-05-30 14:24:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// generate async struct
|
2014-08-28 07:31:57 +08:00
|
|
|
AsyncStruct *data = new (std::nothrow) AsyncStruct(fullpath, callback);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// add async struct into queue
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueueMutex.lock();
|
2013-06-21 15:29:21 +08:00
|
|
|
_asyncStructQueue->push(data);
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueueMutex.unlock();
|
2011-11-25 16:14:06 +08:00
|
|
|
|
2013-06-21 15:43:17 +08:00
|
|
|
_sleepCondition.notify_one();
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 17:08:22 +08:00
|
|
|
void TextureCache::unbindImageAsync(const std::string& filename)
|
|
|
|
{
|
2014-05-23 17:59:34 +08:00
|
|
|
_imageInfoMutex.lock();
|
2014-05-23 17:27:59 +08:00
|
|
|
if (_imageInfoQueue && !_imageInfoQueue->empty())
|
2014-05-23 17:08:22 +08:00
|
|
|
{
|
2014-05-23 17:27:59 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filename);
|
|
|
|
auto found = std::find_if(_imageInfoQueue->begin(), _imageInfoQueue->end(), [&fullpath](ImageInfo* ptr)->bool{ return ptr->asyncStruct->filename == fullpath; });
|
|
|
|
if (found != _imageInfoQueue->end())
|
|
|
|
{
|
|
|
|
(*found)->asyncStruct->callback = nullptr;
|
|
|
|
}
|
2014-05-23 17:08:22 +08:00
|
|
|
}
|
2014-05-23 17:59:34 +08:00
|
|
|
_imageInfoMutex.unlock();
|
2014-05-23 17:08:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::unbindAllImageAsync()
|
|
|
|
{
|
2014-05-23 17:59:34 +08:00
|
|
|
_imageInfoMutex.lock();
|
2014-05-23 17:08:22 +08:00
|
|
|
if (_imageInfoQueue && !_imageInfoQueue->empty())
|
|
|
|
{
|
|
|
|
std::for_each(_imageInfoQueue->begin(), _imageInfoQueue->end(), [](ImageInfo* ptr) { ptr->asyncStruct->callback = nullptr; });
|
|
|
|
}
|
2014-05-23 17:59:34 +08:00
|
|
|
_imageInfoMutex.unlock();
|
2014-05-23 17:08:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
void TextureCache::loadImage()
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
AsyncStruct *asyncStruct = nullptr;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
std::queue<AsyncStruct*> *pQueue = _asyncStructQueue;
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueueMutex.lock();
|
2013-06-21 15:29:21 +08:00
|
|
|
if (pQueue->empty())
|
|
|
|
{
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueueMutex.unlock();
|
2013-06-21 15:29:21 +08:00
|
|
|
if (_needQuit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2013-06-21 15:43:17 +08:00
|
|
|
std::unique_lock<std::mutex> lk(_sleepMutex);
|
|
|
|
_sleepCondition.wait(lk);
|
2013-06-21 15:29:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
asyncStruct = pQueue->front();
|
2013-06-21 15:29:21 +08:00
|
|
|
pQueue->pop();
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueueMutex.unlock();
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
Image *image = nullptr;
|
|
|
|
bool generateImage = false;
|
|
|
|
|
|
|
|
auto it = _textures.find(asyncStruct->filename);
|
|
|
|
if( it == _textures.end() )
|
2013-06-21 15:29:21 +08:00
|
|
|
{
|
2013-12-16 17:12:53 +08:00
|
|
|
_imageInfoMutex.lock();
|
|
|
|
ImageInfo *imageInfo;
|
2013-12-17 16:04:48 +08:00
|
|
|
size_t pos = 0;
|
|
|
|
size_t infoSize = _imageInfoQueue->size();
|
2013-12-16 17:12:53 +08:00
|
|
|
for (; pos < infoSize; pos++)
|
|
|
|
{
|
|
|
|
imageInfo = (*_imageInfoQueue)[pos];
|
2014-06-12 11:21:42 +08:00
|
|
|
if(imageInfo->asyncStruct->filename.compare(asyncStruct->filename) == 0)
|
2013-12-16 17:12:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_imageInfoMutex.unlock();
|
2014-06-12 11:21:42 +08:00
|
|
|
if(infoSize == 0 || pos == infoSize)
|
2013-12-16 17:12:53 +08:00
|
|
|
generateImage = true;
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
if (generateImage)
|
|
|
|
{
|
2013-12-24 15:49:58 +08:00
|
|
|
const std::string& filename = asyncStruct->filename;
|
2013-12-16 17:12:53 +08:00
|
|
|
// generate image
|
2014-08-28 07:31:57 +08:00
|
|
|
image = new (std::nothrow) Image();
|
2013-12-16 17:12:53 +08:00
|
|
|
if (image && !image->initWithImageFileThreadSafe(filename))
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(image);
|
2013-12-24 15:49:58 +08:00
|
|
|
CCLOG("can not load %s", filename.c_str());
|
2013-12-16 17:12:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
// generate image info
|
2014-08-28 07:31:57 +08:00
|
|
|
ImageInfo *imageInfo = new (std::nothrow) ImageInfo();
|
2013-09-07 06:33:28 +08:00
|
|
|
imageInfo->asyncStruct = asyncStruct;
|
|
|
|
imageInfo->image = image;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
|
|
|
// put the image info into the queue
|
2013-06-21 15:49:45 +08:00
|
|
|
_imageInfoMutex.lock();
|
2013-12-16 17:12:53 +08:00
|
|
|
_imageInfoQueue->push_back(imageInfo);
|
2013-06-21 15:49:45 +08:00
|
|
|
_imageInfoMutex.unlock();
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(_asyncStructQueue != nullptr)
|
|
|
|
{
|
|
|
|
delete _asyncStructQueue;
|
2013-06-21 15:43:17 +08:00
|
|
|
_asyncStructQueue = nullptr;
|
2013-06-21 15:29:21 +08:00
|
|
|
delete _imageInfoQueue;
|
2013-06-21 15:43:17 +08:00
|
|
|
_imageInfoQueue = nullptr;
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::addImageAsyncCallBack(float dt)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// the image is generated in loading thread
|
2013-12-16 17:12:53 +08:00
|
|
|
std::deque<ImageInfo*> *imagesQueue = _imageInfoQueue;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-21 15:43:17 +08:00
|
|
|
_imageInfoMutex.lock();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (imagesQueue->empty())
|
|
|
|
{
|
2013-06-21 15:43:17 +08:00
|
|
|
_imageInfoMutex.unlock();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
ImageInfo *imageInfo = imagesQueue->front();
|
2013-12-16 17:12:53 +08:00
|
|
|
imagesQueue->pop_front();
|
2013-06-21 15:43:17 +08:00
|
|
|
_imageInfoMutex.unlock();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
AsyncStruct *asyncStruct = imageInfo->asyncStruct;
|
|
|
|
Image *image = imageInfo->image;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-24 15:49:58 +08:00
|
|
|
const std::string& filename = asyncStruct->filename;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
Texture2D *texture = nullptr;
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
// generate texture in render thread
|
2014-08-28 07:31:57 +08:00
|
|
|
texture = new (std::nothrow) Texture2D();
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
texture->initWithImage(image);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-12-16 17:12:53 +08:00
|
|
|
// cache the texture file name
|
|
|
|
VolatileTextureMgr::addImageTexture(texture, filename);
|
2011-12-01 17:04:56 +08:00
|
|
|
#endif
|
2013-12-16 17:12:53 +08:00
|
|
|
// cache the texture. retain it, since it is added in the map
|
|
|
|
_textures.insert( std::make_pair(filename, texture) );
|
|
|
|
texture->retain();
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto it = _textures.find(asyncStruct->filename);
|
|
|
|
if(it != _textures.end())
|
|
|
|
texture = it->second;
|
|
|
|
}
|
|
|
|
|
2014-05-22 12:42:52 +08:00
|
|
|
if (asyncStruct->callback)
|
|
|
|
{
|
|
|
|
asyncStruct->callback(texture);
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:12:53 +08:00
|
|
|
if(image)
|
|
|
|
{
|
|
|
|
image->release();
|
|
|
|
}
|
2013-09-07 06:33:28 +08:00
|
|
|
delete asyncStruct;
|
|
|
|
delete imageInfo;
|
2012-05-30 14:24:59 +08:00
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
--_asyncRefCount;
|
|
|
|
if (0 == _asyncRefCount)
|
2012-05-30 14:24:59 +08:00
|
|
|
{
|
2014-10-04 00:38:36 +08:00
|
|
|
Director::getInstance()->getScheduler()->unschedule(CC_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack), this);
|
2012-05-30 14:24:59 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-11-16 11:04:29 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D * TextureCache::addImage(const std::string &path)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D * texture = nullptr;
|
|
|
|
Image* image = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Split up directory and filename
|
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2013-09-07 13:55:11 +08:00
|
|
|
if (fullpath.size() == 0)
|
2013-02-27 11:35:38 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-02-27 11:35:38 +08:00
|
|
|
}
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if( it != _textures.end() )
|
|
|
|
texture = it->second;
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-09-07 10:00:12 +08:00
|
|
|
if (! texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
|
|
|
do
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
image = new (std::nothrow) Image();
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(nullptr == image);
|
2012-12-27 16:07:48 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
2013-07-26 17:34:44 +08:00
|
|
|
CC_BREAK_IF(!bRet);
|
2013-05-18 08:09:28 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
texture = new (std::nothrow) Texture2D();
|
2013-07-26 17:34:44 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
if( texture && texture->initWithImage(image) )
|
2013-07-26 17:34:44 +08:00
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-07-26 17:34:44 +08:00
|
|
|
// cache the texture file name
|
2013-12-25 13:57:17 +08:00
|
|
|
VolatileTextureMgr::addImageTexture(texture, fullpath);
|
2011-04-06 16:29:58 +08:00
|
|
|
#endif
|
2013-09-07 13:55:11 +08:00
|
|
|
// texture already retained, no need to re-retain it
|
|
|
|
_textures.insert( std::make_pair(fullpath, texture) );
|
2013-07-26 17:34:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
CCLOG("cocos2d: Couldn't create texture for file:%s in TextureCache", path.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
CC_SAFE_RELEASE(image);
|
2012-12-27 15:42:55 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* TextureCache::addImage(Image *image, const std::string &key)
|
2010-08-26 14:06:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT(image != nullptr, "TextureCache: image MUST not be nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D * texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(key);
|
|
|
|
if( it != _textures.end() ) {
|
|
|
|
texture = it->second;
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevents overloading the autorelease pool
|
2014-08-28 07:31:57 +08:00
|
|
|
texture = new (std::nothrow) Texture2D();
|
2012-08-08 18:39:33 +08:00
|
|
|
texture->initWithImage(image);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
if(texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
_textures.insert( std::make_pair(key, texture) );
|
|
|
|
texture->retain();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Couldn't add UIImage in TextureCache");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
2013-08-16 11:02:44 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-11-08 16:47:33 +08:00
|
|
|
VolatileTextureMgr::addImage(texture, image);
|
2012-04-24 15:02:18 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return texture;
|
2010-08-26 14:06:41 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2015-04-23 16:50:28 +08:00
|
|
|
bool TextureCache::isTextureLoaded(const std::string& fileName)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
|
|
|
if (fullpath.size() > 0)
|
|
|
|
{
|
|
|
|
if (_textures.find(fullpath) != _textures.end()) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-26 11:44:54 +08:00
|
|
|
bool TextureCache::reloadTexture(const std::string& fileName)
|
|
|
|
{
|
|
|
|
Texture2D * texture = nullptr;
|
2014-10-20 10:43:18 +08:00
|
|
|
Image * image = nullptr;
|
2014-02-26 11:44:54 +08:00
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
|
|
|
if (fullpath.size() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if (it != _textures.end()) {
|
|
|
|
texture = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
if (! texture) {
|
|
|
|
texture = this->addImage(fullpath);
|
|
|
|
ret = (texture != nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do {
|
2014-10-20 10:43:18 +08:00
|
|
|
image = new (std::nothrow) Image();
|
2014-02-26 11:44:54 +08:00
|
|
|
CC_BREAK_IF(nullptr == image);
|
|
|
|
|
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
|
|
|
|
ret = texture->initWithImage(image);
|
|
|
|
} while (0);
|
|
|
|
}
|
2014-10-20 10:43:18 +08:00
|
|
|
|
|
|
|
CC_SAFE_RELEASE(image);
|
2014-02-26 11:44:54 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureCache - Remove
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeAllTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it=_textures.begin(); it!=_textures.end(); ++it ) {
|
|
|
|
(it->second)->release();
|
|
|
|
}
|
|
|
|
_textures.clear();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeUnusedTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */) {
|
|
|
|
Texture2D *tex = it->second;
|
2014-01-22 13:47:29 +08:00
|
|
|
if( tex->getReferenceCount() == 1 ) {
|
2013-09-07 13:55:11 +08:00
|
|
|
CCLOG("cocos2d: TextureCache: removing unused texture: %s", it->first.c_str());
|
|
|
|
|
|
|
|
tex->release();
|
|
|
|
_textures.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
2012-04-28 11:34:13 +08:00
|
|
|
}
|
2013-09-07 13:55:11 +08:00
|
|
|
|
2012-04-28 11:34:13 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeTexture(Texture2D* texture)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! texture )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */ ) {
|
|
|
|
if( it->second == texture ) {
|
|
|
|
texture->release();
|
|
|
|
_textures.erase(it++);
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
++it;
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
void TextureCache::removeTextureForKey(const std::string &textureKeyName)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string key = textureKeyName;
|
|
|
|
auto it = _textures.find(key);
|
|
|
|
|
|
|
|
if( it == _textures.end() ) {
|
|
|
|
key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
if( it != _textures.end() ) {
|
|
|
|
(it->second)->release();
|
|
|
|
_textures.erase(it);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
Texture2D* TextureCache::getTextureForKey(const std::string &textureKeyName) const
|
2010-12-30 17:30:11 +08:00
|
|
|
{
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string key = textureKeyName;
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(key);
|
2014-01-15 09:22:45 +08:00
|
|
|
|
|
|
|
if( it == _textures.end() ) {
|
|
|
|
key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
if( it != _textures.end() )
|
|
|
|
return it->second;
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::reloadAllTextures()
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2013-11-11 11:38:02 +08:00
|
|
|
//will do nothing
|
|
|
|
// #if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// VolatileTextureMgr::reloadAllTextures();
|
|
|
|
// #endif
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 18:52:36 +08:00
|
|
|
void TextureCache::waitForQuit()
|
|
|
|
{
|
|
|
|
// notify sub thread to quick
|
|
|
|
_needQuit = true;
|
|
|
|
_sleepCondition.notify_one();
|
|
|
|
if (_loadingThread) _loadingThread->join();
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string TextureCache::getCachedTextureInfo() const
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2014-03-15 01:09:22 +08:00
|
|
|
std::string buffer;
|
2014-01-15 09:22:45 +08:00
|
|
|
char buftmp[4096];
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int totalBytes = 0;
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it = _textures.begin(); it != _textures.end(); ++it ) {
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
memset(buftmp,0,sizeof(buftmp));
|
|
|
|
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* tex = it->second;
|
2013-07-20 04:16:38 +08:00
|
|
|
unsigned int bpp = tex->getBitsPerPixelForFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
// Each texture takes up width * height * bytesPerPixel bytes.
|
2013-12-05 17:19:01 +08:00
|
|
|
auto bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
|
2012-04-19 14:35:52 +08:00
|
|
|
totalBytes += bytes;
|
|
|
|
count++;
|
2014-01-15 09:22:45 +08:00
|
|
|
snprintf(buftmp,sizeof(buftmp)-1,"\"%s\" rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB\n",
|
2013-09-07 13:55:11 +08:00
|
|
|
it->first.c_str(),
|
2014-01-22 13:47:29 +08:00
|
|
|
(long)tex->getReferenceCount(),
|
2012-04-19 14:35:52 +08:00
|
|
|
(long)tex->getName(),
|
|
|
|
(long)tex->getPixelsWide(),
|
|
|
|
(long)tex->getPixelsHigh(),
|
|
|
|
(long)bpp,
|
|
|
|
(long)bytes / 1024);
|
2014-03-15 01:09:22 +08:00
|
|
|
|
|
|
|
buffer += buftmp;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
snprintf(buftmp, sizeof(buftmp)-1, "TextureCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)\n", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
|
2014-03-15 01:09:22 +08:00
|
|
|
buffer += buftmp;
|
2014-01-15 09:22:45 +08:00
|
|
|
|
2014-03-15 01:09:22 +08:00
|
|
|
return buffer;
|
2011-07-05 10:47:25 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
std::list<VolatileTexture*> VolatileTextureMgr::_textures;
|
|
|
|
bool VolatileTextureMgr::_isReloading = false;
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
VolatileTexture::VolatileTexture(Texture2D *t)
|
2013-07-16 16:33:02 +08:00
|
|
|
: _texture(t)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _cashedImageType(kInvalid)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _textureData(nullptr)
|
2013-07-26 04:36:19 +08:00
|
|
|
, _pixelFormat(Texture2D::PixelFormat::RGBA8888)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _fileName("")
|
|
|
|
, _text("")
|
2013-12-18 17:47:20 +08:00
|
|
|
, _uiImage(nullptr)
|
2014-03-29 02:25:57 +08:00
|
|
|
, _hasMipmaps(false)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_texParams.minFilter = GL_LINEAR;
|
|
|
|
_texParams.magFilter = GL_LINEAR;
|
|
|
|
_texParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
_texParams.wrapT = GL_CLAMP_TO_EDGE;
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture::~VolatileTexture()
|
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
CC_SAFE_RELEASE(_uiImage);
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 13:57:17 +08:00
|
|
|
void VolatileTextureMgr::addImageTexture(Texture2D *tt, const std::string& imageFileName)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
return;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImageFile;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_fileName = imageFileName;
|
|
|
|
vt->_pixelFormat = tt->getPixelFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addImage(Texture2D *tt, Image *image)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2012-05-23 11:18:04 +08:00
|
|
|
image->retain();
|
2013-07-16 16:33:02 +08:00
|
|
|
vt->_uiImage = image;
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImage;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
VolatileTexture* VolatileTextureMgr::findVolotileTexture(Texture2D *tt)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
VolatileTexture *vt = 0;
|
2013-07-16 16:33:02 +08:00
|
|
|
auto i = _textures.begin();
|
|
|
|
while (i != _textures.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *v = *i++;
|
2013-07-16 16:33:02 +08:00
|
|
|
if (v->_texture == tt)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vt = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
|
|
|
if (! vt)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
vt = new (std::nothrow) VolatileTexture(tt);
|
2013-11-08 16:47:33 +08:00
|
|
|
_textures.push_back(vt);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return vt;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addDataTexture(Texture2D *tt, void* data, int dataLen, Texture2D::PixelFormat pixelFormat, const Size& contentSize)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImageData;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_textureData = data;
|
2013-07-26 17:34:44 +08:00
|
|
|
vt->_dataLen = dataLen;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_pixelFormat = pixelFormat;
|
|
|
|
vt->_textureSize = contentSize;
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addStringTexture(Texture2D *tt, const char* text, const FontDefinition& fontDefinition)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
return;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kString;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_text = text;
|
2013-07-10 04:21:43 +08:00
|
|
|
vt->_fontDefinition = fontDefinition;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2014-03-25 16:19:34 +08:00
|
|
|
void VolatileTextureMgr::setHasMipmaps(Texture2D *t, bool hasMipmaps)
|
|
|
|
{
|
|
|
|
VolatileTexture *vt = findVolotileTexture(t);
|
|
|
|
vt->_hasMipmaps = hasMipmaps;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::setTexParameters(Texture2D *t, const Texture2D::TexParams &texParams)
|
2012-08-31 03:02:05 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *vt = findVolotileTexture(t);
|
|
|
|
|
2013-07-04 08:59:22 +08:00
|
|
|
if (texParams.minFilter != GL_NONE)
|
|
|
|
vt->_texParams.minFilter = texParams.minFilter;
|
|
|
|
if (texParams.magFilter != GL_NONE)
|
|
|
|
vt->_texParams.magFilter = texParams.magFilter;
|
|
|
|
if (texParams.wrapS != GL_NONE)
|
|
|
|
vt->_texParams.wrapS = texParams.wrapS;
|
|
|
|
if (texParams.wrapT != GL_NONE)
|
|
|
|
vt->_texParams.wrapT = texParams.wrapT;
|
2012-08-31 03:02:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::removeTexture(Texture2D *t)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
auto i = _textures.begin();
|
|
|
|
while (i != _textures.end())
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *vt = *i++;
|
2013-07-16 16:33:02 +08:00
|
|
|
if (vt->_texture == t)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-11-08 16:47:33 +08:00
|
|
|
_textures.remove(vt);
|
2011-11-25 10:48:05 +08:00
|
|
|
delete vt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::reloadAllTextures()
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
_isReloading = true;
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2014-06-13 07:31:46 +08:00
|
|
|
// we need to release all of the glTextures to avoid collisions of texture id's when reloading the textures onto the GPU
|
|
|
|
for(auto iter = _textures.begin(); iter != _textures.end(); ++iter)
|
|
|
|
{
|
|
|
|
(*iter)->_texture->releaseGLTexture();
|
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
CCLOG("reload all texture");
|
2013-07-16 16:33:02 +08:00
|
|
|
auto iter = _textures.begin();
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2013-07-16 16:33:02 +08:00
|
|
|
while (iter != _textures.end())
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = *iter++;
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (vt->_cashedImageType)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImageFile:
|
2012-04-08 14:16:29 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Image* image = new (std::nothrow) Image();
|
2013-07-26 17:34:44 +08:00
|
|
|
|
2013-12-24 18:08:40 +08:00
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(vt->_fileName);
|
|
|
|
|
2013-12-24 19:05:35 +08:00
|
|
|
if (image && image->initWithImageData(data.getBytes(), data.getSize()))
|
2012-04-08 14:16:29 +08:00
|
|
|
{
|
2013-07-25 21:37:12 +08:00
|
|
|
Texture2D::PixelFormat oldPixelFormat = Texture2D::getDefaultAlphaPixelFormat();
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D::setDefaultAlphaPixelFormat(vt->_pixelFormat);
|
2013-09-07 06:33:28 +08:00
|
|
|
vt->_texture->initWithImage(image);
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D::setDefaultAlphaPixelFormat(oldPixelFormat);
|
2012-04-08 14:16:29 +08:00
|
|
|
}
|
2013-07-26 17:34:44 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
CC_SAFE_RELEASE(image);
|
2012-04-08 14:16:29 +08:00
|
|
|
}
|
2012-02-07 11:18:39 +08:00
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImageData:
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 17:34:44 +08:00
|
|
|
vt->_texture->initWithData(vt->_textureData,
|
|
|
|
vt->_dataLen,
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_pixelFormat,
|
|
|
|
vt->_textureSize.width,
|
|
|
|
vt->_textureSize.height,
|
|
|
|
vt->_textureSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kString:
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-15 10:55:14 +08:00
|
|
|
vt->_texture->initWithString(vt->_text.c_str(), vt->_fontDefinition);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImage:
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
vt->_texture->initWithImage(vt->_uiImage);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-25 16:19:34 +08:00
|
|
|
if (vt->_hasMipmaps) {
|
|
|
|
vt->_texture->generateMipmap();
|
|
|
|
}
|
2013-07-16 16:33:02 +08:00
|
|
|
vt->_texture->setTexParameters(vt->_texParams);
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 16:33:02 +08:00
|
|
|
_isReloading = false;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#endif // CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_END
|
2010-07-16 16:28:11 +08:00
|
|
|
|