New Timer, need testing

This commit is contained in:
GabsPuNs
2026-05-12 21:49:46 -04:00
parent 582cf2219c
commit dbb0460c69
2 changed files with 33 additions and 104 deletions

View File

@@ -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<int64_t>(now * 1000.0);
const int64_t passedMsSysTime = msSysTime - lastMsSysTime;
const double adjustTimeT = accumMs / static_cast<double>(passedMsSysTime);
adjustTime += (adjustTimeT - adjustTime) * 0.2f;
lastMsSysTime = msSysTime;
accumMs = 0;
}
if (accumMs < 0)
{
lastMsSysTime = static_cast<int64_t>(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<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
double frameDelta = elapsedSeconds * timeScale * ticksPerSecond;
passedTime += static_cast<float>(frameDelta);
ticks = static_cast<int>(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<float>(MAX_TICKS_PER_UPDATE) * timeScale;
double passedSeconds = static_cast<double>(MAX_TICKS_PER_UPDATE) / static_cast<double>(ticksPerSecond);
passedTime = static_cast<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
ticks = static_cast<int>(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<double>(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<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
ticks = static_cast<int>(0);
if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE;
passedTime -= ticks;
ticks = 0;
passedTime = 0.0f;
a = 0.0f;
}

View File

@@ -3,6 +3,9 @@
#include <sys/sys_time.h>
#endif
#include "System.h"
#ifdef _WINDOWS64
#include <chrono>
#endif
template <class T> void System::arraycopy(arrayWithLength<T> src, unsigned int srcPos, arrayWithLength<T> *dst, unsigned int dstPos, unsigned int length)
{
@@ -52,19 +55,8 @@ void System::arraycopy(arrayWithLength<int> 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<int64_t>(static_cast<double>(counter.QuadPart) * 1000000000.0 / static_cast<double>(s_frequency.QuadPart));
#ifdef _WINDOWS64
return std::chrono::duration_cast<std::chrono::nanoseconds>(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::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
#else
SYSTEMTIME UTCSysTime;