Skip to content

Commit bbb199c

Browse files
committed
feat(timer): improve high-FPS timing with high-resolution Windows clock
1 parent a4691f5 commit bbb199c

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Minecraft.Client/Timer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ void Timer::advanceTime()
2222
{
2323
__int64 nowMs = System::currentTimeMillis();
2424
__int64 passedMs = nowMs - lastMs;
25-
__int64 msSysTime = System::nanoTime() / 1000000;
26-
double now = msSysTime / 1000.0;
25+
26+
// 4J - Use high-resolution timer for 'now' in seconds
27+
double now = System::nanoTime() / 1000000000.0;
2728

2829

2930
if (passedMs > 1000)
@@ -39,6 +40,7 @@ void Timer::advanceTime()
3940
accumMs += passedMs;
4041
if (accumMs > 1000)
4142
{
43+
__int64 msSysTime = (__int64)(now * 1000.0);
4244
__int64 passedMsSysTime = msSysTime - lastMsSysTime;
4345

4446
double adjustTimeT = accumMs / (double) passedMsSysTime;
@@ -49,7 +51,7 @@ void Timer::advanceTime()
4951
}
5052
if (accumMs < 0)
5153
{
52-
lastMsSysTime = msSysTime;
54+
lastMsSysTime = (__int64)(now * 1000.0);
5355
}
5456
}
5557
lastMs = nowMs;
@@ -68,7 +70,6 @@ void Timer::advanceTime()
6870
if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE;
6971

7072
a = passedTime;
71-
7273
}
7374

7475
void Timer::advanceTimeQuickly()

Minecraft.World/system.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,22 @@ void System::arraycopy(arrayWithLength<int> src, unsigned int srcPos, arrayWithL
5252
//The current value of the system timer, in nanoseconds.
5353
__int64 System::nanoTime()
5454
{
55+
#if defined _WINDOWS64 || defined _XBOX || defined _WIN32
56+
static LARGE_INTEGER s_frequency = { 0 };
57+
if (s_frequency.QuadPart == 0)
58+
{
59+
QueryPerformanceFrequency(&s_frequency);
60+
}
61+
62+
LARGE_INTEGER counter;
63+
QueryPerformanceCounter(&counter);
64+
65+
// Using double to avoid 64-bit overflow during multiplication for long uptime
66+
// Precision is sufficient for ~100 days of uptime.
67+
return (__int64)((double)counter.QuadPart * 1000000000.0 / (double)s_frequency.QuadPart);
68+
#else
5569
return GetTickCount() * 1000000LL;
70+
#endif
5671
}
5772

5873
//Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond,

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project contains the source code of Minecraft Legacy Console Edition v1.3.0
1616
- Added support for keyboard and mouse input
1717
- Added fullscreen mode support (toggle using F11)
1818
- Disabled V-Sync for better performance
19+
- Added a high-resolution timer path on Windows for smoother high-FPS gameplay timing
1920
- Device's screen resolution will be used as the game resolution instead of using a fixed resolution (1920x1080)
2021

2122
## Controls (Keyboard & Mouse)

0 commit comments

Comments
 (0)