From 28999d113ada927aeed9de4bc26c48217dbeb585 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 26 Oct 2022 13:07:39 +0200 Subject: [PATCH] Add simple timer functionality to Timeline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Vladimír Vondruš --- src/Magnum/Test/TimelineTest.cpp | 53 +++++++++++++++++++++++++++++--- src/Magnum/Timeline.cpp | 15 +++++++++ src/Magnum/Timeline.h | 27 +++++++++++++++- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/Magnum/Test/TimelineTest.cpp b/src/Magnum/Test/TimelineTest.cpp index dabb8ca2d..05f4e6ab8 100644 --- a/src/Magnum/Test/TimelineTest.cpp +++ b/src/Magnum/Test/TimelineTest.cpp @@ -47,23 +47,38 @@ void TimelineTest::test() { Timeline timeline; CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameTime(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameDuration(), 0.0f); /* And it continues to be 0 */ Utility::System::sleep(50); CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameTime(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameDuration(), 0.0f); /* There's no previous frame right after the start */ timeline.start(); CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); - /* Still no previous frame */ + /* Still no previous frame, but current frame starts growing */ Utility::System::sleep(50); CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.05f + 0.05f, + TestSuite::Compare::Less); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f + 0.05f, + TestSuite::Compare::Less); + CORRADE_COMPARE_WITH(timeline.currentFrameTime(), + timeline.currentFrameDuration(), + TestSuite::Compare::around(0.01f)); - /* Now it is */ + /* Now the previous frame is there */ timeline.nextFrame(); Float firstFrameTime = timeline.previousFrameTime(); Float firstFrameDuration = timeline.previousFrameDuration(); @@ -79,10 +94,19 @@ void TimelineTest::test() { timeline.previousFrameTime(), TestSuite::Compare::LessOrEqual); - /* And it doesn't change until another nextFrame() call */ + /* And it doesn't change until another nextFrame() call. Current frame + grows again. */ Utility::System::sleep(50); CORRADE_COMPARE(timeline.previousFrameTime(), firstFrameTime); CORRADE_COMPARE(timeline.previousFrameDuration(), firstFrameDuration); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.10f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.10f + 0.05f, + TestSuite::Compare::Less); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f + 0.05f, + TestSuite::Compare::Less); /* Third frame being measured now */ timeline.nextFrame(); @@ -97,10 +121,18 @@ void TimelineTest::test() { CORRADE_COMPARE_AS(secondFrameDuration, 0.05f + 0.05f, TestSuite::Compare::Less); - /* And it doesn't change now either */ + /* Previous frame doesn't change now either, current grows */ Utility::System::sleep(50); CORRADE_COMPARE(timeline.previousFrameTime(), secondFrameTime); CORRADE_COMPARE(timeline.previousFrameDuration(), secondFrameDuration); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.15f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.15f + 0.05f, + TestSuite::Compare::Less); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f + 0.05f, + TestSuite::Compare::Less); /* Calling start() resets the time to 0 */ timeline.start(); @@ -109,6 +141,15 @@ void TimelineTest::test() { /* And it continues to be counted */ Utility::System::sleep(50); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameTime(), 0.05f + 0.05f, + TestSuite::Compare::Less); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f, + TestSuite::Compare::GreaterOrEqual); + CORRADE_COMPARE_AS(timeline.currentFrameDuration(), 0.05f + 0.05f, + TestSuite::Compare::Less); + timeline.nextFrame(); CORRADE_COMPARE_AS(timeline.previousFrameTime(), 0.05f, TestSuite::Compare::GreaterOrEqual); @@ -126,12 +167,16 @@ void TimelineTest::test() { timeline.stop(); CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameTime(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameDuration(), 0.0f); /* And it continues to be 0 */ Utility::System::sleep(50); timeline.nextFrame(); CORRADE_COMPARE(timeline.previousFrameTime(), 0.0f); CORRADE_COMPARE(timeline.previousFrameDuration(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameTime(), 0.0f); + CORRADE_COMPARE(timeline.currentFrameDuration(), 0.0f); } }}} diff --git a/src/Magnum/Timeline.cpp b/src/Magnum/Timeline.cpp index 06f3c8ed6..10c43eef3 100644 --- a/src/Magnum/Timeline.cpp +++ b/src/Magnum/Timeline.cpp @@ -61,4 +61,19 @@ Float Timeline::previousFrameTime() const { return duration_cast(_previousFrameTime-_startTime).count()/1e6f; } +Float Timeline::currentFrameDuration() const { + if(!_running) return 0; + + auto now = high_resolution_clock::now(); + auto duration = UnsignedInt(duration_cast(now-_previousFrameTime).count()); + return duration/1e6f; +} + +Float Timeline::currentFrameTime() const { + if(!_running) return 0; + + auto now = high_resolution_clock::now(); + return duration_cast(now-_startTime).count()/1e6f; +} + } diff --git a/src/Magnum/Timeline.h b/src/Magnum/Timeline.h index 6c0fbbdd5..4292043bc 100644 --- a/src/Magnum/Timeline.h +++ b/src/Magnum/Timeline.h @@ -110,16 +110,41 @@ class MAGNUM_EXPORT Timeline { * * Returns time elapsed since @ref start() was called. If the timeline * is stopped, the function returns @cpp 0.0f @ce. + * @see @ref currentFrameTime() */ Float previousFrameTime() const; /** * @brief Duration of previous frame in seconds * - * If the timeline is stopped, the function returns @cpp 0.0f @ce. + * Return time measured between last two @ref nextFrame() calls, or + * between @ref start() and @ref nextFrame(), if the previous frame + * was the first. If the timeline is stopped, the function returns + * @cpp 0.0f @ce. + * @đee @ref currentFrameDuration() */ Float previousFrameDuration() const { return _previousFrameDuration; } + /** + * @brief Current time in seconds + * + * Returns time elapsed since @ref start() was called. Never smaller + * than @ref previousFrameTime(). If the timeline is stopped, the + * function returns @cpp 0.0f @ce. + */ + Float currentFrameTime() const; + + /** + * @brief Time since the last frame in seconds + * + * Returns time elapsed since @ref start() or @ref nextFrame() was + * called, whichever happened last. Compared to + * @ref previousFrameDuration() the returned value is different every + * time. If the timeline is stopped, the function returns + * @cpp 0.0f @ce. + */ + Float currentFrameDuration() const; + private: std::chrono::high_resolution_clock::time_point _startTime{}; std::chrono::high_resolution_clock::time_point _previousFrameTime{};