From c2f1b79e78e8656a94e73e7440a280ab6531d0e2 Mon Sep 17 00:00:00 2001 From: Basit Ayantunde Date: Sun, 15 Dec 2024 06:11:25 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20updated=20animation=20implementa?= =?UTF-8?q?tion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ashura/engine/animation.h | 32 ++++++++++++++++++-------------- ashura/engine/tests/animation.cc | 6 +++++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/ashura/engine/animation.h b/ashura/engine/animation.h index 5b53eaed..96268476 100644 --- a/ashura/engine/animation.h +++ b/ashura/engine/animation.h @@ -259,7 +259,7 @@ struct Timeline timestamps_.extend_uninit(durations.size()).unwrap(); - exclusive_scan(durations, + inclusive_scan(durations, timestamps_.view().slice(times_offset, durations.size()), run_time); @@ -401,24 +401,29 @@ struct AnimationState { CHECK(!timeline.is_empty()); + /// add 1ns so result of modulo operation would be between 0ns and timeline-duration + auto const timeline_end = timeline.duration() + 1ns; + auto const time = - (timeline.duration() == 0ns) ? 0ns : (time_ % timeline.duration()); + (timeline.duration() == 0ns) ? 0ns : (time_ % timeline_end); + + auto const timestamps = timeline.timestamps; // get current frame segment (timestamps are sorted, perform binary - // search (bounds) to get current timepoint in the timeline) - Span const time_span = upper_bound(timeline.timestamps, time); + // search to get current timepoint in the timeline) + Span const span = binary_find(timestamps.slice(1), geq, time); - u64 const end_idx = - static_cast(time_span.data() - timeline.timestamps.data()); + CHECK(!span.is_empty()); - u64 const ease_idx = end_idx - 1; + u64 const end_idx = static_cast(span.pbegin() - timestamps.pbegin()); + u64 const ease_idx = end_idx - 1; u64 const frame_idx = ease_idx * 2; - nanoseconds const time_start = timeline.timestamps[end_idx - 1]; - nanoseconds const time_end = timeline.timestamps[end_idx]; - nanoseconds const duration = time_end - time_start; - nanoseconds const offset = time - time_start; + nanoseconds const start = timestamps[end_idx - 1]; + nanoseconds const end = timestamps[end_idx]; + nanoseconds const duration = end - start; + nanoseconds const offset = time - start; f32 const t = (f32) (((f64) offset.count()) / (f64) duration.count()); @@ -782,11 +787,10 @@ struct StaggeredAnimation void tick(nanoseconds delta) { - // delay update? i.e. after animation - // what about with animation cycles? - // how will we manage this? for (AnimationState & state : states_) { + // update the total runtime + state.run_time_ = timelines_.v0.duration(); state.tick(delta); } } diff --git a/ashura/engine/tests/animation.cc b/ashura/engine/tests/animation.cc index 8162e799..2e1fd3f7 100644 --- a/ashura/engine/tests/animation.cc +++ b/ashura/engine/tests/animation.cc @@ -22,8 +22,12 @@ TEST(AnimationEngine, Basic) EXPECT_EQ(timeline.duration(), 1ms); EXPECT_EQ(animation.animate(0).v0, 20); + + animation.tick(500us); + + EXPECT_EQ(animation.animate(0).v0, 25); animation.tick(2ms); - EXPECT_NE(animation.animate(0).v0, 30); + EXPECT_EQ(animation.animate(0).v0, 30); }