Simplify implementation of sf::Clock

This commit is contained in:
Chris Thrasher 2023-02-11 14:02:22 -07:00 committed by GitHub
parent 6dd5f69d16
commit 949d9f44e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 47 deletions

View File

@ -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<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
using ClockImpl = std::conditional_t<std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock>;
#endif
static_assert(ClockImpl::is_steady, "Provided implementation is not a monotonic clock");
static_assert(std::ratio_less_equal_v<ClockImpl::period, std::micro>,
"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<ClockImpl::period, std::micro>::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

View File

@ -31,34 +31,21 @@
namespace sf
{
////////////////////////////////////////////////////////////
Clock::Clock() = default;
////////////////////////////////////////////////////////////
Time Clock::getElapsedTime() const
{
return durationToTime(ClockImpl::now() - m_startTime);
return std::chrono::duration_cast<std::chrono::microseconds>(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<std::chrono::microseconds>(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<microseconds>(duration);
}
} // namespace sf