Replace float/UInt64 durations by a more precise Time class (#388)

Improve Clock class with atomic RestartIfOver method and allows to choose required precision
This commit is contained in:
Jérôme Leclercq
2022-12-29 21:31:46 +01:00
committed by GitHub
parent 1de5f65536
commit dd421a6385
84 changed files with 1278 additions and 663 deletions

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Vector3.hpp>
namespace Nz
@@ -34,6 +35,7 @@ namespace Nz
virtual float GetAttenuation() const = 0;
virtual float GetMinDistance() const = 0;
virtual float GetPitch() const = 0;
virtual Time GetPlayingOffset() const = 0;
virtual Vector3f GetPosition() const = 0;
virtual UInt32 GetSampleOffset() const = 0;
virtual OffsetWithLatency GetSampleOffsetAndLatency() const = 0;
@@ -53,6 +55,7 @@ namespace Nz
virtual void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) = 0;
virtual void SetMinDistance(float minDistance) = 0;
virtual void SetPitch(float pitch) = 0;
virtual void SetPlayingOffset(Time offset) = 0;
virtual void SetPosition(const Vector3f& position) = 0;
virtual void SetSampleOffset(UInt32 offset) = 0;
virtual void SetVelocity(const Vector3f& velocity) = 0;
@@ -70,7 +73,7 @@ namespace Nz
struct OffsetWithLatency
{
UInt64 sampleOffset;
UInt64 sourceLatency;
Time sourceLatency;
};
private:

View File

@@ -11,6 +11,7 @@
#include <Nazara/Audio/AudioBuffer.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
@@ -23,7 +24,7 @@ namespace Nz
~DummyAudioBuffer() = default;
AudioFormat GetAudioFormat() const;
UInt32 GetDuration() const;
Time GetDuration() const;
UInt64 GetSampleCount() const override;
UInt64 GetSize() const override;
UInt32 GetSampleRate() const override;

View File

@@ -30,6 +30,7 @@ namespace Nz
float GetAttenuation() const override;
float GetMinDistance() const override;
float GetPitch() const override;
Time GetPlayingOffset() const override;
Vector3f GetPosition() const override;
UInt32 GetSampleOffset() const override;
OffsetWithLatency GetSampleOffsetAndLatency() const override;
@@ -49,6 +50,7 @@ namespace Nz
void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) override;
void SetMinDistance(float minDistance) override;
void SetPitch(float pitch) override;
void SetPlayingOffset(Time offset) override;
void SetPosition(const Vector3f& position) override;
void SetSampleOffset(UInt32 offset) override;
void SetVelocity(const Vector3f& velocity) override;
@@ -65,11 +67,11 @@ namespace Nz
private:
void RequeueBuffers();
UInt64 UpdateTime() const;
Time UpdateTime() const;
mutable std::vector<std::shared_ptr<DummyAudioBuffer>> m_queuedBuffers;
mutable std::vector<std::shared_ptr<DummyAudioBuffer>> m_processedBuffers;
mutable Clock m_playClock;
mutable MillisecondClock m_playClock;
mutable SoundStatus m_status;
Vector3f m_position;
Vector3f m_velocity;

View File

