[Cocos2d-x] can display online pictures of CCSprite, cocos2d-xccsprite
The CCSprite of online images can be displayed, and the image data is requested asynchronously using HttpClient. when the request is successful, the image data is saved to the local device, and then the CCSprite texture is updated, the default image is displayed during download (you can set the default image ).
OnlineImageSprite. h
# Ifndef _ ONLINEIMAGESPRITE_H __# define _ ONLINEIMAGESPRITE_H __# include "cocos2d. h "USING_NS_CC; # include" cocos-ext.h "USING_NS_CC_EXT; // CCSpriteclass OnlineImageSprite: public CCSprite {public: /* create a Sprite * defaultImagePath that displays online images (displayed when the image is loaded) * url image url */static OnlineImageSprite * create (const char * defaultImagePath, const char * url); bool init (const char * defaultImagePath, const char * url); void onDownloadCompleted (CCHttpClient * sender, CCHttpResponse * response); protected: // download the image void downloadImage (const char * url); // obtain the image path std: string getImagePath (const char * url ); // whether the image has bool isExist (const char * url); // Save the image std: string saveImage (const std: string & data); private: const char * m_url;}; # endif
OnlineImageSprite. cpp
# Include "OnlineImageSprite. h "# include <string> using namespace std; string & replace_all (string & str, const string & old_value, const string & new_value) {while (true) {string :: size_type pos (0); if (pos = str. find (old_value ))! = String: npos) str. replace (pos, old_value.length (), new_value); else break;} return str;} string & replace_all_distinct (string & str, const string & old_value, const string & new_value) {for (string: size_type pos (0); pos! = String: npos; pos + = new_value.length () {if (pos = str. find (old_value, pos ))! = String: npos) str. replace (pos, old_value.length (), new_value); else break;} return str;} OnlineImageSprite * OnlineImageSprite: create (const char * defaultImagePath, const char * url) {OnlineImageSprite * pSprite = new OnlineImageSprite (); if (pSprite & pSprite-> init (defaultImagePath, url) {pSprite-> autorelease (); return pSprite ;} CC_SAFE_DELETE (pSprite); return NULL;} bool OnlineImageSprite: init (const ch Ar * defaultImagePath, const char * url) {m_url = url; CCTexture2D * pTexture = NULL; // whether the image exists, if the default texture does not exist, download the image if (isExist (url) {const char * path = getImagePath (url ). c_str (); pTexture = CCTextureCache: sharedTextureCache ()-> addImage (path);} else {pTexture = CCTextureCache: sharedTextureCache ()-> addImage (region ); // download the image downloadImage (url);} // initialize if (CCSprite: initWithTexture (pTexture) {return true ;} Return false;} // determine whether the image already exists bool OnlineImageSprite: isExist (const char * url) {std: string path = getImagePath (url); return CCFileUtils :: sharedFileUtils ()-> isFileExist (path);} // obtain the full path of the image std: string OnlineImageSprite: getImagePath (const char * url) {std: string urlStr = url; replace_all (urlStr, "/", ""); replace_all (urlStr, "\", ""); replace_all (urlStr ,":",""); replace_all (urlStr ,". "," "); return CCFileUtils: shar EdFileUtils ()-> getWritablePath (). append ("/"). append (urlStr);} // download the image void OnlineImageSprite: downloadImage (const char * url) {// initiate an http request to download the image CCHttpRequest * request = new CCHttpRequest (); request-> setUrl (url); request-> setRequestType (CCHttpRequest: kHttpGet); request-> setResponseCallback (this, callback (OnlineImageSprite: onDownloadCompleted); CCHttpClient :: getInstance ()-> send (request); reque St-> release ();} // request callback void OnlineImageSprite: onDownloadCompleted (CCHttpClient * sender, CCHttpResponse * response) {if (! Response) {return ;}// return Code int statusCode = response-> getResponseCode (); char statusString [64] ={}; sprintf (statusString, "HTTP Status Code: % d ", statusCode); CCLog (" response code: % d ", statusCode); // if (! Response-> isSucceed () {CCLog ("response failed"); CCLog ("error buffer: % s", response-> getErrorBuffer (); return ;} // dump datastd: vector <char> * buffer = response-> getResponseData (); std: string data (buffer-> begin (), buffer-> end (); std: string path = saveImage (data); // if the image is saved successfully, update the texture if (path! = "") {CCTexture2D * pTexture = CCTextureCache: sharedTextureCache ()-> addImage (path. c_str (); if (pTexture) {setTexture (pTexture) ;}}// Save the picture std: string OnlineImageSprite: saveImage (const std: string & data) {std: string path = this-> getImagePath (m_url); FILE * file = fopen (path. c_str (), "wb"); if (file) {// 1. buff // 2. number of bytes written each time // 3. number of writes completed // 4. file handle fwrite (data. c_str (), 1, data. length (), file); fclose (file); return path;} return "";}
Example:
// Create OnlineImageSpirte. Parameter: 1. default image Path 2. urlCCSprite * pSprite = OnlineImageSprite: create ("HelloWorld.png", "http://cc.cocimg.com/cocos2dx/image/logo.png"); // set the position pSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // Add to Layerthis-> addChild (pSprite, 0 );
Project address: https://coding.net/u/linchaolong/p/OnlineImageSprite/git