diff --git a/spine-c/example/main.c b/spine-c/example/main.c index 49605e3c2..7012967bf 100644 --- a/spine-c/example/main.c +++ b/spine-c/example/main.c @@ -2,7 +2,6 @@ * with internal methods exposed to facilitate extension. */ #include -#include #include #include diff --git a/spine-c/include/spine/Skeleton.h b/spine-c/include/spine/Skeleton.h index 5bbd5bdff..5bce20358 100644 --- a/spine-c/include/spine/Skeleton.h +++ b/spine-c/include/spine/Skeleton.h @@ -41,6 +41,7 @@ struct Skeleton { int boneCount; Bone** bones; + Bone* const root; int slotCount; Slot** slots; @@ -62,7 +63,6 @@ void Skeleton_setToBindPose (const Skeleton* self); void Skeleton_setBonesToBindPose (const Skeleton* self); void Skeleton_setSlotsToBindPose (const Skeleton* self); -Bone* Skeleton_getRootBone (const Skeleton* self); /* Returns 0 if the bone was not found. */ Bone* Skeleton_findBone (const Skeleton* self, const char* boneName); /* Returns -1 if the bone was not found. */ @@ -77,7 +77,8 @@ int Skeleton_findSlotIndex (const Skeleton* self, const char* slotName); * attached if the corresponding attachment from the old skin was attached. * @param skin May be 0.*/ void Skeleton_setSkin (Skeleton* self, Skin* skin); -/* Returns 0 if the skin was not found. See Skeleton_setSkin. */ +/* Returns 0 if the skin was not found. See Skeleton_setSkin. + * @param skinName May be 0. */ int Skeleton_setSkinByName (Skeleton* self, const char* skinName); /* Returns 0 if the slot or attachment was not found. */ diff --git a/spine-c/src/spine/Skeleton.c b/spine-c/src/spine/Skeleton.c index 15d6c82c5..7a4bb1a3a 100644 --- a/spine-c/src/spine/Skeleton.c +++ b/spine-c/src/spine/Skeleton.c @@ -53,6 +53,7 @@ void _Skeleton_init (Skeleton* self, SkeletonData* data) { } self->bones[i] = Bone_create(boneData, parent); } + CONST_CAST(Bone*, self->root) = self->bones[0]; self->slotCount = data->slotCount; self->slots = MALLOC(Slot*, self->slotCount); @@ -122,11 +123,6 @@ void Skeleton_setSlotsToBindPose (const Skeleton* self) { Slot_setToBindPose(self->slots[i]); } -Bone* Skeleton_getRootBone (const Skeleton* self) { - if (self->boneCount == 0) return 0; - return self->bones[0]; -} - Bone* Skeleton_findBone (const Skeleton* self, const char* boneName) { int i; for (i = 0; i < self->boneCount; ++i) @@ -156,6 +152,10 @@ int Skeleton_findSlotIndex (const Skeleton* self, const char* slotName) { } int Skeleton_setSkinByName (Skeleton* self, const char* skinName) { + if (!skinName) { + Skeleton_setSkin(self, 0); + return 1; + } Skin *skin = SkeletonData_findSkin(self->data, skinName); if (!skin) return 0; Skeleton_setSkin(self, skin); diff --git a/spine-cocos2d-iphone/example/ExampleLayer.m b/spine-cocos2d-iphone/example/ExampleLayer.m index 59330da04..455ebb2a1 100644 --- a/spine-cocos2d-iphone/example/ExampleLayer.m +++ b/spine-cocos2d-iphone/example/ExampleLayer.m @@ -13,10 +13,10 @@ self = [super init]; if (!self) return nil; - skeletonNode = [CCSkeleton create:"spineboy.json" atlasFile:"spineboy.atlas"]; - [skeletonNode setMix:"walk" to:"jump" duration:0.4f]; - [skeletonNode setMix:"jump" to:"walk" duration:0.4f]; - [skeletonNode setAnimation:"walk" loop:true]; + skeletonNode = [CCSkeleton create:@"spineboy.json" atlasFile:@"spineboy.atlas"]; + [skeletonNode setMix:@"walk" to:@"jump" duration:0.4f]; + [skeletonNode setMix:@"jump" to:@"walk" duration:0.4f]; + [skeletonNode setAnimation:@"walk" loop:true]; skeletonNode->timeScale = 0.3f; skeletonNode->debugBones = true; @@ -31,9 +31,9 @@ - (void) update:(ccTime)delta { if (strcmp(skeletonNode->state->animation->name, "walk") == 0) { - if (skeletonNode->state->time > 2) [skeletonNode setAnimation:"jump" loop:false]; + if (skeletonNode->state->time > 2) [skeletonNode setAnimation:@"jump" loop:false]; } else { - if (skeletonNode->state->time > 1) [skeletonNode setAnimation:"walk" loop:true]; + if (skeletonNode->state->time > 1) [skeletonNode setAnimation:@"walk" loop:true]; } } diff --git a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h index b6b17ad2a..33304b39d 100644 --- a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h +++ b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h @@ -51,22 +51,23 @@ typedef struct { bool ownsStateData; @public - Skeleton* skeleton; - AnimationState* state; - float timeScale; + Skeleton* const skeleton; + AnimationState* const state; + float timeScale; bool debugSlots; bool debugBones; CCTextureAtlas* atlas; // All region attachments for a skeleton must use the same texture. unsigned int quadCount; + ccBlendFunc blendFunc; } -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlas:(Atlas*)atlas; -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale; ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlas:(Atlas*)atlas; ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale; -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlasFile:(const char*)atlasFile; -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlasFile:(const char*)atlasFile scale:(float)scale; ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile; ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale; + (CCSkeleton*) create:(SkeletonData*)skeletonData; + (CCSkeleton*) create:(SkeletonData*)skeletonData stateData:(AnimationStateData*)stateData; @@ -74,8 +75,36 @@ typedef struct { - init:(SkeletonData*)skeletonData; - init:(SkeletonData*)skeletonData stateData:(AnimationStateData*)stateData; -- (void) setMix:(const char*)fromName to:(const char*)toName duration:(float)duration; -- (void) setAnimation:(const char*)animationName loop:(bool)loop; +- (void) setMix:(NSString*)fromName to:(NSString*)toName duration:(float)duration; +- (void) setAnimation:(NSString*)animationName loop:(bool)loop; + +- (void) updateWorldTransform; + +- (void) setToBindPose; +- (void) setBonesToBindPose; +- (void) setSlotsToBindPose; + +/* Returns 0 if the bone was not found. */ +- (Bone*) findBone:(NSString*)boneName; +/* Returns -1 if the bone was not found. */ +- (int) findBoneIndex:(NSString*)boneName; + +/* Returns 0 if the slot was not found. */ +- (Slot*) findSlot:(NSString*)slotName; +/* Returns -1 if the slot was not found. */ +- (int) findSlotIndex:(NSString*)slotName; + +/* Sets the skin used to look up attachments not found in the SkeletonData defaultSkin. Attachments from the new skin are + * attached if the corresponding attachment from the old skin was attached. Returns false if the skin was not found. + * @param skin May be 0.*/ +- (bool) setSkin:(NSString*)skinName; + +/* Returns 0 if the slot or attachment was not found. */ +- (Attachment*) getAttachmentForSlotName:(NSString*)slotName attachmentName:(NSString*)attachmentName; +/* Returns 0 if the slot or attachment was not found. */ +- (Attachment*) getAttachmentForSlotIndex:(int)slotIndex attachmentName:(NSString*)attachmentName; +/* Returns false if the slot or attachment was not found. */ +- (bool) setAttachment:(NSString*)slotName attachmentName:(NSString*)attachmentName; @end diff --git a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m index 1e612bdbb..8b1b80e3d 100644 --- a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m +++ b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m @@ -68,30 +68,30 @@ Skeleton* _Cocos2dSkeleton_create (SkeletonData* data, CCSkeleton* node) { @implementation CCSkeleton -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlas:(Atlas*)atlas { ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlas:(Atlas*)atlas { return [CCSkeleton create:skeletonDataFile atlas:atlas scale:1]; } -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale { ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale { SkeletonJson* json = SkeletonJson_create(atlas); json->scale = scale; - SkeletonData* skeletonData = SkeletonJson_readSkeletonDataFile(json, skeletonDataFile); + SkeletonData* skeletonData = SkeletonJson_readSkeletonDataFile(json, [skeletonDataFile UTF8String]); SkeletonJson_dispose(json); CCSkeleton* node = skeletonData ? [CCSkeleton create:skeletonData] : 0; node->ownsSkeleton = true; return node; } -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlasFile:(const char*)atlasFile { ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile { return [CCSkeleton create:skeletonDataFile atlasFile:atlasFile scale:1]; } -+ (CCSkeleton*) create:(const char*)skeletonDataFile atlasFile:(const char*)atlasFile scale:(float)scale { - Atlas* atlas = Atlas_readAtlasFile(atlasFile); ++ (CCSkeleton*) create:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale { + Atlas* atlas = Atlas_readAtlasFile([atlasFile UTF8String]); if (!atlas) return 0; SkeletonJson* json = SkeletonJson_create(atlas); json->scale = scale; - SkeletonData* skeletonData = SkeletonJson_readSkeletonDataFile(json, skeletonDataFile); + SkeletonData* skeletonData = SkeletonJson_readSkeletonDataFile(json, [skeletonDataFile UTF8String]); SkeletonJson_dispose(json); if (!skeletonData) { Atlas_dispose(atlas); @@ -119,13 +119,13 @@ Skeleton* _Cocos2dSkeleton_create (SkeletonData* data, CCSkeleton* node) { self = [super init]; if (!self) return nil; - skeleton = _Cocos2dSkeleton_create(skeletonData, self); + CONST_CAST(Skeleton*, skeleton) = _Cocos2dSkeleton_create(skeletonData, self); if (!stateData) { stateData = AnimationStateData_create(skeletonData); ownsStateData = true; } - state = AnimationState_create(stateData); + CONST_CAST(AnimationState*, state) = AnimationState_create(stateData); blendFunc.src = GL_ONE; blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA; @@ -145,14 +145,6 @@ Skeleton* _Cocos2dSkeleton_create (SkeletonData* data, CCSkeleton* node) { [super dealloc]; } -- (void) setMix:(const char*)fromName to:(const char*)toName duration:(float)duration { - AnimationStateData_setMixByName(state->data, fromName, toName, duration); -} - -- (void) setAnimation:(const char*)animationName loop:(bool)loop { - AnimationState_setAnimationByName(state, animationName, loop); -} - - (void) update:(ccTime)deltaTime { Skeleton_update(skeleton, deltaTime); AnimationState_update(state, deltaTime * timeScale); @@ -211,6 +203,57 @@ Skeleton* _Cocos2dSkeleton_create (SkeletonData* data, CCSkeleton* node) { } } +// Convenience methods: + +- (void) setMix:(NSString*)fromName to:(NSString*)toName duration:(float)duration { + AnimationStateData_setMixByName(state->data, [fromName UTF8String], [toName UTF8String], duration); +} +- (void) setAnimation:(NSString*)animationName loop:(bool)loop { + AnimationState_setAnimationByName(state, [animationName UTF8String], loop); +} + +- (void) updateWorldTransform { + Skeleton_updateWorldTransform(skeleton); +} + +- (void) setToBindPose { + Skeleton_setToBindPose(skeleton); +} +- (void) setBonesToBindPose { + Skeleton_setBonesToBindPose(skeleton); +} +- (void) setSlotsToBindPose { + Skeleton_setSlotsToBindPose(skeleton); +} + +- (Bone*) findBone:(NSString*)boneName { + return Skeleton_findBone(skeleton, [boneName UTF8String]); +} +- (int) findBoneIndex:(NSString*)boneName { + return Skeleton_findBoneIndex(skeleton, [boneName UTF8String]); +} + +- (Slot*) findSlot:(NSString*)slotName { + return Skeleton_findSlot(skeleton, [slotName UTF8String]); +} +- (int) findSlotIndex:(NSString*)slotName { + return Skeleton_findSlotIndex(skeleton, [slotName UTF8String]); +} + +- (bool) setSkin:(NSString*)skinName { + return (bool)Skeleton_setSkinByName(skeleton, [skinName UTF8String]); +} + +- (Attachment*) getAttachmentForSlotName:(NSString*)slotName attachmentName:(NSString*)attachmentName { + return Skeleton_getAttachmentForSlotName(skeleton, [slotName UTF8String], [attachmentName UTF8String]); +} +- (Attachment*) getAttachmentForSlotIndex:(int)slotIndex attachmentName:(NSString*)attachmentName { + return Skeleton_getAttachmentForSlotIndex(skeleton, slotIndex, [attachmentName UTF8String]); +} +- (bool) setAttachment:(NSString*)slotName attachmentName:(NSString*)attachmentName { + return (bool)Skeleton_setAttachment(skeleton, [slotName UTF8String], [attachmentName UTF8String]); +} + // CCBlendProtocol - (void) setBlendFunc:(ccBlendFunc)func { diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp index d645cec89..72edf0147 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp @@ -103,14 +103,14 @@ CCSkeleton* CCSkeleton::create (SkeletonData* skeletonData, AnimationStateData* } CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) : - debugSlots(false), debugBones(false) { - skeleton = _Cocos2dxSkeleton_create(skeletonData, this); + skeleton(0), state(0), debugSlots(false), debugBones(false) { + CONST_CAST(Skeleton*, skeleton) = _Cocos2dxSkeleton_create(skeletonData, this); if (!stateData) { stateData = AnimationStateData_create(skeletonData); ownsStateData = true; } - state = AnimationState_create(stateData); + CONST_CAST(AnimationState*, state) = AnimationState_create(stateData); blendFunc.src = GL_ONE; blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA; @@ -127,14 +127,6 @@ CCSkeleton::~CCSkeleton () { AnimationState_dispose(state); } -void CCSkeleton::setMix (const char* fromName, const char* toName, float duration) { - AnimationStateData_setMixByName(state->data, fromName, toName, duration); -} - -void CCSkeleton::setAnimation (const char* animationName, bool loop) { - AnimationState_setAnimationByName(state, animationName, loop); -} - void CCSkeleton::update (float deltaTime) { Skeleton_update(skeleton, deltaTime); AnimationState_update(state, deltaTime * timeScale); @@ -193,6 +185,58 @@ void CCSkeleton::draw () { } } +// Convenience methods: + +void CCSkeleton::setMix (const char* fromName, const char* toName, float duration) { + AnimationStateData_setMixByName(state->data, fromName, toName, duration); +} + +void CCSkeleton::setAnimation (const char* animationName, bool loop) { + AnimationState_setAnimationByName(state, animationName, loop); +} + +void CCSkeleton::updateWorldTransform () { + Skeleton_updateWorldTransform(skeleton); +} + +void CCSkeleton::setToBindPose () { + Skeleton_setToBindPose(skeleton); +} +void CCSkeleton::setBonesToBindPose () { + Skeleton_setBonesToBindPose(skeleton); +} +void CCSkeleton::setSlotsToBindPose () { + Skeleton_setSlotsToBindPose(skeleton); +} + +Bone* CCSkeleton::findBone (const char* boneName) const { + return Skeleton_findBone(skeleton, boneName); +} +int CCSkeleton::findBoneIndex (const char* boneName) const { + return Skeleton_findBoneIndex(skeleton, boneName); +} + +Slot* CCSkeleton::findSlot (const char* slotName) const { + return Skeleton_findSlot(skeleton, slotName); +} +int CCSkeleton::findSlotIndex (const char* slotName) const { + return Skeleton_findSlotIndex(skeleton, slotName); +} + +bool CCSkeleton::setSkin (const char* skinName) { + return (bool)Skeleton_setSkinByName(skeleton, skinName); +} + +Attachment* CCSkeleton::getAttachment (const char* slotName, const char* attachmentName) const { + return Skeleton_getAttachmentForSlotName(skeleton, slotName, attachmentName); +} +Attachment* CCSkeleton::getAttachment (int slotIndex, const char* attachmentName) const { + return Skeleton_getAttachmentForSlotIndex(skeleton, slotIndex, attachmentName); +} +bool CCSkeleton::setAttachment (const char* slotName, const char* attachmentName) { + return (bool)Skeleton_setAttachment(skeleton, slotName, attachmentName); +} + // CCBlendProtocol ccBlendFunc CCSkeleton::getBlendFunc () { diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.h b/spine-cocos2dx/src/spine/spine-cocos2dx.h index 50465d8c0..7e46c5d4c 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.h +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.h @@ -53,11 +53,12 @@ private: bool ownsStateData; public: - Skeleton* skeleton; - AnimationState* state; + Skeleton* const skeleton; + AnimationState* const state; float timeScale; bool debugSlots; bool debugBones; + cocos2d::CCTextureAtlas* atlas; // All region attachments for a skeleton must use the same texture. unsigned int quadCount; @@ -71,6 +72,34 @@ public: void setMix (const char* fromName, const char* toName, float duration); void setAnimation (const char* animationName, bool loop); + void updateWorldTransform (); + + void setToBindPose (); + void setBonesToBindPose (); + void setSlotsToBindPose (); + + /* Returns 0 if the bone was not found. */ + Bone* findBone (const char* boneName) const; + /* Returns -1 if the bone was not found. */ + int findBoneIndex (const char* boneName) const; + + /* Returns 0 if the slot was not found. */ + Slot* findSlot (const char* slotName) const; + /* Returns -1 if the slot was not found. */ + int findSlotIndex (const char* slotName) const; + + /* Sets the skin used to look up attachments not found in the SkeletonData defaultSkin. Attachments from the new skin are + * attached if the corresponding attachment from the old skin was attached. Returns false if the skin was not found. + * @param skin May be 0.*/ + bool setSkin (const char* skinName); + + /* Returns 0 if the slot or attachment was not found. */ + Attachment* getAttachment (const char* slotName, const char* attachmentName) const; + /* Returns 0 if the slot or attachment was not found. */ + Attachment* getAttachment (int slotIndex, const char* attachmentName) const; + /* Returns false if the slot or attachment was not found. */ + bool setAttachment (const char* slotName, const char* attachmentName); + virtual void update (float deltaTime); virtual void draw (); diff --git a/spine-sfml/src/spine/spine-sfml.cpp b/spine-sfml/src/spine/spine-sfml.cpp index 201f32120..991b3927f 100644 --- a/spine-sfml/src/spine/spine-sfml.cpp +++ b/spine-sfml/src/spine/spine-sfml.cpp @@ -200,4 +200,4 @@ char* _Util_readFile (const char* path, int* length) { return _readFile(path, length); } -} +} /* namespace spine */