mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-09 08:38:43 +08:00
Fancier C++/ObjC APIs to avoid C API with simple usage.
NSString instead of cstr for ObjC.
This commit is contained in:
parent
dd27ee184c
commit
6780447b83
@ -2,7 +2,6 @@
|
||||
* with internal methods exposed to facilitate extension. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <spine/spine.h>
|
||||
#include <spine/extension.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. */
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 ();
|
||||
|
||||
|
||||
@ -200,4 +200,4 @@ char* _Util_readFile (const char* path, int* length) {
|
||||
return _readFile(path, length);
|
||||
}
|
||||
|
||||
}
|
||||
} /* namespace spine */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user