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

@@ -15,9 +15,9 @@ namespace Nz
return m_format;
}
UInt32 DummyAudioBuffer::GetDuration() const
Time DummyAudioBuffer::GetDuration() const
{
return SafeCast<UInt32>((1000ULL * m_sampleCount / (GetChannelCount(m_format) * m_sampleRate)));
return Time::Microseconds((1'000'000LL * m_sampleCount / (GetChannelCount(m_format) * m_sampleRate)));
}
UInt64 DummyAudioBuffer::GetSampleCount() const

View File

@@ -39,6 +39,27 @@ namespace Nz
return m_pitch;
}
Time DummyAudioSource::GetPlayingOffset() const
{
if (m_status == SoundStatus::Stopped)
return Time::Zero(); //< Always return 0 when stopped, to mimic OpenAL behavior
Time bufferTime = UpdateTime();
Time playingOffset = Time::Zero();
// All processed buffers count
for (const auto& processedBuffer : m_processedBuffers)
playingOffset += processedBuffer->GetDuration();
if (!m_queuedBuffers.empty())
{
auto& frontBuffer = m_queuedBuffers.front();
playingOffset += std::min(bufferTime, frontBuffer->GetDuration());
}
return playingOffset;
}
Vector3f DummyAudioSource::GetPosition() const
{
return m_position;
@@ -49,7 +70,7 @@ namespace Nz
if (m_status == SoundStatus::Stopped)
return 0; //< Always return 0 when stopped, to mimic OpenAL behavior
UInt64 bufferTime = UpdateTime();
Time bufferTime = UpdateTime();
UInt64 sampleOffset = 0;
// All processed buffers count in sample offset
@@ -59,7 +80,7 @@ namespace Nz
if (!m_queuedBuffers.empty())
{
auto& frontBuffer = m_queuedBuffers.front();
UInt64 bufferOffset = bufferTime * frontBuffer->GetSampleRate() / 1000;
UInt64 bufferOffset = bufferTime.AsMicroseconds() * frontBuffer->GetSampleRate() / 1'000'000ll;
UInt64 bufferDuration = frontBuffer->GetSampleCount() / GetChannelCount(frontBuffer->GetAudioFormat());
sampleOffset += std::min(bufferOffset, bufferDuration);
@@ -72,7 +93,7 @@ namespace Nz
{
OffsetWithLatency info;
info.sampleOffset = GetSampleOffset() * 1000;
info.sourceLatency = 0;
info.sourceLatency = Time::Zero();
return info;
}
@@ -120,19 +141,19 @@ namespace Nz
void DummyAudioSource::Play()
{
if (m_status == SoundStatus::Paused)
m_playClock.Unpause();
else
if (m_status != SoundStatus::Paused)
{
// playing or stopped, restart
RequeueBuffers();
// special case, we are stopped but SetSampleOffset has been called
if (m_status == SoundStatus::Stopped && m_playClock.GetMilliseconds() != 0)
if (m_status == SoundStatus::Stopped && m_playClock.GetElapsedTime() != Time::Zero())
m_playClock.Unpause();
else
m_playClock.Restart(); //< already playing or stopped, restart from beginning
}
else
m_playClock.Unpause();
m_status = SoundStatus::Playing;
}
@@ -161,6 +182,13 @@ namespace Nz
m_pitch = pitch;
}
void DummyAudioSource::SetPlayingOffset(Time offset)
{
// Next UpdateTime call will handle this properly
RequeueBuffers();
m_playClock.Restart(offset, m_playClock.IsPaused());
}
void DummyAudioSource::SetPosition(const Vector3f& position)
{
m_position = position;
@@ -187,7 +215,7 @@ namespace Nz
if (!m_queuedBuffers.empty())
{
UInt64 timeOffset = 1'000'000ULL * offset / m_queuedBuffers.front()->GetSampleRate();
Time timeOffset = Time::Microseconds(1'000'000ll * offset / m_queuedBuffers.front()->GetSampleRate());
m_playClock.Restart(timeOffset, m_playClock.IsPaused());
}
else
@@ -206,7 +234,7 @@ namespace Nz
void DummyAudioSource::Stop()
{
m_playClock.Restart(0, true);
m_playClock.Restart(Time::Zero(), true);
m_status = SoundStatus::Stopped;
}
@@ -246,9 +274,10 @@ namespace Nz
}
}
UInt64 DummyAudioSource::UpdateTime() const
Time DummyAudioSource::UpdateTime() const
{
UInt64 currentTime = m_playClock.GetMilliseconds();
Time currentTime = m_playClock.GetElapsedTime();
bool isPaused = m_playClock.IsPaused();
while (!m_queuedBuffers.empty() && currentTime >= m_queuedBuffers.front()->GetDuration())
{
@@ -278,10 +307,14 @@ namespace Nz
}
}
else
{
m_status = SoundStatus::Stopped;
currentTime = Time::Zero();
isPaused = m_playClock.IsPaused();
}
}
m_playClock.Restart(currentTime * 1000, m_playClock.IsPaused()); //< Adjust time
m_playClock.Restart(currentTime, isPaused); //< Adjust time
return currentTime;
}
}