@@ -9,7 +9,7 @@ namespace Nz
{
inline DummyAudioSource::DummyAudioSource(std::shared_ptr<AudioDevice> device) :
AudioSource(std::move(device)),
m_playClock(0, true),
m_playClock(Time::Zero(), true),
m_status(SoundStatus::Stopped),
m_position(Vector3f::Zero()),
m_velocity(Vector3f::Zero()),

View File

@@ -22,7 +22,7 @@ namespace Nz
{
class AudioBuffer;
class NAZARA_AUDIO_API Music : public Resource, public SoundEmitter
class NAZARA_AUDIO_API Music final : public Resource, public SoundEmitter
{
public:
Music();
@@ -36,11 +36,12 @@ namespace Nz
void EnableLooping(bool loop) override;
UInt32 GetDuration() const override;
Time GetDuration() const override;
AudioFormat GetFormat() const;
UInt32 GetPlayingOffset() const override;
Time GetPlayingOffset() const;
UInt64 GetSampleCount() const;
UInt32 GetSampleRate() const;
UInt64 GetSampleOffset() const override;
UInt32 GetSampleRate() const override;
SoundStatus GetStatus() const override;
bool IsLooping() const override;
@@ -52,7 +53,7 @@ namespace Nz
void Pause() override;
void Play() override;
void SetPlayingOffset(UInt32 offset);
void SeekToSampleOffset(UInt64 offset);
void Stop() override;

View File

@@ -34,6 +34,7 @@ namespace Nz
float GetAttenuation() const override;
float GetMinDistance() const override;
float GetPitch() const override;
Time GetPlayingOffset() const override;
Vector3f GetPosition() const override;
UInt32 GetSampleOffset() const override;
OffsetWithLatency GetSampleOffsetAndLatency() const override;
@@ -53,6 +54,7 @@ namespace Nz
void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) override;
void SetMinDistance(float minDistance) override;
void SetPitch(float pitch) override;
void SetPlayingOffset(Time offset) override;
void SetPosition(const Vector3f& position) override;
void SetSampleOffset(UInt32 offset) override;
void SetVelocity(const Vector3f& velocity) override;

View File

@@ -14,7 +14,7 @@
namespace Nz
{
class NAZARA_AUDIO_API Sound : public SoundEmitter
class NAZARA_AUDIO_API Sound final : public SoundEmitter
{
public:
using SoundEmitter::SoundEmitter;
@@ -27,8 +27,10 @@ namespace Nz
void EnableLooping(bool loop) override;
const std::shared_ptr<SoundBuffer>& GetBuffer() const;
UInt32 GetDuration() const override;
UInt32 GetPlayingOffset() const override;
Time GetDuration() const override;
Time GetPlayingOffset() const override;
UInt64 GetSampleOffset() const override;
UInt32 GetSampleRate() const override;
SoundStatus GetStatus() const override;
bool IsLooping() const override;
@@ -42,7 +44,8 @@ namespace Nz
void Play() override;
void SetBuffer(std::shared_ptr<SoundBuffer> soundBuffer);
void SetPlayingOffset(UInt32 offset);
void SeekToSampleOffset(UInt64 offset) override;
void Stop() override;

View File

@@ -16,6 +16,7 @@
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/ResourceParameters.hpp>
#include <Nazara/Core/Time.hpp>
#include <memory>
#include <unordered_map>
@@ -52,7 +53,7 @@ namespace Nz
const std::shared_ptr<AudioBuffer>& GetAudioBuffer(AudioDevice* device);
inline UInt32 GetDuration() const;
inline Time GetDuration() const;
inline AudioFormat GetFormat() const;
inline const Int16* GetSamples() const;
inline UInt64 GetSampleCount() const;
@@ -76,7 +77,7 @@ namespace Nz
std::unordered_map<AudioDevice*, AudioDeviceEntry> m_audioBufferByDevice;
std::unique_ptr<Int16[]> m_samples;
AudioFormat m_format;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_sampleCount;
};

View File

@@ -10,10 +10,8 @@ namespace Nz
/*!
* \brief Gets the duration of the sound buffer
* \return Duration of the sound buffer in milliseconds
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
inline UInt32 SoundBuffer::GetDuration() const
inline Time SoundBuffer::GetDuration() const
{
return m_duration;
}

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <limits>
@@ -32,11 +33,13 @@ namespace Nz
void EnableSpatialization(bool spatialization);
float GetAttenuation() const;
virtual UInt32 GetDuration() const = 0;
virtual Time GetDuration() const = 0;
float GetMinDistance() const;
float GetPitch() const;
virtual UInt32 GetPlayingOffset() const = 0;
virtual Time GetPlayingOffset() const = 0;
Vector3f GetPosition() const;
virtual UInt64 GetSampleOffset() const = 0;
virtual UInt32 GetSampleRate() const = 0;
Vector3f GetVelocity() const;
virtual SoundStatus GetStatus() const = 0;
float GetVolume() const;
@@ -48,6 +51,9 @@ namespace Nz
virtual void Pause() = 0;
virtual void Play() = 0;
virtual void SeekToPlayingOffset(Time offset);
virtual void SeekToSampleOffset(UInt64 offset) = 0;
void SetAttenuation(float attenuation);
void SetMinDistance(float minDistance);
void SetPitch(float pitch);

View File

@@ -13,6 +13,7 @@
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceParameters.hpp>
#include <Nazara/Core/Time.hpp>
#include <mutex>
namespace Nz
@@ -35,7 +36,7 @@ namespace Nz
SoundStream() = default;
virtual ~SoundStream();
virtual UInt32 GetDuration() const = 0;
virtual Time GetDuration() const = 0;
virtual AudioFormat GetFormat() const = 0;
virtual std::mutex& GetMutex() = 0;
virtual UInt64 GetSampleCount() const = 0;

View File

@@ -9,40 +9,46 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Time.hpp>
#include <optional>
namespace Nz
{
class NAZARA_CORE_API Clock
template<bool HighPrecision>
class Clock
{
public:
Clock(UInt64 startingValue = 0, bool paused = false);
Clock(Time startingValue = Time::Zero(), bool paused = false);
Clock(const Clock& clock) = default;
Clock(Clock&& clock) = default;
Clock(Clock&& clock) noexcept = default;
~Clock() = default;
float GetSeconds() const;
UInt64 GetMicroseconds() const;
UInt64 GetMilliseconds() const;
Time GetElapsedTime() const;
bool IsPaused() const;
void Pause();
UInt64 Restart(UInt64 startingValue = 0, bool paused = false);
Time Restart(Time startingPoint = Time::Zero(), bool paused = false);
std::optional<Time> RestartIfOver(Time time);
void Unpause();
Clock& operator=(const Clock& clock) = default;
Clock& operator=(Clock&& clock) = default;
Clock& operator=(Clock&& clock) noexcept = default;
static Time Now();
private:
UInt64 m_elapsedTime;
UInt64 m_refTime;
Time m_elapsedTime;
Time m_refTime;
bool m_paused;
};
using ClockFunction = UInt64 (*)();
extern NAZARA_CORE_API ClockFunction GetElapsedMicroseconds;
extern NAZARA_CORE_API ClockFunction GetElapsedMilliseconds;
using HighPrecisionClock = Clock<true>;
using MillisecondClock = Clock<false>;
}
#include <Nazara/Core/Clock.inl>
#endif // NAZARA_CORE_CLOCK_HPP

View File

@@ -0,0 +1,151 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
/*!
* \ingroup core
* \class Nz::Clock
* \brief Utility class that measure the elapsed time
*/
/*!
* \brief Constructs a Clock object
*
* \param startingValue The starting time value, in microseconds
* \param paused The clock pause state
*/
template<bool HighPrecision>
Clock<HighPrecision>::Clock(Time startingValue, bool paused) :
m_elapsedTime(startingValue),
m_refTime(Now()),
m_paused(paused)
{
}
/*!
* Returns the elapsed time
* \return Duration elapsed
*/
template<bool HighPrecision>
Time Clock<HighPrecision>::GetElapsedTime() const
{
Time elapsedNanoseconds = m_elapsedTime;
if (!m_paused)
elapsedNanoseconds += Now() - m_refTime;
return elapsedNanoseconds;
}
/*!
* Returns the current pause state of the clock
* \return Boolean indicating if the clock is currently paused
*
* \see Pause, Unpause
*/
template<bool HighPrecision>
bool Clock<HighPrecision>::IsPaused() const
{
return m_paused;
}
/*!
* \brief Pause the clock
*
* Pauses the clock, making the time retrieving functions to always return the value at the time the clock was paused
* This has no effect if the clock is already paused
*
* \see IsPaused, Unpause
*/
template<bool HighPrecision>
void Clock<HighPrecision>::Pause()
{
if (!m_paused)
{
m_elapsedTime += Now() - m_refTime;
m_paused = true;
}
}
/*!
* \brief Restart the clock
* \return Time elapsed since creation or last restart call
*
* Restarts the clock, putting its time counter back to the starting value
* It also compute the elapsed microseconds since the last Restart() call without any time loss (a problem that the combination of GetElapsedTime and Restart have).
*/
template<bool HighPrecision>
Time Clock<HighPrecision>::Restart(Time startingValue, bool paused)
{
Time now = Now();
Time elapsedTime = m_elapsedTime;
if (!m_paused)
elapsedTime += now - m_refTime;
m_elapsedTime = startingValue;
m_refTime = now;
m_paused = paused;
return elapsedTime;
}
/*!
* \brief Restart the clock if more than time elapsed
* \return If more than time elapsed since creation or last restart call
*
* Restarts the clock, putting its time counter back to zero
* This function allows to check the elapsed time of a clock and restart it if over some value in a single call, preventing some loss between GetElapsedTime and Restart
*/
template<bool HighPrecision>
std::optional<Time> Clock<HighPrecision>::RestartIfOver(Time time)
{
Time now = Now();
Time elapsedTime = m_elapsedTime;
if (!m_paused)
elapsedTime += now - m_refTime;
if (elapsedTime < time)
return std::nullopt;
m_elapsedTime = Time::Zero();
m_refTime = now;
return elapsedTime;
}
/*!
* \brief Unpause the clock
*
* Unpauses the clock, making the clock continue to measure the time
* This has no effect if the clock is already unpaused
*
* \see IsPaused, Unpause
*/
template<bool HighPrecision>
void Clock<HighPrecision>::Unpause()
{
if (m_paused)
{
m_refTime = Now();
m_paused = false;
}
}
template<bool HighPrecision>
Time Clock<HighPrecision>::Now()
{
if constexpr (HighPrecision)
return GetElapsedNanoseconds();
else
return GetElapsedMilliseconds();
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,6 +8,7 @@
#define NAZARA_CORE_COMPONENTS_LIFETIMECOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz
@@ -15,14 +16,14 @@ namespace Nz
class LifetimeComponent
{
public:
inline LifetimeComponent(float lifetime);
inline LifetimeComponent(Time lifetime);
LifetimeComponent(const LifetimeComponent&) = default;
LifetimeComponent(LifetimeComponent&&) = default;
~LifetimeComponent() = default;
inline void DecreaseLifetime(float elapsedTime);
inline void DecreaseLifetime(Time elapsedTime);
inline float GetRemainingLifeTime() const;
inline Time GetRemainingLifeTime() const;
inline bool IsAlive() const;
@@ -30,7 +31,7 @@ namespace Nz
LifetimeComponent& operator=(LifetimeComponent&&) = default;
private:
float m_remainingLifetime;
Time m_remainingLifetime;
};
}

View File

@@ -7,24 +7,24 @@
namespace Nz
{
inline LifetimeComponent::LifetimeComponent(float lifetime) :
inline LifetimeComponent::LifetimeComponent(Time lifetime) :
m_remainingLifetime(lifetime)
{
}
inline void LifetimeComponent::DecreaseLifetime(float elapsedTime)
inline void LifetimeComponent::DecreaseLifetime(Time elapsedTime)
{
m_remainingLifetime -= elapsedTime;
}
inline float LifetimeComponent::GetRemainingLifeTime() const
inline Time LifetimeComponent::GetRemainingLifeTime() const
{
return m_remainingLifetime;
}
inline bool LifetimeComponent::IsAlive() const
{
return m_remainingLifetime >= 0.f;
return m_remainingLifetime >= Time::Zero();
}
}

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Utils/TypeList.hpp>
#include <entt/entt.hpp>
@@ -26,7 +27,7 @@ namespace Nz
LifetimeSystem(LifetimeSystem&&) = delete;
~LifetimeSystem() = default;
void Update(float elapsedTime);
void Update(Time elapsedTime);
LifetimeSystem& operator=(const LifetimeSystem&) = delete;
LifetimeSystem& operator=(LifetimeSystem&&) = delete;

View File

@@ -31,7 +31,7 @@ namespace Nz
template<typename T> T& GetSystem() const;
void Update();
void Update(float elapsedTime);
void Update(Time elapsedTime);
SystemGraph& operator=(const SystemGraph&) = delete;
SystemGraph& operator=(SystemGraph&&) = delete;
@@ -41,7 +41,7 @@ namespace Nz
{
virtual ~NodeBase();
virtual void Update(float elapsedTime) = 0;
virtual void Update(Time elapsedTime) = 0;
Int64 executionOrder;
};
@@ -51,7 +51,7 @@ namespace Nz
{
template<typename... Args> Node(Args&&... args);
void Update(float elapsedTime) override;
void Update(Time elapsedTime) override;
T system;
};
@@ -60,7 +60,7 @@ namespace Nz
std::vector<NodeBase*> m_orderedNodes;
std::vector<std::unique_ptr<NodeBase>> m_nodes;
entt::registry& m_registry;
Nz::Clock m_clock;
Nz::HighPrecisionClock m_clock;
bool m_systemOrderUpdated;
};
}

View File

@@ -32,7 +32,7 @@ namespace Nz
}
template<typename T>
void SystemGraph::Node<T>::Update(float elapsedTime)
void SystemGraph::Node<T>::Update(Time elapsedTime)
{
system.Update(elapsedTime);
}

