码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x 3.x使用第三方库播放gif图

时间:2015-09-22 23:42:25      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:

Cocos2d-x 3.x使用第三方库播放gif图

 

效果图:

技术分享

由于kd的项目中需要用到gif,而Cocos2d-x本身是不支持gif播放的。百度 + google 了很久,最终都指向:https://github.com/opentalking/gif-for-cocos2dx

这里,可惜一开始看到的时候是基于2.2的,由于对底层代码不熟悉,根本没办法下手。于是联系原作者,他一个周末就搞出个3.x的版本,大家可以去膜拜下:
地址:https://github.com/opentalking/gif-for-cocos2dx-3.x.git

 

在这里真心要为大神的无私分享精神点个赞~

 

使用方法:

pull下来后,把gif文件夹导入到项目中来,然后就可以像使用Sprite那样使用gif~~~

1
2
3
4
5
6
7
8
9
10
std::string name = "g2.gif";
name = FileUtils::getInstance() -> fullPathForFilename(name.c_str());
GifBase *gif = InstantGif::create(name.c_str());
gif->setPosition(Point(visibleSize.width * 0.5, visibleSize.height * 0.5));
gif -> setScale(2);
this->addChild(gif);
gif2 = CacheGif::create(name.c_str());
gif2->setPosition(Point(500,0));
gif2->setScale(2);
this->addChild(gif2);

用到的类主要是InstantGif 和 CacheGif,两者的使用方法一样,前者是一边播放一边从数据中解析帧,后者是一次性解析完,并放到缓存中,这种方式耗时长,占用内存大(图片帧数太多的话可能会挂),但是相对第一种方式会流畅很多~

Cocos引擎中文官网现面向广大Cocos引擎相关开发者征集优秀教程,欢迎给位童鞋踊跃投稿!来稿请发送至:support@cocos.org。

 

来源网址:http://helkyle.tk/2014/12/11/cocos2dxgif/

 

 

  1 .cpp完整代码
  2 
  3 #include "HelloWorldScene.h"
  4 #include "Gif/GIFMovie.h"
  5 #include "Gif/CacheGif.h"
  6 #include "Gif/InstantGif.h"
  7 
  8 USING_NS_CC;
  9 
 10 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
 11 #define FILE_FORMAT ("/mnt/sdcard/g%d.gif") //On the Android platform, the resources are compressed in the asset directory. Therefore, the resources must be files on the sd card
 12 #else
 13 #define FILE_FORMAT ("g%d.gif")
 14 #endif
 15 
 16 CCScene* HelloWorld::scene()
 17 {
 18     // ‘scene‘ is an autorelease object
 19     CCScene *scene = CCScene::create();
 20     
 21     // ‘layer‘ is an autorelease object
 22     HelloWorld *layer = HelloWorld::create();
 23 
 24     // add layer as a child to scene
 25     scene->addChild(layer);
 26 
 27     // return the scene
 28     return scene;
 29 }
 30 
 31 // on "init" you need to initialize your instance
 32 bool HelloWorld::init()
 33 {
 34     //////////////////////////////
 35     // 1. super init first
 36     if ( !CCLayer::init() )
 37     {
 38         return false;
 39     }
 40       
 41     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 42     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
 43 
 44     /////////////////////////////
 45     // 2. add a menu item with "X" image, which is clicked to quit the program
 46     //    you may modify it.
 47 
 48     // add a "close" icon to exit the progress. it‘s an autorelease object
 49     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
 50         "CloseNormal.png",
 51         "CloseSelected.png",
 52         this,
 53         menu_selector(HelloWorld::menuCloseCallback));
 54 
 55     pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
 56         origin.y + pCloseItem->getContentSize().height/2));
 57 
 58     // create menu, it‘s an autorelease object
 59     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
 60     pMenu->setPosition(CCPointZero);
 61     this->addChild(pMenu, 1);
 62 
 63     return true;
 64 }
 65 
 66 int count = 1;
 67 void HelloWorld::update(float delta)
 68 {
 69     count++ ;
 70     if(count > 240)
 71     {
 72         this->removeAllChildren();
 73     }
 74 }
 75 
 76 void HelloWorld::menuCloseCallback(CCObject* pSender)
 77 {
 78     count++;
 79     while(this->getChildByTag(1000))
 80     {
 81         this->removeChildByTag(1000);
 82     }
 83     CCLOG("%s","------after remove gif-----------");
 84     CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();
 85     if(count % 2 != 0)
 86     {
 87         return ;
 88     }
 89     std::string name = CCString::createWithFormat(FILE_FORMAT,count/2)->getCString();
 90     name = CCFileUtils::sharedFileUtils()->fullPathForFilename(name.c_str());
 91     
 92     GifBase *gif = InstantGif::create(name.c_str());
 93     if(gif == NULL)
 94     {
 95         CCLOG("%s","create gif failed");
 96         return ;
 97     }
 98     gif->setAnchorPoint(ccp(0,0));
 99     this->addChild(gif);
100     gif->setPosition(ccp(0,0));
101     gif->setTag(1000);
102 
103 
104     gif = CacheGif::create(name.c_str());
105     gif->setAnchorPoint(ccp(0,0));
106     this->addChild(gif);
107     gif->setPosition(ccp(500,0));
108     gif->setScale(2);
109     gif->setTag(1000);
110     CCLOG("%s","------after add gif-----------");
111     CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();
112 
113     return ;
114 }

 

在class类中还有Gif文件夹,里面有需要的类

 

Cocos2d-x 3.x使用第三方库播放gif图

标签:

原文地址:http://www.cnblogs.com/dudu580231/p/4830850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!