From 97f85d44cbb222e3897ee532b940bdbe6c8b4ce9 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 22 Jan 2025 13:22:24 +0900 Subject: [PATCH] example/interaction: chores++ --- examples/Example.h | 5 +++++ examples/Interaction.cpp | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/Example.h b/examples/Example.h index b71bb02a..5834902f 100644 --- a/examples/Example.h +++ b/examples/Example.h @@ -71,6 +71,11 @@ struct Example virtual void populate(const char* path) {} virtual ~Example() {} + float timestamp() + { + return float(SDL_GetTicks()) * 0.001f; + } + void scandir(const char* path) { char buf[PATH_MAX]; diff --git a/examples/Interaction.cpp b/examples/Interaction.cpp index 97969575..3de8c422 100644 --- a/examples/Interaction.cpp +++ b/examples/Interaction.cpp @@ -29,6 +29,7 @@ struct UserExample : tvgexam::Example { unique_ptr animation; + float begin = 0.0f; bool hitting(const tvg::Paint* paint, int32_t x, int32_t y, float segment1, float segment2) { @@ -36,7 +37,8 @@ struct UserExample : tvgexam::Example tvgexam::verify(paint->bounds(&px, &py, &pw, &ph, true)); if (x >= px && x <= px + pw && y >= py && y <= py + ph) { tvgexam::verify(animation->segment(segment1, segment2)); - elapsed = 0.0f; + elapsed = 0; + begin = timestamp(); return true; } return false; @@ -122,12 +124,13 @@ struct UserExample : tvgexam::Example { if (!canvas) return false; - auto progress = tvgexam::progress(elapsed, animation->duration()); + auto progress = (timestamp() - begin) / animation->duration(); //Default is a stopped motion - if (progress > 0.95f) { + if (progress > 1.0f) { animation->segment(0.0f, 0.0f); - elapsed = progress = 0.0f; + this->elapsed = 0; + progress = 0.0f; } //Update animation frame only when it's changed