Skip to content

Commit

Permalink
Fix spine::Animation & spine::Timeline can not release normally
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeng-song committed Sep 20, 2024
1 parent 486c562 commit fa73743
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 114 deletions.
4 changes: 3 additions & 1 deletion cocos/spine/lib/spine-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ declare namespace spine {
frameVertices: Array<ArrayLike<number>>;
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, vertices: ArrayLike<number>): void;
setSlotIndex(index: number);
setAttachment(attachment: Attachment);
setFrame(frameIndex: number, time: number, vertices: []): void;
apply(skeleton: Skeleton, lastTime: number, time: number, firedEvents: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;
}
class EventTimeline implements Timeline {
Expand Down
1 change: 1 addition & 0 deletions cocos/spine/lib/spine-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function overrideClass (wasm): void {
spine.Event = wasm.Event;
spine.EventData = wasm.EventData;
spine.Attachment = wasm.Attachment;
spine.SpineAnimationState = wasm.SpineAnimationState;

Check failure on line 124 in cocos/spine/lib/spine-define.ts

View workflow job for this annotation

GitHub Actions / npm_test

Property 'SpineAnimationState' does not exist on type 'typeof spine'. Did you mean 'AnimationState'?
spine.VertexAttachment = wasm.VertexAttachment;
spine.BoundingBoxAttachment = wasm.BoundingBoxAttachment;
spine.ClippingAttachment = wasm.ClippingAttachment;
Expand Down
4 changes: 4 additions & 0 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,10 @@ if(USE_MIDDLEWARE)
cocos/editor-support/spine-creator-support/SkeletonDataMgr.h
NO_WERROR NO_UBUILD cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp
cocos/editor-support/spine-creator-support/SkeletonRenderer.h
NO_WERROR cocos/editor-support/spine-creator-support/SpineAnimation.cpp
cocos/editor-support/spine-creator-support/SpineAnimation.h
NO_WERROR cocos/editor-support/spine-creator-support/SpineAnimationState.cpp
cocos/editor-support/spine-creator-support/SpineAnimationState.h
NO_WERROR cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp
cocos/editor-support/spine-creator-support/spine-cocos2dx.h
NO_WERROR cocos/editor-support/spine-creator-support/VertexEffectDelegate.cpp
Expand Down
3 changes: 3 additions & 0 deletions native/cocos/bindings/manual/jsb_conversions_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,9 @@ bool sevalue_to_native(const se::Value &from, cc::IBArray *to, se::Object * /*ct

// NOLINTNEXTLINE(readability-identifier-naming)
bool sevalue_to_native(const se::Value &val, spine::String *obj, se::Object * /*unused*/) {
if (!val.isString()) {
return false;
}
*obj = val.toString().data();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*****************************************************************************/

#include "spine-creator-support/SkeletonAnimation.h"
#include "SpineAnimationState.h"

#include <algorithm>
#include "base/DeferredReleasePool.h"
#include "base/Log.h"
Expand Down Expand Up @@ -99,7 +101,7 @@ void SkeletonAnimation::initialize() {
super::initialize();

_ownsAnimationStateData = true;
_state = new (__FILE__, __LINE__) AnimationState(new (__FILE__, __LINE__) AnimationStateData(_skeleton->getData()));
_state = new (__FILE__, __LINE__) SpineAnimationState(new (__FILE__, __LINE__) AnimationStateData(_skeleton->getData()));
_state->setRendererObject(this);
_state->setListener(animationCallback);
}
Expand Down Expand Up @@ -140,7 +142,7 @@ void SkeletonAnimation::setAnimationStateData(AnimationStateData *stateData) {
}

_ownsAnimationStateData = false;
_state = new (__FILE__, __LINE__) AnimationState(stateData);
_state = new (__FILE__, __LINE__) SpineAnimationState(stateData);
_state->setRendererObject(this);
_state->setListener(animationCallback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ SkeletonCache *SkeletonCacheMgr::buildSkeletonCache(const std::string &uuid) {
void SkeletonCacheMgr::removeSkeletonCache(const std::string &uuid) {
auto it = _caches.find(uuid);
if (it != _caches.end()) {
auto *item = it->second;
delete item;
_caches.erase(it);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "SpineAnimation.h"

using namespace spine;

spine::Vector<Timeline*> convertAndUseVector(const std::vector<std::shared_ptr<Timeline>>& stdVec) {
spine::Vector<Timeline*> spineVec;

for (const auto& element : stdVec) {
spineVec.add(element.get());
}

return spineVec;
}

SpineAnimation::SpineAnimation(const String& name, std::vector<std::shared_ptr<Timeline>> timelines, float duration) : Animation(name, convertAndUseVector(timelines), duration) {
_vecTimelines = timelines;
}

SpineAnimation::~SpineAnimation() {
_vecTimelines.clear();
auto& timelines = getTimelines();
timelines.clear();
}
19 changes: 19 additions & 0 deletions native/cocos/editor-support/spine-creator-support/SpineAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <spine/Animation.h>
#include <vector>
#include <memory>

namespace spine {

class SpineAnimation : public Animation {
public:
SpineAnimation(const String &name, std::vector<std::shared_ptr<Timeline>> timelines, float duration);

~SpineAnimation();

private:
std::vector<std::shared_ptr<Timeline>> _vecTimelines;
};

} // namespace spine
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include "SpineAnimationState.h"

using namespace spine;

SpineAnimationState::SpineAnimationState(AnimationStateData* data) : AnimationState(data) {

}

SpineAnimationState::~SpineAnimationState() {
_vecAnimations.clear();
}

TrackEntry* SpineAnimationState::addAnimation(size_t trackIndex, std::shared_ptr<Animation> animation, bool loop, float delay) {
_vecAnimations.push_back(animation);
return AnimationState::addAnimation(trackIndex, animation.get(), loop, delay);
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#pragma once

#include <spine/AnimationState.h>
#include "spine/Animation.h"
#include<vector>
#include<memory>

namespace spine {


class SP_API SpineAnimationState : public AnimationState {

public:
explicit SpineAnimationState(AnimationStateData* data);

~SpineAnimationState();

TrackEntry* addAnimation(size_t trackIndex, std::shared_ptr<Animation> animation, bool loop, float delay);

private:
std::vector<std::shared_ptr<Animation>> _vecAnimations;
};
} // namespace spine

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "spine-creator-support/SkeletonCacheMgr.h"
#include "spine-creator-support/SkeletonDataMgr.h"
#include "spine-creator-support/SkeletonRenderer.h"
#include "spine-creator-support/SpineAnimation.h"
#include "spine-creator-support/SpineAnimationState.h"
#include "spine/spine.h"

namespace spine {
Expand Down
18 changes: 18 additions & 0 deletions native/cocos/editor-support/spine-wasm/SpineAnimationState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include "SpineAnimationState.h"

using namespace spine;

SpineAnimationState::SpineAnimationState(AnimationStateData* data) : AnimationState(data) {
}

SpineAnimationState::~SpineAnimationState() {
_mapAnimations.clear();
}

TrackEntry* SpineAnimationState::addAnimation(size_t trackIndex, Animation* animation, bool loop, float delay) {
if (_mapAnimations.count(animation) == 0) {
_mapAnimations[animation] = std::shared_ptr<Animation>(animation);
}
return AnimationState::addAnimation(trackIndex, animation, loop, delay);
}
21 changes: 21 additions & 0 deletions native/cocos/editor-support/spine-wasm/SpineAnimationState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <spine/AnimationState.h>
#include <map>
#include <memory>
#include "spine/Animation.h"

namespace spine {

class SP_API SpineAnimationState : public AnimationState {
public:
explicit SpineAnimationState(AnimationStateData* data);

~SpineAnimationState();

TrackEntry* addAnimation(size_t trackIndex, Animation* animation, bool loop, float delay);

private:
std::map<Animation*, std::shared_ptr<Animation>> _mapAnimations;
};
} // namespace spine
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "spine-mesh-data.h"
#include "spine-wasm.h"
#include "util-function.h"
#include "../spine-creator-support/SpineAnimationState.h"

SlotMesh globalMesh(nullptr, nullptr, 0, 0);

Expand Down Expand Up @@ -57,7 +58,7 @@ Skeleton *SpineSkeletonInstance::initSkeleton(SkeletonData *data) {
_skeletonData = data;
_skeleton = new Skeleton(_skeletonData);
_animStateData = new AnimationStateData(_skeletonData);
_animState = new AnimationState(_animStateData);
_animState = new SpineAnimationState(_animStateData);
_clipper = new SkeletonClipping();
_skeleton->setToSetupPose();
_skeleton->updateWorldTransform();
Expand Down
Loading

0 comments on commit fa73743

Please sign in to comment.