From 113ff70aecc1a89fc823a4839146f674b961f6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 8 Dec 2012 22:45:50 +0100 Subject: [PATCH] Ability to retrieve absolute time in previous frame to Timeline. The time is relative to moment when Timeline::start() was called. --- src/Timeline.cpp | 16 +++++++++++----- src/Timeline.h | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Timeline.cpp b/src/Timeline.cpp index 7183c899d..3a4419464 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -23,13 +23,15 @@ namespace Magnum { void Timeline::start() { running = true; - previousFrameTime = high_resolution_clock::now(); + _startTime = high_resolution_clock::now(); + _previousFrameTime = _startTime; _previousFrameDuration = 0; } void Timeline::stop() { running = false; - previousFrameTime = high_resolution_clock::time_point(); + _startTime = high_resolution_clock::time_point(); + _previousFrameTime = _startTime; _previousFrameDuration = 0; } @@ -37,16 +39,20 @@ void Timeline::nextFrame() { if(!running) return; auto now = high_resolution_clock::now(); - std::uint32_t duration = duration_cast(now-previousFrameTime).count(); + std::uint32_t duration = duration_cast(now-_previousFrameTime).count(); _previousFrameDuration = duration/1e6f; if(_previousFrameDuration < _minimalFrameTime) { Corrade::Utility::sleep(_minimalFrameTime*1000 - duration/1000); now = high_resolution_clock::now(); - _previousFrameDuration = duration_cast(now-previousFrameTime).count()/1e6f; + _previousFrameDuration = duration_cast(now-_previousFrameTime).count()/1e6f; } - previousFrameTime = now; + _previousFrameTime = now; +} + +GLfloat Timeline::previousFrameTime() const { + return duration_cast(_previousFrameTime-_startTime).count()/1e6f; } } diff --git a/src/Timeline.h b/src/Timeline.h index a6d5b714e..42c22249e 100644 --- a/src/Timeline.h +++ b/src/Timeline.h @@ -98,7 +98,7 @@ class MAGNUM_EXPORT Timeline { /** * @brief Start timeline * - * Sets previous frame duration to `0`. + * Sets previous frame time and duration to `0`. * @see stop(), previousFrameDuration() */ void start(); @@ -120,13 +120,26 @@ class MAGNUM_EXPORT Timeline { */ void nextFrame(); - /** @brief Duration of previous frame (in seconds) */ + /** + * @brief Time at previous frame (in seconds) + * + * Returns time elapsed since start() was called. If the timeline is + * stopped, the function returns `0.0f`. + */ + GLfloat previousFrameTime() const; + + /** + * @brief Duration of previous frame (in seconds) + * + * If the timeline is stopped, the function returns `0.0f`. + */ inline constexpr GLfloat previousFrameDuration() const { return _previousFrameDuration; } private: - std::chrono::high_resolution_clock::time_point previousFrameTime; + std::chrono::high_resolution_clock::time_point _startTime; + std::chrono::high_resolution_clock::time_point _previousFrameTime; GLfloat _minimalFrameTime; GLfloat _previousFrameDuration;