View File

@@ -0,0 +1,104 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_TIME_HPP
#define NAZARA_CORE_TIME_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Config.hpp>
#include <chrono>
#include <ostream>
#include <type_traits>
namespace Nz
{
struct SerializationContext;
class Time
{
public:
Time() = default;
Time(const Time&) = default;
Time(Time&&) = default;
~Time() = default;
template<typename T> constexpr T AsDuration() const;
template<typename T = float> constexpr T AsSeconds() const;
constexpr Int64 AsMicroseconds() const;
constexpr Int64 AsMilliseconds() const;
constexpr Int64 AsNanoseconds() const;
Time& operator=(const Time&) = default;
Time& operator=(Time&&) = default;
constexpr Time& operator+=(Time time);
constexpr Time& operator-=(Time time);
constexpr Time& operator*=(Time time);
constexpr Time& operator/=(Time time);
constexpr Time& operator%=(Time time);
constexpr explicit operator Int64() const;
template<class Rep, class Period> static constexpr Time FromDuration(const std::chrono::duration<Rep, Period>& d);
static constexpr Time Microsecond();
static constexpr Time Microseconds(Int64 microseconds);
static constexpr Time Millisecond();
static constexpr Time Milliseconds(Int64 milliseconds);
static constexpr Time Nanosecond();
static constexpr Time Nanoseconds(Int64 nanoseconds);
static constexpr Time Second();
template<typename T> static constexpr Time Seconds(T seconds);
static constexpr Time TickDuration(Int64 tickRate);
static constexpr Time Zero();
// External part
friend constexpr Time operator+(Time time);
friend constexpr Time operator-(Time time);
friend constexpr Time operator+(Time lhs, Time rhs);
friend constexpr Time operator-(Time lhs, Time rhs);
friend constexpr Time operator*(Time lhs, Time rhs);
friend constexpr Time operator/(Time lhs, Time rhs);
friend constexpr Time operator%(Time lhs, Time rhs);
friend constexpr bool operator==(Time lhs, Time rhs);
friend constexpr bool operator!=(Time lhs, Time rhs);
friend constexpr bool operator<(Time lhs, Time rhs);
friend constexpr bool operator<=(Time lhs, Time rhs);
friend constexpr bool operator>(Time lhs, Time rhs);
friend constexpr bool operator>=(Time lhs, Time rhs);
friend inline std::ostream& operator<<(std::ostream& out, Time time);
friend inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>);
friend inline bool Unserialize(SerializationContext& context, Time* time, TypeTag<Time>);
private:
constexpr explicit Time(Int64 nanoseconds);
Int64 m_nanoseconds;
};
namespace Literals
{
constexpr Time operator ""_ms(unsigned long long milliseconds);
constexpr Time operator ""_ns(unsigned long long nanoseconds);
constexpr Time operator ""_us(unsigned long long microseconds);
constexpr Time operator ""_s(long double seconds);
constexpr Time operator ""_s(unsigned long long seconds);
}
using GetElapsedTimeFunction = Time(*)();
extern NAZARA_CORE_API GetElapsedTimeFunction GetElapsedMilliseconds;
extern NAZARA_CORE_API GetElapsedTimeFunction GetElapsedNanoseconds;
}
#include <Nazara/Core/Time.inl>
#endif // NAZARA_CORE_TIME_HPP

