CCTextureCache是用于缓存纹理的类,最常用的就是addImage了,下面简单分析下这部分的代码。
CCTexture2D * CCTextureCache::addImage(const char * path)
{
CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
CCTexture2D * texture = NULL;
CCImage* pImage = NULL;
// Split up directory and filename
// MUTEX:
// Needed since addImageAsync calls this method from a different thread
//pthread_mutex_lock(m_pDictLock);
// 使用文件完整路径作为tex的key,保证了key的唯一性,也可防止多次加载同一张图,但是少了点灵活性
// 如果想使用自定义的key,可以自己搞个映射
std::string pathKey = path;
pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
if (pathKey.size() == 0)
{
return NULL;
}
texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str());
std::string fullpath = pathKey; // (CCFileUtils::sharedFileUtils()->fullPat