1. 程式人生 > >cocos2dx 3.16中spine版本向下相容 timeline slipX slipY

cocos2dx 3.16中spine版本向下相容 timeline slipX slipY

在引用了一個spine 出現如下報錯:

cocos Invalid timeline type for a bone: flipX

spine 的高版本中已經沒有 filpX 和filpY了

cocos 3.16中也已經不再支援

於是

參照 3.10 中的 做了相容了

修改如下

1.spine/Bone.h

加入int/*bool*/flipX, flipY;

2. Animation.h

SP_TIMELINE_FLIPX,

 SP_TIMELINE_FLIPY

新增

typedef struct spFlipTimeline {

    spTimeline super;

    int const x;

    int const framesCount;

    float* const frames; /* time, flip, ... */

    int boneIndex;

#ifdef __cplusplus

    spFlipTimeline() :

        super(),

        x(0),

        framesCount(0),

        frames(0),

        boneIndex(0) {

    }

#endif

} spFlipTimeline;

spFlipTimeline* spFlipTimeline_create(int framesCount, int/*bool*/x);

void spFlipTimeline_setFrame(spFlipTimeline* self, int frameIndex, float time, int/*bool*/flip);

3.Animation.c   _spFlipTimeline_apply 圖中有錯誤會在ios平臺編譯不過 具體看程式碼 下面程式碼是正確的

void _spFlipTimeline_apply(const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
    spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
    int frameIndex;
    spFlipTimeline* self = (spFlipTimeline*)timeline;

    if (time < self->frames[0]) {
        if (lastTime > time) _spFlipTimeline_apply(timeline, skeleton, lastTime, (float)INT_MAX, 0, 0, 0, 0, 0);
        return;
    }
    else if (lastTime > time) /**/
        lastTime = -1;

    frameIndex = (time >= self->frames[self->framesCount - 2] ?
        self->framesCount : binarySearch(self->frames, self->framesCount, time, 2)) - 2;
    if (self->frames[frameIndex] < lastTime) return;

    if (self->x)
        skeleton->bones[self->boneIndex]->flipX = (int)self->frames[frameIndex + 1];
    else
        skeleton->bones[self->boneIndex]->flipY = (int)self->frames[frameIndex + 1];
}

void _spFlipTimeline_dispose(spTimeline* timeline) {

    spFlipTimeline* self = SUB_CAST(spFlipTimeline, timeline);

    _spTimeline_deinit(SUPER(self));

    FREE(self->frames);

    FREE(self);

}

int _spFlipXTimeline_getPropertyId(const spTimeline* timeline) {

    return (SP_TIMELINE_FLIPX << 24) + SUB_CAST(spFlipTimeline, timeline)->boneIndex;

}

int _spFlipYTimeline_getPropertyId(const spTimeline* timeline) {

    return (SP_TIMELINE_FLIPY << 24) + SUB_CAST(spFlipTimeline, timeline)->boneIndex;

}

spFlipTimeline* spFlipTimeline_create(int framesCount, int/*bool*/x) {

    spFlipTimeline* self = NEW(spFlipTimeline);

    //_spTimeline_init(SUPER(self), SP_TIMELINE_DRAWORDER, _spDrawOrderTimeline_dispose, _spDrawOrderTimeline_apply, _spDrawOrderTimeline_getPropertyId);

    _spTimeline_init(SUPER(self), x ? SP_TIMELINE_FLIPX : SP_TIMELINE_FLIPY, _spFlipTimeline_dispose, _spFlipTimeline_apply, x ? _spFlipXTimeline_getPropertyId : _spFlipYTimeline_getPropertyId);

    CONST_CAST(int, self->x) = x;

    CONST_CAST(int, self->framesCount) = framesCount << 1;

    CONST_CAST(float*, self->frames) = CALLOC(float, self->framesCount);

    return self;

}

void spFlipTimeline_setFrame(spFlipTimeline* self, int frameIndex, float time, int/*bool*/flip) {

    frameIndex <<= 1;

    self->frames[frameIndex] = time;

    self->frames[frameIndex + 1] = (float)flip;

}

4.SkeletonJson.c

else if (strcmp(timelineMap->name, "flipX") == 0 || strcmp(timelineMap->name, "flipY") == 0) {

                    int x = strcmp(timelineMap->name, "flipX") == 0;

                    const char* field = x ? "x" : "y";

                    spFlipTimeline *timeline = spFlipTimeline_create(timelineMap->size, x);

                    timeline->boneIndex = boneIndex;

                    for (valueMap = timelineMap->child, frameIndex = 0; valueMap; valueMap = valueMap->next, ++frameIndex)

                        spFlipTimeline_setFrame(timeline, frameIndex, Json_getFloat(valueMap, "time", 0), Json_getInt(valueMap, field, 0));

                    animation->timelines[animation->timelinesCount++] = SUPER_CAST(spTimeline, timeline);

                    float duration = timeline->frames[timelineMap->size * 2 - 2];

                    if (duration > animation->duration) animation->duration = duration;


                }