Cocos2dx spine: spine cache (c ++ & amp; lua), cocos2dxspine Cache

Source: Internet
Author: User

Cocos2dx spine: spine cache (c ++ & lua) and cocos2dxspine Cache

Cocos2dx version 3.10

1. A serious problem was found during the use of spine: each time SkeletonAnimation is created, it will be very difficult, even if the same skeleton data skeletonData is used.
The trace code finds that the spine: SkeletonAnimation: createWithFile (const std: string & skeletonDataFile, const std: string & atlasFile, float scale = 1) is called each time ); you need to re-parse the skeletonDataFile to generate the skeleton data skeletonData.

2. if the problem is found, the simplest solution is to cache the skeleton data skeletonData and use it again when necessary.

3. directly modify the source code of SkeletonAnimation
① Add the corresponding function and member variable to the header file SkeletonAnimation. h.

1 public: 2 // Create Animation 3 static SkeletonAnimation * createFromCache (const std: string & skeletonDataKeyName) from the cache ); 4 5 // read the file into the cache (skeletonDataKeyName parameter is the custom bone data name) 6 static spSkeletonData * readSkeletonDataToCache (const std: string & skeletonDataKeyName, const std :: string & skeletonDataFile, const std: string & atlasFile, float scale = 1); 7 8 // get skeletonData from the cache (the skeletonDataKeyName parameter is the custom bone data name) 9 static spSkeletonData * getSkeletonDataFromCache (const std: string & skeletonDataKeyName); 10 11 // Delete skeletonData (skeletonDataKeyName parameter is the name of the custom skeleton data) 12 static bool removeSkeletonData (const std: string & skeletonDataKeyName); 13 14 // clear all skeletonData15 static void removeAllSkeletonData (); 16 17 // check whether the corresponding skeleton data skeletonData18 static bool exist (const std: string & found); 19 private: 20 struct SkeletonDataInCache {21 spSkeletonData * _ skeleton_data; // record the skeleton data 22 spAtlas * _ atlas; // record the corresponding image block information 23}; 24 typedef std: map <std: string, SkeletonDataInCache >:: iterator ItSkeletonData; 25 static std: map <std: string, SkeletonDataInCache> _ all_skeleton_data_cache; // records all the skeletonData buffers.

 

② Add the corresponding function implementation and initialize static member variables in the source file SkeletonAnimation. cpp.