View File

@@ -0,0 +1,270 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Time.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
constexpr Time::Time(Int64 microseconds) :
m_nanoseconds(microseconds)
{
}
template<typename T>
constexpr T Time::AsDuration() const
{
if constexpr (std::is_same_v<T, std::chrono::nanoseconds>)
return std::chrono::nanoseconds(m_nanoseconds); //< make sure it's a no-op
else
return std::chrono::duration_cast<T>(std::chrono::nanoseconds(m_nanoseconds));
}
template<typename T>
constexpr T Time::AsSeconds() const
{
static_assert(std::is_floating_point_v<T>);
// TODO: Improve precision
return AsMicroseconds() / T(1'000'000.0) + (m_nanoseconds % 1000) / T(1'000'000'000);
}
constexpr Int64 Time::AsMicroseconds() const
{
return m_nanoseconds / 1'000;
}
constexpr Int64 Time::AsMilliseconds() const
{
return m_nanoseconds / 1'000'000;
}
constexpr Int64 Time::AsNanoseconds() const
{
return m_nanoseconds;
}
constexpr Time& Time::operator+=(Time time)
{
m_nanoseconds += time.m_nanoseconds;
return *this;
}
constexpr Time& Time::operator-=(Time time)
{
m_nanoseconds -= time.m_nanoseconds;
return *this;
}
constexpr Time& Time::operator*=(Time time)
{
m_nanoseconds *= time.m_nanoseconds;
return *this;
}
constexpr Time& Time::operator/=(Time time)
{
m_nanoseconds /= time.m_nanoseconds;
return *this;
}
constexpr Time& Time::operator%=(Time time)
{
m_nanoseconds %= time.m_nanoseconds;
return *this;
}
constexpr Time::operator Int64() const
{
return m_nanoseconds;
}
template<class Rep, class Period>
constexpr Time Time::FromDuration(const std::chrono::duration<Rep, Period>& d)
{
return Nanoseconds(std::chrono::duration_cast<std::chrono::nanoseconds>(d).count());
}
constexpr Time Time::Microsecond()
{
return Time(1'000);
}
constexpr Time Time::Microseconds(Int64 microseconds)
{
return Time(microseconds * 1'000);
}
constexpr Time Time::Millisecond()
{
return Time(1'000'000);
}
constexpr Time Time::Milliseconds(Int64 milliseconds)
{
return Time(milliseconds * 1'000'000);
}
constexpr Time Time::Nanosecond()
{
return Time(1);
}
constexpr Time Time::Nanoseconds(Int64 nanoseconds)
{
return Time(nanoseconds);
}
constexpr Time Time::Second()
{
return Time(1'000'000'000ull);
}
template<typename T>
constexpr Time Time::Seconds(T seconds)
{
if constexpr (std::is_floating_point_v<T>)
return Nanoseconds(static_cast<UInt64>(seconds * T(1'000'000'000.0)));
else if constexpr (std::is_integral_v<T>)
return Nanoseconds(seconds * 1'000'000'000LL);
else
static_assert(AlwaysFalse<T>(), "not an arithmetic type");
}
constexpr Time Time::TickDuration(Int64 tickRate)
{
return Second() / Nanoseconds(tickRate);
}
constexpr Time Time::Zero()
{
return Time(0);
}
constexpr Time operator+(Time time)
{
return time;
}
constexpr Time operator-(Time time)
{
return Time(-time.m_nanoseconds);
}
constexpr Time operator+(Time lhs, Time rhs)
{
return Time(lhs.m_nanoseconds + rhs.m_nanoseconds);
}
constexpr Time operator-(Time lhs, Time rhs)
{
return Time(lhs.m_nanoseconds - rhs.m_nanoseconds);
}
constexpr Time operator*(Time lhs, Time rhs)
{
return Time(lhs.m_nanoseconds * rhs.m_nanoseconds);
}
constexpr Time operator/(Time lhs, Time rhs)
{
return Time(lhs.m_nanoseconds / rhs.m_nanoseconds);
}
constexpr Time operator%(Time lhs, Time rhs)
{
return Time(lhs.m_nanoseconds % rhs.m_nanoseconds);
}
constexpr bool operator==(Time lhs, Time rhs)
{
return lhs.m_nanoseconds == rhs.m_nanoseconds;
}
constexpr bool operator!=(Time lhs, Time rhs)
{
return lhs.m_nanoseconds != rhs.m_nanoseconds;
}
constexpr bool operator<(Time lhs, Time rhs)
{
return lhs.m_nanoseconds < rhs.m_nanoseconds;
}
constexpr bool operator<=(Time lhs, Time rhs)
{
return lhs.m_nanoseconds <= rhs.m_nanoseconds;
}
constexpr bool operator>(Time lhs, Time rhs)
{
return lhs.m_nanoseconds > rhs.m_nanoseconds;
}
constexpr bool operator>=(Time lhs, Time rhs)
{
return lhs.m_nanoseconds >= rhs.m_nanoseconds;
}
inline std::ostream& operator<<(std::ostream& out, Time time)
{
if (time > Time::Second())
return out << time.AsSeconds<double>() << "s";
else
{
Int64 ns = time.AsNanoseconds();
if (time > Time::Millisecond())
return out << ns / 1'000'000.0 << "ms";
else if (time > Time::Microsecond())
return out << ns / 1'000.0 << "us";
else
return out << ns << "ns";
}
}
inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>)
{
if (!Serialize(context, time.m_nanoseconds))
return false;
return true;
}
inline bool Unserialize(SerializationContext& context, Time* time, TypeTag<Time>)
{
if (!Unserialize(context, &time->m_nanoseconds))
return false;
return true;
}
namespace Literals
{
constexpr Time operator ""_ms(unsigned long long milliseconds)
{
return Time::Milliseconds(static_cast<Int64>(milliseconds));
}
constexpr Time operator ""_ns(unsigned long long nanoseconds)
{
return Time::Nanoseconds(static_cast<Int64>(nanoseconds));
}
constexpr Time operator ""_us(unsigned long long microseconds)
{
return Time::Microseconds(static_cast<Int64>(microseconds));
}
constexpr Time operator ""_s(long double milliseconds)
{
return Time::Seconds(milliseconds);
}
constexpr Time operator ""_s(unsigned long long milliseconds)
{
return Time::Seconds(milliseconds);
}
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,6 +8,7 @@
#define NAZARA_GRAPHICS_SYSTEMS_RENDERSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Graphics/ElementRendererRegistry.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
@@ -45,7 +46,7 @@ namespace Nz
inline FramePipeline& GetFramePipeline();
inline const FramePipeline& GetFramePipeline() const;
void Update(float elapsedTime);
void Update(Time elapsedTime);
RenderSystem& operator=(const RenderSystem&) = delete;
RenderSystem& operator=(RenderSystem&&) = delete;

View File

@@ -111,8 +111,8 @@ namespace Nz
inline void ENetHost::UpdateServiceTime()
{
// Compute service time as microseconds for extra precision
m_serviceTime = static_cast<UInt32>(GetElapsedMicroseconds() / 1000);
// Use high precision clock for extra precision
m_serviceTime = static_cast<UInt32>(GetElapsedNanoseconds().AsMilliseconds());
}
}

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Angle.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Physics2D/Config.hpp>
@@ -59,7 +60,7 @@ namespace Nz
cpSpace* GetHandle() const;
std::size_t GetIterationCount() const;
std::size_t GetMaxStepCount() const;
float GetStepSize() const;
Time GetStepSize() const;
bool NearestBodyQuery(const Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RigidBody2D** nearestBody = nullptr);
bool NearestBodyQuery(const Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, NearestQueryResult* result);
@@ -78,10 +79,10 @@ namespace Nz
void SetGravity(const Vector2f& gravity);
void SetIterationCount(std::size_t iterationCount);
void SetMaxStepCount(std::size_t maxStepCount);
void SetSleepTime(float sleepTime);
void SetStepSize(float stepSize);
void SetSleepTime(Time sleepTime);
void SetStepSize(Time stepSize);
void Step(float timestep);
void Step(Time timestep);
void UseSpatialHash(float cellSize, std::size_t entityCount);
@@ -156,8 +157,8 @@ namespace Nz
std::unordered_map<cpCollisionHandler*, std::unique_ptr<Callback>> m_callbacks;
std::unordered_map<RigidBody2D*, PostStepContainer> m_rigidPostSteps;
cpSpace* m_handle;
float m_stepSize;
float m_timestepAccumulator;
Time m_stepSize;
Time m_timestepAccumulator;
};
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_PHYSICS2D_SYSTEMS_PHYSICS2DSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Physics2D/PhysWorld2D.hpp>
#include <Nazara/Physics2D/Components/RigidBody2DComponent.hpp>
#include <Nazara/Utils/TypeList.hpp>
@@ -31,7 +32,7 @@ namespace Nz
inline PhysWorld2D& GetPhysWorld();
inline const PhysWorld2D& GetPhysWorld() const;
void Update(float elapsedTime);
void Update(Time elapsedTime);
Physics2DSystem& operator=(const Physics2DSystem&) = delete;
Physics2DSystem& operator=(Physics2DSystem&&) = delete;

View File

@@ -8,6 +8,7 @@
#define NAZARA_PHYSICS3D_PHYSWORLD3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Physics3D/Config.hpp>
@@ -44,12 +45,12 @@ namespace Nz
NewtonWorld* GetHandle() const;
int GetMaterial(const std::string& name);
std::size_t GetMaxStepCount() const;
float GetStepSize() const;
Time GetStepSize() const;
unsigned int GetThreadCount() const;
void SetGravity(const Vector3f& gravity);
void SetMaxStepCount(std::size_t maxStepCount);
void SetStepSize(float stepSize);
void SetStepSize(Time stepSize);
void SetThreadCount(unsigned int threadCount);
void SetMaterialCollisionCallback(int firstMaterial, int secondMaterial, AABBOverlapCallback aabbOverlapCallback, CollisionCallback collisionCallback);
@@ -59,7 +60,7 @@ namespace Nz
void SetMaterialDefaultSoftness(int firstMaterial, int secondMaterial, float softness);
void SetMaterialSurfaceThickness(int firstMaterial, int secondMaterial, float thickness);
void Step(float timestep);
void Step(Time timestep);
PhysWorld3D& operator=(const PhysWorld3D&) = delete;
PhysWorld3D& operator=(PhysWorld3D&&) noexcept;
@@ -79,8 +80,8 @@ namespace Nz
std::size_t m_maxStepCount;
MovablePtr<NewtonWorld> m_world;
Vector3f m_gravity;
float m_stepSize;
float m_timestepAccumulator;
Time m_stepSize;
Time m_timestepAccumulator;
};
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_PHYSICS3D_SYSTEMS_PHYSICS3DSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Physics3D/PhysWorld3D.hpp>
#include <Nazara/Physics3D/Components/RigidBody3DComponent.hpp>
#include <Nazara/Utils/TypeList.hpp>
@@ -31,7 +32,7 @@ namespace Nz
inline PhysWorld3D& GetPhysWorld();
inline const PhysWorld3D& GetPhysWorld() const;
void Update(float elapsedTime);
void Update(Time elapsedTime);
Physics3DSystem& operator=(const Physics3DSystem&) = delete;
Physics3DSystem& operator=(Physics3DSystem&&) = delete;

View File

@@ -56,7 +56,7 @@ namespace Nz
std::shared_ptr<RenderDevice> m_renderDevice;
std::unique_ptr<RenderSurface> m_surface;
std::unique_ptr<RenderWindowImpl> m_impl;
Clock m_clock;
MillisecondClock m_clock;
RenderWindowParameters m_parameters;
unsigned int m_framerateLimit;
};

View File

@@ -8,6 +8,7 @@
#define NAZARA_UTILITY_SYSTEMS_SKELETONSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Utility/Config.hpp>
#include <entt/entt.hpp>
@@ -24,7 +25,7 @@ namespace Nz
SkeletonSystem(SkeletonSystem&&) = delete;
~SkeletonSystem();
void Update(float elapsedTime);
void Update(Time elapsedTime);
SkeletonSystem& operator=(const SkeletonSystem&) = delete;
SkeletonSystem& operator=(SkeletonSystem&&) = delete;

View File

@@ -81,7 +81,6 @@ namespace Nz
Vk::QueueHandle m_presentQueue;
Vk::QueueHandle m_transferQueue;
Vk::Swapchain m_swapchain;
Clock m_clock;
RenderWindow& m_owner;
Vector2ui m_swapchainSize;
VkFormat m_depthStencilFormat;