From ae790f5a9e69b22c684a8b66427ba7017f39d5d8 Mon Sep 17 00:00:00 2001 From: Patedam Date: Sun, 9 Aug 2026 11:32:06 -0400 Subject: [PATCH] moved delta time computation to the HAL --- Juliet/include/Core/HAL/OS/OS.h | 3 +++ Juliet/src/Core/HAL/OS/OS.cpp | 22 ++++++++++++++++++++++ Juliet/src/Core/HAL/OS/OS_Private.h | 3 ++- Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp | 20 ++++++++++++++++++++ Juliet/src/Engine/Engine.cpp | 24 +++--------------------- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Juliet/include/Core/HAL/OS/OS.h b/Juliet/include/Core/HAL/OS/OS.h index fe4d30d..5c0eff6 100644 --- a/Juliet/include/Core/HAL/OS/OS.h +++ b/Juliet/include/Core/HAL/OS/OS.h @@ -34,6 +34,9 @@ namespace Juliet namespace Time { uint64 Timestamp(); + void ComputeDeltaTime(); + float GetDeltaTime(); + uint64 GetFrameNumber(); } // namespace Time namespace Debug diff --git a/Juliet/src/Core/HAL/OS/OS.cpp b/Juliet/src/Core/HAL/OS/OS.cpp index 128ddb7..dc1905e 100644 --- a/Juliet/src/Core/HAL/OS/OS.cpp +++ b/Juliet/src/Core/HAL/OS/OS.cpp @@ -23,10 +23,32 @@ namespace Juliet namespace Time { + namespace + { + float DeltaTime = 0.0f; + uint64 Frame = 0; + } // namespace + uint64 Timestamp() { return Internal::OS_Timestamp(); } + + void ComputeDeltaTime() + { + DeltaTime = Internal::OS_ComputeDeltaTime(); + ++Frame; + } + + float GetDeltaTime() + { + return DeltaTime; + } + + uint64 GetFrameNumber() + { + return Frame; + } } // namespace Time namespace Debug diff --git a/Juliet/src/Core/HAL/OS/OS_Private.h b/Juliet/src/Core/HAL/OS/OS_Private.h index 201a320..d16e6ed 100644 --- a/Juliet/src/Core/HAL/OS/OS_Private.h +++ b/Juliet/src/Core/HAL/OS/OS_Private.h @@ -17,7 +17,8 @@ namespace Juliet namespace Time::Internal { uint64 OS_Timestamp(); - } + float OS_ComputeDeltaTime(); + } // namespace Time::Internal namespace Internal { diff --git a/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp b/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp index 57bb574..23723ea 100644 --- a/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp +++ b/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp @@ -54,6 +54,26 @@ namespace Juliet return (static_cast(ft.dwHighDateTime) << 32) + ft.dwLowDateTime; } + + float OS_ComputeDeltaTime() + { + static LARGE_INTEGER frequency = {}; + static LARGE_INTEGER lastTime = {}; + if (frequency.QuadPart == 0) + { + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&lastTime); + } + + LARGE_INTEGER currentTime; + QueryPerformanceCounter(¤tTime); + const float deltaTime = + static_cast(currentTime.QuadPart - lastTime.QuadPart) / static_cast(frequency.QuadPart); + lastTime = currentTime; + + return deltaTime; + } + } // namespace Time::Internal namespace Debug::Internal diff --git a/Juliet/src/Engine/Engine.cpp b/Juliet/src/Engine/Engine.cpp index 59c690d..b7d5ef6 100644 --- a/Juliet/src/Engine/Engine.cpp +++ b/Juliet/src/Engine/Engine.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -19,14 +20,6 @@ #include #endif -// TODO Move to os hal for query perf -#ifdef _WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -#endif - namespace Juliet { #if JULIET_DEBUG @@ -219,20 +212,9 @@ namespace Juliet ImGuiRenderer_NewFrame(); #endif - // TODO : Move to os side as it is win32 only - static LARGE_INTEGER frequency = {}; - static LARGE_INTEGER lastTime = {}; - if (frequency.QuadPart == 0) - { - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&lastTime); - } + Time::ComputeDeltaTime(); - LARGE_INTEGER currentTime; - QueryPerformanceCounter(¤tTime); - float deltaTime = - static_cast(currentTime.QuadPart - lastTime.QuadPart) / static_cast(frequency.QuadPart); - lastTime = currentTime; + const float deltaTime = Time::GetDeltaTime(); Events_NewFrame(deltaTime);