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:
@@ -133,7 +133,7 @@ int main()
|
||||
SpriteRenderData spriteRenderData1 = BuildSpriteData(*device, spriteRenderPipeline, Nz::Rectf(margin, windowSize.y - margin - textureSize, textureSize, textureSize), Nz::Vector2f(windowSize), *texture, *textureSampler);
|
||||
SpriteRenderData spriteRenderData2 = BuildSpriteData(*device, spriteRenderPipeline, Nz::Rectf(windowSize.x - textureSize - margin, windowSize.y - margin - textureSize, textureSize, textureSize), Nz::Vector2f(windowSize), *targetTexture, *textureSampler);
|
||||
|
||||
Nz::Clock secondClock;
|
||||
Nz::MillisecondClock fpsClock;
|
||||
unsigned int fps = 0;
|
||||
|
||||
while (window.IsOpen())
|
||||
@@ -205,13 +205,10 @@ int main()
|
||||
|
||||
fps++;
|
||||
|
||||
if (secondClock.GetMilliseconds() >= 1000)
|
||||
if (fpsClock.RestartIfOver(Nz::Time::Second()))
|
||||
{
|
||||
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS");
|
||||
|
||||
fps = 0;
|
||||
|
||||
secondClock.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -116,8 +116,8 @@ int main()
|
||||
|
||||
window.EnableEventPolling(true);
|
||||
|
||||
Nz::Clock updateClock;
|
||||
Nz::Clock secondClock;
|
||||
Nz::MillisecondClock updateClock;
|
||||
Nz::MillisecondClock fpsClock;
|
||||
unsigned int fps = 0;
|
||||
|
||||
Nz::Mouse::SetRelativeMouseMode(true);
|
||||
@@ -181,10 +181,9 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
if (updateClock.GetMilliseconds() > 1000 / 60)
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
float cameraSpeed = 2.f * updateClock.GetSeconds();
|
||||
updateClock.Restart();
|
||||
float cameraSpeed = 2.f * deltaTime->AsSeconds();
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z))
|
||||
viewerPos += camQuat * Nz::Vector3f::Forward() * cameraSpeed;
|
||||
@@ -231,23 +230,10 @@ int main()
|
||||
// On incrémente le compteur de FPS improvisé
|
||||
fps++;
|
||||
|
||||
if (secondClock.GetMilliseconds() >= 1000) // Toutes les secondes
|
||||
if (fpsClock.RestartIfOver(Nz::Time::Second()))
|
||||
{
|
||||
// Et on insère ces données dans le titre de la fenêtre
|
||||
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS");
|
||||
|
||||
/*
|
||||
Note: En C++11 il est possible d'insérer de l'Unicode de façon standard, quel que soit l'encodage du fichier,
|
||||
via quelque chose de similaire à u8"Cha\u00CEne de caract\u00E8res".
|
||||
Cependant, si le code source est encodé en UTF-8 (Comme c'est le cas dans ce fichier),
|
||||
cela fonctionnera aussi comme ceci : "Chaîne de caractères".
|
||||
*/
|
||||
|
||||
// Et on réinitialise le compteur de FPS
|
||||
fps = 0;
|
||||
|
||||
// Et on relance l'horloge pour refaire ça dans une seconde
|
||||
secondClock.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -253,8 +253,8 @@ int main()
|
||||
|
||||
window.EnableEventPolling(true);
|
||||
|
||||
Nz::Clock updateClock;
|
||||
Nz::Clock secondClock;
|
||||
Nz::MillisecondClock updateClock;
|
||||
Nz::MillisecondClock secondClock;
|
||||
unsigned int fps = 0;
|
||||
bool uboUpdate = true;
|
||||
|
||||
@@ -304,10 +304,9 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
if (updateClock.GetMilliseconds() > 1000 / 60)
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
float cameraSpeed = 2.f * updateClock.GetSeconds();
|
||||
updateClock.Restart();
|
||||
float cameraSpeed = 2.f * deltaTime->AsSeconds();
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z))
|
||||
viewerPos += camQuat * Nz::Vector3f::Forward() * cameraSpeed;
|
||||
@@ -412,7 +411,7 @@ int main()
|
||||
// On incrémente le compteur de FPS improvisé
|
||||
fps++;
|
||||
|
||||
if (secondClock.GetMilliseconds() >= 1000) // Toutes les secondes
|
||||
if (secondClock.RestartIfOver(Nz::Time::Second()))
|
||||
{
|
||||
// Et on insère ces données dans le titre de la fenêtre
|
||||
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS");
|
||||
@@ -426,9 +425,6 @@ int main()
|
||||
|
||||
// Et on réinitialise le compteur de FPS
|
||||
fps = 0;
|
||||
|
||||
// Et on relance l'horloge pour refaire ça dans une seconde
|
||||
secondClock.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ std::filesystem::path GetAssetDir();
|
||||
|
||||
SCENARIO("Music", "[AUDIO][MUSIC]")
|
||||
{
|
||||
using namespace Nz::Literals;
|
||||
|
||||
GIVEN("A music")
|
||||
{
|
||||
Nz::Music music;
|
||||
@@ -19,9 +21,9 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(music.GetDuration() == 63059); // 1 min 03 = 63s = 63000ms
|
||||
CHECK(music.GetDuration() == 63'059'591_us); // 1 min 03 = 63s = 63000ms
|
||||
CHECK(music.GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(music.GetPlayingOffset() == 0);
|
||||
CHECK(music.GetPlayingOffset() == 0_ms);
|
||||
CHECK(music.GetSampleCount() <= 64 * 44100 * 2); // * 2 (stereo)
|
||||
CHECK(music.GetSampleCount() >= 63 * 44100 * 2); // * 2 (stereo)
|
||||
CHECK(music.GetSampleRate() == 44100 /* Hz */);
|
||||
@@ -30,7 +32,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
|
||||
CHECK(music.IsSpatializationEnabled());
|
||||
CHECK(music.GetMinDistance() == 1.f);
|
||||
CHECK(music.GetPitch() == 1.f);
|
||||
CHECK(music.GetPlayingOffset() == 0);
|
||||
CHECK(music.GetPlayingOffset() == 0_ms);
|
||||
CHECK(music.GetPosition() == Nz::Vector3f::Zero());
|
||||
CHECK(music.GetVelocity() == Nz::Vector3f::Zero());
|
||||
CHECK(music.GetVolume() == 1.f);
|
||||
@@ -42,53 +44,54 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
|
||||
|
||||
music.Play();
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
CHECK(music.GetPlayingOffset() >= 950);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
Nz::Time t = music.GetPlayingOffset();
|
||||
CHECK(music.GetPlayingOffset() >= 950_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(music.GetPlayingOffset() <= 1500);
|
||||
CHECK(music.GetPlayingOffset() <= 1500_ms);
|
||||
|
||||
music.SetPlayingOffset(4200);
|
||||
music.SeekToPlayingOffset(4200_ms);
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(music.GetPlayingOffset() >= 4150);
|
||||
CHECK(music.GetPlayingOffset() < 4500);
|
||||
CHECK(music.GetPlayingOffset() >= 4150_ms);
|
||||
CHECK(music.GetPlayingOffset() < 4500_ms);
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
|
||||
music.Pause();
|
||||
Nz::UInt32 playingOffset = music.GetPlayingOffset();
|
||||
Nz::Time playingOffset = music.GetPlayingOffset();
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Paused);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Paused);
|
||||
CHECK(music.GetPlayingOffset() == playingOffset);
|
||||
|
||||
music.SetPlayingOffset(3500);
|
||||
music.SeekToPlayingOffset(3500_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(music.GetPlayingOffset() == 3500);
|
||||
CHECK(music.GetPlayingOffset() == 3500_ms);
|
||||
|
||||
music.Play();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(music.GetPlayingOffset() >= 3650);
|
||||
CHECK(music.GetPlayingOffset() >= 3650_ms);
|
||||
|
||||
AND_WHEN("We let the sound stop by itself")
|
||||
{
|
||||
REQUIRE(music.GetDuration() == 63059);
|
||||
REQUIRE(music.GetDuration() == 63'059'591_us);
|
||||
|
||||
music.SetPlayingOffset(62900);
|
||||
music.SeekToPlayingOffset(62900_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Stopped);
|
||||
CHECK(music.GetPlayingOffset() == 0);
|
||||
CHECK(music.GetPlayingOffset() == 0_ms);
|
||||
|
||||
music.SetPlayingOffset(64000);
|
||||
music.SeekToPlayingOffset(64000_ms);
|
||||
music.Play();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(music.GetPlayingOffset() < 100);
|
||||
CHECK(music.GetPlayingOffset() < 100_ms);
|
||||
|
||||
music.Stop();
|
||||
music.SetPlayingOffset(62900);
|
||||
music.SeekToPlayingOffset(62900_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Stopped);
|
||||
CHECK(music.GetPlayingOffset() == 0); //< playing offset has no effect until Play()
|
||||
CHECK(music.GetPlayingOffset() == 0_ms); //< playing offset has no effect until Play()
|
||||
|
||||
AND_WHEN("We enable looping")
|
||||
{
|
||||
@@ -96,10 +99,10 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
|
||||
CHECK(music.IsLooping());
|
||||
music.Play();
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(music.GetPlayingOffset() >= 62900);
|
||||
CHECK(music.GetPlayingOffset() >= 62900_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(music.GetPlayingOffset() < 300);
|
||||
CHECK(music.GetPlayingOffset() < 300_ms);
|
||||
}
|
||||
}
|
||||
Nz::Audio::Instance()->GetDefaultDevice()->SetGlobalVolume(100.f);
|
||||
|
||||
@@ -6,6 +6,8 @@ std::filesystem::path GetAssetDir();
|
||||
|
||||
SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
|
||||
{
|
||||
using namespace Nz::Literals;
|
||||
|
||||
GIVEN("A sound buffer")
|
||||
{
|
||||
WHEN("We load a .flac file")
|
||||
@@ -15,7 +17,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundBuffer->GetDuration() == 8192);
|
||||
CHECK(soundBuffer->GetDuration() == 8192_ms);
|
||||
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundBuffer->GetSampleRate() == 96000);
|
||||
}
|
||||
@@ -28,7 +30,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundBuffer->GetDuration() == 27193);
|
||||
CHECK(soundBuffer->GetDuration() == 27'193'468_us);
|
||||
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundBuffer->GetSampleRate() == 32000);
|
||||
}
|
||||
@@ -41,7 +43,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundBuffer->GetDuration() == 63059);
|
||||
CHECK(soundBuffer->GetDuration() == 63'059'591_us);
|
||||
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundBuffer->GetSampleRate() == 44100);
|
||||
}
|
||||
@@ -54,7 +56,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundBuffer->GetDuration() == 2490);
|
||||
CHECK(soundBuffer->GetDuration() == 2'490'340_us);
|
||||
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Mono);
|
||||
CHECK(soundBuffer->GetSampleRate() == 44100);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ std::filesystem::path GetAssetDir();
|
||||
|
||||
SCENARIO("SoundStream", "[AUDIO][SoundStream]")
|
||||
{
|
||||
using namespace Nz::Literals;
|
||||
|
||||
GIVEN("A sound buffer")
|
||||
{
|
||||
WHEN("We load a .flac file")
|
||||
@@ -15,7 +17,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundStream->GetDuration() == 8192);
|
||||
CHECK(soundStream->GetDuration() == 8192_ms);
|
||||
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundStream->GetSampleRate() == 96000);
|
||||
}
|
||||
@@ -28,7 +30,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundStream->GetDuration() == 27193);
|
||||
CHECK(soundStream->GetDuration() == 27'193'468_us);
|
||||
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundStream->GetSampleRate() == 32000);
|
||||
}
|
||||
@@ -41,7 +43,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundStream->GetDuration() == 63059);
|
||||
CHECK(soundStream->GetDuration() == 63'059'591_us);
|
||||
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
|
||||
CHECK(soundStream->GetSampleRate() == 44100);
|
||||
}
|
||||
@@ -54,7 +56,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(soundStream->GetDuration() == 2490);
|
||||
CHECK(soundStream->GetDuration() == 2'490'340_us);
|
||||
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Mono);
|
||||
CHECK(soundStream->GetSampleRate() == 44100);
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
std::filesystem::path GetAssetDir();
|
||||
|
||||
SCENARIO("Sound", "[AUDIO][SOUND]")
|
||||
{
|
||||
using namespace Nz::Literals;
|
||||
|
||||
GIVEN("A sound")
|
||||
{
|
||||
Nz::Sound sound;
|
||||
@@ -19,14 +22,14 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
|
||||
|
||||
THEN("We can ask the informations of the file")
|
||||
{
|
||||
CHECK(sound.GetDuration() == 8192);
|
||||
CHECK(sound.GetDuration() == 8192_ms);
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
|
||||
CHECK_FALSE(sound.IsLooping());
|
||||
CHECK(sound.IsPlayable());
|
||||
CHECK(sound.IsSpatializationEnabled());
|
||||
CHECK(sound.GetMinDistance() == 1.f);
|
||||
CHECK(sound.GetPitch() == 1.f);
|
||||
CHECK(sound.GetPlayingOffset() == 0);
|
||||
CHECK(sound.GetPlayingOffset() == 0_ms);
|
||||
CHECK(sound.GetPosition() == Nz::Vector3f::Zero());
|
||||
CHECK(sound.GetVelocity() == Nz::Vector3f::Zero());
|
||||
CHECK(sound.GetVolume() == 1.f);
|
||||
@@ -38,42 +41,45 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
|
||||
|
||||
sound.Play();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
CHECK(sound.GetPlayingOffset() >= 950);
|
||||
|
||||
CHECK(sound.GetPlayingOffset() >= 950_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(sound.GetPlayingOffset() <= 1500);
|
||||
CHECK(sound.GetPlayingOffset() <= 1500_ms);
|
||||
sound.Pause();
|
||||
Nz::UInt32 playingOffset = sound.GetPlayingOffset();
|
||||
Nz::Time playingOffset = sound.GetPlayingOffset();
|
||||
Nz::UInt64 sampleOffset = sound.GetSampleOffset();
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Paused);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Paused);
|
||||
CHECK(sound.GetPlayingOffset() == playingOffset);
|
||||
CHECK(sound.GetSampleOffset() == sampleOffset);
|
||||
|
||||
sound.SetPlayingOffset(3500);
|
||||
CHECK(sound.GetPlayingOffset() == 3500);
|
||||
sound.SeekToPlayingOffset(3500_ms);
|
||||
CHECK(sound.GetPlayingOffset() == 3500_ms);
|
||||
|
||||
sound.Play();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(sound.GetPlayingOffset() >= 1650);
|
||||
CHECK(sound.GetPlayingOffset() >= 1650_ms);
|
||||
|
||||
AND_WHEN("We let the sound stop by itself")
|
||||
{
|
||||
REQUIRE(sound.GetDuration() == 8192);
|
||||
REQUIRE(sound.GetDuration() == 8192_ms);
|
||||
|
||||
sound.SetPlayingOffset(8000);
|
||||
sound.SeekToPlayingOffset(8000_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
|
||||
CHECK(sound.GetPlayingOffset() == 0);
|
||||
CHECK(sound.GetPlayingOffset() == 0_ms);
|
||||
|
||||
sound.SetPlayingOffset(9000);
|
||||
sound.SeekToPlayingOffset(9000_ms);
|
||||
sound.Play();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
|
||||
|
||||
sound.Stop();
|
||||
sound.SetPlayingOffset(8000);
|
||||
sound.SeekToPlayingOffset(8000_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
|
||||
CHECK(sound.GetPlayingOffset() == 0); //< playing offset has no effect until Play()
|
||||
CHECK(sound.GetPlayingOffset() == 0_ms); //< playing offset has no effect until Play()
|
||||
|
||||
AND_WHEN("We enable looping")
|
||||
{
|
||||
@@ -81,10 +87,10 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
|
||||
CHECK(sound.IsLooping());
|
||||
sound.Play();
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(sound.GetPlayingOffset() >= 8000);
|
||||
CHECK(sound.GetPlayingOffset() >= 8000_ms);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
|
||||
CHECK(sound.GetPlayingOffset() < 300);
|
||||
CHECK(sound.GetPlayingOffset() < 300_ms);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,14 +8,14 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
{
|
||||
GIVEN("A clock paused")
|
||||
{
|
||||
Nz::UInt64 initialTime = 100;
|
||||
Nz::Clock clock(initialTime, true);
|
||||
Nz::Time initialTime = Nz::Time::Microseconds(100);
|
||||
Nz::HighPrecisionClock clock(initialTime, true);
|
||||
|
||||
WHEN("We get time since it is paused")
|
||||
{
|
||||
THEN("Time must be the initialTime")
|
||||
{
|
||||
CHECK(clock.GetMicroseconds() == initialTime);
|
||||
CHECK(clock.GetElapsedTime() == initialTime);
|
||||
CHECK(clock.IsPaused());
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,11 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
THEN("Time must not be the initialTime")
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
Nz::UInt64 microSeconds = clock.GetMicroseconds();
|
||||
CHECK(microSeconds != initialTime);
|
||||
CHECK(microSeconds / 1000 <= clock.GetMilliseconds());
|
||||
CHECK(microSeconds / (1000.f * 1000.f) <= clock.GetSeconds());
|
||||
Nz::Time elapsedTime = clock.GetElapsedTime();
|
||||
Nz::Int64 microseconds = elapsedTime.AsMicroseconds();
|
||||
CHECK(microseconds != initialTime.AsMicroseconds());
|
||||
CHECK(microseconds / 1000 <= elapsedTime.AsMilliseconds());
|
||||
CHECK(microseconds / (1000.f * 1000.f) <= elapsedTime.AsSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
CHECK(!clock.IsPaused());
|
||||
clock.Pause();
|
||||
CHECK(clock.IsPaused());
|
||||
CHECK(clock.GetMicroseconds() != initialTime);
|
||||
CHECK(clock.GetElapsedTime() != initialTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
52
tests/UnitTests/Engine/Core/TimeTest.cpp
Normal file
52
tests/UnitTests/Engine/Core/TimeTest.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
SCENARIO("Time", "[CORE][Time]")
|
||||
{
|
||||
auto ToString = [](Nz::Time time) -> std::string
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << time;
|
||||
return std::move(ss).str();
|
||||
};
|
||||
|
||||
GIVEN("One second")
|
||||
{
|
||||
Nz::Time time = Nz::Time::Second();
|
||||
CHECK(time.AsDuration<std::chrono::seconds>() == std::chrono::seconds(1));
|
||||
CHECK(time.AsDuration<std::chrono::milliseconds>() == std::chrono::milliseconds(1'000));
|
||||
CHECK(time.AsDuration<std::chrono::microseconds>() == std::chrono::microseconds(1'000'000));
|
||||
CHECK(time.AsMicroseconds() == 1'000'000);
|
||||
CHECK(time.AsMilliseconds() == 1'000);
|
||||
CHECK(time.AsSeconds() == 1.f);
|
||||
CHECK(time == Nz::Time::Second());
|
||||
CHECK(time == Nz::Time::Seconds(1.f));
|
||||
CHECK(time == Nz::Time::Milliseconds(1'000));
|
||||
CHECK(time == Nz::Time::Seconds(2) - Nz::Time::Milliseconds(1'000));
|
||||
|
||||
CHECK(ToString(time) == "1000ms");
|
||||
}
|
||||
|
||||
GIVEN("One arbitrary duration")
|
||||
{
|
||||
Nz::Time time = Nz::Time::FromDuration(std::chrono::nanoseconds(1'234'567'890ULL)) - Nz::Time::Nanoseconds(890);
|
||||
CHECK(time == Nz::Time::Microseconds(1'234'567));
|
||||
CHECK_FALSE(time == Nz::Time::Microseconds(1'234'568));
|
||||
CHECK(time != Nz::Time::Microseconds(1'234'566));
|
||||
CHECK_FALSE(time != Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time > Nz::Time::Microseconds(1'234'000));
|
||||
CHECK(time > Nz::Time::Zero());
|
||||
CHECK(time >= Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time >= Nz::Time::Microseconds(1'234'000));
|
||||
CHECK(time < Nz::Time::Milliseconds(1'235));
|
||||
CHECK(time <= Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time <= Nz::Time::Seconds(2));
|
||||
|
||||
CHECK(ToString(time) == "1.23457s");
|
||||
CHECK(ToString(time - Nz::Time::Second()) == "234.567ms");
|
||||
CHECK(ToString(time - Nz::Time::Seconds(1.234f)) == "567us");
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ SCENARIO("PhysWorld2D", "[PHYSICS2D][PHYSWORLD2D]")
|
||||
}
|
||||
}
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We ask for the nearest body")
|
||||
{
|
||||
@@ -130,7 +130,7 @@ SCENARIO("PhysWorld2D", "[PHYSICS2D][PHYSWORLD2D]")
|
||||
Nz::RigidBody2D trigger(&world, 0.f, triggerBox);
|
||||
trigger.SetPosition(Nz::Vector2f(2.f, 0.f));
|
||||
|
||||
world.Step(0.f);
|
||||
world.Step(Nz::Time::Zero());
|
||||
|
||||
Nz::PhysWorld2D::Callback characterTriggerCallback;
|
||||
characterTriggerCallback.startCallback = [&](Nz::PhysWorld2D&, Nz::Arbiter2D&, Nz::RigidBody2D&, Nz::RigidBody2D&, void*) -> bool {
|
||||
@@ -164,27 +164,29 @@ SCENARIO("PhysWorld2D", "[PHYSICS2D][PHYSWORLD2D]")
|
||||
{
|
||||
character.SetVelocity(Nz::Vector2f(1.f, 0.f));
|
||||
for (int i = 0; i != 11; ++i)
|
||||
world.Step(0.1f);
|
||||
world.Step(Nz::Time::TickDuration(10));
|
||||
|
||||
THEN("It should trigger several collisions")
|
||||
{
|
||||
CHECK(statusTriggerCollision == 3);
|
||||
for (int i = 0; i != 20; ++i)
|
||||
world.Step(0.1f);
|
||||
world.Step(Nz::Time::TickDuration(10));
|
||||
CHECK(statusTriggerCollision == 11);
|
||||
|
||||
CHECK(character.GetPosition().x == Catch::Approx(3.1f).margin(0.01f));
|
||||
|
||||
for (int i = 0; i != 9; ++i)
|
||||
world.Step(0.1f);
|
||||
world.Step(Nz::Time::TickDuration(10));
|
||||
|
||||
CHECK(character.GetPosition().x == Catch::Approx(4.f).margin(0.01f));
|
||||
world.Step(0.1f);
|
||||
world.Step(Nz::Time::TickDuration(10));
|
||||
CHECK(character.GetPosition().x == Catch::Approx(4.f).margin(0.01f));
|
||||
CHECK(statusWallCollision == 1); // It should be close to the wall
|
||||
|
||||
character.SetVelocity(Nz::Vector2f(-2.f, 0.f));
|
||||
for (int i = 0; i != 10; ++i)
|
||||
world.Step(0.1f);
|
||||
world.Step(Nz::Time::TickDuration(10));
|
||||
|
||||
CHECK(statusWallCollision == 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,14 +34,14 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
bool userdata = false;
|
||||
body.SetUserdata(&userdata);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We copy construct the body")
|
||||
{
|
||||
body.AddForce(Nz::Vector2f(3.f, 5.f));
|
||||
Nz::RigidBody2D copiedBody(body);
|
||||
EQUALITY(copiedBody, body);
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
EQUALITY(copiedBody, body);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
float radius = 5.f;
|
||||
body.SetGeom(std::make_shared<Nz::CircleCollider2D>(radius));
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
THEN("The aabb should be updated")
|
||||
{
|
||||
@@ -96,7 +96,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
tmp.push_back(CreateBody(world));
|
||||
tmp.push_back(CreateBody(world));
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
THEN("They should be valid")
|
||||
{
|
||||
@@ -124,7 +124,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::Vector2f position = Nz::Vector2f::Zero();
|
||||
body.SetPosition(position);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We retrieve standard information")
|
||||
{
|
||||
@@ -150,7 +150,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::Vector2f velocity(Nz::Vector2f::Unit());
|
||||
body.SetVelocity(velocity);
|
||||
position += velocity;
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
THEN("We expect those to be true")
|
||||
{
|
||||
@@ -164,7 +164,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
AND_THEN("We apply an impulse in the opposite direction")
|
||||
{
|
||||
body.AddImpulse(-velocity);
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
REQUIRE(body.GetVelocity() == Nz::Vector2f::Zero());
|
||||
}
|
||||
@@ -174,23 +174,24 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
{
|
||||
Nz::RadianAnglef angularSpeed = Nz::RadianAnglef::FromDegrees(90.f);
|
||||
body.SetAngularVelocity(angularSpeed);
|
||||
world.Step(1.f);
|
||||
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
THEN("We expect those to be true")
|
||||
{
|
||||
CHECK(body.GetAngularVelocity() == angularSpeed);
|
||||
CHECK(body.GetRotation() == angularSpeed);
|
||||
CHECK(body.GetAABB() == Nz::Rectf(-6.f, 3.f, 2.f, 1.f));
|
||||
CHECK(body.GetAABB().ApproxEquals(Nz::Rectf(-6.f, 3.f, 2.f, 1.f), 0.00001f));
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
CHECK(body.GetRotation() == 2.f * angularSpeed);
|
||||
CHECK(body.GetAABB() == Nz::Rectf(-4.f, -6.f, 1.f, 2.f));
|
||||
CHECK(body.GetAABB().ApproxEquals(Nz::Rectf(-4.f, -6.f, 1.f, 2.f), 0.00001f));
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
CHECK(body.GetRotation() == 3.f * angularSpeed);
|
||||
CHECK(body.GetAABB() == Nz::Rectf(4.f, -4.f, 2.f, 1.f));
|
||||
CHECK(body.GetAABB().ApproxEquals(Nz::Rectf(4.f, -4.f, 2.f, 1.f), 0.00001f));
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
CHECK(body.GetRotation() == 4.f * angularSpeed);
|
||||
}
|
||||
}
|
||||
@@ -198,7 +199,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
WHEN("We apply a torque")
|
||||
{
|
||||
body.AddTorque(Nz::DegreeAnglef(90.f));
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
THEN("It is also counter-clockwise")
|
||||
{
|
||||
@@ -220,7 +221,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::RigidBody2D body(&world, mass);
|
||||
body.SetGeom(circle, true, false);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We ask for the aabb of the circle")
|
||||
{
|
||||
@@ -251,7 +252,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::RigidBody2D body(&world, mass);
|
||||
body.SetGeom(compound, true, false);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We ask for the aabb of the compound")
|
||||
{
|
||||
@@ -280,7 +281,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::RigidBody2D body(&world, mass);
|
||||
body.SetGeom(convex, true, false);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We ask for the aabb of the convex")
|
||||
{
|
||||
@@ -304,7 +305,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
|
||||
Nz::RigidBody2D body(&world, mass);
|
||||
body.SetGeom(segment, true, false);
|
||||
|
||||
world.Step(1.f);
|
||||
world.Step(Nz::Time::Second());
|
||||
|
||||
WHEN("We ask for the aabb of the segment")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user