ANIM
This commit is contained in:
@@ -23,6 +23,9 @@ void animationInit(
|
||||
anim->speed = 1.0f;
|
||||
anim->loop = false;
|
||||
anim->playing = false;
|
||||
anim->eventTime = 0.0f;
|
||||
anim->eventCallback = NULL;
|
||||
anim->eventUser = NULL;
|
||||
}
|
||||
|
||||
void animationUpdate(animation_t *anim) {
|
||||
@@ -30,17 +33,39 @@ void animationUpdate(animation_t *anim) {
|
||||
if(!anim->playing) return;
|
||||
|
||||
float_t duration = keyframeSetGetDuration(&anim->keyframes);
|
||||
float_t prevTime = anim->time;
|
||||
anim->time += TIME.delta * anim->speed;
|
||||
|
||||
// Loops that overshoot duration wrap back into [0, duration) -- an event
|
||||
// near the end must still be checked across that wrap, as two segments:
|
||||
// (prevTime, duration] then [0, newTime].
|
||||
bool_t wrapped = anim->loop && duration > 0.0f && anim->time >= duration;
|
||||
|
||||
if(anim->loop) {
|
||||
if(duration > 0.0f) anim->time = mathModFloat(anim->time, duration);
|
||||
return;
|
||||
}
|
||||
|
||||
if(anim->time >= duration) {
|
||||
} else if(anim->time >= duration) {
|
||||
anim->time = duration;
|
||||
anim->playing = false;
|
||||
}
|
||||
|
||||
if(!anim->eventCallback) return;
|
||||
|
||||
bool_t crossed = wrapped
|
||||
? (prevTime < anim->eventTime || anim->time >= anim->eventTime)
|
||||
: (prevTime < anim->eventTime && anim->time >= anim->eventTime);
|
||||
if(crossed) anim->eventCallback(anim, anim->eventUser);
|
||||
}
|
||||
|
||||
void animationSetEvent(
|
||||
animation_t *anim,
|
||||
const float_t time,
|
||||
const animationeventcallback_t callback,
|
||||
void *user
|
||||
) {
|
||||
assertNotNull(anim, "Animation pointer cannot be null.");
|
||||
anim->eventTime = time;
|
||||
anim->eventCallback = callback;
|
||||
anim->eventUser = user;
|
||||
}
|
||||
|
||||
float_t animationGetValue(animation_t *anim, const uint16_t trackIndex) {
|
||||
|
||||
@@ -6,7 +6,18 @@
|
||||
#pragma once
|
||||
#include "keyframeset.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct animation_t animation_t;
|
||||
|
||||
/**
|
||||
* Callback fired once when animationUpdate() advances time past
|
||||
* eventTime (see animationSetEvent()).
|
||||
*
|
||||
* @param anim The animation whose event fired.
|
||||
* @param user The user pointer passed to animationSetEvent().
|
||||
*/
|
||||
typedef void (*animationeventcallback_t)(animation_t *anim, void *user);
|
||||
|
||||
typedef struct animation_t {
|
||||
/** The animation's tracks/channels and their raw keyframe data. */
|
||||
keyframeset_t keyframes;
|
||||
|
||||
@@ -22,6 +33,13 @@ typedef struct {
|
||||
|
||||
/** True while animationUpdate() should advance time each call. */
|
||||
bool_t playing;
|
||||
|
||||
/** Time (seconds) at which eventCallback fires; see animationSetEvent(). */
|
||||
float_t eventTime;
|
||||
/** Callback fired once when time advances past eventTime, or NULL. */
|
||||
animationeventcallback_t eventCallback;
|
||||
/** User pointer passed to eventCallback unchanged. */
|
||||
void *eventUser;
|
||||
} animation_t;
|
||||
|
||||
/**
|
||||
@@ -48,10 +66,31 @@ void animationInit(
|
||||
* [0, duration) on reaching it; otherwise time is clamped there and
|
||||
* playing is cleared.
|
||||
*
|
||||
* If eventCallback is set, it fires once when this call advances time
|
||||
* from at or before eventTime to after it (checked across the loop wrap
|
||||
* point too, so an event near the end of a looping animation still fires
|
||||
* every loop).
|
||||
*
|
||||
* @param anim The animation to update.
|
||||
*/
|
||||
void animationUpdate(animation_t *anim);
|
||||
|
||||
/**
|
||||
* Sets (or clears, with callback NULL) the single time-triggered event
|
||||
* fired by animationUpdate().
|
||||
*
|
||||
* @param anim The animation to set the event on.
|
||||
* @param time The time, in seconds, at which callback fires.
|
||||
* @param callback The callback to invoke, or NULL to clear any event.
|
||||
* @param user Arbitrary pointer forwarded to callback unchanged.
|
||||
*/
|
||||
void animationSetEvent(
|
||||
animation_t *anim,
|
||||
const float_t time,
|
||||
const animationeventcallback_t callback,
|
||||
void *user
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets the value of one of the animation's tracks at its current
|
||||
* playback time.
|
||||
|
||||
@@ -127,14 +127,6 @@ errorret_t assetMeshLoaderSync(assetloading_t *loading) {
|
||||
errorChain(ret);
|
||||
}
|
||||
|
||||
#if defined(DUSK_OPENGL) && !defined(DUSK_OPENGL_LEGACY)
|
||||
// VBO owns the data now; CPU copy is no longer needed. The platform
|
||||
// mesh object itself still needs meshDispose later - tracked via
|
||||
// out->meshInitialized, independent of the CPU buffer's lifetime.
|
||||
memoryFree(out->vertices);
|
||||
out->vertices = NULL;
|
||||
#endif
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user