First round of animation improvements

This commit is contained in:
2026-07-20 16:07:38 -05:00
parent 7ceb9e571d
commit 0826908068
20 changed files with 1049 additions and 49 deletions
+1
View File
@@ -3,6 +3,7 @@
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
add_subdirectory(animation)
add_subdirectory(assert)
add_subdirectory(asset)
add_subdirectory(error)
+11
View File
@@ -0,0 +1,11 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
include(dusktest)
# Tests
dusktest(test_keyframe.c)
dusktest(test_keyframeset.c)
dusktest(test_animation.c)
+111
View File
@@ -0,0 +1,111 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "time/time.h"
#include "animation/animation.h"
static void test_animationInitDefaults(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
assert_float_equal(anim.time, 0.0f, 0.0001f);
assert_float_equal(anim.speed, 1.0f, 0.0001f);
assert_false(anim.loop);
assert_false(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 0.0f, 0.0001f);
}
static void test_animationUpdateNoopWhenNotPlaying(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
TIME.delta = 1.0f;
animationUpdate(&anim);
assert_float_equal(anim.time, 0.0f, 0.0001f);
}
static void test_animationUpdateStopsAtEndWhenNotLooping(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
anim.playing = true;
TIME.delta = 0.5f;
animationUpdate(&anim);
assert_true(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 5.0f, 0.0001f);
animationUpdate(&anim);
assert_false(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 10.0f, 0.0001f);
// Stays clamped/held once stopped, even if updated again.
animationUpdate(&anim);
assert_float_equal(anim.time, 1.0f, 0.0001f);
}
static void test_animationUpdateLoopsWithMultipleChannels(void **state) {
keyframe_t xKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t yKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 2.0f, .value = 100.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { xKeyframes, yKeyframes };
uint16_t trackCounts[] = { 2, 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 2);
anim.loop = true;
anim.playing = true;
// Duration is the longer (Y) channel's 2s -- looping wraps against
// that, not X's shorter 1s.
TIME.delta = 1.5f;
animationUpdate(&anim);
assert_true(anim.playing);
assert_float_equal(anim.time, 1.5f, 0.0001f);
animationUpdate(&anim);
// 1.5 + 1.5 = 3.0 -> wraps to 1.0 against a 2.0s duration.
assert_true(anim.playing);
assert_float_equal(anim.time, 1.0f, 0.0001f);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_animationInitDefaults),
cmocka_unit_test(test_animationUpdateNoopWhenNotPlaying),
cmocka_unit_test(test_animationUpdateStopsAtEndWhenNotLooping),
cmocka_unit_test(test_animationUpdateLoopsWithMultipleChannels),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
+66
View File
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "animation/keyframe.h"
static void test_keyframeGetValueInterpolatesLinear(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
assert_float_equal(keyframeGetValue(keyframes, 2, 0.5f), 5.0f, 0.0001f);
}
static void test_keyframeGetValueClampsAtBoundaries(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
// Exactly on, and beyond, the final keyframe: holds its value rather
// than dividing by zero (start == end at that boundary).
assert_float_equal(keyframeGetValue(keyframes, 2, 1.0f), 10.0f, 0.0001f);
assert_float_equal(keyframeGetValue(keyframes, 2, 5.0f), 10.0f, 0.0001f);
// Exactly on the first keyframe: same boundary case at the start.
assert_float_equal(keyframeGetValue(keyframes, 2, 0.0f), 0.0f, 0.0001f);
}
static void test_keyframeGetValueRespectsEasing(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_IN_QUAD },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_IN_QUAD }
};
// easingInQuad(0.5) = 0.25 -> lerp(0, 10, 0.25) = 2.5, not the linear
// midpoint (5.0).
assert_float_equal(keyframeGetValue(keyframes, 2, 0.5f), 2.5f, 0.0001f);
}
static void test_keyframeGetValueWithThreeKeyframes(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR },
{ .time = 2.0f, .value = 0.0f, .easing = EASING_LINEAR }
};
assert_float_equal(keyframeGetValue(keyframes, 3, 1.0f), 10.0f, 0.0001f);
assert_float_equal(keyframeGetValue(keyframes, 3, 1.5f), 5.0f, 0.0001f);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_keyframeGetValueInterpolatesLinear),
cmocka_unit_test(test_keyframeGetValueClampsAtBoundaries),
cmocka_unit_test(test_keyframeGetValueRespectsEasing),
cmocka_unit_test(test_keyframeGetValueWithThreeKeyframes),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
+77
View File
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "animation/keyframeset.h"
static void test_keyframeSetGetValuePerTrack(void **state) {
keyframe_t xKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t yKeyframes[] = {
{ .time = 0.0f, .value = 100.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 200.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { xKeyframes, yKeyframes };
uint16_t trackCounts[] = { 2, 2 };
keyframeset_t set;
keyframeSetInit(&set, tracks, trackCounts, 2);
assert_float_equal(keyframeSetGetValue(&set, 0, 0.5f), 5.0f, 0.0001f);
assert_float_equal(keyframeSetGetValue(&set, 1, 0.5f), 150.0f, 0.0001f);
}
static void test_keyframeSetGetValuesFillsAllTracks(void **state) {
keyframe_t xKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t yKeyframes[] = {
{ .time = 0.0f, .value = 100.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 200.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { xKeyframes, yKeyframes };
uint16_t trackCounts[] = { 2, 2 };
keyframeset_t set;
keyframeSetInit(&set, tracks, trackCounts, 2);
float_t values[2];
keyframeSetGetValues(&set, 0.5f, values);
assert_float_equal(values[0], 5.0f, 0.0001f);
assert_float_equal(values[1], 150.0f, 0.0001f);
}
static void test_keyframeSetGetDurationIsLongestTrack(void **state) {
keyframe_t shortKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t longKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 3.5f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { shortKeyframes, longKeyframes };
uint16_t trackCounts[] = { 2, 2 };
keyframeset_t set;
keyframeSetInit(&set, tracks, trackCounts, 2);
assert_float_equal(keyframeSetGetDuration(&set), 3.5f, 0.0001f);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_keyframeSetGetValuePerTrack),
cmocka_unit_test(test_keyframeSetGetValuesFillsAllTracks),
cmocka_unit_test(test_keyframeSetGetDurationIsLongestTrack),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
+1
View File
@@ -9,3 +9,4 @@ include(dusktest)
dusktest(test_entitymanager.c)
dusktest(test_entityposition.c)
dusktest(test_entitytrigger.c)
dusktest(test_entityanimation.c)
+209
View File
@@ -0,0 +1,209 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "util/memory.h"
#include "time/time.h"
#include "entity/entitymanager.h"
#include "entity/component/animation/entityanimation.h"
static void test_entityAnimationSetKeyframesDefaults(void **state) {
entitymanager_t mgr;
entityManagerInit(&mgr);
entityid_t entity = entityManagerAdd(&mgr);
componentid_t anim = entityAddComponent(
&mgr, entity, COMPONENT_TYPE_ANIMATION
);
static keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *channels[] = { keyframes };
uint16_t channelCounts[] = { 2 };
entityAnimationSetKeyframes(&mgr, entity, anim, channels, channelCounts, 1);
assert_false(entityAnimationIsPlaying(&mgr, entity, anim));
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 0.0f, 0.0001f
);
entityManagerDispose(&mgr);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_entityAnimationPlayAdvancesAndStopsNonLooping(void **state) {
entitymanager_t mgr;
entityManagerInit(&mgr);
entityid_t entity = entityManagerAdd(&mgr);
componentid_t anim = entityAddComponent(
&mgr, entity, COMPONENT_TYPE_ANIMATION
);
static keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *channels[] = { keyframes };
uint16_t channelCounts[] = { 2 };
entityAnimationSetKeyframes(&mgr, entity, anim, channels, channelCounts, 1);
entityAnimationPlay(&mgr, entity, anim);
assert_true(entityAnimationIsPlaying(&mgr, entity, anim));
TIME.delta = 0.5f;
entityUpdate(&mgr, entity);
assert_true(entityAnimationIsPlaying(&mgr, entity, anim));
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 5.0f, 0.0001f
);
// Reaches the final keyframe exactly: stops, holds the final value
// (rather than dividing by zero evaluating exactly at the last
// keyframe -- see keyframeGetValue()).
entityUpdate(&mgr, entity);
assert_false(entityAnimationIsPlaying(&mgr, entity, anim));
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 10.0f, 0.0001f
);
entityManagerDispose(&mgr);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_entityAnimationLoopsAndWrapsTime(void **state) {
entitymanager_t mgr;
entityManagerInit(&mgr);
entityid_t entity = entityManagerAdd(&mgr);
componentid_t anim = entityAddComponent(
&mgr, entity, COMPONENT_TYPE_ANIMATION
);
static keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *channels[] = { keyframes };
uint16_t channelCounts[] = { 2 };
entityAnimationSetKeyframes(&mgr, entity, anim, channels, channelCounts, 1);
entityAnimationSetLoop(&mgr, entity, anim, true);
entityAnimationPlay(&mgr, entity, anim);
TIME.delta = 0.7f;
entityUpdate(&mgr, entity);
assert_true(entityAnimationIsPlaying(&mgr, entity, anim));
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 7.0f, 0.0001f
);
// Wraps back around instead of stopping: 0.7 + 0.7 = 1.4 -> 0.4.
entityUpdate(&mgr, entity);
assert_true(entityAnimationIsPlaying(&mgr, entity, anim));
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 4.0f, 0.0001f
);
entityManagerDispose(&mgr);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_entityAnimationStopAndSpeed(void **state) {
entitymanager_t mgr;
entityManagerInit(&mgr);
entityid_t entity = entityManagerAdd(&mgr);
componentid_t anim = entityAddComponent(
&mgr, entity, COMPONENT_TYPE_ANIMATION
);
static keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *channels[] = { keyframes };
uint16_t channelCounts[] = { 2 };
entityAnimationSetKeyframes(&mgr, entity, anim, channels, channelCounts, 1);
entityAnimationSetSpeed(&mgr, entity, anim, 2.0f);
entityAnimationPlay(&mgr, entity, anim);
TIME.delta = 0.25f;
entityUpdate(&mgr, entity);
// 0.25s of real time at 2x speed = 0.5 track-seconds -> halfway.
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 5.0f, 0.0001f
);
entityAnimationStop(&mgr, entity, anim);
assert_false(entityAnimationIsPlaying(&mgr, entity, anim));
// Stopping doesn't reset time -- value is held where it was.
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 5.0f, 0.0001f
);
entityUpdate(&mgr, entity);
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 5.0f, 0.0001f
);
entityManagerDispose(&mgr);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_entityAnimationMultipleChannels(void **state) {
entitymanager_t mgr;
entityManagerInit(&mgr);
entityid_t entity = entityManagerAdd(&mgr);
componentid_t anim = entityAddComponent(
&mgr, entity, COMPONENT_TYPE_ANIMATION
);
// X goes 0 -> 10 over 1s; Y goes 0 -> 100 over 2s -- different lengths,
// sharing one timeline.
static keyframe_t xKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
static keyframe_t yKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 2.0f, .value = 100.0f, .easing = EASING_LINEAR }
};
keyframe_t *channels[] = { xKeyframes, yKeyframes };
uint16_t channelCounts[] = { 2, 2 };
entityAnimationSetKeyframes(&mgr, entity, anim, channels, channelCounts, 2);
entityAnimationPlay(&mgr, entity, anim);
TIME.delta = 1.0f;
entityUpdate(&mgr, entity);
// Still playing: the set's duration is the longer (Y's) 2s.
assert_true(entityAnimationIsPlaying(&mgr, entity, anim));
// X reached/passed its own end -- held at its final value.
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 0), 10.0f, 0.0001f
);
// Y is halfway through its own 2s span.
assert_float_equal(
entityAnimationGetValue(&mgr, entity, anim, 1), 50.0f, 0.0001f
);
entityManagerDispose(&mgr);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_entityAnimationSetKeyframesDefaults),
cmocka_unit_test(test_entityAnimationPlayAdvancesAndStopsNonLooping),
cmocka_unit_test(test_entityAnimationLoopsAndWrapsTime),
cmocka_unit_test(test_entityAnimationStopAndSpeed),
cmocka_unit_test(test_entityAnimationMultipleChannels),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}