diff --git a/Runtime/CBasics.hpp b/Runtime/CBasics.hpp index 9a02674b8..bee1dd9e4 100644 --- a/Runtime/CBasics.hpp +++ b/Runtime/CBasics.hpp @@ -30,7 +30,7 @@ struct OSCalendarTime class CBasics { public: - static void Init(); + static void Initialize(); static const char* Stringize(const char* fmt, ...); static const u64 SECONDS_TO_2000; @@ -40,6 +40,7 @@ public: static std::chrono::system_clock::time_point FromWiiTime(OSTime wiiTime); static u64 GetGCTicks(); + static constexpr u64 GetGCTicksPerSec() { return 486000000ull; } static OSCalendarTime ToCalendarTime(OSTime time) { return ToCalendarTime(FromWiiTime(time)); } static OSCalendarTime ToCalendarTime(std::chrono::system_clock::time_point time); diff --git a/Runtime/CBasicsPC.cpp b/Runtime/CBasicsPC.cpp index 52b5fa83a..a1fda12ec 100644 --- a/Runtime/CBasicsPC.cpp +++ b/Runtime/CBasicsPC.cpp @@ -1,14 +1,42 @@ +#ifndef _WIN32 +#include +#include +#if __APPLE__ +#include +#endif +#else +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + #include #include #include #include "CBasics.hpp" +#if __APPLE__ +static u64 MachToDolphinNum; +static u64 MachToDolphinDenom; +#elif _WIN32 +static LARGE_INTEGER PerfFrequency; +#endif + namespace urde { -void CBasics::Init() +void CBasics::Initialize() { +#if __APPLE__ + mach_timebase_info_data_t timebase; + mach_timebase_info(&timebase); + MachToDolphinNum = GetGCTicksPerSec() * timebase.numer; + MachToDolphinDenom = 1000000000ull * timebase.denom; +#elif _WIN32 + QueryPerformanceFrequency(&PerfFrequency); +#endif } const char* CBasics::Stringize(const char* fmt, ...) @@ -23,9 +51,22 @@ const char* CBasics::Stringize(const char* fmt, ...) u64 CBasics::GetGCTicks() { - auto nanos = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()).count(); - return nanos * 486000000 / 1000000000; +#if __APPLE__ + return mach_absolute_time() * MachToDolphinNum / MachToDolphinDenom; +#elif __linux__ || __FreeBSD__ + struct timespec tp; + clock_gettime(CLOCK_MONOTONIC, &tp); + + return u64((tp.tv_sec * 1000000000ull) + tp.tv_nsec) * GetGCTicksPerSec() / 1000000000ull; +#elif _WIN32 + LARGE_INTEGER perf; + QueryPerformanceCounter(&perf); + perf.QuadPart *= GetGCTicksPerSec(); + perf.QuadPart /= PerfFrequency.QuadPart; + return perf.QuadPart; +#else + return 0; +#endif } const u64 CBasics::SECONDS_TO_2000 = 946684800LL; diff --git a/Runtime/MP1/MP1.cpp b/Runtime/MP1/MP1.cpp index 7c71d37be..94dc2746e 100644 --- a/Runtime/MP1/MP1.cpp +++ b/Runtime/MP1/MP1.cpp @@ -9,6 +9,7 @@ #include "Graphics/Shaders/CXRayBlurFilter.hpp" #include "Character/CCharLayoutInfo.hpp" #include "CGBASupport.hpp" +#include "CBasics.hpp" namespace urde { @@ -114,6 +115,7 @@ void CMain::ResetGameState() void CMain::InitializeSubsystems(const hecl::Runtime::FileStoreManager& storeMgr) { + CBasics::Initialize(); CModelShaders::Initialize(storeMgr, CGraphics::g_BooFactory); CMoviePlayer::Initialize(); CLineRenderer::Initialize();