From dbb0460c69f1deccb7068133491ee4acaa2721da Mon Sep 17 00:00:00 2001 From: GabsPuNs Date: Tue, 12 May 2026 21:49:46 -0400 Subject: [PATCH] New Timer, need testing --- Minecraft.Client/Timer.cpp | 116 ++++++++----------------------------- Minecraft.World/system.cpp | 21 +++---- 2 files changed, 33 insertions(+), 104 deletions(-) diff --git a/Minecraft.Client/Timer.cpp b/Minecraft.Client/Timer.cpp index 3ef51eb7..f8f16f26 100644 --- a/Minecraft.Client/Timer.cpp +++ b/Minecraft.Client/Timer.cpp @@ -20,120 +20,54 @@ Timer::Timer(float ticksPerSecond) void Timer::advanceTime() { - int64_t nowMs = System::currentTimeMillis(); - int64_t passedMs = nowMs - lastMs; + int64_t nowNano = System::nanoTime(); + double nowSeconds = nowNano / 1000000000.0; + + static double lastFrameTime = nowSeconds; + double elapsedSeconds = nowSeconds - lastFrameTime; + lastFrameTime = nowSeconds; - // 4J - Use high-resolution timer for 'now' in seconds - double now = System::nanoTime() / 1000000000.0; + if (elapsedSeconds < 0) elapsedSeconds = 0; + if (elapsedSeconds > 1.0) elapsedSeconds = 1.0; - - if (passedMs > 1000) - { - lastTime = now; - } - else if (passedMs < 0) - { - lastTime = now; - } - else - { - accumMs += passedMs; - if (accumMs > 1000) - { - const int64_t msSysTime = static_cast(now * 1000.0); - const int64_t passedMsSysTime = msSysTime - lastMsSysTime; - - const double adjustTimeT = accumMs / static_cast(passedMsSysTime); - adjustTime += (adjustTimeT - adjustTime) * 0.2f; - - lastMsSysTime = msSysTime; - accumMs = 0; - } - if (accumMs < 0) - { - lastMsSysTime = static_cast(now * 1000.0); - } - } - lastMs = nowMs; - - double passedSeconds = (now - lastTime) * adjustTime; - lastTime = now; - - if (passedSeconds < 0) passedSeconds = 0; - if (passedSeconds > 1) passedSeconds = 1; - - passedTime = static_cast(passedTime + (passedSeconds * timeScale * ticksPerSecond)); + double frameDelta = elapsedSeconds * timeScale * ticksPerSecond; + + passedTime += static_cast(frameDelta); ticks = static_cast(passedTime); passedTime -= ticks; - if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE; + if (ticks > MAX_TICKS_PER_UPDATE) + ticks = MAX_TICKS_PER_UPDATE; a = passedTime; } void Timer::advanceTimeQuickly() { + passedTime += static_cast(MAX_TICKS_PER_UPDATE) * timeScale; - double passedSeconds = static_cast(MAX_TICKS_PER_UPDATE) / static_cast(ticksPerSecond); - - passedTime = static_cast(passedTime + (passedSeconds * timeScale * ticksPerSecond)); ticks = static_cast(passedTime); passedTime -= ticks; - a = passedTime; + + a = 0.0f; lastMs = System::currentTimeMillis(); + lastMsSysTime = System::nanoTime() / 1000000; - } void Timer::skipTime() { - int64_t nowMs = System::currentTimeMillis(); - int64_t passedMs = nowMs - lastMs; - int64_t msSysTime = System::nanoTime() / 1000000; - double now = msSysTime / 1000.0; + int64_t nowNano = System::nanoTime(); + double nowSeconds = nowNano / 1000000000.0; + lastMs = System::currentTimeMillis(); + lastMsSysTime = nowNano / 1000000; + lastTime = nowSeconds; - if (passedMs > 1000) - { - lastTime = now; - } - else if (passedMs < 0) - { - lastTime = now; - } - else - { - accumMs += passedMs; - if (accumMs > 1000) - { - int64_t passedMsSysTime = msSysTime - lastMsSysTime; - - double adjustTimeT = accumMs / static_cast(passedMsSysTime); - adjustTime += (adjustTimeT - adjustTime) * 0.2f; - - lastMsSysTime = msSysTime; - accumMs = 0; - } - if (accumMs < 0) - { - lastMsSysTime = msSysTime; - } - } - lastMs = nowMs; - - - double passedSeconds = (now - lastTime) * adjustTime; - lastTime = now; - - if (passedSeconds < 0) passedSeconds = 0; - if (passedSeconds > 1) passedSeconds = 1; - - passedTime = static_cast(passedTime + (passedSeconds * timeScale * ticksPerSecond)); - - ticks = static_cast(0); - if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE; - passedTime -= ticks; + ticks = 0; + passedTime = 0.0f; + a = 0.0f; } \ No newline at end of file diff --git a/Minecraft.World/system.cpp b/Minecraft.World/system.cpp index 3103d6d3..7c9f0d73 100644 --- a/Minecraft.World/system.cpp +++ b/Minecraft.World/system.cpp @@ -3,6 +3,9 @@ #include #endif #include "System.h" +#ifdef _WINDOWS64 +#include +#endif template void System::arraycopy(arrayWithLength src, unsigned int srcPos, arrayWithLength *dst, unsigned int dstPos, unsigned int length) { @@ -52,19 +55,8 @@ void System::arraycopy(arrayWithLength src, unsigned int srcPos, arrayWithL //The current value of the system timer, in nanoseconds. int64_t System::nanoTime() { -#if defined _WINDOWS64 || defined _XBOX || defined _WIN32 - static LARGE_INTEGER s_frequency = { 0 }; - if (s_frequency.QuadPart == 0) - { - QueryPerformanceFrequency(&s_frequency); - } - - LARGE_INTEGER counter; - QueryPerformanceCounter(&counter); - - // Using double to avoid 64-bit overflow during multiplication for long uptime - // Precision is sufficient for ~100 days of uptime. - return static_cast(static_cast(counter.QuadPart) * 1000000000.0 / static_cast(s_frequency.QuadPart)); +#ifdef _WINDOWS64 + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); #else return GetTickCount64() * 1000000LL; #endif @@ -103,6 +95,9 @@ int64_t System::currentTimeMillis() int64_t systTime = (((((((Time.day * 24) + Time.hour) * 60) + Time.minute) * 60) + Time.second) * 1000) + (Time.microsecond / 1000); return systTime;*/ +#elif defined _WINDOWS64 + return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + #else SYSTEMTIME UTCSysTime;