View File

@@ -105,7 +105,7 @@ namespace Nz
drwav_uninit(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -172,7 +172,7 @@ namespace Nz
m_format = *formatOpt;
m_duration = static_cast<UInt32>(1000ULL * m_decoder.totalPCMFrameCount / m_decoder.sampleRate);
m_duration = Time::Microseconds(1'000'000LL * m_decoder.totalPCMFrameCount / m_decoder.sampleRate);
m_sampleCount = m_decoder.totalPCMFrameCount * m_decoder.channels;
m_sampleRate = m_decoder.sampleRate;
@@ -230,7 +230,7 @@ namespace Nz
std::vector<Int16> m_mixBuffer;
AudioFormat m_format;
drwav m_decoder;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -265,7 +265,7 @@ namespace Nz
}
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -333,7 +333,7 @@ namespace Nz
m_sampleCount = frameCount * m_channelCount;
m_sampleRate = meta->data.stream_info.sample_rate;
m_duration = UInt32(1000ULL * frameCount / m_sampleRate);
m_duration = Time::Microseconds(1'000'000LL * frameCount / m_sampleRate);
};
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &FlacReadCallback, &FlacSeekCallback, &FlacTellCallback, &FlacLengthCallback, &FlacEofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &m_userData);
@@ -477,8 +477,8 @@ namespace Nz
FLAC__StreamDecoder* m_decoder;
AudioFormat m_format;
FlacUserdata m_userData;
Time m_duration;
UInt32 m_channelCount;
UInt32 m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -184,7 +184,7 @@ namespace Nz
ov_clear(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -258,7 +258,7 @@ namespace Nz
UInt64 frameCount = UInt64(ov_pcm_total(&m_decoder, -1));
m_channelCount = info->channels;
m_duration = UInt32(1000ULL * frameCount / info->rate);
m_duration = Time::Microseconds(1'000'000LL * frameCount / info->rate);
m_sampleCount = UInt64(frameCount * info->channels);
m_sampleRate = info->rate;
@@ -319,8 +319,8 @@ namespace Nz
std::vector<Int16> m_mixBuffer;
AudioFormat m_format;
OggVorbis_File m_decoder;
Time m_duration;
UInt32 m_channelCount;
UInt32 m_duration;
UInt32 m_sampleRate;
UInt64 m_sampleCount;
bool m_mixToMono;

View File

@@ -130,7 +130,7 @@ namespace Nz
mp3dec_ex_close(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -214,7 +214,7 @@ namespace Nz
m_format = *formatOpt;
m_duration = static_cast<UInt32>(1000ULL * m_decoder.samples / (m_decoder.info.hz * m_decoder.info.channels));
m_duration = Time::Microseconds(1'000'000LL * m_decoder.samples / (m_decoder.info.hz * m_decoder.info.channels));
m_sampleCount = m_decoder.samples;
m_sampleRate = m_decoder.info.hz;
@@ -275,7 +275,7 @@ namespace Nz
AudioFormat m_format;
mp3dec_ex_t m_decoder;
mp3dec_io_t m_io;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -67,7 +67,7 @@ namespace Nz
m_chunkSamples.resize(GetChannelCount(format) * m_sampleRate); // One second of samples
m_stream = std::move(soundStream);
SetPlayingOffset(0);
SeekToSampleOffset(0);
return true;
}
@@ -102,7 +102,7 @@ namespace Nz
*
* \remark Music must be valid when calling this function
*/
UInt32 Music::GetDuration() const
Time Music::GetDuration() const
{
NazaraAssert(m_stream, "Music not created");
@@ -123,30 +123,30 @@ namespace Nz
}
/*!
* \brief Gets the current offset in the music
* \return Offset in milliseconds (works with entire seconds)
*
* \remark Music must be valid when calling this function
* \brief Gets the current playing offset of the music
* \return Time offset
*/
UInt32 Music::GetPlayingOffset() const
Time Music::GetPlayingOffset() const
{
NazaraAssert(m_stream, "Music not created");
if (!m_streaming)
return 0;
return Time::Zero();
// Prevent music thread from enqueuing new buffers while we're getting the count
std::lock_guard<std::recursive_mutex> lock(m_sourceLock);
UInt32 sampleOffset = m_source->GetSampleOffset();
UInt32 playingOffset = SafeCast<UInt32>((1000ULL * (sampleOffset + (m_processedSamples / GetChannelCount(m_stream->GetFormat())))) / m_sampleRate);
UInt32 duration = m_stream->GetDuration();
if (playingOffset > duration)
Time playingOffset = m_source->GetPlayingOffset();
Time processedTime = Time::Microseconds(1'000'000ll * m_processedSamples / (GetChannelCount(m_stream->GetFormat()) * m_sampleRate));
playingOffset += processedTime;
Time sampleCount = m_stream->GetDuration();
if (playingOffset > sampleCount)
{
if (m_looping)
playingOffset %= duration;
playingOffset %= sampleCount;
else
playingOffset = 0; //< stopped
playingOffset = Time::Zero(); //< stopped
}
return playingOffset;
@@ -165,6 +165,33 @@ namespace Nz
return m_stream->GetSampleCount();
}
/*!
* \brief Gets the current offset in the music
* \return Offset in samples
*/
UInt64 Music::GetSampleOffset() const
{
NazaraAssert(m_stream, "Music not created");
if (!m_streaming)
return 0;
// Prevent music thread from enqueuing new buffers while we're getting the count
std::lock_guard<std::recursive_mutex> lock(m_sourceLock);
UInt64 sampleOffset = m_processedSamples + m_source->GetSampleOffset();
UInt64 sampleCount = m_stream->GetSampleCount();
if (sampleOffset > sampleCount)
{
if (m_looping)
sampleOffset %= sampleCount;
else
sampleOffset = 0; //< stopped
}
return sampleOffset;
}
/*!
* \brief Gets the rates of sample in the music
* \return Rate of sample in Hertz (Hz)
@@ -296,7 +323,7 @@ namespace Nz
switch (GetStatus())
{
case SoundStatus::Playing:
SetPlayingOffset(0);
SeekToSampleOffset(0);
break;
case SoundStatus::Paused:
@@ -325,11 +352,11 @@ namespace Nz
*
* If the music is not playing, this sets the playing offset for the next Play call
*
* \param offset The offset in milliseconds
* \param offset The offset in samples
*
* \remark Music must be valid when calling this function
*/
void Music::SetPlayingOffset(UInt32 offset)
void Music::SeekToSampleOffset(UInt64 offset)
{
NazaraAssert(m_stream, "Music not created");
@@ -339,7 +366,7 @@ namespace Nz
if (isPlaying)
StopThread();
UInt64 sampleOffset = UInt64(offset) * m_sampleRate * GetChannelCount(m_stream->GetFormat()) / 1000ULL;
UInt64 sampleOffset = offset * GetChannelCount(m_stream->GetFormat());
m_processedSamples = sampleOffset;
m_streamOffset = sampleOffset;
@@ -356,7 +383,7 @@ namespace Nz
void Music::Stop()
{
StopThread();
SetPlayingOffset(0);
SeekToSampleOffset(0);
}
bool Music::FillAndQueueBuffer(std::shared_ptr<AudioBuffer> buffer)

View File

@@ -62,6 +62,29 @@ namespace Nz
return pitch;
}
Time OpenALSource::GetPlayingOffset() const
{
GetDevice().MakeContextCurrent();
#ifdef AL_SOFT_source_latency
if (GetDevice().IsExtensionSupported(OpenALExtension::SourceLatency))
{
// alGetSourcedvSOFT has extra precision thanks to double
ALdouble playingOffset;
m_library.alGetSourcedvSOFT(m_sourceId, AL_SEC_OFFSET, &playingOffset);
return Time::Seconds(playingOffset);
}
else
#endif
{
ALfloat playingOffset;
m_library.alGetSourcefv(m_sourceId, AL_SEC_OFFSET, &playingOffset);
return Time::Seconds(playingOffset);
}
}
Vector3f OpenALSource::GetPosition() const
{
GetDevice().MakeContextCurrent();
@@ -94,14 +117,14 @@ namespace Nz
m_library.alGetSourcei64vSOFT(m_sourceId, AL_SAMPLE_OFFSET_LATENCY_SOFT, values.data());
offsetWithLatency.sampleOffset = ((values[0] & 0xFFFFFFFF00000000) >> 32) * 1'000;
offsetWithLatency.sourceLatency = values[1] / 1'000;
offsetWithLatency.sourceLatency = Time::Nanoseconds(values[1] / 1'000);
}
else
#endif
{
offsetWithLatency.sampleOffset = GetSampleOffset() * 1'000;
offsetWithLatency.sourceLatency = 0;
offsetWithLatency.sourceLatency = Time::Zero();
}
return offsetWithLatency;
@@ -239,6 +262,19 @@ namespace Nz
m_library.alSourcef(m_sourceId, AL_PITCH, pitch);
}
void OpenALSource::SetPlayingOffset(Time offset)
{
GetDevice().MakeContextCurrent();
#ifdef AL_SOFT_source_latency
if (GetDevice().IsExtensionSupported(OpenALExtension::SourceLatency))
// alGetSourcedvSOFT has extra precision thanks to double
m_library.alSourcedSOFT(m_sourceId, AL_SEC_OFFSET, offset.AsSeconds<ALdouble>());
else
#endif
m_library.alSourcef(m_sourceId, AL_SEC_OFFSET, offset.AsSeconds<ALfloat>());
}
void OpenALSource::SetPosition(const Vector3f& position)
{
GetDevice().MakeContextCurrent();

View File

@@ -72,7 +72,7 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no buffer
*/
UInt32 Sound::GetDuration() const
Time Sound::GetDuration() const
{
NazaraAssert(m_buffer, "Invalid sound buffer");
@@ -80,13 +80,30 @@ namespace Nz
}
/*!
* \brief Gets the current offset in the sound
* \return Offset in milliseconds (works with entire seconds)
* \brief Gets the current playing offset of the sound
* \return Offset
*/
UInt32 Sound::GetPlayingOffset() const
Time Sound::GetPlayingOffset() const
{
UInt32 sampleCount = m_source->GetSampleOffset();
return SafeCast<UInt32>(1000ULL * sampleCount / m_buffer->GetSampleRate());
return m_source->GetPlayingOffset();
}
/*!
* \brief Gets the current sample offset of the sound
* \return Offset
*/
UInt64 Sound::GetSampleOffset() const
{
return m_source->GetSampleOffset();
}
/*!
* \brief Gets the sample rate of the sound
* \return Offset
*/
UInt32 Sound::GetSampleRate() const
{
return m_buffer->GetSampleRate();
}
/*!
@@ -220,19 +237,17 @@ namespace Nz
}
/*!
* \brief Sets the playing offset for the sound
* \brief Sets the source to a sample offset
*
* \param offset Offset in the sound in milliseconds
* \param offset Sample offset
*/
void Sound::SetPlayingOffset(UInt32 offset)
void Sound::SeekToSampleOffset(UInt64 offset)
{
m_source->SetSampleOffset(SafeCast<UInt32>(UInt64(offset) * m_buffer->GetSampleRate() / 1000));
m_source->SetSampleOffset(SafeCast<UInt32>(offset));
}
/*!
* \brief Stops the sound
*
* \remark This is one of the only function that can be called on a moved sound (and does nothing)
*/
void Sound::Stop()
{

View File

@@ -47,7 +47,7 @@ namespace Nz
NazaraAssert(sampleRate > 0, "sample rate must be different from zero");
NazaraAssert(samples, "invalid samples");
m_duration = SafeCast<UInt32>((1000ULL*sampleCount / (GetChannelCount(format) * sampleRate)));
m_duration = Time::Microseconds((1'000'000LL * sampleCount / (GetChannelCount(format) * sampleRate)));
m_format = format;
m_sampleCount = sampleCount;
m_sampleRate = sampleRate;

View File

@@ -105,6 +105,17 @@ namespace Nz
return m_source->IsSpatializationEnabled();
}
/*!
* \brief Seek the sound to a point in time
*
* \param offset Time offset to seek
*/
void SoundEmitter::SeekToPlayingOffset(Time offset)
{
UInt64 microseconds = static_cast<UInt64>(std::max(offset.AsMicroseconds(), Int64(0)));
SeekToSampleOffset(SafeCast<UInt32>(microseconds * GetSampleRate() / 1'000'000));
}
/*!
* \brief Sets the attenuation
*

View File

@@ -1,162 +0,0 @@
// 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>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/ClockImpl.hpp>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <Nazara/Core/Posix/ClockImpl.hpp>
#else
#error OS not handled
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace Detail
{
UInt64 GetMicrosecondsLowPrecision()
{
return ClockImplGetElapsedMilliseconds()*1000ULL;
}
UInt64 GetElapsedMicrosecondsFirstRun()
{
if (ClockImplInitializeHighPrecision())
GetElapsedMicroseconds = ClockImplGetElapsedMicroseconds;
else
GetElapsedMicroseconds = GetMicrosecondsLowPrecision;
return GetElapsedMicroseconds();
}
}
/*!
* \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
*/
Clock::Clock(UInt64 startingValue, bool paused) :
m_elapsedTime(startingValue),
m_refTime(GetElapsedMicroseconds()),
m_paused(paused)
{
}
/*!
* Returns the elapsed time in seconds
* \return Seconds elapsed
*
* \see GetMicroseconds, GetMilliseconds
*/
float Clock::GetSeconds() const
{
return GetMicroseconds()/1'000'000.f;
}
/*!
* Returns the elapsed time in microseconds
* \return Microseconds elapsed
*
* \see GetMilliseconds, GetSeconds
*/
UInt64 Clock::GetMicroseconds() const
{
UInt64 elapsedMicroseconds = m_elapsedTime;
if (!m_paused)
elapsedMicroseconds += (GetElapsedMicroseconds() - m_refTime);
return elapsedMicroseconds;
}
/*!
* Returns the elapsed time in milliseconds
* \return Milliseconds elapsed
*
* \see GetMicroseconds, GetSeconds
*/
UInt64 Clock::GetMilliseconds() const
{
return GetMicroseconds()/1000;
}
/*!
* Returns the current pause state of the clock
* \return Boolean indicating if the clock is currently paused
*
* \see Pause, Unpause
*/
bool Clock::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
*/
void Clock::Pause()
{
if (!m_paused)
{
m_elapsedTime += GetElapsedMicroseconds() - m_refTime;
m_paused = true;
}
}
/*!
* \brief Restart the clock
* \return Microseconds elapsed
*
* Restarts the clock, putting it's time counter back to zero (as if the clock got constructed).
* It also compute the elapsed microseconds since the last Restart() call without any time loss (a problem that the combination of GetElapsedMicroseconds and Restart have).
*/
UInt64 Clock::Restart(UInt64 startingValue, bool paused)
{
Nz::UInt64 now = GetElapsedMicroseconds();
Nz::UInt64 elapsedTime = m_elapsedTime;
if (!m_paused)
elapsedTime += (now - m_refTime);
m_elapsedTime = startingValue;
m_refTime = now;
m_paused = paused;
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
*/
void Clock::Unpause()
{
if (m_paused)
{
m_refTime = GetElapsedMicroseconds();
m_paused = false;
}
}
ClockFunction GetElapsedMicroseconds = Detail::GetElapsedMicrosecondsFirstRun;
ClockFunction GetElapsedMilliseconds = ClockImplGetElapsedMilliseconds;
}

View File

@@ -0,0 +1,29 @@
// 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/Darwin/TimeImpl.hpp>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer()
{
return true; //< No initialization required
}
Time GetElapsedNanosecondsImpl()
{
UInt64 nanoseconds = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
return Time::Nanoseconds(static_cast<Int64>(nanoseconds));
}
Time GetElapsedMillisecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
}

View File

@@ -0,0 +1,20 @@
// 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_DARWIN_TIMEIMPL_HPP
#define NAZARA_CORE_DARWIN_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_DARWIN_TIMEIMPL_HPP

View File

@@ -1,29 +0,0 @@
// Copyright (C) 2022 Alexandre Janniaux
// 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/Posix/ClockImpl.hpp>
#include <sys/time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision()
{
return true; // No initialization needed
}
UInt64 ClockImplGetElapsedMicroseconds()
{
timeval clock;
gettimeofday(&clock, nullptr);
return static_cast<UInt64>(clock.tv_sec*1000000 + clock.tv_usec);
}
UInt64 ClockImplGetElapsedMilliseconds()
{
timeval clock;
gettimeofday(&clock, nullptr);
return static_cast<UInt64>(clock.tv_sec*1000 + (clock.tv_usec/1000));
}
}

