Implement sf::Time with <chrono>

This commit is contained in:
Chris Thrasher 2022-06-16 17:06:05 -06:00 committed by Lukas Dürrenberger
parent 41053f9983
commit ebb2d9d186
2 changed files with 6 additions and 4 deletions

View File

@ -30,6 +30,8 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/System/Export.hpp> #include <SFML/System/Export.hpp>
#include <chrono>
namespace sf namespace sf
{ {
@ -103,7 +105,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Int64 m_microseconds; //!< Time value stored as microseconds std::chrono::microseconds m_microseconds; //!< Time value stored as microseconds
}; };
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -32,21 +32,21 @@ constexpr Time::Time() : m_microseconds(0)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
constexpr float Time::asSeconds() const constexpr float Time::asSeconds() const
{ {
return static_cast<float>(static_cast<double>(m_microseconds) / 1000000.0); return std::chrono::duration<float>(m_microseconds).count();
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
constexpr Int32 Time::asMilliseconds() const constexpr Int32 Time::asMilliseconds() const
{ {
return static_cast<Int32>(m_microseconds / 1000); return std::chrono::duration_cast<std::chrono::duration<Int32, std::milli>>(m_microseconds).count();
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
constexpr Int64 Time::asMicroseconds() const constexpr Int64 Time::asMicroseconds() const
{ {
return m_microseconds; return m_microseconds.count();
} }