2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCTexture2D.h"
|
|
|
|
#include "ccMacros.h"
|
2010-07-30 16:35:07 +08:00
|
|
|
#include "CCDirector.h"
|
2010-08-25 14:14:31 +08:00
|
|
|
#include "platform/platform.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCThread.h"
|
|
|
|
#include "platform/CCImage.h"
|
2011-07-28 17:32:09 +08:00
|
|
|
#include "support/ccUtils.h"
|
2011-11-16 11:04:29 +08:00
|
|
|
#include "CCScheduler.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCString.h"
|
2012-05-28 02:41:12 +08:00
|
|
|
#include <errno.h>
|
2012-06-19 16:20:46 +08:00
|
|
|
#include <stack>
|
|
|
|
#include <string>
|
|
|
|
#include <cctype>
|
|
|
|
#include <queue>
|
|
|
|
#include <list>
|
|
|
|
#include <pthread.h>
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-11-16 11:04:29 +08:00
|
|
|
typedef struct _AsyncStruct
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
std::string filename;
|
|
|
|
CCObject *target;
|
|
|
|
SEL_CallFuncO selector;
|
2011-11-16 11:04:29 +08:00
|
|
|
} AsyncStruct;
|
|
|
|
|
2011-11-22 16:47:24 +08:00
|
|
|
typedef struct _ImageInfo
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
AsyncStruct *asyncStruct;
|
|
|
|
CCImage *image;
|
|
|
|
CCImage::EImageFormat imageType;
|
2011-11-22 16:47:24 +08:00
|
|
|
} ImageInfo;
|
|
|
|
|
|
|
|
static pthread_t s_loadingThread;
|
|
|
|
|
2013-04-27 00:55:36 +08:00
|
|
|
static pthread_mutex_t s_SleepMutex;
|
|
|
|
static pthread_cond_t s_SleepCondition;
|
|
|
|
|
2012-05-30 14:24:59 +08:00
|
|
|
static pthread_mutex_t s_asyncStructQueueMutex;
|
2011-11-22 16:47:24 +08:00
|
|
|
static pthread_mutex_t s_ImageInfoMutex;
|
|
|
|
|
2013-05-08 08:25:39 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
// Hack to get ASM.JS validation (no undefined symbols allowed).
|
|
|
|
#define pthread_cond_signal(_)
|
|
|
|
#endif // EMSCRIPTEN
|
2012-05-28 02:41:12 +08:00
|
|
|
|
2013-04-27 00:55:36 +08:00
|
|
|
static unsigned long s_nAsyncRefCount = 0;
|
2012-05-28 02:41:12 +08:00
|
|
|
|
2012-05-30 14:24:59 +08:00
|
|
|
static bool need_quit = false;
|
2011-11-22 16:47:24 +08:00
|
|
|
|
2012-05-30 14:24:59 +08:00
|
|
|
static std::queue<AsyncStruct*>* s_pAsyncStructQueue = NULL;
|
|
|
|
static std::queue<ImageInfo*>* s_pImageQueue = NULL;
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2011-12-22 15:38:31 +08:00
|
|
|
static CCImage::EImageFormat computeImageFormatType(string& filename)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCImage::EImageFormat ret = CCImage::kFmtUnKnown;
|
|
|
|
|
|
|
|
if ((std::string::npos != filename.find(".jpg")) || (std::string::npos != filename.find(".jpeg")))
|
|
|
|
{
|
|
|
|
ret = CCImage::kFmtJpg;
|
|
|
|
}
|
|
|
|
else if ((std::string::npos != filename.find(".png")) || (std::string::npos != filename.find(".PNG")))
|
|
|
|
{
|
|
|
|
ret = CCImage::kFmtPng;
|
|
|
|
}
|
2012-05-23 17:26:06 +08:00
|
|
|
else if ((std::string::npos != filename.find(".tiff")) || (std::string::npos != filename.find(".TIFF")))
|
|
|
|
{
|
|
|
|
ret = CCImage::kFmtTiff;
|
|
|
|
}
|
2012-12-29 13:58:02 +08:00
|
|
|
else if ((std::string::npos != filename.find(".webp")) || (std::string::npos != filename.find(".WEBP")))
|
|
|
|
{
|
|
|
|
ret = CCImage::kFmtWebp;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
2011-12-22 15:38:31 +08:00
|
|
|
}
|
|
|
|
|
2011-11-16 11:04:29 +08:00
|
|
|
static void* loadImage(void* data)
|
|
|
|
{
|
2011-11-22 16:47:24 +08:00
|
|
|
AsyncStruct *pAsyncStruct = NULL;
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
while (true)
|
|
|
|
{
|
2013-01-14 16:32:30 +08:00
|
|
|
// create autorelease pool for iOS
|
|
|
|
CCThread thread;
|
|
|
|
thread.createAutoreleasePool();
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
std::queue<AsyncStruct*> *pQueue = s_pAsyncStructQueue;
|
|
|
|
pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue
|
2011-11-25 16:14:06 +08:00
|
|
|
if (pQueue->empty())
|
|
|
|
{
|
2011-11-22 16:47:24 +08:00
|
|
|
pthread_mutex_unlock(&s_asyncStructQueueMutex);
|
2013-04-27 00:55:36 +08:00
|
|
|
if (need_quit) {
|
2012-05-28 02:41:12 +08:00
|
|
|
break;
|
2013-04-27 00:55:36 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pthread_cond_wait(&s_SleepCondition, &s_SleepMutex);
|
2012-05-28 02:41:12 +08:00
|
|
|
continue;
|
2013-04-27 00:55:36 +08:00
|
|
|
}
|
2011-11-25 16:14:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pAsyncStruct = pQueue->front();
|
|
|
|
pQueue->pop();
|
|
|
|
pthread_mutex_unlock(&s_asyncStructQueueMutex);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *filename = pAsyncStruct->filename.c_str();
|
|
|
|
|
|
|
|
// compute image type
|
|
|
|
CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename);
|
|
|
|
if (imageType == CCImage::kFmtUnKnown)
|
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCLOG("unsupported format %s",filename);
|
2012-04-19 14:35:52 +08:00
|
|
|
delete pAsyncStruct;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate image
|
|
|
|
CCImage *pImage = new CCImage();
|
2012-12-27 16:07:48 +08:00
|
|
|
if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-27 16:07:48 +08:00
|
|
|
CC_SAFE_RELEASE(pImage);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("can not load %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate image info
|
|
|
|
ImageInfo *pImageInfo = new ImageInfo();
|
|
|
|
pImageInfo->asyncStruct = pAsyncStruct;
|
|
|
|
pImageInfo->image = pImage;
|
|
|
|
pImageInfo->imageType = imageType;
|
|
|
|
|
|
|
|
// put the image info into the queue
|
|
|
|
pthread_mutex_lock(&s_ImageInfoMutex);
|
|
|
|
s_pImageQueue->push(pImageInfo);
|
|
|
|
pthread_mutex_unlock(&s_ImageInfoMutex);
|
|
|
|
}
|
|
|
|
|
2013-04-27 00:55:36 +08:00
|
|
|
if( s_pAsyncStructQueue != NULL )
|
2012-05-28 02:41:12 +08:00
|
|
|
{
|
2012-05-30 14:24:59 +08:00
|
|
|
delete s_pAsyncStructQueue;
|
2013-04-27 00:55:36 +08:00
|
|
|
s_pAsyncStructQueue = NULL;
|
2012-05-30 14:24:59 +08:00
|
|
|
delete s_pImageQueue;
|
2013-04-27 00:55:36 +08:00
|
|
|
s_pImageQueue = NULL;
|
2013-04-27 23:44:33 +08:00
|
|
|
|
|
|
|
pthread_mutex_destroy(&s_asyncStructQueueMutex);
|
|
|
|
pthread_mutex_destroy(&s_ImageInfoMutex);
|
|
|
|
pthread_mutex_destroy(&s_SleepMutex);
|
|
|
|
pthread_cond_destroy(&s_SleepCondition);
|
2012-05-28 02:41:12 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return 0;
|
2011-11-16 11:04:29 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// implementation CCTextureCache
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureCache - Alloc, Init & Dealloc
|
2012-05-30 14:24:59 +08:00
|
|
|
static CCTextureCache *g_sharedTextureCache = NULL;
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
CCTextureCache * CCTextureCache::sharedTextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!g_sharedTextureCache)
|
2012-05-30 14:24:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
g_sharedTextureCache = new CCTextureCache();
|
2012-05-30 14:24:59 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
return g_sharedTextureCache;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
CCTextureCache::CCTextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(g_sharedTextureCache == NULL, "Attempted to allocate a second instance of a singleton.");
|
|
|
|
|
|
|
|
m_pTextures = new CCDictionary();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
CCTextureCache::~CCTextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
CCLOGINFO("cocos2d: deallocing CCTextureCache: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
need_quit = true;
|
2013-04-27 00:55:36 +08:00
|
|
|
|
|
|
|
pthread_cond_signal(&s_SleepCondition);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pTextures);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
void CCTextureCache::purgeSharedTextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(g_sharedTextureCache);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
const char* CCTextureCache::description()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
return CCString::createWithFormat("<CCTextureCache | Number of textures = %u>", m_pTextures->count())->getCString();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
CCDictionary* CCTextureCache::snapshotTextures()
|
|
|
|
{
|
|
|
|
CCDictionary* pRet = new CCDictionary();
|
|
|
|
CCDictElement* pElement = NULL;
|
|
|
|
CCDICT_FOREACH(m_pTextures, pElement)
|
|
|
|
{
|
|
|
|
pRet->setObject(pElement->getObject(), pElement->getStrKey());
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-01-08 21:03:16 +08:00
|
|
|
void CCTextureCache::addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-05-08 08:25:39 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
CCLOGWARN("Cannot load image %s asynchronously in Emscripten builds.", path);
|
|
|
|
return;
|
|
|
|
#endif // EMSCRIPTEN
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
|
|
|
|
|
|
|
|
CCTexture2D *texture = NULL;
|
|
|
|
|
|
|
|
// optimization
|
|
|
|
|
|
|
|
std::string pathKey = path;
|
|
|
|
|
2013-01-25 20:51:52 +08:00
|
|
|
pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str());
|
|
|
|
|
|
|
|
std::string fullpath = pathKey;
|
|
|
|
if (texture != NULL)
|
|
|
|
{
|
|
|
|
if (target && selector)
|
|
|
|
{
|
|
|
|
(target->*selector)(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy init
|
2013-04-27 00:55:36 +08:00
|
|
|
if (s_pAsyncStructQueue == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2011-11-22 16:47:24 +08:00
|
|
|
s_pAsyncStructQueue = new queue<AsyncStruct*>();
|
2012-04-19 14:35:52 +08:00
|
|
|
s_pImageQueue = new queue<ImageInfo*>();
|
2012-05-28 02:41:12 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pthread_mutex_init(&s_asyncStructQueueMutex, NULL);
|
|
|
|
pthread_mutex_init(&s_ImageInfoMutex, NULL);
|
2013-04-27 00:55:36 +08:00
|
|
|
pthread_mutex_init(&s_SleepMutex, NULL);
|
|
|
|
pthread_cond_init(&s_SleepCondition, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
pthread_create(&s_loadingThread, NULL, loadImage, NULL);
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
need_quit = false;
|
2012-05-28 02:41:12 +08:00
|
|
|
}
|
|
|
|
|
2012-05-30 14:24:59 +08:00
|
|
|
if (0 == s_nAsyncRefCount)
|
|
|
|
{
|
|
|
|
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
++s_nAsyncRefCount;
|
|
|
|
|
2012-05-28 02:41:12 +08:00
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
target->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// generate async struct
|
|
|
|
AsyncStruct *data = new AsyncStruct();
|
|
|
|
data->filename = fullpath.c_str();
|
|
|
|
data->target = target;
|
|
|
|
data->selector = selector;
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// add async struct into queue
|
|
|
|
pthread_mutex_lock(&s_asyncStructQueueMutex);
|
|
|
|
s_pAsyncStructQueue->push(data);
|
|
|
|
pthread_mutex_unlock(&s_asyncStructQueueMutex);
|
2011-11-25 16:14:06 +08:00
|
|
|
|
2013-04-27 00:55:36 +08:00
|
|
|
pthread_cond_signal(&s_SleepCondition);
|
2011-11-16 11:04:29 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCTextureCache::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
|
|
|
|
std::queue<ImageInfo*> *imagesQueue = s_pImageQueue;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s_ImageInfoMutex);
|
|
|
|
if (imagesQueue->empty())
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&s_ImageInfoMutex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImageInfo *pImageInfo = imagesQueue->front();
|
|
|
|
imagesQueue->pop();
|
|
|
|
pthread_mutex_unlock(&s_ImageInfoMutex);
|
|
|
|
|
|
|
|
AsyncStruct *pAsyncStruct = pImageInfo->asyncStruct;
|
|
|
|
CCImage *pImage = pImageInfo->image;
|
|
|
|
|
|
|
|
CCObject *target = pAsyncStruct->target;
|
|
|
|
SEL_CallFuncO selector = pAsyncStruct->selector;
|
|
|
|
const char* filename = pAsyncStruct->filename.c_str();
|
|
|
|
|
|
|
|
// generate texture in render thread
|
|
|
|
CCTexture2D *texture = new CCTexture2D();
|
2012-03-21 10:36:20 +08:00
|
|
|
#if 0 //TODO: (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2012-04-19 14:35:52 +08:00
|
|
|
texture->initWithImage(pImage, kCCResolutioniPhone);
|
2012-03-21 10:36:20 +08:00
|
|
|
#else
|
|
|
|
texture->initWithImage(pImage);
|
|
|
|
#endif
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2012-05-30 14:24:59 +08:00
|
|
|
// cache the texture file name
|
|
|
|
VolatileTexture::addImageTexture(texture, filename, pImageInfo->imageType);
|
2011-12-01 17:04:56 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// cache the texture
|
|
|
|
m_pTextures->setObject(texture, filename);
|
|
|
|
texture->autorelease();
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (target && selector)
|
|
|
|
{
|
|
|
|
(target->*selector)(texture);
|
|
|
|
target->release();
|
|
|
|
}
|
2011-11-16 11:04:29 +08:00
|
|
|
|
2012-05-30 14:24:59 +08:00
|
|
|
pImage->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
delete pAsyncStruct;
|
|
|
|
delete pImageInfo;
|
2012-05-30 14:24:59 +08:00
|
|
|
|
|
|
|
--s_nAsyncRefCount;
|
|
|
|
if (0 == s_nAsyncRefCount)
|
|
|
|
{
|
2012-05-30 14:27:30 +08:00
|
|
|
CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CCTextureCache::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
|
|
|
|
2010-09-08 09:28:28 +08:00
|
|
|
CCTexture2D * CCTextureCache::addImage(const char * path)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTexture2D * texture = NULL;
|
2012-12-27 15:42:55 +08:00
|
|
|
CCImage* pImage = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Split up directory and filename
|
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
|
|
|
|
|
|
|
//pthread_mutex_lock(m_pDictLock);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-03-09 12:00:04 +08:00
|
|
|
std::string pathKey = path;
|
2010-12-30 17:30:11 +08:00
|
|
|
|
2013-01-25 20:51:52 +08:00
|
|
|
pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
|
2013-02-27 11:35:38 +08:00
|
|
|
if (pathKey.size() == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str());
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
std::string fullpath = pathKey; // (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(path));
|
2013-02-27 11:35:38 +08:00
|
|
|
if (! texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
std::string lowerCase(pathKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
for (unsigned int i = 0; i < lowerCase.length(); ++i)
|
|
|
|
{
|
|
|
|
lowerCase[i] = tolower(lowerCase[i]);
|
|
|
|
}
|
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (std::string::npos != lowerCase.find(".pvr"))
|
|
|
|
{
|
|
|
|
texture = this->addPVRImage(fullpath.c_str());
|
|
|
|
}
|
2013-05-27 14:42:22 +08:00
|
|
|
else if (std::string::npos != lowerCase.find(".pkm"))
|
|
|
|
{
|
|
|
|
// ETC1 file format, only supportted on Android
|
|
|
|
texture = this->addETCImage(fullpath.c_str());
|
|
|
|
}
|
2012-05-25 16:52:47 +08:00
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-05-25 16:52:47 +08:00
|
|
|
CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
|
|
|
|
if (std::string::npos != lowerCase.find(".png"))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-05-25 16:52:47 +08:00
|
|
|
eImageFormat = CCImage::kFmtPng;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-05-25 16:52:47 +08:00
|
|
|
else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg"))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-05-25 16:52:47 +08:00
|
|
|
eImageFormat = CCImage::kFmtJpg;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-05-25 16:52:47 +08:00
|
|
|
else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff"))
|
2012-05-23 17:26:06 +08:00
|
|
|
{
|
2012-05-25 16:52:47 +08:00
|
|
|
eImageFormat = CCImage::kFmtTiff;
|
2012-05-23 17:26:06 +08:00
|
|
|
}
|
2012-12-29 10:27:43 +08:00
|
|
|
else if (std::string::npos != lowerCase.find(".webp"))
|
|
|
|
{
|
|
|
|
eImageFormat = CCImage::kFmtWebp;
|
|
|
|
}
|
2012-05-25 16:52:47 +08:00
|
|
|
|
2012-12-27 16:07:48 +08:00
|
|
|
pImage = new CCImage();
|
|
|
|
CC_BREAK_IF(NULL == pImage);
|
|
|
|
|
2013-05-18 08:09:28 +08:00
|
|
|
bool bRet = pImage->initWithImageFile(fullpath.c_str(), eImageFormat);
|
2013-05-16 11:22:43 +08:00
|
|
|
CC_BREAK_IF(!bRet);
|
2013-05-18 08:09:28 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
texture = new CCTexture2D();
|
2012-07-14 15:06:11 +08:00
|
|
|
|
|
|
|
if( texture &&
|
2012-12-27 15:42:55 +08:00
|
|
|
texture->initWithImage(pImage) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
// cache the texture file name
|
2012-05-25 16:52:47 +08:00
|
|
|
VolatileTexture::addImageTexture(texture, fullpath.c_str(), eImageFormat);
|
2011-04-06 16:29:58 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTextures->setObject(texture, pathKey.c_str());
|
2012-06-19 16:31:26 +08:00
|
|
|
texture->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 11:35:38 +08:00
|
|
|
CCLOG("cocos2d: Couldn't create texture for file:%s in CCTextureCache", path);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2012-12-27 15:42:55 +08:00
|
|
|
CC_SAFE_RELEASE(pImage);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//pthread_mutex_unlock(m_pDictLock);
|
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
CCTexture2D * CCTextureCache::addPVRImage(const char* path)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be nil");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-07-14 15:21:42 +08:00
|
|
|
CCTexture2D* texture = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
std::string key(path);
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2012-07-14 15:21:42 +08:00
|
|
|
if( (texture = (CCTexture2D*)m_pTextures->objectForKey(key.c_str())) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-07-14 15:21:42 +08:00
|
|
|
return texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
// Split up directory and filename
|
2013-01-25 20:51:52 +08:00
|
|
|
std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(key.c_str());
|
2012-07-14 15:21:42 +08:00
|
|
|
texture = new CCTexture2D();
|
|
|
|
if(texture != NULL && texture->initWithPVRFile(fullpath.c_str()) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2012-04-08 14:16:29 +08:00
|
|
|
// cache the texture file name
|
2012-07-14 23:44:56 +08:00
|
|
|
VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtRawData);
|
2012-02-07 11:18:39 +08:00
|
|
|
#endif
|
2012-07-14 15:21:42 +08:00
|
|
|
m_pTextures->setObject(texture, key.c_str());
|
|
|
|
texture->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add PVRImage:%s in CCTextureCache",key.c_str());
|
2012-07-14 15:21:42 +08:00
|
|
|
CC_SAFE_DELETE(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2012-07-14 15:21:42 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-05-27 14:42:22 +08:00
|
|
|
CCTexture2D* CCTextureCache::addETCImage(const char* path)
|
|
|
|
{
|
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be nil");
|
|
|
|
|
|
|
|
CCTexture2D* texture = NULL;
|
|
|
|
std::string key(path);
|
|
|
|
|
|
|
|
if( (texture = (CCTexture2D*)m_pTextures->objectForKey(key.c_str())) )
|
|
|
|
{
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split up directory and filename
|
|
|
|
std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(key.c_str());
|
|
|
|
texture = new CCTexture2D();
|
|
|
|
if(texture != NULL && texture->initWithETCFile(fullpath.c_str()))
|
|
|
|
{
|
|
|
|
m_pTextures->setObject(texture, key.c_str());
|
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add ETCImage:%s in CCTextureCache",key.c_str());
|
|
|
|
CC_SAFE_DELETE(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCTexture2D* CCTextureCache::addUIImage(CCImage *image, const char *key)
|
2010-08-26 14:06:41 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(image != NULL, "TextureCache: image MUST not be nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCTexture2D * texture = NULL;
|
|
|
|
// textureForKey() use full path,so the key should be full path
|
|
|
|
std::string forKey;
|
|
|
|
if (key)
|
|
|
|
{
|
2013-01-25 20:51:52 +08:00
|
|
|
forKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(key);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't have to lock here, because addImageAsync() will not
|
|
|
|
// invoke opengl function in loading thread.
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// If key is nil, then create a new texture each time
|
|
|
|
if(key && (texture = (CCTexture2D *)m_pTextures->objectForKey(forKey.c_str())))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevents overloading the autorelease pool
|
|
|
|
texture = new CCTexture2D();
|
2012-08-08 18:39:33 +08:00
|
|
|
texture->initWithImage(image);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if(key && texture)
|
|
|
|
{
|
|
|
|
m_pTextures->setObject(texture, forKey.c_str());
|
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add UIImage in CCTextureCache");
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture::addCCImage(texture, image);
|
|
|
|
#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
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureCache - Remove
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
void CCTextureCache::removeAllTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTextures->removeAllObjects();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
void CCTextureCache::removeUnusedTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-28 11:34:13 +08:00
|
|
|
/*
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDictElement* pElement = NULL;
|
|
|
|
CCDICT_FOREACH(m_pTextures, pElement)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTextureCache: texture: %s", pElement->getStrKey());
|
|
|
|
CCTexture2D *value = (CCTexture2D*)pElement->getObject();
|
|
|
|
if (value->retainCount() == 1)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", pElement->getStrKey());
|
|
|
|
m_pTextures->removeObjectForElememt(pElement);
|
|
|
|
}
|
|
|
|
}
|
2012-04-28 11:34:13 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** Inter engineer zhuoshi sun finds that this way will get better performance
|
|
|
|
*/
|
|
|
|
if (m_pTextures->count())
|
|
|
|
{
|
|
|
|
// find elements to be removed
|
|
|
|
CCDictElement* pElement = NULL;
|
|
|
|
list<CCDictElement*> elementToRemove;
|
|
|
|
CCDICT_FOREACH(m_pTextures, pElement)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTextureCache: texture: %s", pElement->getStrKey());
|
|
|
|
CCTexture2D *value = (CCTexture2D*)pElement->getObject();
|
|
|
|
if (value->retainCount() == 1)
|
|
|
|
{
|
|
|
|
elementToRemove.push_back(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove elements
|
|
|
|
for (list<CCDictElement*>::iterator iter = elementToRemove.begin(); iter != elementToRemove.end(); ++iter)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", (*iter)->getStrKey());
|
|
|
|
m_pTextures->removeObjectForElememt(*iter);
|
|
|
|
}
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
void CCTextureCache::removeTexture(CCTexture2D* 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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCArray* keys = m_pTextures->allKeysForObject(texture);
|
|
|
|
m_pTextures->removeObjectsForKeys(keys);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
void CCTextureCache::removeTextureForKey(const char *textureKeyName)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (textureKeyName == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-01-25 20:51:52 +08:00
|
|
|
string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(textureKeyName);
|
|
|
|
m_pTextures->removeObjectForKey(fullPath);
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2D* CCTextureCache::textureForKey(const char* key)
|
|
|
|
{
|
2013-01-25 20:51:52 +08:00
|
|
|
return (CCTexture2D*)m_pTextures->objectForKey(CCFileUtils::sharedFileUtils()->fullPathForFilename(key));
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2011-04-06 16:29:58 +08:00
|
|
|
void CCTextureCache::reloadAllTextures()
|
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
VolatileTexture::reloadAllTextures();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
void CCTextureCache::dumpCachedTextureInfo()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int totalBytes = 0;
|
|
|
|
|
|
|
|
CCDictElement* pElement = NULL;
|
|
|
|
CCDICT_FOREACH(m_pTextures, pElement)
|
|
|
|
{
|
|
|
|
CCTexture2D* tex = (CCTexture2D*)pElement->getObject();
|
|
|
|
unsigned int bpp = tex->bitsPerPixelForFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
// Each texture takes up width * height * bytesPerPixel bytes.
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
|
|
|
|
totalBytes += bytes;
|
|
|
|
count++;
|
|
|
|
CCLOG("cocos2d: \"%s\" rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB",
|
|
|
|
pElement->getStrKey(),
|
|
|
|
(long)tex->retainCount(),
|
|
|
|
(long)tex->getName(),
|
|
|
|
(long)tex->getPixelsWide(),
|
|
|
|
(long)tex->getPixelsHigh(),
|
|
|
|
(long)bpp,
|
|
|
|
(long)bytes / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLOG("cocos2d: CCTextureCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
|
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
|
|
|
|
2011-11-25 10:48:05 +08:00
|
|
|
std::list<VolatileTexture*> VolatileTexture::textures;
|
2011-04-06 16:29:58 +08:00
|
|
|
bool VolatileTexture::isReloading = false;
|
2011-11-25 10:48:05 +08:00
|
|
|
|
|
|
|
VolatileTexture::VolatileTexture(CCTexture2D *t)
|
|
|
|
: texture(t)
|
|
|
|
, m_eCashedImageType(kInvalid)
|
|
|
|
, m_pTextureData(NULL)
|
|
|
|
, m_PixelFormat(kTexture2DPixelFormat_RGBA8888)
|
|
|
|
, m_strFileName("")
|
|
|
|
, m_FmtImage(CCImage::kFmtPng)
|
2012-06-13 18:33:44 +08:00
|
|
|
, m_alignment(kCCTextAlignmentCenter)
|
|
|
|
, m_vAlignment(kCCVerticalTextAlignmentCenter)
|
2011-11-25 10:48:05 +08:00
|
|
|
, m_strFontName("")
|
|
|
|
, m_strText("")
|
2012-04-24 15:02:18 +08:00
|
|
|
, uiImage(NULL)
|
2011-11-25 10:48:05 +08:00
|
|
|
, m_fFontSize(0.0f)
|
|
|
|
{
|
|
|
|
m_size = CCSizeMake(0, 0);
|
2012-08-31 03:02:05 +08:00
|
|
|
m_texParams.minFilter = GL_LINEAR;
|
|
|
|
m_texParams.magFilter = GL_LINEAR;
|
|
|
|
m_texParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
m_texParams.wrapT = GL_CLAMP_TO_EDGE;
|
2011-11-25 10:48:05 +08:00
|
|
|
textures.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture::~VolatileTexture()
|
|
|
|
{
|
|
|
|
textures.remove(this);
|
2012-05-23 11:18:04 +08:00
|
|
|
CC_SAFE_RELEASE(uiImage);
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolatileTexture::addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format)
|
|
|
|
{
|
2011-04-06 16:29:58 +08:00
|
|
|
if (isReloading)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
vt->m_eCashedImageType = kImageFile;
|
|
|
|
vt->m_strFileName = imageFileName;
|
|
|
|
vt->m_FmtImage = format;
|
2011-12-29 16:00:36 +08:00
|
|
|
vt->m_PixelFormat = tt->getPixelFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
void VolatileTexture::addCCImage(CCTexture2D *tt, CCImage *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();
|
2012-04-24 15:02:18 +08:00
|
|
|
vt->uiImage = image;
|
|
|
|
vt->m_eCashedImageType = kImage;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture* VolatileTexture::findVolotileTexture(CCTexture2D *tt)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
VolatileTexture *vt = 0;
|
|
|
|
std::list<VolatileTexture *>::iterator i = textures.begin();
|
2012-04-24 15:02:18 +08:00
|
|
|
while (i != textures.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *v = *i++;
|
2012-04-24 15:02:18 +08:00
|
|
|
if (v->texture == tt)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vt = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
|
|
|
if (! vt)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vt = new VolatileTexture(tt);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return vt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolatileTexture::addDataTexture(CCTexture2D *tt, void* data, CCTexture2DPixelFormat pixelFormat, const CCSize& contentSize)
|
|
|
|
{
|
|
|
|
if (isReloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
vt->m_eCashedImageType = kImageData;
|
|
|
|
vt->m_pTextureData = data;
|
|
|
|
vt->m_PixelFormat = pixelFormat;
|
|
|
|
vt->m_TextureSize = contentSize;
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2012-06-13 18:33:44 +08:00
|
|
|
void VolatileTexture::addStringTexture(CCTexture2D *tt, const char* text, const CCSize& dimensions, CCTextAlignment alignment,
|
|
|
|
CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
|
|
|
if (isReloading)
|
|
|
|
{
|
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
|
|
|
|
2011-07-28 17:32:09 +08:00
|
|
|
vt->m_eCashedImageType = kString;
|
|
|
|
vt->m_size = dimensions;
|
2011-04-06 16:29:58 +08:00
|
|
|
vt->m_strFontName = fontName;
|
|
|
|
vt->m_alignment = alignment;
|
2012-06-13 18:33:44 +08:00
|
|
|
vt->m_vAlignment = vAlignment;
|
2011-04-06 16:29:58 +08:00
|
|
|
vt->m_fFontSize = fontSize;
|
|
|
|
vt->m_strText = text;
|
|
|
|
}
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2012-08-31 03:02:05 +08:00
|
|
|
void VolatileTexture::setTexParameters(CCTexture2D *t, ccTexParams *texParams)
|
|
|
|
{
|
|
|
|
VolatileTexture *vt = findVolotileTexture(t);
|
|
|
|
|
|
|
|
if (texParams->minFilter != GL_NONE)
|
|
|
|
vt->m_texParams.minFilter = texParams->minFilter;
|
|
|
|
if (texParams->magFilter != GL_NONE)
|
|
|
|
vt->m_texParams.magFilter = texParams->magFilter;
|
|
|
|
if (texParams->wrapS != GL_NONE)
|
|
|
|
vt->m_texParams.wrapS = texParams->wrapS;
|
|
|
|
if (texParams->wrapT != GL_NONE)
|
|
|
|
vt->m_texParams.wrapT = texParams->wrapT;
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
void VolatileTexture::removeTexture(CCTexture2D *t)
|
|
|
|
{
|
2011-11-25 10:48:05 +08:00
|
|
|
|
|
|
|
std::list<VolatileTexture *>::iterator i = textures.begin();
|
2012-04-24 15:02:18 +08:00
|
|
|
while (i != textures.end())
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *vt = *i++;
|
2012-04-24 15:02:18 +08:00
|
|
|
if (vt->texture == t)
|
|
|
|
{
|
2011-11-25 10:48:05 +08:00
|
|
|
delete vt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolatileTexture::reloadAllTextures()
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
|
|
|
isReloading = true;
|
|
|
|
|
|
|
|
CCLOG("reload all texture");
|
2012-04-24 15:02:18 +08:00
|
|
|
std::list<VolatileTexture *>::iterator iter = textures.begin();
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2012-04-24 15:02:18 +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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
switch (vt->m_eCashedImageType)
|
|
|
|
{
|
2012-04-08 14:16:29 +08:00
|
|
|
case kImageFile:
|
|
|
|
{
|
|
|
|
std::string lowerCase(vt->m_strFileName.c_str());
|
|
|
|
for (unsigned int i = 0; i < lowerCase.length(); ++i)
|
|
|
|
{
|
|
|
|
lowerCase[i] = tolower(lowerCase[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::string::npos != lowerCase.find(".pvr"))
|
|
|
|
{
|
|
|
|
CCTexture2DPixelFormat oldPixelFormat = CCTexture2D::defaultAlphaPixelFormat();
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(vt->m_PixelFormat);
|
|
|
|
|
|
|
|
vt->texture->initWithPVRFile(vt->m_strFileName.c_str());
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(oldPixelFormat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-27 16:07:48 +08:00
|
|
|
CCImage* pImage = new CCImage();
|
2012-06-19 16:31:26 +08:00
|
|
|
unsigned long nSize = 0;
|
|
|
|
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(vt->m_strFileName.c_str(), "rb", &nSize);
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-12-27 16:07:48 +08:00
|
|
|
if (pImage && pImage->initWithImageData((void*)pBuffer, nSize, vt->m_FmtImage))
|
2012-04-08 14:16:29 +08:00
|
|
|
{
|
|
|
|
CCTexture2DPixelFormat oldPixelFormat = CCTexture2D::defaultAlphaPixelFormat();
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(vt->m_PixelFormat);
|
2012-12-27 15:42:55 +08:00
|
|
|
vt->texture->initWithImage(pImage);
|
2012-04-08 14:16:29 +08:00
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(oldPixelFormat);
|
|
|
|
}
|
2012-06-19 16:31:26 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(pBuffer);
|
2012-12-27 16:07:48 +08:00
|
|
|
CC_SAFE_RELEASE(pImage);
|
2012-04-08 14:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-07 11:18:39 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
case kImageData:
|
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
vt->texture->initWithData(vt->m_pTextureData,
|
|
|
|
vt->m_PixelFormat,
|
|
|
|
vt->m_TextureSize.width,
|
|
|
|
vt->m_TextureSize.height,
|
|
|
|
vt->m_TextureSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kString:
|
|
|
|
{
|
|
|
|
vt->texture->initWithString(vt->m_strText.c_str(),
|
2012-11-16 14:23:14 +08:00
|
|
|
vt->m_strFontName.c_str(),
|
|
|
|
vt->m_fFontSize,
|
|
|
|
vt->m_size,
|
|
|
|
vt->m_alignment,
|
|
|
|
vt->m_vAlignment
|
|
|
|
);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-04-24 15:02:18 +08:00
|
|
|
case kImage:
|
|
|
|
{
|
2012-08-08 18:39:33 +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;
|
|
|
|
}
|
2012-08-31 03:02:05 +08:00
|
|
|
vt->texture->setTexParameters(&vt->m_texParams);
|
2011-11-25 10:48:05 +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
|
|
|
|