2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
#include <string>
|
2010-07-26 17:32:23 +08:00
|
|
|
#include <cctype>
|
2010-07-16 16:28:11 +08:00
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCTexture2D.h"
|
|
|
|
#include "ccMacros.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCData.h"
|
2010-07-30 16:35:07 +08:00
|
|
|
#include "CCDirector.h"
|
2010-08-25 14:14:31 +08:00
|
|
|
#include "platform/platform.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCFileUtils.h"
|
|
|
|
#include "CCImage.h"
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-11-22 18:09:38 +08:00
|
|
|
namespace cocos2d {
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCAsyncObject : CCObject
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
fpAsyncCallback m_pfnCallback;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject* m_pTarget;
|
2010-07-20 13:49:13 +08:00
|
|
|
std::string * m_pData;
|
2010-07-16 16:28:11 +08:00
|
|
|
public:
|
|
|
|
CCAsyncObject();
|
|
|
|
~CCAsyncObject()
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d: deallocing CCAsyncObject.");
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(m_pTarget);
|
|
|
|
CC_SAFE_DELETE(m_pData);
|
2010-07-16 16:28:11 +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
|
|
|
|
static CCTextureCache *g_sharedTextureCache;
|
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
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
if (!g_sharedTextureCache)
|
|
|
|
g_sharedTextureCache = new CCTextureCache();
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-16 16:28:11 +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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(g_sharedTextureCache == NULL, "Attempted to allocate a second instance of a singleton.");
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
m_pTextures = new CCMutableDictionary<std::string, CCTexture2D*>();
|
2011-03-09 14:40:05 +08:00
|
|
|
m_pDictLock = new CCLock();
|
|
|
|
m_pContextLock = new CCLock();
|
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
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
CCLOG("cocos2d: deallocing CCTextureCache.");
|
2010-09-01 12:02:36 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE(m_pTextures);
|
|
|
|
CC_SAFE_DELETE(m_pDictLock);
|
|
|
|
CC_SAFE_DELETE(m_pContextLock);
|
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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(g_sharedTextureCache);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-06 16:05:19 +08:00
|
|
|
char * CCTextureCache::description()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-08-06 16:05:19 +08:00
|
|
|
char *ret = new char[100];
|
2010-09-03 11:50:04 +08:00
|
|
|
sprintf(ret, "<CCTextureCache | Number of textures = %u>", m_pTextures->count());
|
2010-07-21 11:13:32 +08:00
|
|
|
return ret;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureCache - Add Images
|
2010-09-28 12:14:12 +08:00
|
|
|
/* @todo EAGLContext
|
2010-07-16 16:28:11 +08:00
|
|
|
void CCTextureCache::addImageWithAsyncObject(CCAsyncObject* async)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-09-28 12:14:12 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAutoreleasePool *autoreleasepool = [[CCAutoreleasePool alloc] init];
|
2010-07-16 14:15:06 +08:00
|
|
|
|
|
|
|
// textures will be created on the main OpenGL context
|
|
|
|
// it seems that in SDK 2.2.x there can't be 2 threads creating textures at the same time
|
|
|
|
// the lock is used for this purpose: issue #472
|
|
|
|
[contextLock lock];
|
|
|
|
if( auxEAGLcontext == nil ) {
|
|
|
|
auxEAGLcontext = [[EAGLContext alloc]
|
|
|
|
initWithAPI:kEAGLRenderingAPIOpenGLES1
|
|
|
|
sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]];
|
|
|
|
|
|
|
|
if( ! auxEAGLcontext )
|
|
|
|
CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");
|
|
|
|
}
|
|
|
|
|
|
|
|
if( [EAGLContext setCurrentContext:auxEAGLcontext] ) {
|
|
|
|
|
|
|
|
// load / create the texture
|
|
|
|
CCTexture2D *tex = [self addImage:async.data];
|
|
|
|
|
|
|
|
// The callback will be executed on the main thread
|
|
|
|
[async.target performSelectorOnMainThread:async.selector withObject:tex waitUntilDone:NO];
|
|
|
|
|
|
|
|
[EAGLContext setCurrentContext:nil];
|
|
|
|
} else {
|
|
|
|
CCLOG(@"cocos2d: TetureCache: EAGLContext error");
|
|
|
|
}
|
|
|
|
[contextLock unlock];
|
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
[autoreleasepool release];
|
|
|
|
}*/
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
/* @todo selector, NSThread
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCTextureCache::addImageAsync(const char* filename, CCObject *target, fpAsyncCallback func)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(filename != NULL , "TextureCache: fileimage MUST not be nill");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
|
|
|
// optimization
|
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
CCTexture2D * tex;
|
|
|
|
|
|
|
|
if ( (tex = m_pTextures->objectForKey(filename)) )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
target->
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( (tex=[textures objectForKey: filename] ) ) {
|
|
|
|
[target performSelector:selector withObject:tex];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// schedule the load
|
|
|
|
|
|
|
|
CCAsyncObject *asyncObject = [[CCAsyncObject alloc] init];
|
|
|
|
asyncObject.selector = selector;
|
|
|
|
asyncObject.target = target;
|
|
|
|
asyncObject.data = filename;
|
|
|
|
|
|
|
|
[NSThread detachNewThreadSelector:@selector(addImageWithAsyncObject:) toTarget:self withObject:asyncObject];
|
|
|
|
[asyncObject release];
|
|
|
|
}*/
|
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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
CCTexture2D * texture = NULL;
|
2010-09-08 09:28:28 +08:00
|
|
|
// Split up directory and filename
|
2010-07-16 14:15:06 +08:00
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
2010-07-26 17:32:23 +08:00
|
|
|
|
|
|
|
m_pDictLock->lock();
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
// remove possible -HD suffix to prevent caching the same image twice (issue #1040)
|
2011-03-09 12:00:04 +08:00
|
|
|
std::string pathKey = path;
|
2011-02-18 18:30:22 +08:00
|
|
|
CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);
|
2010-12-30 17:30:11 +08:00
|
|
|
|
2011-03-11 11:04:22 +08:00
|
|
|
pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());
|
2011-02-18 18:30:22 +08:00
|
|
|
texture = m_pTextures->objectForKey(pathKey);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-03-11 11:04:22 +08:00
|
|
|
std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));
|
2010-09-01 12:02:36 +08:00
|
|
|
if( ! texture )
|
|
|
|
{
|
2010-09-25 10:28:27 +08:00
|
|
|
std::string lowerCase(path);
|
|
|
|
for (unsigned int i = 0; i < lowerCase.length(); ++i)
|
2010-09-01 12:02:36 +08:00
|
|
|
{
|
2010-09-25 10:28:27 +08:00
|
|
|
lowerCase[i] = tolower(lowerCase[i]);
|
2010-09-01 12:02:36 +08:00
|
|
|
}
|
2010-09-25 10:28:27 +08:00
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
|
|
|
// if ( [[path lowercaseString] hasSuffix:@".pvr"] )
|
2010-10-12 11:23:47 +08:00
|
|
|
do
|
2010-08-02 10:58:00 +08:00
|
|
|
{
|
2010-10-12 11:23:47 +08:00
|
|
|
if (std::string::npos != lowerCase.find(".pvr"))
|
|
|
|
{
|
2010-08-31 16:53:25 +08:00
|
|
|
#ifdef _POWERVR_SUPPORT_
|
2010-10-12 11:23:47 +08:00
|
|
|
texture = this->addPVRTCImage(fullpath.c_str());
|
2010-08-31 16:53:25 +08:00
|
|
|
#endif
|
2010-09-26 10:09:05 +08:00
|
|
|
}
|
2010-11-22 18:09:38 +08:00
|
|
|
// Issue #886: TEMPORARY FIX FOR TRANSPARENT JPEGS IN IOS4
|
2010-10-12 11:23:47 +08:00
|
|
|
else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg"))
|
2010-09-27 17:26:35 +08:00
|
|
|
{
|
2011-03-29 17:32:52 +08:00
|
|
|
CCImage image;
|
2011-03-29 11:41:44 +08:00
|
|
|
CCFileData data(fullpath.c_str(), "rb");
|
|
|
|
unsigned long nSize = data.getSize();
|
|
|
|
unsigned char* pBuffer = data.getBuffer();
|
2011-03-29 17:32:52 +08:00
|
|
|
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg));
|
|
|
|
|
2010-10-12 11:23:47 +08:00
|
|
|
texture = new CCTexture2D();
|
2011-03-29 17:32:52 +08:00
|
|
|
texture->initWithImage(&image);
|
2010-10-12 11:23:47 +08:00
|
|
|
|
|
|
|
if( texture )
|
|
|
|
{
|
2011-02-18 18:30:22 +08:00
|
|
|
m_pTextures->setObject(texture, pathKey);
|
2010-10-12 11:23:47 +08:00
|
|
|
texture->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
|
|
|
|
}
|
2010-09-27 17:26:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-22 18:09:38 +08:00
|
|
|
//# work around for issue #910
|
|
|
|
#if 0
|
|
|
|
UIImage *image = [UIImage imageNamed:path];
|
|
|
|
tex = [ [CCTexture2D alloc] initWithImage: image ];
|
|
|
|
#else
|
2010-10-12 11:23:47 +08:00
|
|
|
// prevents overloading the autorelease pool
|
2011-03-29 17:32:52 +08:00
|
|
|
CCImage image;
|
2011-03-29 11:41:44 +08:00
|
|
|
CCFileData data(fullpath.c_str(), "rb");
|
|
|
|
unsigned long nSize = data.getSize();
|
|
|
|
unsigned char* pBuffer = data.getBuffer();
|
2011-03-29 17:32:52 +08:00
|
|
|
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng));
|
|
|
|
|
2010-10-12 11:23:47 +08:00
|
|
|
texture = new CCTexture2D();
|
2011-03-29 17:32:52 +08:00
|
|
|
texture->initWithImage(&image);
|
2010-11-22 18:09:38 +08:00
|
|
|
#endif
|
2010-10-12 11:23:47 +08:00
|
|
|
if( texture )
|
|
|
|
{
|
2011-02-18 18:30:22 +08:00
|
|
|
m_pTextures->setObject(texture, pathKey);
|
2010-10-12 11:23:47 +08:00
|
|
|
texture->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
|
|
|
|
}
|
2010-08-26 17:22:23 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-10-12 11:23:47 +08:00
|
|
|
} while (0);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
2010-08-04 15:46:12 +08:00
|
|
|
m_pDictLock->unlock();
|
2010-08-06 14:32:32 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-31 16:53:25 +08:00
|
|
|
#ifdef _POWERVR_SUPPORT_
|
2010-07-22 11:15:25 +08:00
|
|
|
CCTexture2D* CCTextureCache::addPVRTCImage(const char* path, int bpp, bool hasAlpha, int width)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be nill");
|
|
|
|
CCAssert( bpp==2 || bpp==4, "TextureCache: bpp must be either 2 or 4");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
CCTexture2D * texture;
|
2010-07-26 17:32:23 +08:00
|
|
|
std::string temp(path);
|
2010-08-06 14:32:32 +08:00
|
|
|
if ( (texture = m_pTextures->objectForKey(temp)) )
|
2010-08-02 10:58:00 +08:00
|
|
|
{
|
2010-08-06 14:32:32 +08:00
|
|
|
return texture;
|
2010-08-02 10:58:00 +08:00
|
|
|
}
|
2010-07-26 17:32:23 +08:00
|
|
|
|
2010-07-16 14:15:06 +08:00
|
|
|
// Split up directory and filename
|
2010-07-26 17:32:23 +08:00
|
|
|
std::string fullpath( CCFileUtils::fullPathFromRelativePath(path) );
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCData * data = CCData::dataWithContentsOfFile(fullpath);
|
2010-08-06 14:32:32 +08:00
|
|
|
texture = new CCTexture2D();
|
|
|
|
texture->initWithPVRTCData(data->bytes(), 0, bpp, hasAlpha, width);
|
|
|
|
if( texture )
|
2010-09-08 14:52:56 +08:00
|
|
|
{
|
2010-08-06 14:32:32 +08:00
|
|
|
m_pTextures->setObject(texture, temp);
|
2010-09-08 14:52:56 +08:00
|
|
|
texture->autorelease();
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
else
|
2010-09-08 14:52:56 +08:00
|
|
|
{
|
2010-07-26 17:32:23 +08:00
|
|
|
CCLOG("cocos2d: Couldn't add PVRTCImage:%s in CCTextureCache",path);
|
2010-09-08 14:52:56 +08:00
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(data);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-22 11:15:25 +08:00
|
|
|
CCTexture2D * CCTextureCache::addPVRTCImage(const char* fileimage)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(fileimage != NULL, "TextureCache: fileimage MUST not be nill");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
CCTexture2D * texture;
|
2010-07-22 11:20:52 +08:00
|
|
|
std::string key(fileimage);
|
2010-08-06 14:32:32 +08:00
|
|
|
if( (texture = m_pTextures->objectForKey(key)) )
|
2010-07-21 11:13:32 +08:00
|
|
|
{
|
2010-08-06 14:32:32 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
texture = new CCTexture2D();
|
|
|
|
texture = texture->initWithPVRTCFile(fileimage);
|
|
|
|
if( texture )
|
2010-09-08 14:52:56 +08:00
|
|
|
{
|
2010-08-06 14:32:32 +08:00
|
|
|
m_pTextures-> setObject( texture, key);
|
2010-09-08 14:52:56 +08:00
|
|
|
texture->autorelease();
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
else
|
2010-09-08 14:52:56 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add PVRTCImage:%s in CCTextureCache",fileimage);
|
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
2010-08-31 16:53:25 +08:00
|
|
|
#endif
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
/* @todo CGImageRef
|
2010-07-20 13:49:13 +08:00
|
|
|
-(CCTexture2D*) addCGImage: (CGImageRef) imageref forKey: (string & )key
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(imageref != nil, @"TextureCache: image MUST not be nill");
|
2010-07-16 14:15:06 +08:00
|
|
|
|
|
|
|
CCTexture2D * tex = nil;
|
|
|
|
|
|
|
|
// If key is nil, then create a new texture each time
|
|
|
|
if( key && (tex=[textures objectForKey: key] ) ) {
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevents overloading the autorelease pool
|
|
|
|
UIImage *image = [[UIImage alloc] initWithCGImage:imageref];
|
|
|
|
tex = [[CCTexture2D alloc] initWithImage: image];
|
|
|
|
[image release];
|
|
|
|
|
|
|
|
if(tex && key)
|
|
|
|
[textures setObject: tex forKey:key];
|
|
|
|
else
|
|
|
|
CCLOG(@"cocos2d: Couldn't add CGImage in CCTextureCache");
|
|
|
|
|
|
|
|
return [tex autorelease];
|
2010-07-16 16:28:11 +08:00
|
|
|
}*/
|
2011-03-07 17:11:57 +08:00
|
|
|
CCTexture2D* CCTextureCache::addUIImage(CCImage *image, const char *key)
|
2010-08-26 14:06:41 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(image != NULL && key != NULL, "TextureCache: image MUST not be nill");
|
2010-08-26 14:06:41 +08:00
|
|
|
|
|
|
|
CCTexture2D * texture = NULL;
|
2010-09-08 09:28:28 +08:00
|
|
|
std::string forKey = key;
|
2010-08-26 14:06:41 +08:00
|
|
|
|
2010-10-12 11:23:47 +08:00
|
|
|
m_pDictLock->lock();
|
2010-08-26 14:06:41 +08:00
|
|
|
|
2010-10-12 11:23:47 +08:00
|
|
|
do
|
2010-08-26 14:06:41 +08:00
|
|
|
{
|
2010-10-12 11:23:47 +08:00
|
|
|
// If key is nil, then create a new texture each time
|
|
|
|
if(texture = m_pTextures->objectForKey(forKey))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevents overloading the autorelease pool
|
|
|
|
texture = new CCTexture2D();
|
|
|
|
texture->initWithImage(image);
|
|
|
|
|
|
|
|
if(texture)
|
|
|
|
{
|
|
|
|
m_pTextures->setObject(texture, forKey);
|
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't add UIImage in CCTextureCache");
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
m_pDictLock->unlock();
|
|
|
|
|
2010-08-26 14:06:41 +08:00
|
|
|
return texture;
|
|
|
|
}
|
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
|
|
|
{
|
2010-07-21 17:40:10 +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
|
|
|
{
|
2010-07-30 16:35:07 +08:00
|
|
|
std::vector<std::string> keys = m_pTextures->allKeys();
|
|
|
|
std::vector<std::string>::iterator it;
|
2010-11-22 18:09:38 +08:00
|
|
|
for (it = keys.begin(); it != keys.end(); it++)
|
2010-08-02 10:58:00 +08:00
|
|
|
{
|
|
|
|
CCTexture2D *value = m_pTextures->objectForKey(*it);
|
|
|
|
if (value->retainCount() == 1)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", (*it).c_str());
|
|
|
|
m_pTextures->removeObjectForKey(*it);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2010-08-06 14:32:32 +08:00
|
|
|
if( ! texture )
|
2010-07-16 14:15:06 +08:00
|
|
|
return;
|
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
std::vector<std::string> keys = m_pTextures->allKeysForObject(texture);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
for (unsigned int i = 0; i < keys.size(); i++)
|
|
|
|
{
|
|
|
|
m_pTextures->removeObjectForKey(keys[i]);
|
|
|
|
}
|
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
|
|
|
{
|
2010-12-30 17:30:11 +08:00
|
|
|
if (textureKeyName == NULL)
|
|
|
|
{
|
2010-07-16 14:15:06 +08:00
|
|
|
return;
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
m_pTextures->removeObjectForKey(string(textureKeyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2D* CCTextureCache::textureForKey(const char* key)
|
|
|
|
{
|
|
|
|
return m_pTextures->objectForKey(string(key));
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-11-22 18:09:38 +08:00
|
|
|
}//namespace cocos2d
|
2010-07-16 16:28:11 +08:00
|
|
|
|