View File

@@ -1,19 +0,0 @@
// Copyright (C) 2022 Alexandre Janniaux
// 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_POSIX_CLOCKIMPL_HPP
#define NAZARA_CORE_POSIX_CLOCKIMPL_HPP
#include <Nazara/Prerequisites.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision();
UInt64 ClockImplGetElapsedMicroseconds();
UInt64 ClockImplGetElapsedMilliseconds();
}
#endif // NAZARA_CORE_POSIX_CLOCKIMPL_HPP

View File

@@ -0,0 +1,31 @@
// 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/Posix/TimeImpl.hpp>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer()
{
return true; //< No initialization required
}
Time GetElapsedNanosecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
Time GetElapsedMillisecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
}

View File

@@ -4,16 +4,17 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#define NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#ifndef NAZARA_CORE_POSIX_TIMEIMPL_HPP
#define NAZARA_CORE_POSIX_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision();
UInt64 ClockImplGetElapsedMicroseconds();
UInt64 ClockImplGetElapsedMilliseconds();
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#endif // NAZARA_CORE_POSIX_TIMEIMPL_HPP

View File

@@ -8,7 +8,7 @@
namespace Nz
{
void LifetimeSystem::Update(float elapsedTime)
void LifetimeSystem::Update(Time elapsedTime)
{
auto view = m_registry.view<LifetimeComponent>();
for (auto [entity, lifetimeComponent] : view.each())

View File

@@ -11,10 +11,10 @@ namespace Nz
void SystemGraph::Update()
{
return Update(m_clock.Restart() / 1'000'000.f);
return Update(m_clock.Restart());
}
void SystemGraph::Update(float elapsedTime)
void SystemGraph::Update(Time elapsedTime)
{
if (!m_systemOrderUpdated)
{

36
src/Nazara/Core/Time.cpp Normal file
View File

@@ -0,0 +1,36 @@
// 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>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/TimeImpl.hpp>
#elif defined(NAZARA_PLATFORM_MACOS) || defined(NAZARA_PLATFORM_IOS)
#include <Nazara/Core/Darwin/TimeImpl.hpp>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <Nazara/Core/Posix/TimeImpl.hpp>
#else
#error OS not handled
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
Time GetElapsedNanosecondsFirstRun()
{
if (InitializeHighPrecisionTimer())
GetElapsedNanoseconds = GetElapsedNanosecondsImpl;
else
GetElapsedNanoseconds = GetElapsedMillisecondsImpl;
return GetElapsedNanoseconds();
}
}
GetElapsedTimeFunction GetElapsedMilliseconds = GetElapsedMillisecondsImpl;
GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun);
}

View File

@@ -1,51 +0,0 @@
// 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/Win32/ClockImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // La fréquence ne varie pas pas au cours de l'exécution
}
bool ClockImplInitializeHighPrecision()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0;
}
UInt64 ClockImplGetElapsedMicroseconds()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
//HANDLE thread = GetCurrentThread();
//DWORD oldMask = SetThreadAffinityMask(thread, 1);
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
//SetThreadAffinityMask(thread, oldMask);
return time.QuadPart*1000000ULL / s_frequency.QuadPart;
}
UInt64 ClockImplGetElapsedMilliseconds()
{
#ifdef NAZARA_PLATFORM_WINDOWS_VISTA
return GetTickCount64();
#else
return GetTickCount();
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -5,7 +5,7 @@
#include <Nazara/Core/Win32/FileImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Core/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp>

View File

@@ -0,0 +1,62 @@
// 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/Win32/TimeImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // The frequency of the performance counter is fixed at system boot and is consistent across all processors
}
bool InitializeHighPrecisionTimer()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0 && s_frequency.QuadPart != 0;
}
Time GetElapsedNanosecondsImpl()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
if (s_frequency.QuadPart == 10'000'000) //< seems to be a common value
return Time::Nanoseconds(100ll * time.QuadPart);
else
{
// Compute using 128bits precisions
// https://stackoverflow.com/questions/23378063/how-can-i-use-mach-absolute-time-without-overflowing
UInt64 num = 1'000'000'000ll;
UInt64 denom = s_frequency.QuadPart;
UInt64 value = time.QuadPart;
UInt64 high = (value >> 32) * num;
UInt64 low = (value & 0xFFFFFFFFull) * num / denom;
UInt64 highRem = ((high % denom) << 32) / denom;
high /= denom;
return Time::Nanoseconds(SafeCast<Int64>((high << 32) + highRem + low));
}
}
Time GetElapsedMillisecondsImpl()
{
#ifdef NAZARA_UTILS_WINDOWS_NT6
return Time::Milliseconds(GetTickCount64());
#else
return Time::Milliseconds(GetTickCount());
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -0,0 +1,20 @@
// 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_WIN32_TIMEIMPL_HPP
#define NAZARA_CORE_WIN32_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_WIN32_TIMEIMPL_HPP

View File

@@ -2,7 +2,7 @@
// 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/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz

View File

@@ -4,8 +4,8 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_TIME_HPP
#define NAZARA_CORE_WIN32_TIME_HPP
#ifndef NAZARA_CORE_WIN32_UTILS_HPP
#define NAZARA_CORE_WIN32_UTILS_HPP
#include <Nazara/Prerequisites.hpp>
#include <ctime>
@@ -16,4 +16,4 @@ namespace Nz
time_t FileTimeToTime(FILETIME* time);
}
#endif // NAZARA_CORE_WIN32_TIME_HPP
#endif // NAZARA_CORE_WIN32_UTILS_HPP

