Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十八课:Cocos2D-X缓存机制1-4大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

【麦可网】Cocos2d-X@H_944_4@跨平台游戏开发---@H_944_4@学习笔记

第十八课:Cocos2D-X@H_944_4@缓存机制1-4

=======================================================================================================================================================================

课程目标:

-Cocos2D-X@H_944_4@缓存机制

课程重点:

-Cocos2D-X@H_944_4@纹理缓存

-Cocos2D-X@H_944_4@精灵帧缓存

-Cocos2D-X@H_944_4@动画缓存

核目标:

-Cocos2D-X@H_944_4@纹理缓存

-Cocos2D-X@H_944_4@精灵帧缓存

-Cocos2D-X@H_944_4@动画缓存

-TexturePacker@H_944_4@图片拼合

@H_944_4@

@H_944_4@

@H_944_4@

@H_944_4@

@H_944_4@

@H_944_4@

=======================================================================================================================================================================

一、纹理缓存

@H_673_138@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); } @H_874_140@ @H_874_140@

二、图片拼合

@H_673_138@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); } @H_874_140@ @H_874_140@

===================================================================

总结:

理解了帧缓存,就知道怎么优化动画显示了。

@H_874_140@

开心一刻:

一个小偷偷了别人一只鸡,当他正在小河边拔毛的时候,鸡的主人来了。

  小偷吓得赶紧把鸡扔进小河里,鸡的主人看了看他,又看了看河里,问道:“河里 面是什么?”

  小偷回答:“是一只鸡在洗澡。”

  那人又问:“那为什么你在这里?”

  小偷结结巴巴的说:“我在帮鸡看衣服。”

【麦可网】Cocos2d-X@H_944_4@跨平台游戏开发---教程下载:http://pan.baidu.com/s/1kTio1Av

【麦可网】Cocos2d-X@H_944_4@跨平台游戏开发---@H_944_4@笔记系列:http://blog.csdn.net/qiulanzhu

大佬总结

以上是大佬教程为你收集整理的【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十八课:Cocos2D-X缓存机制1-4全部内容,希望文章能够帮你解决【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十八课:Cocos2D-X缓存机制1-4所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。