【Cocos2d-x】可以显示在线图片的CCSprite

前端之家收集整理的这篇文章主要介绍了【Cocos2d-x】可以显示在线图片的CCSprite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


可以显示在线图片的CCSprite,使用HttpClient异步请求图片数据,当请求成功时,保存图片数据到本地,然后更新CCSprite的纹理,下载中时显示默认图片(可以设置默认图片)。


OnlineImageSprite.h

  1. #ifndef __ONLINEIMAGESPRITE_H__
  2. #define __ONLINEIMAGESPRITE_H__
  3.  
  4. #include "cocos2d.h"
  5. USING_NS_CC;
  6. #include "cocos-ext.h"
  7. USING_NS_CC_EXT;
  8.  
  9. // 可以显示在线图片的CCSprite
  10. class OnlineImageSprite:public CCSprite{
  11.  
  12. public:
  13. /*
  14. 创建一个显示在线图片的Sprite
  15. * defaultImagePath 默认图片路径(当图片加载中时显示
  16. * url 图片url
  17. */
  18. static OnlineImageSprite* create(const char* defaultImagePath,const char* url);
  19.  
  20. bool init(const char* defaultImagePath,const char* url);
  21.  
  22. void onDownloadCompleted(CCHttpClient *sender,CCHttpResponse *response);
  23. protected:
  24. // 下载图片
  25. void downloadImage(const char* url);
  26. // 获取图片路径
  27. std::string getImagePath(const char* url);
  28. // 图片是否存在
  29. bool isExist(const char* url);
  30. // 保存图片
  31. std::string saveImage(const std::string& data);
  32. private:
  33. const char* m_url;
  34. };
  35.  
  36. #endif

OnlineImageSprite.cpp

  1. #include "OnlineImageSprite.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. string& replace_all(string& str,const string& old_value,const string& new_value)
  6. {
  7. while(true) {
  8. string::size_type pos(0);
  9. if( (pos=str.find(old_value))!=string::npos )
  10. str.replace(pos,old_value.length(),new_value);
  11. else break;
  12. }
  13. return str;
  14. }
  15. string& replace_all_distinct(string& str,const string& new_value)
  16. {
  17. for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) {
  18. if( (pos=str.find(old_value,pos))!=string::npos )
  19. str.replace(pos,new_value);
  20. else break;
  21. }
  22. return str;
  23. }
  24.  
  25. OnlineImageSprite* OnlineImageSprite::create(const char* defaultImagePath,const char* url){
  26. OnlineImageSprite* pSprite = new OnlineImageSprite();
  27. if (pSprite && pSprite->init(defaultImagePath,url))
  28. {
  29. pSprite->autorelease();
  30. return pSprite;
  31. }
  32. CC_SAFE_DELETE(pSprite);
  33. return NULL;
  34. }
  35.  
  36. bool OnlineImageSprite::init(const char* defaultImagePath,const char* url){
  37. m_url = url;
  38. CCTexture2D* pTexture = NULL;
  39.  
  40. // 图片是否存在,如果不存在使用默认纹理并下载图片
  41. if (isExist(url))
  42. {
  43. const char* path = getImagePath(url).c_str();
  44. pTexture = CCTextureCache::sharedTextureCache()->addImage(path);
  45. }else{
  46. pTexture = CCTextureCache::sharedTextureCache()->addImage(defaultImagePath);
  47. // 下载图片
  48. downloadImage(url);
  49. }
  50. // 初始化
  51. if (CCSprite::initWithTexture(pTexture))
  52. {
  53. return true;
  54. }
  55. return false;
  56. }
  57.  
  58. //判断图片是否已经存在
  59. bool OnlineImageSprite::isExist(const char* url){
  60. std::string path = getImagePath(url);
  61. return CCFileUtils::sharedFileUtils()->isFileExist(path);
  62. }
  63.  
  64. //获取图片全路径
  65. std::string OnlineImageSprite::getImagePath(const char* url){
  66. std::string urlStr = url;
  67. replace_all(urlStr,"/","");
  68. replace_all(urlStr,"\\",":",".","");
  69. return CCFileUtils::sharedFileUtils()->getWritablePath().append("/").append(urlStr);
  70. }
  71.  
  72. // 下载图片
  73. void OnlineImageSprite::downloadImage(const char* url){
  74. // 发起http请求,下载图片
  75. CCHttpRequest* request = new CCHttpRequest();
  76. request->setUrl(url);
  77. request->setRequestType(CCHttpRequest::kHttpGet);
  78. request->setResponseCallback(this,httpresponse_selector(OnlineImageSprite::onDownloadCompleted));
  79. CCHttpClient::getInstance()->send(request);
  80. request->release();
  81. }
  82.  
  83. // 请求回调
  84. void OnlineImageSprite::onDownloadCompleted(CCHttpClient *sender,CCHttpResponse *response){
  85. if (!response)
  86. {
  87. return;
  88. }
  89. // 返回码
  90. int statusCode = response->getResponseCode();
  91. char statusString[64] = {};
  92. sprintf(statusString,"HTTP Status Code: %d",statusCode);
  93. CCLog("response code: %d",statusCode);
  94.  
  95. // 请求失败
  96. if (!response->isSucceed())
  97. {
  98. CCLog("response Failed");
  99. CCLog("error buffer: %s",response->getErrorBuffer());
  100. return;
  101. }
  102.  
  103. // dump data
  104. std::vector<char> *buffer = response->getResponseData();
  105. std::string data (buffer->begin(),buffer->end());
  106. std::string path =saveImage(data);
  107. //如果保存图片成功,更新纹理
  108. if (path != "")
  109. {
  110. CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage(path.c_str());
  111. if (pTexture)
  112. {
  113. setTexture(pTexture);
  114. }
  115. }
  116. }
  117.  
  118. // 保存图片
  119. std::string OnlineImageSprite::saveImage(const std::string& data){
  120. std::string path = this->getImagePath(m_url);
  121. FILE* file = fopen(path.c_str(),"wb");
  122. if (file)
  123. {
  124. // 1.buff
  125. // 2.每次写的字节数
  126. // 3.写多少次结束
  127. // 4.文件句柄
  128. fwrite(data.c_str(),1,data.length(),file);
  129. fclose(file);
  130. return path;
  131. }
  132. return "";
  133. }

使用示例:
  1. //创建OnlineImageSpirte,参数:1.默认图片路径 2.在线图片的url
  2. CCSprite* pSprite = OnlineImageSprite::create("HelloWorld.png","http://cc.cocimg.com/cocos2dx/image/logo.png");
  3.  
  4. //设置位置
  5. pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
  6.  
  7. //添加到Layer
  8. this->addChild(pSprite,0);

项目地址:https://coding.net/u/linchaolong/p/OnlineImageSprite/git

猜你在找的Cocos2d-x相关文章