COCOS2X (C++/lua) spine file pre-load

Source: Internet
Author: User

Before, I wrote a blog, through Lua loading scenes loaded Spineanimation Animation wizard, saved in the table, and then in the game to create animation sprites, extract the loaded Spineanimaiton in the

Spskeletondata to create the spineanimation, but later I found that when the spineanimation was created repeatedly, all the same spskeletondata would reuse the same spskeletondata, The following is the source code for the Spskeletondata copy method:

spskeleton* spskeleton_create (spskeletondata* data) {

int I, II;

_spskeleton* internal = NEW (_spskeleton);

spskeleton* self = SUPER (internal);

Const_cast (spskeletondata*, self->data) = data;

Self->bonescount = self->data->bonescount;

Self->bones = MALLOC (spbone*, Self->bonescount);

for (i = 0; i < self->bonescount; ++i) {

spbonedata* Bonedata = self->data->bones[i];

spbone* parent = 0;

if (bonedata->parent) {

/* Find parent bone. */

for (ii = 0; II < self->bonescount; ++ii) {

if (data->bones[ii] = = bonedata->parent) {

Parent = self->bones[ii];

Break

}

}

}

Self->bones[i] = Spbone_create (Bonedata, self, parent);

}

Const_cast (spbone*, self->root) = self->bones[0];

Self->slotscount = data->slotscount;

Self->slots = MALLOC (spslot*, Self->slotscount);

for (i = 0; i < self->slotscount; ++i) {

Spslotdata *slotdata = data->slots[i];

/* Find bone for the Slotdata ' s bonedata. */

spbone* bone = 0;

for (ii = 0; II < self->bonescount; ++ii) {

if (data->bones[ii] = = Slotdata->bonedata) {

Bone = self->bones[ii];

Break

}

}

Self->slots[i] = spslot_create (Slotdata, bone);

}

Self->draworder = MALLOC (spslot*, Self->slotscount);

memcpy (Self->draworder, self->slots, sizeof (spslot*) * self->slotscount);

Self->r = 1;

Self->g = 1;

Self->b = 1;

Self->a = 1;

Self->ikconstraintscount = data->ikconstraintscount;

self->ikconstraints = MALLOC (spikconstraint*, Self->ikconstraintscount);

for (i = 0; i < self->data->ikconstraintscount; ++i)

Self->ikconstraints[i] = Spikconstraint_create (Self->data->ikconstraints[i], self);

Spskeleton_updatecache (self);

return self;

}

In the copy, the bone data does open up a new memory and copies a new spskeletondata, but since Spskeletondata belongs to the skeletonanimation saved in the table, Affected by skeletonanimation, you cannot use Spskeletondata in skeletonanimation to create another skeletonanimation.

I also tried to create spskeletondata data from Lua, save it in a table, and then pass Spskeletondata in the game to create Skeletonanimation,

However, during the switching scenario, the Spskeletondata is released, so it cannot be reused.

Then I start from C + +, save Spskeletondata in C + + static dictionary, extract Spskeletondata by key value to create skeletonanimation, avoid releasing the problem.

Here's how it's implemented:

Spineanimation_new.h

#include <stdio.h>

#include "Cocos/editor-support/spine/skeletonanimation.h"

#include "cocos/editor-support/spine/spine.h"

#include "Spine/skeletonrenderer.h"

#include "Cocos2d.h"

using namespace spine;

using namespace Std;

Class Spineanimation_new:spine::skeletonanimation

{

Public

Static Spineanimation_new *createwithskeletondata (spskeletondata* skeletondata);

Static spineanimation_new *createwithkey (const std::string& key);

Virtual ~spineanimation_new ();

spanimationstate* getState () {return skeletonanimation::getstate ();}

spskeletondata* Getskeletondata ()

{

Spskeletondata*skdata =skeletonrenderer::getskeleton ()->data;

return skdata;

}

Static spskeletondata*loadskeletondata (const std::string& atlasfile,const std::string& jsonFile,const std:: string& key);

static Map<const std::string,spskeletondata*> Skeletondatamap;

static void Clear ();

Private

Spineanimation_new (Spskeletondata*skeletondata);

Spineanimation_new (const std::string& skeletondatafile, const std::string& atlasfile, Float scale = 1);

};

#endif

Spineanimation_new.cpp

#include "Spineanimation_new.h"

#include "Cocos2d.h"

#include "Cocos/scripting/lua-bindings/manual/cocos2d/luascripthandlermgr.h"

#include "CCLuaStack.h"

#include "CCLuaEngine.h"

USING_NS_CC;

Map<const std::string,spskeletondata*> Spineanimation_new::skeletondatamap;

Spineanimation_new *spineanimation_new::createwithskeletondata (spskeletondata* skeletonData)

{

spineanimation_new* node = new (Std::nothrow) spineanimation_new (skeletondata);

Node->autorelease ();

return node;

}

Spskeletondata*spineanimation_new::loadskeletondata (const std::string& Atlasfile,const std::string& Jsonfile,const std::string& Key)

{

spatlas* Atlas = Spatlas_createfromfile (Atlasfile.c_str (), 0);

spskeletonjson* JSON = Spskeletonjson_create (Atlas);

Json->scale = 1;

spskeletondata* SD = Spskeletonjson_readskeletondatafile (JSON, JSONFILE.C_STR ());

Spineanimation_new::skeletondatamap.insert (Pair<const std::string,spskeletondata*> (KEY,SD));

return SD;

}

Spineanimation_new *spineanimation_new::createwithkey (const std::string& key)

{

Map<const Std::string,spskeletondata*>::iterator iter = Spineanimation_new::skeletondatamap.find (key);

spskeletondata* SD = iter->second;

Spineanimation_new*sa = Createwithskeletondata (SD);

Return SA;

}

Spineanimation_new::~spineanimation_new ()

{

Scripthandlermgr::getinstance ()->removeobjectallhandlers ((void*) this);

}

Spineanimation_new::spineanimation_new (Spskeletondata*skeletondata):

Skeletonanimation (Skeletondata)

{

}

Spineanimation_new::spineanimation_new (const std::string& skeletondatafile, const std::string& AtlasFile, Float scale): skeletonanimation (Skeletondatafile,atlasfile,scale)

{

}

void Spineanimation_new::clear ()

{

Spineanimation_new::skeletondatamap.clear ();

}

(Please specify the source when reproduced, from the blog Garden: Hemjohn)

COCOS2X (C++/lua) spine file pre-load

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.