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:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
31
src/Nazara/Core/Posix/TimeImpl.cpp
Normal file
31
src/Nazara/Core/Posix/TimeImpl.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
20
src/Nazara/Core/Posix/TimeImpl.hpp
Normal file
20
src/Nazara/Core/Posix/TimeImpl.hpp
Normal 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_POSIX_TIMEIMPL_HPP
|
||||
#define NAZARA_CORE_POSIX_TIMEIMPL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool InitializeHighPrecisionTimer();
|
||||
Time GetElapsedNanosecondsImpl();
|
||||
Time GetElapsedMillisecondsImpl();
|
||||
}
|
||||
|
||||
#endif // NAZARA_CORE_POSIX_TIMEIMPL_HPP
|
||||
Reference in New Issue
Block a user