Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,27 @@
*/
public class Animator {
private long mNativeObject;
private Boolean mIsOwner = false;
private boolean mIsOwner = false;

Animator(long nativeObject) {
mNativeObject = nativeObject;
}

/**
* Creates an Animator that can manipulate animations in the provided FilamentAsset.
* <p>
* <strong>Important:</strong> This Animator manages native resources that must be
* explicitly freed by calling {@link #destroy()} when no longer needed. Failing to
* call {@link #destroy()} will result in native memory leaks.
* </p>
*
* @param asset The FilamentAsset containing the animations
* @param instance The FilamentInstance to animate
*/
public Animator(FilamentAsset asset, FilamentInstance instance) {
mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject());
mIsOwner = true;
}

@Override
public void finalize() {
try {
super.finalize();
} catch (Throwable t) { // Ignore
} finally {
if (mIsOwner) {
nDestroyAnimator(mNativeObject);
mNativeObject = 0;
}
}
}

/**
* Applies rotation, translation, and scale to entities that have been targeted by the given
Expand Down Expand Up @@ -145,6 +143,19 @@ public String getAnimationName(@IntRange(from = 0) int animationIndex) {
return nGetAnimationName(getNativeObject(), animationIndex);
}

/**
* Explicitly destroys this Animator and frees all its associated native resources.
* The object will be unusable after this call.
*/
public void destroy() {
if (mNativeObject != 0) {
if (mIsOwner) {
nDestroyAnimator(mNativeObject);
}
mNativeObject = 0;
}
}

long getNativeObject() {
if (mNativeObject == 0) {
throw new IllegalStateException("Using Animator on destroyed asset");
Expand Down