@H_
673_138@CCSprite---CCTexture2D---CCTextureCache
---------------------createWithTexture-------------------------
bool CCSprite::initWithFile(
const char *pszFile
Name)
{
C
Cassert(pszFil
ename != NULL,"Invalid fil
ename for sprite"
);
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFile
Name);
if (pTextur
E)
{
CCRect rect = CCRectZero;
rect.size = pTexture->getContentSize(
);
return initWithTexture(pTexture,rect
);
}
// don't release here.
// when load texture failed,it's better to get a "transparent" sprite than a crashed program
// this->release(
);
return
false;
}
---------------------纹理缓存-------------------------
CCTexture2D * CCTextureCache::addImage(
const char * path)
{
C
Cassert(path
!= NULL,"TextureCache: fileimage MUST not be NULL"
);
CCTexture2D * texture = NULL;
CCImage* pImage = NULL;
// Split up directory and fil
ename
// MUTEX:
// Needed since addImageAsync calls this method from a different thread
//pthread_mutex_lock(m_p
DictLock
);
std:
:string pathKey = path;
pathKey = CCFileUtils::sharedFileUtils()->fullPathForFil
ename(pathKey.c_str()
);
if (pathKey.size() == 0)
{
return NULL;
}
//纹理字典:判断纹理是否被加载
texture = (CCTexture2D*)m_pTextures->ob
jectForKey(pathKey.c_str()
);
std:
:string fullpath = pathKey; // (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(path)
);
if (! textur
E)
{
std:
:string lowerCase(pathKey
);
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()
);
}
else if (std:
:string::npos
!= lowerCase.find(".pkm"))
{
// ETC1 file format,only supportted on Android
texture = this->addETCImage(fullpath.c_str()
);
}
else
{
CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
if (std:
:string::npos
!= lowerCase.find(".png"))
{
eImageFormat = CCImage::kFmtPng;
}
else if (std:
:string::npos
!= lowerCase.find(".jpg") || std:
:string::npos
!= lowerCase.find(".jpeg"))
{
eImageFormat = CCImage::kFmtJpg;
}
else if (std:
:string::npos
!= lowerCase.find(".tif") || std:
:string::npos
!= lowerCase.find(".tiff"))
{
eImageFormat = CCImage::kFmtTiff;
}
else if (std:
:string::npos
!= lowerCase.find(".webp"))
{
eImageFormat = CCImage::kFmtWebp;
}
pImage = new CCImage(
);
CC_BREAK_IF(NULL == pImag
E);
bool bRet = pImage->initWithImageFile(fullpath.c_str(),eImageFormat
);
CC_BREAK_IF(!bRet
);
texture = new CCTexture2D(
);
if( texture &&
texture->initWithImage(pImag
E) )
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture file name
VolatileTexture::addImageTexture(texture,fullpath.c_str(),eImageFormat
);
#endif
m_pTextures->setOb
ject(texture,pathKey.c_str()
);
texture->release(
);
}
else
{
CCLOG("cocos2d: Couldn't create texture for file:%s in CCTextureCache",path
);
}
}
} while (0
);
}
CC_SAFE_RELEASE(pImag
E);
//pthread_mutex_unlock(m_p
DictLock
);
return texture;
}
-----------------------纹理缓存的体现------------------------
低效率:
for (int i
=0; i<1000; i++)
{
CCSprite* sprite_flower = CCSprite::create("hua.png"
);
sprite_flower->setPosition(ccp(CCRANDOM_0_1()*480,CCRANDOM_0_1()*320)
);
this->addChild(sprite_flower,1
);
}
高效率:
CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create("hua.png"
);
this->addChild(spriteBatch,1
);
spriteBatch->setPosition(CCPointZero
);
for (int i
=0; i<5000; i++)
{
CCSprite* sprite = CCSprite::createWithTexture(spriteBatch->getTexture()
);
sprite->setPosition(ccp(CCRANDOM_0_1()*480,CCRANDOM_0_1()*320)
);
spriteBatch->addChild(sprit
E);
}
@H_
874_140@
@H_
874_140@
_138@CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache(