Orange/include/Orange/Core/Time.h

56 lines
1.3 KiB
C++

#pragma once
#if defined(_WIN32)
#include <windows.h>
#else
#include <chrono>
#endif
namespace orange
{
#if defined(_WIN32)
struct Time
{
static constexpr bool is_steady = true;
using rep = int64_t;
using period = std::nano;
using duration = std::chrono::nanoseconds;
using time_point = std::chrono::time_point<Time>;
static inline time_point now() noexcept
{
// Keep the frequency static, this doesn't change at all.
static const int64_t freq = get_frequency();
const int64_t counter = get_counter();
const int64_t whole = (counter / freq) * period::den;
const int64_t part = (counter % freq) * period::den / freq;
return time_point(duration(whole + part));
}
static inline int64_t get_frequency() noexcept
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return freq.QuadPart;
}
static inline int64_t get_counter() noexcept
{
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return count.QuadPart;
}
};
#else
using Time = std::chrono::high_resolution_clock;
#endif
using TimePoint = Time::time_point;
using Seconds = std::chrono::duration<float>;
}