View File

@@ -48,7 +48,7 @@ namespace Nz
m_pipeline.reset();
}
void RenderSystem::Update(float /*elapsedTime*/)
void RenderSystem::Update(Time /*elapsedTime*/)
{
UpdateObservers();
UpdateVisibility();

View File

@@ -1245,7 +1245,7 @@ namespace Nz
void ENetHost::ThrottleBandwidth()
{
UInt32 currentTime = UInt32(GetElapsedMilliseconds());
UInt32 currentTime = UInt32(GetElapsedMilliseconds().AsMilliseconds());
UInt32 elapsedTime = currentTime - m_bandwidthThrottleEpoch;
if (elapsedTime < ENetConstants::ENetHost_BandwidthThrottleInterval)

View File

@@ -80,8 +80,8 @@ namespace Nz
PhysWorld2D::PhysWorld2D() :
m_maxStepCount(50),
m_stepSize(0.005f),
m_timestepAccumulator(0.f)
m_stepSize(Time::TickDuration(200)),
m_timestepAccumulator(Time::Zero())
{
m_handle = cpSpaceNew();
cpSpaceSetUserData(m_handle, this);
@@ -154,7 +154,7 @@ namespace Nz
return m_maxStepCount;
}
float PhysWorld2D::GetStepSize() const
Time PhysWorld2D::GetStepSize() const
{
return m_stepSize;
}
@@ -328,7 +328,7 @@ namespace Nz
void PhysWorld2D::SetIterationCount(std::size_t iterationCount)
{
cpSpaceSetIterations(m_handle, int(iterationCount));
cpSpaceSetIterations(m_handle, SafeCast<int>(iterationCount));
}
void PhysWorld2D::SetMaxStepCount(std::size_t maxStepCount)
@@ -336,30 +336,32 @@ namespace Nz
m_maxStepCount = maxStepCount;
}
void PhysWorld2D::SetSleepTime(float sleepTime)
void PhysWorld2D::SetSleepTime(Time sleepTime)
{
if (sleepTime > 0)
cpSpaceSetSleepTimeThreshold(m_handle, cpFloat(sleepTime));
if (sleepTime > Time::Zero())
cpSpaceSetSleepTimeThreshold(m_handle, sleepTime.AsSeconds<cpFloat>());
else
cpSpaceSetSleepTimeThreshold(m_handle, std::numeric_limits<cpFloat>::infinity());
}
void PhysWorld2D::SetStepSize(float stepSize)
void PhysWorld2D::SetStepSize(Time stepSize)
{
m_stepSize = stepSize;
}
void PhysWorld2D::Step(float timestep)
void PhysWorld2D::Step(Time timestep)
{
m_timestepAccumulator += timestep;
std::size_t stepCount = std::min(static_cast<std::size_t>(m_timestepAccumulator / m_stepSize), m_maxStepCount);
std::size_t stepCount = std::min(static_cast<std::size_t>(static_cast<Int64>(m_timestepAccumulator / m_stepSize)), m_maxStepCount);
float invStepCount = 1.f / stepCount;
cpFloat dt = m_stepSize.AsSeconds<float>(); //< FIXME: AsSeconds<cpFloat> is more precise but it fails unit tests on Linux
for (std::size_t i = 0; i < stepCount; ++i)
{
OnPhysWorld2DPreStep(this, invStepCount);
cpSpaceStep(m_handle, m_stepSize);
cpSpaceStep(m_handle, dt);
OnPhysWorld2DPostStep(this, invStepCount);
if (!m_rigidPostSteps.empty())

View File

@@ -33,7 +33,7 @@ namespace Nz
rigidBodyComponent.Destroy();
}
void Physics2DSystem::Update(float elapsedTime)
void Physics2DSystem::Update(Time elapsedTime)
{
m_physWorld.Step(elapsedTime);

View File

@@ -13,8 +13,8 @@ namespace Nz
PhysWorld3D::PhysWorld3D() :
m_maxStepCount(50),
m_gravity(Vector3f::Zero()),
m_stepSize(1.f / 120.f),
m_timestepAccumulator(0.f)
m_stepSize(Time::TickDuration(120)),
m_timestepAccumulator(Time::Zero())
{
m_world = NewtonCreate();
NewtonWorldSetUserData(m_world, this);
@@ -86,7 +86,7 @@ namespace Nz
return m_maxStepCount;
}
float PhysWorld3D::GetStepSize() const
Time PhysWorld3D::GetStepSize() const
{
return m_stepSize;
}
@@ -106,7 +106,7 @@ namespace Nz
m_maxStepCount = maxStepCount;
}
void PhysWorld3D::SetStepSize(float stepSize)
void PhysWorld3D::SetStepSize(Time stepSize)
{
m_stepSize = stepSize;
}
@@ -159,14 +159,16 @@ namespace Nz
NewtonMaterialSetSurfaceThickness(m_world, firstMaterial, secondMaterial, thickness);
}
void PhysWorld3D::Step(float timestep)
void PhysWorld3D::Step(Time timestep)
{
m_timestepAccumulator += timestep;
std::size_t stepCount = 0;
float dt = m_stepSize.AsSeconds<float>();
while (m_timestepAccumulator >= m_stepSize && stepCount < m_maxStepCount)
{
NewtonUpdate(m_world, m_stepSize);
NewtonUpdate(m_world, dt);
m_timestepAccumulator -= m_stepSize;
stepCount++;
}

View File

@@ -22,7 +22,7 @@ namespace Nz
rigidBodyComponent.Destroy();
}
void Physics3DSystem::Update(float elapsedTime)
void Physics3DSystem::Update(Time elapsedTime)
{
m_physWorld.Step(elapsedTime);

View File

@@ -22,7 +22,7 @@ namespace Nz
if (m_framerateLimit > 0)
{
int remainingTime = 1000 / static_cast<int>(m_framerateLimit) - static_cast<int>(m_clock.GetMilliseconds());
int remainingTime = 1000 / static_cast<int>(m_framerateLimit) - static_cast<int>(m_clock.GetElapsedTime().AsMilliseconds());
if (remainingTime > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(remainingTime));

View File

@@ -23,7 +23,7 @@ namespace Nz
m_skeletonConstructObserver.disconnect();
}
void SkeletonSystem::Update(float /*elapsedTime*/)
void SkeletonSystem::Update(Time /*elapsedTime*/)
{
m_sharedSkeletonConstructObserver.each([&](entt::entity entity)
{

View File

@@ -277,8 +277,6 @@ namespace Nz
return false;
}
m_clock.Restart();
return true;
}