spine-cocos2dx updated to latest spine-c API.

This has some API breakage, sorry. The new AnimationState now handles multiple "tracks", which makes it easier to apply and queue multiple animations at the same time, no longer do we need multiple AnimationStates.
This commit is contained in:
NathanSweet 2013-09-26 12:14:55 +02:00
parent 5b2dfe3311
commit 067a84339b
7 changed files with 69 additions and 79 deletions

View File

@ -22,11 +22,11 @@ bool ExampleLayer::init () {
skeletonNode->setMix("walk", "jump", 0.2f);
skeletonNode->setMix("jump", "walk", 0.4f);
skeletonNode->setAnimation("walk", true);
skeletonNode->setAnimation(0, "walk", true);
// This shows how to setup animations to play back to back.
//skeletonNode->addAnimation("jump", true);
//skeletonNode->addAnimation("walk", true);
//skeletonNode->addAnimation("jump", true);
//skeletonNode->addAnimation(0, "jump", false);
//skeletonNode->addAnimation(0, "walk", false);
//skeletonNode->addAnimation(0, "jump", true);
skeletonNode->timeScale = 0.3f;
skeletonNode->debugBones = true;
@ -46,10 +46,11 @@ bool ExampleLayer::init () {
}
void ExampleLayer::update (float deltaTime) {
if (skeletonNode->states[0]->loop) {
if (skeletonNode->states[0]->time > 2) skeletonNode->setAnimation("jump", false);
TrackEntry* entry = skeletonNode->getCurrent(0);
if (entry->loop) {
if (entry->time > 2) skeletonNode->setAnimation(0, "jump", false);
} else {
if (skeletonNode->states[0]->time > 1) skeletonNode->setAnimation("walk", true);
if (entry->time > 1) skeletonNode->setAnimation(0, "walk", true);
}
// if (skeletonNode->states[0]->time > 0.1) CCDirector::sharedDirector()->replaceScene(ExampleLayer::scene());
// if (entry->time > 0.1) CCDirector::sharedDirector()->replaceScene(ExampleLayer::scene());
}

View File

@ -134,6 +134,8 @@
<ClInclude Include="..\..\..\spine-c\include\spine\Bone.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\BoneData.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\BoundingBoxAttachment.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\Event.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\EventData.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\extension.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\RegionAttachment.h" />
<ClInclude Include="..\..\..\spine-c\include\spine\Skeleton.h" />
@ -164,6 +166,8 @@
<ClCompile Include="..\..\..\spine-c\src\spine\Bone.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\BoneData.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\BoundingBoxAttachment.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\Event.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\EventData.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\extension.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\Json.c" />
<ClCompile Include="..\..\..\spine-c\src\spine\RegionAttachment.c" />

View File

@ -99,6 +99,12 @@
<ClInclude Include="..\..\..\spine-c\include\spine\SkeletonBounds.h">
<Filter>Classes\spine-c</Filter>
</ClInclude>
<ClInclude Include="..\..\..\spine-c\include\spine\Event.h">
<Filter>Classes\spine-c</Filter>
</ClInclude>
<ClInclude Include="..\..\..\spine-c\include\spine\EventData.h">
<Filter>Classes\spine-c</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
@ -179,5 +185,11 @@
<ClCompile Include="..\..\..\spine-c\src\spine\SkeletonBounds.c">
<Filter>Classes\spine-c</Filter>
</ClCompile>
<ClCompile Include="..\..\..\spine-c\src\spine\Event.c">
<Filter>Classes\spine-c</Filter>
</ClCompile>
<ClCompile Include="..\..\..\spine-c\src\spine\EventData.c">
<Filter>Classes\spine-c</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -39,9 +39,7 @@
namespace spine {
/**
Draws a skeleton.
*/
/** Draws a skeleton. */
class CCSkeleton: public cocos2d::CCNodeRGBA, public cocos2d::CCBlendProtocol {
public:
Skeleton* skeleton;

View File

@ -60,96 +60,71 @@ CCSkeletonAnimation* CCSkeletonAnimation::createWithFile (const char* skeletonDa
return node;
}
void CCSkeletonAnimation::initialize () {
state = AnimationState_create(AnimationStateData_create(skeleton->data));
}
CCSkeletonAnimation::CCSkeletonAnimation (SkeletonData *skeletonData)
: CCSkeleton(skeletonData) {
addAnimationState();
initialize();
}
CCSkeletonAnimation::CCSkeletonAnimation (const char* skeletonDataFile, Atlas* atlas, float scale)
: CCSkeleton(skeletonDataFile, atlas, scale) {
addAnimationState();
initialize();
}
CCSkeletonAnimation::CCSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale)
: CCSkeleton(skeletonDataFile, atlasFile, scale) {
addAnimationState();
initialize();
}
CCSkeletonAnimation::~CCSkeletonAnimation () {
for (std::vector<AnimationStateData*>::iterator iter = stateDatas.begin(); iter != stateDatas.end(); ++iter)
AnimationStateData_dispose(*iter);
for (std::vector<AnimationState*>::iterator iter = states.begin(); iter != states.end(); ++iter)
AnimationState_dispose(*iter);
if (ownsAnimationStateData) AnimationStateData_dispose(state->data);
AnimationState_dispose(state);
}
void CCSkeletonAnimation::update (float deltaTime) {
super::update(deltaTime);
deltaTime *= timeScale;
for (std::vector<AnimationState*>::iterator iter = states.begin(); iter != states.end(); ++iter) {
AnimationState_update(*iter, deltaTime);
AnimationState_apply(*iter, skeleton);
}
AnimationState_update(state, deltaTime);
AnimationState_apply(state, skeleton);
Skeleton_updateWorldTransform(skeleton);
}
void CCSkeletonAnimation::addAnimationState (AnimationStateData* stateData) {
if (!stateData) {
stateData = AnimationStateData_create(skeleton->data);
stateDatas.push_back(stateData);
}
AnimationState* state = AnimationState_create(stateData);
states.push_back(state);
}
AnimationState* CCSkeletonAnimation::getAnimationState (int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
return states[stateIndex];
}
void CCSkeletonAnimation::setAnimationStateData (AnimationStateData* stateData, int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
void CCSkeletonAnimation::setAnimationStateData (AnimationStateData* stateData) {
CCAssert(stateData, "stateData cannot be null.");
AnimationState* state = states[stateIndex];
for (std::vector<AnimationStateData*>::iterator iter = stateDatas.begin(); iter != stateDatas.end(); ++iter) {
if (state->data == *iter) {
AnimationStateData_dispose(state->data);
stateDatas.erase(iter);
break;
}
}
for (std::vector<AnimationState*>::iterator iter = states.begin(); iter != states.end(); ++iter) {
if (state == *iter) {
states.erase(iter);
break;
}
}
if (ownsAnimationStateData) AnimationStateData_dispose(state->data);
AnimationState_dispose(state);
ownsAnimationStateData = true;
state = AnimationState_create(stateData);
states[stateIndex] = state;
}
void CCSkeletonAnimation::setMix (const char* fromAnimation, const char* toAnimation, float duration, int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
AnimationStateData_setMixByName(states[stateIndex]->data, fromAnimation, toAnimation, duration);
void CCSkeletonAnimation::setMix (const char* fromAnimation, const char* toAnimation, float duration) {
AnimationStateData_setMixByName(state->data, fromAnimation, toAnimation, duration);
}
void CCSkeletonAnimation::setAnimation (const char* name, bool loop, int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
AnimationState_setAnimationByName(states[stateIndex], name, loop);
TrackEntry* CCSkeletonAnimation::setAnimation (int trackIndex, const char* name, bool loop) {
return AnimationState_setAnimationByName(state, trackIndex, name, loop);
}
void CCSkeletonAnimation::addAnimation (const char* name, bool loop, float delay, int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
AnimationState_addAnimationByName(states[stateIndex], name, loop, delay);
TrackEntry* CCSkeletonAnimation::addAnimation (int trackIndex, const char* name, bool loop, float delay) {
return AnimationState_addAnimationByName(state, trackIndex, name, loop, delay);
}
void CCSkeletonAnimation::clearAnimation (int stateIndex) {
CCAssert(stateIndex >= 0 && stateIndex < (int)states.size(), "stateIndex out of range.");
AnimationState_clearAnimation(states[stateIndex]);
TrackEntry* CCSkeletonAnimation::getCurrent (int trackIndex) {
return AnimationState_getCurrent(state, trackIndex);
}
void CCSkeletonAnimation::clearAnimation () {
AnimationState_clear(state);
}
void CCSkeletonAnimation::clearAnimation (int trackIndex) {
AnimationState_clearTrack(state, trackIndex);
}
}

View File

@ -40,12 +40,11 @@
namespace spine {
/**
Draws an animated skeleton, providing a simple API for applying one or more animations and queuing animations to be played later.
*/
/** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be
* played later. */
class CCSkeletonAnimation: public CCSkeleton {
public:
std::vector<AnimationState*> states;
AnimationState* state;
static CCSkeletonAnimation* createWithData (SkeletonData* skeletonData);
static CCSkeletonAnimation* createWithFile (const char* skeletonDataFile, Atlas* atlas, float scale = 1);
@ -59,20 +58,21 @@ public:
virtual void update (float deltaTime);
void addAnimationState (AnimationStateData* stateData = 0);
void setAnimationStateData (AnimationStateData* stateData, int stateIndex = 0);
AnimationState* getAnimationState (int stateIndex = 0);
void setMix (const char* fromAnimation, const char* toAnimation, float duration, int stateIndex = 0);
void setAnimation (const char* name, bool loop, int stateIndex = 0);
void addAnimation (const char* name, bool loop, float delay = 0, int stateIndex = 0);
void clearAnimation (int stateIndex = 0);
void setAnimationStateData (AnimationStateData* stateData);
void setMix (const char* fromAnimation, const char* toAnimation, float duration);
TrackEntry* setAnimation (int trackIndex, const char* name, bool loop);
TrackEntry* addAnimation (int trackIndex, const char* name, bool loop, float delay = 0);
TrackEntry* getCurrent (int trackIndex = 0);
void clearAnimation ();
void clearAnimation (int trackIndex = 0);
protected:
CCSkeletonAnimation ();
private:
typedef CCSkeleton super;
std::vector<AnimationStateData*> stateDatas;
bool ownsAnimationStateData;
void initialize ();
};

View File

@ -95,7 +95,7 @@ void spineboy () {
Slot* headSlot = Skeleton_findSlot(skeleton, "head");
drawable->state->listener = callback;
if (true) {
if (false) {
AnimationState_setAnimationByName(drawable->state, 0, "drawOrder", true);
} else {
AnimationState_setAnimationByName(drawable->state, 0, "walk", true);