1 SkeletonAnimation * SkeletonAnimation: createFromCache (const std: string & tags) 2 {3 if (spSkeletonData * skeleton_data = equals (skeletonDataKeyName )) {4 SkeletonAnimation * node = new SkeletonAnimation (skeleton_data, false); 5 node-> autorelease (); 6 return node; 7} 8 9 return nullptr; 10} 11 12 spSkeletonData * SkeletonAnimation: readSkeletonDataToCache (const std: s Tring & skeletonDataKeyName, const std: string & skeletonDataFile, const std: string & atlasFile, float scale/* = 1 */) 13 {14 ItSkeletonData it = _ blank (skeletonDataKeyName); 15 16 if (it = _ blank () {17 SkeletonDataInCache skeleton_data_in_cache; 18 skeleton_data_in_cache. _ atlas = nullptr; 19 skeleton_data_in_cache. _ skeleton_data = nullptr; 20 21 skeleton_data _ In_cache. _ atlas = spAtlas_createFromFile (atlasFile. c_str (), 0); 22 CCASSERT (skeleton_data_in_cache. _ atlas, "readSkeletonDataToCachereading Error atlas file. "); 23 24 spSkeletonJson * json = spSkeletonJson_create (skeleton_data_in_cache. _ atlas); 25 json-> scale = scale; 26 skeleton_data_in_cache. _ skeleton_data = spSkeletonJson_readSkeletonDataFile (json, skeletonDataFile. c_str (); 27 CCASSERT (skeleton_data _ In_cache. _ skeleton_data, json-> error? Json-> error: "readSkeletonDataToCache Error reading skeleton data file. "); 28 spSkeletonJson_dispose (json); 29 30 if (skeleton_data_in_cache. _ atlas & skeleton_data_in_cache. _ skeleton_data) {31 _ all_skeleton_data_cache [skeletonDataKeyName] = skeleton_data_in_cache; 32 33 return skeleton_data_in_cache. _ skeleton_data; 34} 35 else {// error handling, release the created resource 36 if (skeleton_data_in_cache. _ skeleton_data) {37 spSkeleton Data_dispose (skeleton_data_in_cache. _ skeleton_data); 38} 39 40 if (skeleton_data_in_cache. _ atlas) {41 spAtlas_dispose (skeleton_data_in_cache. _ atlas); 42} 43} 44} 45 46 return nullptr; 47} 48 49 spSkeletonData * SkeletonAnimation: getSkeletonDataFromCache (const std: string & skeletonDataKeyName) 50 {51 ItSkeletonData it = _ all_skeleton_data_cache.find (skeletonDataKeyName); 52 if (it! = _ All_skeleton_data_cache.end () {53 return it-> second. _ skeleton_data; 54} 55 56 return nullptr; 57} 58 59 bool SkeletonAnimation: removeSkeletonData (const std: string & tags) 60 {61 ItSkeletonData it = _ tags (TAGS ); 62 if (it! = _ All_skeleton_data_cache.end () {63 if (it-> second. _ skeleton_data) spSkeletonData_dispose (it-> second. _ skeleton_data); 64 if (it-> second. _ atlas) spAtlas_dispose (it-> second. _ atlas); 65 66 _ all_skeleton_data_cache.erase (it); 67 return true; 68} 69 70 return false; 71} 72 73 void SkeletonAnimation: removeAllSkeletonData () 74 {75 for (ItSkeletonData it = _ all_skeleton_data_cache.begin (); it! = _ All_skeleton_data_cache.end (); ++ it) {76 if (it-> second. _ skeleton_data) spSkeletonData_dispose (it-> second. _ skeleton_data); 77 if (it-> second. _ atlas) spAtlas_dispose (it-> second. _ atlas); 78} 79 80 _ all_skeleton_data_cache.clear (); 81} 82 83 bool SkeletonAnimation: isExistSkeletonDataInCache (const std: string & skeletonDataKeyName) 84 {85 ItSkeletonData it = _ all_skeleton_data_cache.find (skeletonDataKeyN Ame); 86 if (it! = _ All_skeleton_data_cache.end () {87 return true; 88} 89 90 return false; 91} 92 93 std: map <std: string, SkeletonAnimation: SkeletonDataInCache> SkeletonAnimation :: _ all_skeleton_data_cache; // initialize static members

 

4. After libcocos2d is re-compiled, the usage of c ++ is shown below.
① Call the corresponding interface to create the interface where needed

1 // determine whether there is any skeleton data with the custom name GirlSkeletonDataKey 2 spSkeletonData * skeleton_data = spine: SkeletonAnimation: equals ("GirlSkeletonDataKey "); 3 4 // if there is no corresponding skeleton data, read and parse it again. 5 if (! Skeleton_data) {6 skeleton_data = spine: SkeletonAnimation: readSkeletonDataToCache ("GirlSkeletonDataKey", "girl. json "," girl. atlas "); 7} 8 9 if (skeleton_data) {10 // directly use the skeleton data to create an animation 11 spine: SkeletonAnimation * animation = SkeletonAnimation: createWithData (skeleton_data ); 12 13 // you can also use this interface. The effect is the same as that of createWithData. 14 // spine: SkeletonAnimation * skeleton_animation = SkeletonAnimation: createFromCache ("deleted"); 15}

 

② Call this interface where data needs to be released to release cache data for all skeleton data

1 spine::SkeletonAnimation::removeAllSkeletonData();

 

5. lua usage
① Directly go to the cocos2d-x-3.10 \ tools \ tolua and run genbindings. py to regenerate the binding file between c ++ and lua
② Recompile libluaco cos2d
③ The following describes how to use lua.

1 -- Because spSkeletonData is not bound to lua, the returned value of the readSkeletonDataToCache function is invalid (the same is true for the getSkeletonDataFromCache function). The returned value cannot be determined! 2 if not sp. skeletonAnimation: isExistSkeletonDataInCache ("GirlSkeletonDataKey") then3 sp. skeletonAnimation: readSkeletonDataToCache ("GirlSkeletonDataKey", "girl. json "," girl. atlas "); 4 end5 -- because the SkeletonAnimation: createWithData function is not bound in cocos2dx_spine.ini, this function cannot use 6 local skeleton_animation = sp in lua. skeletonAnimation: createFromCache ("GirlSkeletonDataKey ");

 

Above, complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.