Sheado

Hi All,

Is there a best practice for caching spine atlas and skeleton data in Cocos2d-x? There's been a few posts on the subject (in which people decided to write their own caching maps), but I figure with things changing so quickly all the time I should ask before diving in myself.

Here's a few of the posts I've found related to the subject:
http://discuss.cocos2d-x.org/t/about-spine-data-cache/27016
Cocos2d-x - Batch all Skeletons / Sprites from one atlas
Skeleton Animation performance


Thanks!

---

Went ahead and implemented what seems correct.. please let me know if I've missed something:

static unordered_map<string, spSkeletonData*> skeletonDataMap;
static vector<spAtlas*> atlases;
SkeletonAnimation* createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {

spSkeletonData* skeletonData = NULL;
auto iterator = skeletonDataMap.find( skeletonDataFile );
// TODO - optimize - some skeletons use the same atlas - we could map the atlases too
if( iterator == skeletonDataMap.end() )
{
spAtlas* _atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
CCASSERT(_atlas, "Error reading atlas file.");

spSkeletonJson* json = spSkeletonJson_create(_atlas);
json->scale = scale;
skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data file.");
spSkeletonJson_dispose(json);

skeletonDataMap.insert({skeletonDataFile,skeletonData});
atlases.push_back(_atlas);

}
else
skeletonData = iterator->second;

SkeletonAnimation* node = new SkeletonAnimation(skeletonData, false);
node->autorelease();
return node;
}
And then I cleanup the cache in between stage loads:
void cleanupCache()
{
for ( auto it = skeletonDataMap.begin(); it != skeletonDataMap.end(); ++it )
{
spSkeletonData_dispose( it->second );
}

for( auto atlas : atlases )
spAtlas_dispose(atlas);

skeletonDataMap.clear();
atlases.clear();
}
Sheado
Posts: 66

Pharan

The caching part seems correct.
spine-unity does something similar. (it also caches the atlas.)
What's likely a good setup is that you generate the cache at a loading screen so the json loading can be done there, then just use it when you need to create new skeletons.
User avatar
Pharan

Pharan
Posts: 4493

Sheado

Cool, thanks for reviewing my code Pharan.
That's pretty much how I'm using it - caching during the loading screen.
Sheado
Posts: 66


Return to Runtimes