【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十八课:Cocos2D-X缓存机制1-4

【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十八课:Cocos2D-X缓存机制1-4,第1张

概述【麦可网】Cocos2d-X跨平台游戏开发---学习笔记 第十八课:Cocos2D-X缓存机制1-4 =====================================================================================================================================================

【麦可网】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

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1062348.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存