From 949d9f44e1c9757ab4c26ebaec5810b9593bc961 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 11 Feb 2023 14:02:22 -0700 Subject: [PATCH] Simplify implementation of `sf::Clock` --- include/SFML/System/Clock.hpp | 39 ++++++++--------------------------- src/SFML/System/Clock.cpp | 21 ++++--------------- 2 files changed, 13 insertions(+), 47 deletions(-) diff --git a/include/SFML/System/Clock.hpp b/include/SFML/System/Clock.hpp index 529275b6..5c66ab10 100644 --- a/include/SFML/System/Clock.hpp +++ b/include/SFML/System/Clock.hpp @@ -69,13 +69,15 @@ namespace priv /// //////////////////////////////////////////////////////////// #if defined(SFML_SYSTEM_ANDROID) && defined(SFML_ANDROID_USE_SUSPEND_AWARE_CLOCK) -using MostSuitableClock = SuspendAwareClock; +using ClockImpl = SuspendAwareClock; #else -using MostSuitableClock = std::conditional_t; +using ClockImpl = std::conditional_t; #endif +static_assert(ClockImpl::is_steady, "Provided implementation is not a monotonic clock"); +static_assert(std::ratio_less_equal_v, + "Clock resolution is too low. Expecting at least a microsecond precision"); + } // namespace priv class Time; @@ -83,18 +85,12 @@ class Time; //////////////////////////////////////////////////////////// /// \brief Utility class that measures the elapsed time /// +/// The clock starts automatically after being constructed. +/// //////////////////////////////////////////////////////////// class SFML_SYSTEM_API Clock { public: - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - /// The clock starts automatically after being constructed. - /// - //////////////////////////////////////////////////////////// - Clock(); - //////////////////////////////////////////////////////////// /// \brief Get the elapsed time /// @@ -119,27 +115,10 @@ public: Time restart(); private: - using ClockImpl = priv::MostSuitableClock; - - static_assert(ClockImpl::is_steady, "Provided implementation is not a monotonic clock"); - static_assert(std::ratio_less_equal::value, - "Clock resolution is too low. Expecting at least a microsecond precision"); - - //////////////////////////////////////////////////////////// - /// \brief Convert clock duration to Time - /// - /// This function acts as a utility for converting clock - /// duration type instance into sf::Time - /// - /// \return Time instance - /// - //////////////////////////////////////////////////////////// - [[nodiscard]] static Time durationToTime(ClockImpl::duration duration); - //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - ClockImpl::time_point m_startTime{ClockImpl::now()}; //!< Time of last reset + priv::ClockImpl::time_point m_startTime{priv::ClockImpl::now()}; //!< Time of last reset }; } // namespace sf diff --git a/src/SFML/System/Clock.cpp b/src/SFML/System/Clock.cpp index 47c93376..e73fbafa 100644 --- a/src/SFML/System/Clock.cpp +++ b/src/SFML/System/Clock.cpp @@ -31,34 +31,21 @@ namespace sf { -//////////////////////////////////////////////////////////// -Clock::Clock() = default; - - //////////////////////////////////////////////////////////// Time Clock::getElapsedTime() const { - return durationToTime(ClockImpl::now() - m_startTime); + return std::chrono::duration_cast(priv::ClockImpl::now() - m_startTime); } //////////////////////////////////////////////////////////// Time Clock::restart() { - const ClockImpl::time_point now = ClockImpl::now(); - Time elapsed = durationToTime(now - m_startTime); - m_startTime = now; + const auto now = priv::ClockImpl::now(); + const auto elapsed = std::chrono::duration_cast(now - m_startTime); + m_startTime = now; return elapsed; } - -//////////////////////////////////////////////////////////// -Time Clock::durationToTime(Clock::ClockImpl::duration duration) -{ - using std::chrono::duration_cast; - using std::chrono::microseconds; - return duration_cast(duration); -} - } // namespace sf