【麦可网】Cocos2d-X跨平台游戏开发---学习笔记
第十八课:Cocos2D-X缓存机制1-4
=======================================================================================================================================================================
课程目标:
-Cocos2D-X缓存机制
课程重点:
-Cocos2D-X纹理缓存
-Cocos2D-X精灵帧缓存
-Cocos2D-X动画缓存
考核目标:
-Cocos2D-X纹理缓存
-Cocos2D-X精灵帧缓存
-Cocos2D-X动画缓存
-TexturePacker图片拼合
=======================================================================================================================================================================
一、纹理缓存
CCSprite---CCTexture2D---CCTextureCache ---------------------createWithTexture------------------------- bool CCSprite::initWithFile(const char *pszFilename) { CCAssert(pszFilename != NULL,"Invalid filename for sprite"); CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename); if (pTexture) { 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) { 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); 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()->fullPathFromRelativePath(path)); if (! texture) { 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 == pImage); bool bRet = pImage->initWithImageFile(fullpath.c_str(),eImageFormat); CC_BREAK_IF(!bRet); texture = new CCTexture2D(); if( texture && texture->initWithImage(pImage) ) { #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture,fullpath.c_str(),eImageFormat); #endif m_pTextures->setObject(texture,pathKey.c_str()); texture->release(); } else { CCLOG("cocos2d: Couldn't create texture for file:%s in CCTextureCache",path); } } } while (0); } CC_SAFE_RELEASE(pImage); //pthread_mutex_unlock(m_pDictLock); 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(sprite); }
二、图片拼合
CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); spriteFrameCache->addSpriteFramesWithFile("hy.plist"); CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create("hy.png"); this->addChild(spriteBatch,1); spriteBatch->setPosition(CCPointZero); for (int i=1; i<6; i++) { char str[50] = {0}; sprintf(str,"image %d.png",i); CCSprite* sprite = CCSprite::createWithSpriteFrameName(str); sprite->setPosition(ccp(CCRANDOM_0_1()*480,CCRANDOM_0_1()*320)); spriteBatch->addChild(sprite); }
===================================================================
总结:
理解了帧缓存,就知道怎么优化动画显示了。
开心一刻:
一个小偷偷了别人一只鸡,当他正在小河边拔毛的时候,鸡的主人来了。
小偷吓得赶紧把鸡扔进小河里,鸡的主人看了看他,又看了看河里,问道:“河里 面是什么?”
小偷回答:“是一只鸡在洗澡。”
那人又问:“那为什么你在这里?”
小偷结结巴巴的说:“我在帮鸡看衣服。”
【麦可网】Cocos2d-X跨平台游戏开发---教程下载:http://pan.baidu.com/s/1kTio1Av
【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu